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

Add equivalence library entry for swap to ECR or CZ #12312

Merged
merged 10 commits into from
May 2, 2024
65 changes: 65 additions & 0 deletions qiskit/circuit/library/standard_gates/equivalence_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,71 @@ def _cnot_rxx_decompose(plus_ry: bool = True, plus_rxx: bool = True):
def_swap.append(inst, qargs, cargs)
_sel.add_equivalence(SwapGate(), def_swap)

# SwapGate
#
# q_0: ─X─
# │ ≡
# q_1: ─X─
#
# ┌──────────┐┌──────┐ ┌────┐ ┌──────┐┌──────────┐┌──────┐
# q_0: ┤ Rz(-π/2) ├┤0 ├───┤ √X ├───┤1 ├┤ Rz(-π/2) ├┤0 ├
# └──┬────┬──┘│ Ecr │┌──┴────┴──┐│ Ecr │└──┬────┬──┘│ Ecr │
# q_1: ───┤ √X ├───┤1 ├┤ Rz(-π/2) ├┤0 ├───┤ √X ├───┤1 ├
# └────┘ └──────┘└──────────┘└──────┘ └────┘ └──────┘
#
q = QuantumRegister(2, "q")
def_swap_ecr = QuantumCircuit(q)
def_swap_ecr.rz(-pi / 2, 0)
def_swap_ecr.sx(1)
def_swap_ecr.ecr(0, 1)
def_swap_ecr.rz(-pi / 2, 1)
def_swap_ecr.sx(0)
def_swap_ecr.ecr(1, 0)
def_swap_ecr.rz(-pi / 2, 0)
def_swap_ecr.sx(1)
def_swap_ecr.ecr(0, 1)
_sel.add_equivalence(SwapGate(), def_swap_ecr)

mtreinish marked this conversation as resolved.
Show resolved Hide resolved
# SwapGate
#
# q_0: ─X─
# │ ≡
# q_1: ─X─
#
# global phase: 7π/4
# ┌─────────┐ ┌────┐ ┌────┐ ┌────────┐»
# q_0: ┤ Rz(π/2) ├──┤ √X ├───■────┤ √X ├──────────────────────■──┤ Rz(-π) ├»
# └──┬────┬─┘┌─┴────┴─┐ │ ┌──┴────┴──┐┌────┐┌──────────┐ │ ┌┴────────┤»
# q_1: ───┤ √X ├──┤ Rz(-π) ├─■─┤ Rz(-π/2) ├┤ √X ├┤ Rz(-π/2) ├─■─┤ Rz(π/2) ├»
# └────┘ └────────┘ └──────────┘└────┘└──────────┘ └─────────┘»
# « ┌────┐┌──────────┐ ┌────┐ ┌─────────┐
# «q_0: ┤ √X ├┤ Rz(-π/2) ├─■────┤ √X ├──┤ Rz(π/2) ├
# « ├────┤└──────────┘ │ ┌──┴────┴─┐└─────────┘
# «q_1: ┤ √X ├─────────────■─┤ Rz(π/2) ├───────────
# « └────┘ └─────────┘
q = QuantumRegister(2, "q")
def_swap_cz = QuantumCircuit(q, global_phase=7 * pi / 4)
def_swap_cz.rz(pi / 2, 0)
def_swap_cz.sx(0)
def_swap_cz.sx(1)
def_swap_cz.rz(-pi, 1)
def_swap_cz.cz(0, 1)
def_swap_cz.sx(0)
def_swap_cz.rz(-pi / 2, 1)
def_swap_cz.sx(1)
def_swap_cz.rz(-pi / 2, 1)
def_swap_cz.cz(0, 1)
def_swap_cz.rz(-pi, 0)
def_swap_cz.sx(0)
def_swap_cz.rz(-pi / 2, 0)
def_swap_cz.rz(pi / 2, 1)
def_swap_cz.sx(1)
def_swap_cz.cz(0, 1)
def_swap_cz.sx(0)
def_swap_cz.rz(pi / 2, 0)
def_swap_cz.rz(pi / 2, 1)
_sel.add_equivalence(SwapGate(), def_swap_cz)
mtreinish marked this conversation as resolved.
Show resolved Hide resolved

# iSwapGate
#
# ┌────────┐ ┌───┐┌───┐ ┌───┐
Expand Down
14 changes: 12 additions & 2 deletions test/python/circuit/test_gate_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.quantum_info import Operator
from qiskit.circuit import ParameterVector, Gate, ControlledGate
from qiskit.circuit.singleton import SingletonGate, SingletonControlledGate
from qiskit.circuit.library import standard_gates
from qiskit.circuit.library import (
HGate,
Expand Down Expand Up @@ -260,7 +261,12 @@ class TestGateEquivalenceEqual(QiskitTestCase):
"""Test the decomposition of a gate in terms of other gates
yields the same matrix as the hardcoded matrix definition."""

class_list = Gate.__subclasses__() + ControlledGate.__subclasses__()
class_list = (
SingletonGate.__subclasses__()
+ SingletonControlledGate.__subclasses__()
+ Gate.__subclasses__()
+ ControlledGate.__subclasses__()
)
exclude = {
"ControlledGate",
"DiagonalGate",
Expand Down Expand Up @@ -313,7 +319,11 @@ def test_equivalence_phase(self, gate_class):
with self.subTest(msg=gate.name + "_" + str(ieq)):
op1 = Operator(gate)
op2 = Operator(equivalency)
self.assertEqual(op1, op2)
msg = (
f"Equivalence entry from '{gate.name}' to:\n"
f"{str(equivalency.draw('text'))}\nfailed"
)
self.assertEqual(op1, op2, msg)


@ddt
Expand Down
2 changes: 1 addition & 1 deletion test/qpy_compat/test_qpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ def generate_annotated_circuits():
CXGate(), [InverseModifier(), ControlModifier(1), PowerModifier(1.4), InverseModifier()]
)
op2 = AnnotatedOperation(XGate(), InverseModifier())
qc = QuantumCircuit(6, 1)
qc = QuantumCircuit(6, 1, name="Annotated circuits")
qc.cx(0, 1)
qc.append(op1, [0, 1, 2])
qc.h(4)
Expand Down
Loading