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

OptimizeSwapBeforeMeasure should only affect last measurements, not mid-circuit measurements #5905

Closed
1ucian0 opened this issue Feb 25, 2021 · 1 comment
Labels
bug Something isn't working mod: transpiler Issues and PRs related to Transpiler

Comments

@1ucian0
Copy link
Member

1ucian0 commented Feb 25, 2021

In a tangentially related PR review, @kdk noticed that OptimizeSwapBeforeMeasure is broken when using mid-circuit measurements.

For example:

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.transpiler.passes import OptimizeSwapBeforeMeasure

qr1 = QuantumRegister(1, 'qr1')
qr2 = QuantumRegister(2, 'qr2')
cr = ClassicalRegister(3, 'cr')
circuit = QuantumCircuit(qr1, qr2, cr)
circuit.h(qr1[0])
circuit.h(qr2[1])
circuit.swap(qr1[0], qr2[0])
circuit.measure(qr1[0], cr[0])
circuit.measure(qr2[0], cr[1])
circuit.measure(qr2[1], cr[2])
circuit.cx(qr1[0], qr2[ 1])
circuit.swap(qr1[0], qr2[0])
circuit.measure(qr1[0], cr[0])
circuit.measure(qr2[0], cr[1])
circuit.measure(qr2[1], cr[2])


print(circuit)
print(OptimizeSwapBeforeMeasure()(circuit))
       ┌───┐   ┌─┐           ┌─┐
qr1_0: ┤ H ├─X─┤M├─────■───X─┤M├───
       └───┘ │ └╥┘┌─┐  │   │ └╥┘┌─┐
qr2_0: ──────X──╫─┤M├──┼───X──╫─┤M├
       ┌───┐┌─┐ ║ └╥┘┌─┴─┐┌─┐ ║ └╥┘
qr2_1: ┤ H ├┤M├─╫──╫─┤ X ├┤M├─╫──╫─
       └───┘└╥┘ ║  ║ └───┘└╥┘ ║  ║
 cr: 3/══════╩══╩══╩═══════╩══╩══╩═
             2  0  1       2  0  1
       ┌───┐              ┌─┐   ┌─┐
qr1_0: ┤ H ├───────────■──┤M├───┤M├
       └───┘┌─┐┌─┐     │  └╥┘   └╥┘
qr2_0: ─────┤M├┤M├─────┼───╫─────╫─
       ┌───┐└╥┘└╥┘┌─┐┌─┴─┐ ║ ┌─┐ ║
qr2_1: ┤ H ├─╫──╫─┤M├┤ X ├─╫─┤M├─╫─
       └───┘ ║  ║ └╥┘└───┘ ║ └╥┘ ║
 cr: 3/══════╩══╩══╩═══════╩══╩══╩═
             0  0  2       1  2  1

The expected result is that OptimizeSwapBeforeMeasure should only affect "final" measurements.

@HuangJunye HuangJunye added the mod: transpiler Issues and PRs related to Transpiler label Jan 4, 2023
@kdk kdk added the bug Something isn't working label Jun 19, 2023
@jakelishman
Copy link
Member

This is no longer reproducible as of Qiskit 1.0.0 - probably it was fixed by #11413.

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: transpiler Issues and PRs related to Transpiler
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants