Skip to content

Commit

Permalink
Bugfix for comparison to zero with torch tensor (#5183)
Browse files Browse the repository at this point in the history
**Context:**
`ctrl_decomp_zyz` with a target with `torch` parameters fails because
`qml.math.isclose(torch.Tensor, float)` raises a `TypeError`. From forum
post
[here](https://discuss.pennylane.ai/t/precautions-to-take-when-switching-to-an-ibm-device/3982/6).

**Description of the Change:**
Switching to `qml.math.isclose(float, torch.Tensor)` seems to appease
PyTorch.

**Related GitHub Issues:**
#5185
  • Loading branch information
lillian542 committed Feb 9, 2024
1 parent 4cfde5a commit 6b127b8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,14 @@
* `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)

* Comparing `Prod` and `Sum` objects now works regardless of nested structure with `qml.equal` if the
operators have a valid `pauli_rep` property.
[(#5177)](https://github.com/PennyLaneAI/pennylane/pull/5177)


<h3>Contributors ✍️</h3>

This release contains contributions from (in alphabetical order):
Expand All @@ -473,6 +477,7 @@ Gabriel Bottrill,
Astral Cai,
Isaac De Vlugt,
Diksha Dhawan,
Lillian Frederiksen,
Eugenio Gigante,
Diego Guala,
Soran Jahangiri,
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

0 comments on commit 6b127b8

Please sign in to comment.