Skip to content
Merged
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 @@ -514,6 +514,9 @@

<h3>Bug fixes 🐛</h3>

* Simplifying operators raised to integer powers no longer causes recursion errors.
[(#8044)](https://github.com/PennyLaneAI/pennylane/pull/8044)

* Fixes the GPU selection issue in `qml.math` with PyTorch when multiple GPUs are present.
[(#8008)](https://github.com/PennyLaneAI/pennylane/pull/8008)

Expand Down
2 changes: 1 addition & 1 deletion pennylane/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ def __neg__(self):
"""The negation operation of an Operator object."""
return qml.s_prod(scalar=-1, operator=self, lazy=False)

def __pow__(self, other: TensorLike):
def __pow__(self, other: TensorLike) -> "Operator":
r"""The power operation of an Operator object."""
if isinstance(other, TensorLike):
return qml.pow(self, z=other)
Expand Down
5 changes: 3 additions & 2 deletions pennylane/ops/op_math/pow.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,9 @@ def simplify(self) -> Union["Pow", Identity]:
ops = base.pow(z=self.z)
if not ops:
return qml.Identity(self.wires)
op = qml.prod(*ops) if len(ops) > 1 else ops[0]
return op if qml.capture.enabled() else op.simplify()
if not qml.capture.enabled():
ops = [op.simplify() for op in ops]
return qml.prod(*ops) if len(ops) > 1 else ops[0]
except PowUndefinedError:
return Pow(base=base, z=self.z)

Expand Down
9 changes: 9 additions & 0 deletions tests/ops/op_math/test_pow_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ def test_nonlazy_simplification_queueing(self):

assert original_op not in q.queue

def test_simplify_squared(self):
"""Test that an op without a special pow method can still be simplified when raised to an integer power."""

class DummyOp(qml.operation.Operator):
pass

simplified = (DummyOp(0) ** 2).simplify()
qml.assert_equal(simplified, DummyOp(0) @ DummyOp(0))


@pytest.mark.parametrize("power_method", [Pow, pow_using_dunder_method, qml.pow])
class TestInheritanceMixins:
Expand Down