Closed p51lee closed 7 months ago
I would like to work on this!
The bug is in OptimizeSwapBeforeMeasure
, which uses DAGCircuit.successors
(immediate successors) as opposed to DAGCircuit.descendants
(all nodes that are topologically after the given). It should hopefully be an easy fix there, though it'd be good to add a couple more tests, such as when there's multiple swaps before several multiply-measured qubits, just to be sure.
A more minimal reproducer showing the bug:
from qiskit import QuantumCircuit
from qiskit.transpiler.passes import OptimizeSwapBeforeMeasure
pass_ = OptimizeSwapBeforeMeasure()
qc = QuantumCircuit(2, 1)
qc.swap(0, 1)
qc.measure(0, 0)
qc.measure(0, 0)
print(qc.draw())
print(pass_(qc).draw())
This prints
┌─┐┌─┐
q_0: ─X─┤M├┤M├
│ └╥┘└╥┘
q_1: ─X──╫──╫─
║ ║
c: 1/════╩══╩═
0 0
┌─┐
q_0: ┤M├───
└╥┘┌─┐
q_1: ─╫─┤M├
║ └╥┘
c: 1/═╩══╩═
0 0
but the second circuit should be
q_0: ──────
┌─┐┌─┐
q_1: ┤M├┤M├
└╥┘└╥┘
c: 1/═╩══╩═
0 0
@border-b: I can assign you, thanks!
@jakelishman, sorry I was a bit busy with work these past couple of weeks.
You're right, changing DAGCircuit.successors
to DAGCircuit.descendants
does seem to solve the issue. I'm gonna try to add some tests.
Environment
What is happening?
Hello, The
qiskit.transpile
function atoptimization_level=3
incorrectly optimizes circuits involving swap and measure instructions. This leads to incorrect possible outcomes when the circuit is executed.How can we reproduce the issue?
The issue can be replicated using the following Python code:
The possible outcomes differ between the original and optimized circuits:
What should happen?
The optimized circuit should yield only
000
and010
outcomes, consistent with the original circuit.Any suggestions?
The potential source of the bug could be in qiskit/transpiler/passes/optimization/optimize_swap_before_measure