Skip to content

Commit 53df3dc

Browse files
JerryChen97albi3ro
andauthored
Fix double QubitUnitary bug from Catalyst (#6926)
**Context:** A recent deprecation #6840 deprecated the usage of `QubitUnitary` type input `base` for instantiation of `ControlledQubitUnitary`. However, a modification that was equivalent in PennyLane caused unexpected behavior in Catalyst, PennyLaneAI/catalyst#1494. After investigation, it appears to be the issue with the private method `_try_wrap_in_custom_ctrl_op` of `pennylane/ops/op_math/controlled.py`, since it used to call the deprecated `init` to directly use base as `QubitUnitary`, and to safely deprecate it we decided to locally replace with equivalent `ControlledQubitUnitary(base=op.matrix(), ...)`, this caused Catalyst to received double `QubitUnitary`. It is also noteable that the call `ControlledQubitUnitary` should not appear in this script, since logically all the classes living in `controlled_ops` depend on `controlled.py`, which means that from the very beginning the affected private method served as a tricky hack with cyclic dependence. Therefore, we would like to replace the `ControlledQubitUnitary` call with a direct `ControlledOp`. **Description of the Change:** source files: as mentioned above. tests: necessary improvements were made to correctly reflect the assertion results. **Benefits:** - Remove bugs - Less cyclic dependency **Possible Drawbacks:** - Probably there're more that depend on this method, which might be affected by this fix **Related GitHub Issues:** PennyLaneAI/catalyst#1494 --------- Co-authored-by: Christina Lee <[email protected]>
1 parent 0649606 commit 53df3dc

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

doc/releases/changelog-dev.md

+2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@
192192
<h3>Deprecations 👋</h3>
193193

194194
* The ``ControlledQubitUnitary`` will stop accepting `QubitUnitary` objects as arguments as its ``base``. Instead, use ``qml.ctrl`` to construct a controlled `QubitUnitary`.
195+
A folllow-on PR fixed accidental double-queuing when using `qml.ctrl` with `QubitUnitary`.
195196
[(#6840)](https://github.com/PennyLaneAI/pennylane/pull/6840)
197+
[(#6926)](https://github.com/PennyLaneAI/pennylane/pull/6926)
196198

197199
* The `control_wires` argument in `qml.ControlledQubitUnitary` has been deprecated.
198200
Instead, use the `wires` argument as the second positional argument.

pennylane/ops/op_math/controlled.py

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""
1515
This submodule defines the symbolic operation that indicates the control of an operator.
1616
"""
17+
# pylint: disable=too-many-positional-arguments
1718
import functools
1819
import warnings
1920
from collections.abc import Callable, Sequence
@@ -338,6 +339,7 @@ def _try_wrap_in_custom_ctrl_op(op, control, control_values=None, work_wires=Non
338339
return ops_with_custom_ctrl_ops[custom_key](*op.data, control + op.wires)
339340

340341
if isinstance(op, qml.QubitUnitary):
342+
qml.QueuingManager.remove(op)
341343
return qml.ControlledQubitUnitary(
342344
op.matrix(),
343345
wires=control + op.wires,

tests/ops/op_math/test_controlled.py

+7
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,13 @@ def test_controlled_qaoa_embedding(self, features, weights, num_wires, batch_siz
16711671
class TestCtrl:
16721672
"""Tests for the ctrl transform."""
16731673

1674+
def test_no_redundant_queue(self):
1675+
"""Test that the ctrl transform does not add redundant operations to the queue. https://github.com/PennyLaneAI/pennylane/pull/6926"""
1676+
with qml.queuing.AnnotatedQueue() as q:
1677+
qml.ctrl(qml.QubitUnitary(np.eye(2), 0), 1)
1678+
1679+
assert len(q.queue) == 1
1680+
16741681
def test_invalid_input_error(self):
16751682
"""Test that a ValueError is raised upon invalid inputs."""
16761683
with pytest.raises(ValueError, match=r"<class 'int'> is not an Operator or callable."):

0 commit comments

Comments
 (0)