Skip to content

Unnecessary check and exception in GateDirection pass #10824

@chriseclectic

Description

@chriseclectic

Environment

  • Qiskit Terra version: 0.25
  • Python version: 3.9
  • Operating system: MacOS

What is happening?

The GateDirection transpiler pass has an unnecessary check that input circuits only contain 1 register:

https://github.com/Qiskit/qiskit/blob/main/qiskit/transpiler/passes/utils/gate_direction.py#L328-L332

How can we reproduce the issue?

from qiskit.circuit import QuantumRegister, QuantumCircuit
from qiskit.transpiler import PassManager, CouplingMap
from qiskit.transpiler.passes import GateDirection

q1 = QuantumRegister(1)
q2 = QuantumRegister(1)
qc = QuantumCircuit(q1, q2)
qc.cx(1, 0)
qc.cx(0, 1)

cmap = CouplingMap([[0, 1]])
pm = PassManager([GateDirection(coupling_map=cmap)])
pm.run(qc)

raises

TranspilerError: "GateDirection expects a single qreg input DAG,but input DAG had qregs: OrderedDict([('q67', QuantumRegister(1, 'q67')), ('q68', QuantumRegister(1, 'q68'))])."

What should happen?

If the check is removed it works as you would expect:

class GateDirectionFixed(GateDirection):
    
    def run(self, dag):
        layout_map = {bit: i for i, bit in enumerate(dag.qubits)}
        if self.target is None:
            return self._run_coupling_map(dag, layout_map)
        return self._run_target(dag, layout_map)

pm = PassManager([GateDirectionFixed(coupling_map=cmap)])
pm.run(qc).draw()
     ┌───┐     ┌───┐     
q77: ┤ H ├──■──┤ H ├──■──
     ├───┤┌─┴─┐├───┤┌─┴─┐
q78: ┤ H ├┤ X ├┤ H ├┤ X ├
     └───┘└───┘└───┘└───┘

Any suggestions?

Remove the incorrect check for a single register from the GateDirection.run method

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions