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 decomposition for gates.CRY #1547

Merged
merged 3 commits into from
Dec 18, 2024
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
8 changes: 8 additions & 0 deletions src/qibo/gates/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,14 @@ def __init__(self, q0, q1, theta, trainable=True):
def qasm_label(self):
return "cry"

def decompose(self) -> List[Gate]:
"""Decomposition of :math:`\\text{CRY}` gate."""
from qibo.transpiler.decompositions import ( # pylint: disable=C0415
standard_decompositions,
)

return standard_decompositions(self)


class CRZ(_CRn_):
"""Controlled rotation around the Z-axis for the Bloch sphere.
Expand Down
10 changes: 10 additions & 0 deletions src/qibo/transpiler/decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,16 @@ def _decomposition_generalized_RBS(ins, outs, theta, phi, controls):
standard_decompositions.add(
gates.CSXDG, [gates.H(1), gates.CU1(0, 1, -np.pi / 2), gates.H(1)]
)
standard_decompositions.add(
gates.CRY,
lambda gate: [
gates.RY(1, gate.parameters[0] / 4),
gates.CNOT(0, 1),
gates.RY(1, -gate.parameters[0] / 2),
gates.CNOT(0, 1),
gates.RY(1, gate.parameters[0] / 4),
],
)
standard_decompositions.add(
gates.RZX,
lambda gate: [
Expand Down
9 changes: 9 additions & 0 deletions tests/test_gates_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,9 @@ def test_cun(backend, name, params):

gate = getattr(gates, name)(0, 1, **params)

if name == "CRY":
decomposition = gate.decompose()

assert gate.unitary

if name != "CU2":
Expand All @@ -722,6 +725,12 @@ def test_cun(backend, name, params):

backend.assert_allclose(final_state, target_state, atol=1e-6)

if name == "CRY":
matrix = Circuit(2)
matrix.add(decomposition)
renatomello marked this conversation as resolved.
Show resolved Hide resolved
matrix = matrix.unitary(backend=backend)
backend.assert_allclose(matrix, _matrix, atol=1e-10)


def test_swap(backend):
final_state = apply_gates(backend, [gates.X(1), gates.SWAP(0, 1)], nqubits=2)
Expand Down
Loading