Skip to content

Commit

Permalink
Merge branch 'master' into rename_methods
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Dec 18, 2024
2 parents 1a77c4a + da0d5cd commit c6b7295
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
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)
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

0 comments on commit c6b7295

Please sign in to comment.