diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 8d43c417638..fc0a25b817a 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -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) diff --git a/pennylane/operation.py b/pennylane/operation.py index b2073f7a025..d8c3845cb84 100644 --- a/pennylane/operation.py +++ b/pennylane/operation.py @@ -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.""" diff --git a/tests/ops/qutrit/test_qutrit_non_parametric_ops.py b/tests/ops/qutrit/test_qutrit_non_parametric_ops.py index 5375cf6a87d..2e30a37dd59 100644 --- a/tests/ops/qutrit/test_qutrit_non_parametric_ops.py +++ b/tests/ops/qutrit/test_qutrit_non_parametric_ops.py @@ -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): @@ -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] for pow in pows: with pytest.raises(qml.operation.PowUndefinedError):