Environment
- Qiskit Terra version: 0.19.1
- Python version: 3.8.0
- Operating system: Ubuntu 22.04.3
What is happening?
When running a specific quantum circuit multiple times, I got different state vector results. To the best of my knowledge, the state vector should not change given a fixed quantum circuit.
How can we reproduce the issue?
The below code is a minimal reproduction example for the issue. It defines a quantum circuit with four gates, runs it 1,000 times, and counts the number of different state vector results.
from qiskit import Aer, transpile, execute
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit.circuit.library.standard_gates import *
from qiskit.circuit.add_control import add_control
from qiskit.quantum_info import Statevector
import numpy as np
qr = QuantumRegister(6, name='qr')
cr = ClassicalRegister(6, name='cr')
qc = QuantumCircuit(qr, cr, name='qc')
qc.append(ECRGate(), qargs=[qr[1], qr[0]], cargs=[])
qc.append(C4XGate().control(1), qargs=[qr[5], qr[2], qr[0], qr[3], qr[4], qr[1]], cargs=[])
qc.append(add_control(UGate(4.993516280749269, 0.48372007916715914, 3.4370453288464713), num_ctrl_qubits=1, label=None, ctrl_state=None), qargs=[qr[0], qr[1]], cargs=[])
qc.append(add_control(UGate(5.61106634998484, 4.625022873964695, 0.6622664050777831), num_ctrl_qubits=1, label=None, ctrl_state=None), qargs=[qr[0], qr[1]], cargs=[])
qc = transpile(qc, basis_gates=None, optimization_level=1, coupling_map=None)
simulator = Aer.get_backend('statevector_simulator')
def add_or_update_count_list(st_count_list, st):
for i, (key_st, count) in enumerate(st_count_list):
# if np.allclose(key_st, st):
if key_st.equiv(st):
st_count_list[i] = (key_st, count + 1)
return
st_count_list.append((st, 1))
def count_result_statevector(result_statevector_list: list):
"""Count the number of different values of result statevectors."""
result_statevecotr_count = []
for st in result_statevector_list:
add_or_update_count_list(result_statevecotr_count, st)
for i, (key_st, count) in enumerate(result_statevecotr_count):
np_key = np.array(key_st)
original_len = len(np_key)
np_key_trimmed = np.trim_zeros(np_key, trim='b')
trimmed_len = len(np_key_trimmed)
print(f"{i}: {count}, emitting {original_len - trimmed_len} zeros, {np_key_trimmed}, ") # trim trailing zeros
print(f"Number of different result statevectors: {len(result_statevecotr_count)}")
return
results_list = []
for i in range(1000):
result = execute(qc, simulator).result().get_statevector(qc)
results_list.append(Statevector(result))
count_result_statevector(results_list)
I ran the above code and got the following results, which show 29 types of different result state vectors.
0: 956, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -3.01633221e-01+4.07897115e-01j
7.07106781e-01-1.29893408e-16j -2.24140623e-02+4.92072102e-01j],
1: 4, emitting 60 zeros, [5.30245156e-33+4.32978028e-17j 4.32660408e-01+2.64891729e-01j
7.07106781e-01-1.29893408e-16j 4.92151082e-01-2.06071794e-02j],
2: 1, emitting 60 zeros, [5.30245156e-33+4.32978028e-17j 5.02607531e-01-2.47077153e-01j
7.07106781e-01-1.29893408e-16j 4.86492157e-02-4.28919345e-01j],
3: 1, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -5.21450038e-01-2.37083130e-01j
7.07106781e-01-1.29893408e-16j -2.52338686e-01-3.28947769e-01j],
4: 1, emitting 60 zeros, [5.30245156e-33+4.32978028e-17j 5.17810814e-01+2.44929609e-01j
7.07106781e-01-1.29893408e-16j 2.47343054e-01+3.32720395e-01j],
5: 1, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -2.77601817e-01-4.86414104e-01j
7.07106781e-01-1.29893408e-16j -4.31103095e-01-2.21059176e-02j],
6: 2, emitting 60 zeros, [5.30245156e-33+4.32978028e-17j 5.31748765e-01+1.75797328e-01j
7.07106781e-01-1.29893408e-16j 3.35201161e-01-2.71990316e-01j],
7: 1, emitting 60 zeros, [5.30245156e-33+4.32978028e-17j 5.50452165e-01+1.03265983e-01j
7.07106781e-01-1.29893408e-16j 2.95907541e-01-3.14288527e-01j],
8: 4, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -5.52198647e-01-1.52299728e-01j
7.07106781e-01-1.29893408e-16j -3.00841521e-01-2.85264486e-01j],
9: 1, emitting 60 zeros, [5.30245156e-33+4.32978028e-17j 5.59937887e-01+1.14460538e-02j
7.07106781e-01-1.29893408e-16j 2.40263216e-01-3.58625344e-01j],
10: 1, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -4.88417950e-02-5.70730262e-01j
7.07106781e-01-1.29893408e-16j 1.63103885e-01-3.81154260e-01j],
11: 3, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -2.86868306e-01+4.95807551e-01j
7.07106781e-01-1.29893408e-16j -3.52015941e-01+2.19011927e-01j],
12: 2, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -4.85148835e-01-3.04547469e-01j
7.07106781e-01-1.29893408e-16j -2.06185495e-01-3.59679008e-01j],
13: 3, emitting 60 zeros, [5.30245156e-33+4.32978028e-17j 5.31054102e-01-1.77884767e-01j
7.07106781e-01-1.29893408e-16j 1.05381981e-01-4.18608634e-01j],
14: 1, emitting 60 zeros, [5.30245156e-33+4.32978028e-17j 4.46688893e-01-3.81766143e-01j
7.07106781e-01-1.29893408e-16j 2.99019891e-01-2.55559679e-01j],
15: 3, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -5.72289783e-01-2.45551724e-02j
7.07106781e-01-1.29893408e-16j -3.57163088e-01-2.10513600e-01j],
16: 1, emitting 60 zeros, [5.30245156e-33+4.32978028e-17j 1.96385111e-01+5.24494364e-01j
7.07106781e-01-1.29893408e-16j 4.22044406e-01+9.06480570e-02j],
17: 1, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j 1.56829525e-01+5.50929263e-01j
7.07106781e-01-1.29893408e-16j -8.73908570e-02+4.05270632e-01j],
18: 1, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -5.39973137e-01-1.48628600e-01j
7.07106781e-01-1.29893408e-16j -3.20989258e-01+2.88625097e-01j],
19: 1, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -4.86772582e-01+1.42881453e-01j
7.07106781e-01-1.29893408e-16j -3.15090517e-01+3.78622912e-01j],
20: 2, emitting 60 zeros, [5.30245156e-33+4.32978028e-17j 5.72756047e-01+8.31045951e-03j
7.07106781e-01-1.29893408e-16j 3.62991237e-01+2.00296803e-01j],
21: 1, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -5.28242306e-01+2.21536946e-01j
7.07106781e-01-1.29893408e-16j -4.12809402e-01-3.83385552e-02j],
22: 1, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -2.73309770e-01-5.03408703e-01j
7.07106781e-01-1.29893408e-16j -3.18319667e-03-4.14573654e-01j],
23: 1, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j 1.56522639e-01-5.37737959e-01j
7.07106781e-01-1.29893408e-16j -2.83892003e-01-3.25182842e-01j],
24: 1, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -5.57921415e-01-4.88379369e-02j
7.07106781e-01-1.29893408e-16j -2.63691064e-01+3.41768304e-01j],
25: 1, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -4.37260206e-01-3.92530085e-01j
7.07106781e-01-1.29893408e-16j -2.92708193e-01-2.62765215e-01j],
26: 1, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -1.54460694e-02+5.72608044e-01j
7.07106781e-01-1.29893408e-16j -2.04803946e-01+3.60467462e-01j],
27: 2, emitting 60 zeros, [ 5.30245156e-33+4.32978028e-17j -3.38928204e-01+4.61785909e-01j
7.07106781e-01-1.29893408e-16j -3.73684386e-01+1.79558980e-01j],
28: 1, emitting 60 zeros, [5.30245156e-33+4.32978028e-17j 5.43731324e-01-1.34230015e-01j
7.07106781e-01-1.29893408e-16j 1.38984240e-01-4.08683168e-01j],
Number of different result statevectors: 29
What should happen?
I expected the state vector results to be consistent between multiple runs. Please let me know if my hypothesis is wrong.
Any suggestions?
No response
Environment
What is happening?
When running a specific quantum circuit multiple times, I got different state vector results. To the best of my knowledge, the state vector should not change given a fixed quantum circuit.
How can we reproduce the issue?
The below code is a minimal reproduction example for the issue. It defines a quantum circuit with four gates, runs it 1,000 times, and counts the number of different state vector results.
I ran the above code and got the following results, which show 29 types of different result state vectors.
What should happen?
I expected the state vector results to be consistent between multiple runs. Please let me know if my hypothesis is wrong.
Any suggestions?
No response