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

midcircuit measurement issue with StatevectorSampler #12281

Open
diemilio opened this issue Apr 19, 2024 · 0 comments
Open

midcircuit measurement issue with StatevectorSampler #12281

diemilio opened this issue Apr 19, 2024 · 0 comments
Labels
bug Something isn't working mod: primitives Related to the Primitives module

Comments

@diemilio
Copy link
Contributor

diemilio commented Apr 19, 2024

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?

@diemilio diemilio added the bug Something isn't working label Apr 19, 2024
@mtreinish mtreinish added the mod: primitives Related to the Primitives module label Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: primitives Related to the Primitives module
Projects
None yet
Development

No branches or pull requests

2 participants