From d20bab19efefbfac34d1dffda47de8dbd542789f Mon Sep 17 00:00:00 2001 From: lillian542 Date: Thu, 8 Feb 2024 15:45:38 -0500 Subject: [PATCH 1/2] reverse order in isclose for torch --- pennylane/ops/op_math/controlled_decompositions.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pennylane/ops/op_math/controlled_decompositions.py b/pennylane/ops/op_math/controlled_decompositions.py index b66a71386dd..26c4c5a1a50 100644 --- a/pennylane/ops/op_math/controlled_decompositions.py +++ b/pennylane/ops/op_math/controlled_decompositions.py @@ -193,9 +193,9 @@ def decomp_circuit(op): decomp = [] - if not qml.math.isclose(phi, 0.0, atol=1e-8, rtol=0): + if not qml.math.isclose(0.0, phi, atol=1e-8, rtol=0): decomp.append(qml.RZ(phi, wires=target_wire)) - if not qml.math.isclose(theta / 2, 0.0, atol=1e-8, rtol=0): + if not qml.math.isclose(0.0, theta / 2, atol=1e-8, rtol=0): decomp.extend( [ qml.RY(theta / 2, wires=target_wire), @@ -205,10 +205,10 @@ def decomp_circuit(op): ) else: decomp.append(qml.ctrl(qml.PauliX(wires=target_wire), control=control_wires)) - if not qml.math.isclose(-(phi + omega) / 2, 0.0, atol=1e-6, rtol=0): + if not qml.math.isclose(0.0, -(phi + omega) / 2, atol=1e-6, rtol=0): decomp.append(qml.RZ(-(phi + omega) / 2, wires=target_wire)) decomp.append(qml.ctrl(qml.PauliX(wires=target_wire), control=control_wires)) - if not qml.math.isclose((omega - phi) / 2, 0.0, atol=1e-8, rtol=0): + if not qml.math.isclose(0.0, (omega - phi) / 2, atol=1e-8, rtol=0): decomp.append(qml.RZ((omega - phi) / 2, wires=target_wire)) return decomp From 4939bc56c03340ac51f60b26b210e31153975c8b Mon Sep 17 00:00:00 2001 From: lillian542 Date: Thu, 8 Feb 2024 17:14:15 -0500 Subject: [PATCH 2/2] add tests and changelog --- doc/releases/changelog-dev.md | 3 +++ .../op_math/test_controlled_decompositions.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 82522d9d509..8c30b0d0572 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -385,6 +385,9 @@ * `default.mixed` no longer throws `ValueError` when applying a state vector that is not of type `complex128` when used with tensorflow. [(#5155)](https://github.com/PennyLaneAI/pennylane/pull/5155) +* `ctrl_decomp_zyz` no longer raises a `TypeError` if the rotation parameters are of type `torch.Tensor` + [(#5183)](https://github.com/PennyLaneAI/pennylane/pull/5183) +

Contributors ✍️

This release contains contributions from (in alphabetical order): diff --git a/tests/ops/op_math/test_controlled_decompositions.py b/tests/ops/op_math/test_controlled_decompositions.py index d0b2560c16f..b51dd44adaf 100644 --- a/tests/ops/op_math/test_controlled_decompositions.py +++ b/tests/ops/op_math/test_controlled_decompositions.py @@ -257,6 +257,22 @@ def test_zyz_decomp_control_values(self, test_expand): expected = qml.ops.ctrl_decomp_zyz(base, (0,)) assert equal_list(decomp, expected) + @pytest.mark.torch + def test_zyz_decomp_with_torch_params(self): + """Tests that the ZYZ decomposition runs when the target operation parameters + are of type torch.Tensor""" + import torch + + target_op1 = qml.RY(torch.Tensor([1.2]), 0) + target_op2 = qml.RY(1.2, 0) + + torch_decomp = ctrl_decomp_zyz(target_op1, 1) + decomp = ctrl_decomp_zyz(target_op2, 1) + + assert np.all( + [qml.equal(op1, op2, check_interface=False) for op1, op2 in zip(torch_decomp, decomp)] + ) + class TestControlledBisectOD: """tests for qml.ops._ctrl_decomp_bisect_od"""