Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect Simulation Results #13116

Closed
glanzz opened this issue Sep 9, 2024 · 10 comments
Closed

Incorrect Simulation Results #13116

glanzz opened this issue Sep 9, 2024 · 10 comments
Labels
bug Something isn't working

Comments

@glanzz
Copy link

glanzz commented Sep 9, 2024

Environment

  • Qiskit version: 1.2.0
  • Python version: 3.11
  • Operating system: Apple M2 Air

What is happening?

The following circuit is expected to provide a 110 as dominant state. But it is resulting in state 100 for all shots randomly for optimisation level 2 and 3. The issue could possibly be in the transpilation of the circuit.

How can we reproduce the issue?

from qiskit_aer import StatevectorSimulator
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit import QuantumCircuit
from qiskit.circuit.library import CU3Gate

cu3 = CU3Gate(2.7955,5.1982,3.3315,)
cu32 = CU3Gate(-2.7955,-3.3315,-5.1982)

qc = QuantumCircuit(3)
qc.x(1)
qc.x(2)
qc.id(1)
qc.swap(0,2)
qc.csx(0,1)
qc.u(5.6631,3.9488,1.066, 2)
qc.tdg(0)
qc.p(2.8007, 1)
qc.r(0.71942,4.7156, 2)


qc.ry(1.8777, 0)
qc.rxx(4.836, 1,2)
qc.rz(3.5166, 0)
qc.r(0.6993,2.7793, 1)
qc.u(0.3461,0.42597,3.202, 2)
qc.z(1)
qc.append(cu3, [2,0])
#qc.cu3( )
qc.ry(4.7962, 0)
qc.rx(2.4125, 1)
qc.r(2.4677,3.5935, 2)
qc.r(-2.4677,3.5935, 2)
qc.rx(-2.4125, 1)
qc.ry(-4.7962, 0)
qc.append(cu32, [2,0])
qc.z(1)
qc.u(-0.3461,-3.202,-0.42597, 2)
qc.r(-0.6993,2.7793, 1)
qc.rz(-3.5166, 0)
qc.rxx(-4.836, 1,2)
qc.ry(-1.8777, 0)
qc.r(-0.71942,4.7156, 2)
qc.p(-2.8007, 1)

qc.t(0)
qc.u(-5.6631,-1.066,-3.9488, 2)
qc.csx(0,1).inverse()
qc.swap(0,2)
qc.id(1)

qc.measure_all()


backend = StatevectorSimulator()
for i in range(4):
  pass_manager=generate_preset_pass_manager(backend=backend, optimization_level=i)
  transpiled_circuit = pass_manager.run(qc)
  #print(transpiled_circuit)
  job = backend.run(transpiled_circuit, shots=11000)
  results = job.result().get_counts()
  print(f"Optimisation Level: {i}, Results: {results}")

What should happen?

Running the script for 1st time provides correct results

python3 issue.py
Optimisation Level: 0, Results: {'110': 11000}
Optimisation Level: 1, Results: {'110': 11000}
Optimisation Level: 2, Results: {'110': 11000}
Optimisation Level: 3, Results: {'110': 11000}

But running the scirpt for the 3rd time gave me the following result:

python3 issue.py
Optimisation Level: 0, Results: {'110': 11000}
Optimisation Level: 1, Results: {'110': 11000}
Optimisation Level: 2, Results: {'100': 11000}
Optimisation Level: 3, Results: {'100': 11000}

But running the same result on Qiskit 1.1.0 is working properly.

Any suggestions?

No response

@glanzz glanzz added the bug Something isn't working label Sep 9, 2024
@glanzz
Copy link
Author

glanzz commented Sep 9, 2024

Seems like the issue is specific to statevector simulation methods. Running the same script for the backend = AerSimulator(method='matrix_product_state') does work properly.

@Cryoris
Copy link
Contributor

Cryoris commented Sep 10, 2024

Hmm I can't reproduce this using either of qiskit==1.2.0 or main and qiskit-aer==0.15.0 on Python 3.11 and Mac M1 (not M2 though). What version of Qiskit Aer were you using?

@glanzz
Copy link
Author

glanzz commented Sep 10, 2024

I am running the same version of qiskit-aer and qiskit !

@Cryoris
Copy link
Contributor

Cryoris commented Sep 11, 2024

Hmm interesting. If you change the code to run on Qiskit's Statevector class, does the problem remain? This means changing:

from qiskit.quantum_info import Statevector

# your code.... 
qc.csx(0,1).inverse()
qc.swap(0,2)
qc.id(1)

# no measurements!
# qc.measure_all()

for i in range(4):
  pass_manager=generate_preset_pass_manager(backend=backend, optimization_level=i)
  transpiled_circuit = pass_manager.run(qc)
  results = Statevector(transpiled_circuit).probabilities_dict(decimals=6)
  print(f"Optimisation Level: {i}, Results: {results}")

@glanzz
Copy link
Author

glanzz commented Sep 11, 2024

These are the results:

Optimisation Level: 0, Results: {np.str_('110'): np.float64(1.0)}
Optimisation Level: 1, Results: {np.str_('110'): np.float64(1.0)}
Optimisation Level: 2, Results: {np.str_('100'): np.float64(1.0)}
Optimisation Level: 3, Results: {np.str_('100'): np.float64(1.0)}

@Cryoris
Copy link
Contributor

Cryoris commented Sep 13, 2024

The problem was reproducable on Qiskit 1.2.0, but disappears on 1.2.1 (I also tried on an M2 mac). This was likely a bug with the CU3Gate which we fixed in 1.2.1. That version was released yesterday, so updating Qiskit should fix the issue 🙂

@glanzz
Copy link
Author

glanzz commented Sep 13, 2024

@Cryoris Can i know why this issue was specific to optimisation level 2 or 3 ?

@Cryoris
Copy link
Contributor

Cryoris commented Sep 13, 2024

The issue with the CU3Gate/CUGate (#13121) would only occur when we ran ConsolidateBlocks, which we did on optimization levels 2 and 3 to collect and re-synthesize blocks of 2-qubit gates.

@glanzz
Copy link
Author

glanzz commented Sep 15, 2024

@Cryoris But, why did it giving correct output in some runs and incorrect output in some results ?

@jakelishman
Copy link
Member

The transpiler has stochastic components to it, so unless you set the seed_transpiler argument to generate_preset_pass_manager or transpile, the results of the output circuit might be a bit different each time, though (bugs like this one aside) it should always be an equivalent circuit.

I'll close the issue now since it appears to have been fixed in 1.2.1, but feel free to re-open if there's more to discuss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants