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

Bugfix for comparison to zero with torch tensor #5183

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<h3>Contributors ✍️</h3>

This release contains contributions from (in alphabetical order):
Expand Down
8 changes: 4 additions & 4 deletions pennylane/ops/op_math/controlled_decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/ops/op_math/test_controlled_decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
Loading