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

[WIP][Bug Fix] Fixing the pow method on qml.TShift #6356

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -321,6 +321,9 @@
[(#6278)](https://github.com/PennyLaneAI/pennylane/pull/6278)
[(#6310)](https://github.com/PennyLaneAI/pennylane/pull/6310)

* Fixes a bug preventing `qml.TAdd`, `qml.TClock`, and `qml.TShift` from being raised to the power of 2.
[(#6356)](https://github.com/PennyLaneAI/pennylane/pull/6356)

* Fixes a test after updating to the nightly version of Catalyst.
[(#6362)](https://github.com/PennyLaneAI/pennylane/pull/6362)

Expand Down
19 changes: 9 additions & 10 deletions pennylane/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1478,16 +1478,15 @@ def pow(self, z: float) -> list["Operator"]:
list[:class:`~.operation.Operator`]

"""
# Child methods may call super().pow(z%period) where op**period = I
# For example, PauliX**2 = I, SX**4 = I
# Hence we define 0 and 1 special cases here.
if z == 0:
return []
if z == 1:
if QueuingManager.recording():
return [qml.apply(self)]
return [copy.copy(self)]
raise PowUndefinedError
if not isinstance(z, int):
raise PowUndefinedError

if z < 0:
raise PowUndefinedError

if QueuingManager.recording():
return [qml.apply(self) for _ in range(z)]
return [copy.copy(self) for _ in range(z)]

def queue(self, context: QueuingManager = QueuingManager):
"""Append the operator to the Operator queue."""
Expand Down
13 changes: 9 additions & 4 deletions tests/ops/qutrit/test_qutrit_non_parametric_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,17 @@ def test_period_three_ops_pow_offset_1(self, op, offset):
@pytest.mark.parametrize("op", period_three_ops)
@pytest.mark.parametrize("offset", (0, 3))
def test_period_three_ops_pow_offset_2(self, op, offset):
"""Tests that ops with a period ==3 raise a PowUndefinedError when raised
"""Tests that ops with a period ==3 two queued copies of themselves when
to a power that is 2+multiple of three."""

# When raising to power == 2 mod 3
with pytest.raises(qml.operation.PowUndefinedError):
op.pow(2 + offset)
with qml.queuing.AnnotatedQueue() as q:
op_list = op.pow(2 + offset)

assert q.queue[0] is op_list[0]
assert q.queue[1] is op_list[1]
qml.assert_equal(op_list[0], op)
qml.assert_equal(op_list[1], op)

@pytest.mark.parametrize("op", period_three_ops + period_two_ops)
def test_period_two_three_noninteger_power(self, op):
Expand All @@ -168,7 +173,7 @@ def test_no_pow_ops(self, op):
assert len(op_pow) == 1
assert op_pow[0].__class__ == op.__class__

pows = [0.1, 2, -2, -2.5]
pows = [0.1, -2.5]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pows = [0.1, -2.5]
pows = [0.1, -2, -2.5]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-2 should not raise PowUndefinedError since -2 % 4 = 2 is what gets passed into pow which is now defined. Maybe this test is not necessary since THadamard is tested in test_period_two_three_noninteger_power 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the %4?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I just realized that this is a test for qutrit ops


for pow in pows:
with pytest.raises(qml.operation.PowUndefinedError):
Expand Down
Loading