From 8b28b839d7d2ea9f3c24d3aec490df558123176d Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Tue, 17 Dec 2024 13:35:28 +0400 Subject: [PATCH 1/3] decompose CRY --- src/qibo/gates/gates.py | 8 ++++++++ src/qibo/transpiler/decompositions.py | 10 ++++++++++ tests/test_gates_gates.py | 6 ++++++ 3 files changed, 24 insertions(+) diff --git a/src/qibo/gates/gates.py b/src/qibo/gates/gates.py index 618279c897..1a56e8cdfb 100644 --- a/src/qibo/gates/gates.py +++ b/src/qibo/gates/gates.py @@ -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. diff --git a/src/qibo/transpiler/decompositions.py b/src/qibo/transpiler/decompositions.py index 47cec5cec0..43ab7092fe 100644 --- a/src/qibo/transpiler/decompositions.py +++ b/src/qibo/transpiler/decompositions.py @@ -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: [ diff --git a/tests/test_gates_gates.py b/tests/test_gates_gates.py index f47f26d613..67fe0229f2 100644 --- a/tests/test_gates_gates.py +++ b/tests/test_gates_gates.py @@ -722,6 +722,12 @@ def test_cun(backend, name, params): backend.assert_allclose(final_state, target_state, atol=1e-6) + if name == "CRY": + decomposition = Circuit(2) + decomposition.add(gate.decompose()) + decomposition = decomposition.unitary(backend=backend) + backend.assert_allclose(decomposition, _matrix) + def test_swap(backend): final_state = apply_gates(backend, [gates.X(1), gates.SWAP(0, 1)], nqubits=2) From 52c5c1b77cadf923e379f1aca083ad3d1e784b82 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Tue, 17 Dec 2024 14:37:57 +0400 Subject: [PATCH 2/3] fix test --- tests/test_gates_gates.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/test_gates_gates.py b/tests/test_gates_gates.py index 67fe0229f2..ed3e6cc4a3 100644 --- a/tests/test_gates_gates.py +++ b/tests/test_gates_gates.py @@ -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": @@ -723,10 +726,10 @@ def test_cun(backend, name, params): backend.assert_allclose(final_state, target_state, atol=1e-6) if name == "CRY": - decomposition = Circuit(2) - decomposition.add(gate.decompose()) - decomposition = decomposition.unitary(backend=backend) - backend.assert_allclose(decomposition, _matrix) + matrix = Circuit(2) + matrix.add(decomposition) + matrix = matrix.unitary(backend=backend) + backend.assert_allclose(matrix, _matrix) def test_swap(backend): From 62bed01dc9d2efe7af4158eb0a28e6e5b50bbb4b Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Tue, 17 Dec 2024 15:02:08 +0400 Subject: [PATCH 3/3] add tolerance --- tests/test_gates_gates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_gates_gates.py b/tests/test_gates_gates.py index ed3e6cc4a3..ccd04dd939 100644 --- a/tests/test_gates_gates.py +++ b/tests/test_gates_gates.py @@ -729,7 +729,7 @@ def test_cun(backend, name, params): matrix = Circuit(2) matrix.add(decomposition) matrix = matrix.unitary(backend=backend) - backend.assert_allclose(matrix, _matrix) + backend.assert_allclose(matrix, _matrix, atol=1e-10) def test_swap(backend):