-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Description
Environment
- Qiskit version: 1.01
- Python version: 3.11
- Operating system: macOS
What is happening?
StatevectorSampler
is not supposed to support midcircuit measurements. However, in its current implementation, there are instances where instead of throwing an error, it produces the wrong result.
How can we reproduce the issue?
Example:
from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler
from qiskit.providers.basic_provider import BasicSimulator
simulator = BasicSimulator()
sampler = StatevectorSampler()
qc = QuantumCircuit(2,2)
qc.h(1)
qc.measure(1,1)
qc.x(0).c_if(1,1)
qc.measure(0,0)
display(qc.draw('text',cregbundle=False))
sim_counts = simulator.run(qc, shots=1024).result().get_counts()
samp_counts = sampler.run([qc], shots=1024).result()[0].data.c.get_counts()
print(f'BasicSimulator Counts: {sim_counts}')
print(f'StatevectorSampler Counts: {samp_counts}')
Output:
┌───┐┌─┐
q_1: ┤ H ├┤M├────────
└───┘└╥┘┌───┐┌─┐
q_0: ──────╫─┤ X ├┤M├
║ └─╥─┘└╥┘
c_1: ══════╩═══■═══╬═
║
c_0: ══════════════╩═
BasicSimulator Counts: {'00': 519, '11': 505}
StatevectorSampler Counts: {'01': 503, '11': 521}
Notice that the expected output is ~ 50% 00
- 50% 11
, but sampler returns ~ 50% 01
- 50% 11
.
What should happen?
StatevectorSampler
should throw an error because the measurement result in c_1
is being used to control an XGate
, but instead it is ignoring the control and always applying the gate independent on the value on c_1
, producing incorrect results.
Any suggestions?
Haven't spent much time looking at the code, but it seems that the issue stems from StatevectorSampler
only checking if there are repeated measurement blocks on the same qubit. In this example, there's only one measurement per qubit, so both get removed as if they were "final measurements". The circuit then executes without error, but obviously produces incorrect results because it is treating c_1
as always being equal to 0
.
Not sure what the right solution would be at this point, but probably checking for dynamic-circuit conditionals?