Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
4.85k stars 2.29k forks source link

Transpilation Failure at Optimization Level 3 #12125

Open p51lee opened 3 months ago

p51lee commented 3 months ago

Environment

What is happening?

qiskit.transpile with optimization_level=3 fails to transpile this circuit:

circ = qiskit.QuantumCircuit(3, 1)
circ.swap(1, 2)
circ.measure(1, 0)
circ.measure(0, 0)

How can we reproduce the issue?

To reproduce the issue, execute the following code:

import qiskit

circ = qiskit.QuantumCircuit(3, 1)
circ.swap(1, 2)
circ.measure(1, 0)
circ.measure(0, 0)

print(qiskit.transpile(circ, optimization_level=3))

Running this code results in ValueError:

File "qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py", line 65, in run
    new_measure_qarg = swap_qargs[swap_qargs.index(old_measure_qarg) - 1]
ValueError: tuple.index(x): x not in tuple

What should happen?

The circuit should be transpiled without any errors.

Any suggestions?

No response

jakelishman commented 3 months ago

Thanks for the report. I was just looking into this and made #12128 to point out a few more things I'd spotted in OptimizeSwapBeforeMeasure, but as I was doing that, I found a bunch more places where the pass really doesn't seem to have the correct behaviour. There's changes coming in Rustworkx 0.15 that should let us do this efficiently rather more soundly - at the moment, I'm a bit worried that there's more soundness holes in OptimizeSwapBeforeMeasure.

1ucian0 commented 3 months ago

This issue also affects 0.46