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.91k stars 2.31k forks source link

transpiler at optimization level 3 does not honor the coupling map direction #6406

Open 1ucian0 opened 3 years ago

1ucian0 commented 3 years ago

Information

What is the current behavior?

Take the following scenario:
```python
from qiskit import *
circuit = QuantumCircuit(QuantumRegister(5, "qr"))
circuit.ccx(0, 1, 2)
transpiled = transpile(circuit, coupling_map=[[0, 1], [1, 0], [1, 2], [2, 1], [3, 1], [3, 4], [4, 3]],
                     basis_gates=['id', 'rz', 'sx', 'x', 'cx'], optimization_level=3, seed_transpiler=42)

Notice that the edge [1, 3] is not there. However, this is part of the output:

«qr_4 -> 0 ─────────────────────────────────────────────────────────────────»
«                    ┌────┐    ┌───────┐                                    »
«qr_3 -> 1 ──■───────┤ √X ├────┤ RZ(π) ├────────────────────────────────────»
«            │       └────┘    └───────┘                                    »
«qr_2 -> 2 ──┼──────────────────────────────────────────────────────────────»
«          ┌─┴─┐┌─────────────┐  ┌────┐ ┌─────────────┐┌────┐┌─────────────┐»
«qr_1 -> 3 ┤ X ├┤ RZ(-2.4938) ├──┤ √X ├─┤ RZ(-1.0478) ├┤ √X ├┤ RZ(-0.5834) ├»
«          └───┘└─────────────┘  └────┘ └─────────────┘└────┘└─────────────┘»
«qr_0 -> 4 ─────────────────────────────────────────────────────────────────»

The first cnot in this page is going in the 1, 3 direction.

I suspect that the optimization loop is assuming a symmetric coupling map. If I move the GateDirection at the end of the pass manager, it works correctly.

1ucian0 commented 3 years ago

I simplified the example even further:

from qiskit import *
circuit = QuantumCircuit(QuantumRegister(2, "qr"))
circuit.cx(0, 1)
transpiled=transpile(circuit, coupling_map=[[1, 0]],
                     basis_gates=['id', 'rz', 'sx', 'x', 'cx'], optimization_level=3,
                     initial_layout=range(2), seed_transpiler=42)
global phase: π
             ┌───┐                                                   ┌───┐    »
qr_0 -> 0 ───┤ X ├─────────────────────────────────────────────■─────┤ X ├────»
          ┌──┴───┴──┐┌────┐┌──────────────┐┌────┐┌──────────┐┌─┴─┐┌──┴───┴───┐»
qr_1 -> 1 ┤ RZ(π/2) ├┤ √X ├┤ RZ(-0.76394) ├┤ √X ├┤ RZ(-π/2) ├┤ X ├┤ RZ(-π/2) ├»
          └─────────┘└────┘└──────────────┘└────┘└──────────┘└───┘└──────────┘»
«          ┌────────┐                                
«qr_0 -> 0 ┤ RZ(-π) ├────────────────────────────────
«          └─┬────┬─┘┌─────────────┐┌────┐┌─────────┐
«qr_1 -> 1 ──┤ √X ├──┤ RZ(-2.3777) ├┤ √X ├┤ RZ(π/2) ├
«            └────┘  └─────────────┘└────┘└─────────┘