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

Fix flipping of gate direction in transpiler for CZ gate #8625

Merged
merged 9 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion qiskit/transpiler/passes/utils/gate_direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from qiskit.circuit import QuantumRegister
from qiskit.dagcircuit import DAGCircuit
from qiskit.circuit.library.standard_gates import RYGate, HGate, CXGate, ECRGate, RZXGate
from qiskit.circuit.library.standard_gates import RYGate, HGate, CXGate, CZGate, ECRGate, RZXGate


class GateDirection(TransformationPass):
Expand Down Expand Up @@ -78,6 +78,11 @@ def __init__(self, coupling_map, target=None):
self._ecr_dag.apply_operation_back(HGate(), [qr[0]], [])
self._ecr_dag.apply_operation_back(HGate(), [qr[1]], [])

self._cz_dag = DAGCircuit()
qr = QuantumRegister(2)
self._cz_dag.add_qreg(qr)
self._cz_dag.apply_operation_back(CZGate(), [qr[1], qr[0]], [])

@staticmethod
def _rzx_dag(parameter):
_rzx_dag = DAGCircuit()
Expand Down Expand Up @@ -138,6 +143,8 @@ def run(self, dag):
if (physical_q0, physical_q1) not in cmap_edges:
if node.name == "cx":
dag.substitute_node_with_dag(node, self._cx_dag)
elif node.name == "cz":
dag.substitute_node_with_dag(node, self._cz_dag)
elif node.name == "ecr":
dag.substitute_node_with_dag(node, self._ecr_dag)
elif node.name == "rzx":
Expand Down Expand Up @@ -169,6 +176,16 @@ def run(self, dag):
"The circuit requires a connection between physical "
"qubits %s and %s for cx" % (physical_q0, physical_q1)
)
elif node.name == "cz":
if (physical_q0, physical_q1) in self.target["cz"]:
continue
if (physical_q1, physical_q0) in self.target["cz"]:
dag.substitute_node_with_dag(node, self._cz_dag)
else:
raise TranspilerError(
"The circuit requires a connection between physical "
"qubits %s and %s for cz" % (physical_q0, physical_q1)
)
elif node.name == "ecr":
if (physical_q0, physical_q1) in self.target["ecr"]:
continue
Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/fix-flipping-cz-gate-fd08305ca12d9a79.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Fix a problem in the `GateDirection` transpiler pass for the CZ gate. The CZ is symmetric, so flipping the qubit arguments is allowed to match the directed coupling map.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Fix a problem in the `GateDirection` transpiler pass for the CZ gate. The CZ is symmetric, so flipping the qubit arguments is allowed to match the directed coupling map.
Fixed an issue in the :class:`~.GateDirection` transpiler pass for circuits with :class:`~.CZGate. The CZ gate is symmetric, so flipping the qubit arguments is allowed to match the directed coupling map.


16 changes: 16 additions & 0 deletions test/python/transpiler/test_gate_direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from math import pi

from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit
from qiskit.compiler import transpile
from qiskit.transpiler import TranspilerError
from qiskit.transpiler import CouplingMap
from qiskit.transpiler.passes import GateDirection
Expand Down Expand Up @@ -245,6 +246,21 @@ def test_preserves_conditions(self):

self.assertEqual(circuit_to_dag(expected), after)

def test_regression_gh_8387(self):
"""Regression test for flipping of CZ gate"""
qc = QuantumCircuit(3)
qc.cz(1, 0)
qc.barrier()
qc.cz(2, 0)

coupling_map = CouplingMap([[0, 1], [1, 2]])
_ = transpile(
qc,
basis_gates=["cz", "cx", "u3", "u2", "u1"],
coupling_map=coupling_map,
optimization_level=2,
)


if __name__ == "__main__":
unittest.main()