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 crzz decomposition #74

Merged
merged 2 commits into from
May 2, 2023
Merged
Changes from 1 commit
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
Next Next commit
add crzz decomposition
kevinsung committed Apr 29, 2023
commit f4ff8f9ccb053956a406e0acb34d6f767f1e4e3b
40 changes: 37 additions & 3 deletions qiskit_research/utils/gate_decompositions.py
Original file line number Diff line number Diff line change
@@ -17,14 +17,15 @@
from collections.abc import Iterator

from qiskit import QuantumRegister
from qiskit.circuit import Gate, Qubit
from qiskit.circuit import ControlledGate, Gate, Qubit
from qiskit.circuit.library import (
CXGate,
HGate,
SGate,
SdgGate,
RXGate,
RZGate,
RZXGate,
SdgGate,
SGate,
XGate,
XXMinusYYGate,
XXPlusYYGate,
@@ -122,6 +123,39 @@ def run(
return dag


class ControlledRZZToCX(TransformationPass):
"""Transformation pass to decompose Controlled RZZGate to CXGate."""

def _decomposition(
self,
register: QuantumRegister,
gate: ControlledGate,
) -> Iterator[tuple[Gate, tuple[Qubit, ...]]]:
a, b, c = register
(theta,) = gate.params

yield CXGate(), (b, c)
yield RZGate(theta).control(1), (a, c)
yield CXGate(), (b, c)

def run(
self,
dag: DAGCircuit,
) -> DAGCircuit:
for run in dag.collect_runs(["crzz"]):
for node in run:
mini_dag = DAGCircuit()
register = QuantumRegister(3)
mini_dag.add_qreg(register)

for instr, qargs in self._decomposition(register, node.op):
mini_dag.apply_operation_back(instr, qargs)

dag.substitute_node_with_dag(node, mini_dag)

return dag


class XXPlusYYtoRZX(TransformationPass):
"""Transformation pass to decompose XXPlusYYGate to RZXGate."""

18 changes: 16 additions & 2 deletions test/utils/test_gate_decompositions.py
Original file line number Diff line number Diff line change
@@ -16,10 +16,12 @@

import numpy as np
from qiskit.circuit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XXMinusYYGate, XXPlusYYGate
from qiskit.transpiler import PassManager
from qiskit.circuit.library import RZZGate, XXMinusYYGate, XXPlusYYGate
from qiskit.quantum_info import Operator
from qiskit.transpiler import PassManager

from qiskit_research.utils.gate_decompositions import (
ControlledRZZToCX,
RZXWeylDecomposition,
XXMinusYYtoRZX,
XXPlusYYtoRZX,
@@ -29,6 +31,18 @@
class TestPasses(unittest.TestCase):
"""Test passes."""

def test_controlled_rzz_to_cx(self):
"""Test controlled RZZGate to CXGate decomposition."""
theta = np.random.uniform(-10, 10)
gate = RZZGate(theta).control(1)
register = QuantumRegister(3)
circuit = QuantumCircuit(register)
circuit.append(gate, register)
pass_ = ControlledRZZToCX()
pass_manager = PassManager([pass_])
decomposed = pass_manager.run(circuit)
self.assertTrue(Operator(circuit).equiv(Operator(decomposed)))

def test_xxplusyy_to_rzx(self):
"""Test XXPlusYYGate to RZXGate decomposition."""
theta = np.random.uniform(-10, 10)