Skip to content

Commit b4e77d7

Browse files
albi3roJerryChen97
andauthored
Deprecate WiresEnum, AllWires, and AnyWires (#7313)
PR #7312 just does the num_wires = None part of this change and will allow us to update catalyst before merging this PR. Context: AllWires and AnyWires are overkill that don't really need to exist. Setting Operator.num_wires = None can sufficiently communicate "I can accept any number of wires". We haven't validated AllWires or done anything special about it for many releases. All these serve to do it tell the default Operator.__init__ to not validate how many wires the user passes in. We can do that with None. All they really do is make the interface more complicated and cluttered. Description of the Change: Deprecate AllWires, AnyWires, and WiresEnum. Benefits: Cleaner Operator interface. Possible Drawbacks: Deprecations and removals always run the risk of breaking something for someone, but I think this a harmless enough change. Related GitHub Issues: [[sc-89159](https://app.shortcut.com/xanaduai/story/89159)] --------- Co-authored-by: Yushao Chen (Jerry) <[email protected]>
1 parent c1d096b commit b4e77d7

File tree

5 files changed

+65
-8
lines changed

5 files changed

+65
-8
lines changed

doc/development/deprecations.rst

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ deprecations are listed below.
99
Pending deprecations
1010
--------------------
1111

12+
* ``qml.operation.WiresEnum``, ``qml.operation.AllWires``, and ``qml.operation.AnyWires`` are deprecated. If an operation can act
13+
on any number of wires ``Operator.num_wires = None`` should be used instead. This is the default, and does not need
14+
to be overridden unless the operator developer wants to validate that the correct number of wires is passed.
15+
16+
- Deprecated in v0.42
17+
- Will be removed in v0.43
18+
1219
* The :func:`qml.QNode.get_gradient_fn` method is now deprecated. Instead, use :func:`~.workflow.get_best_diff_method` to obtain the differentiation method.
1320

1421
- Deprecated in v0.42

doc/releases/changelog-dev.md

+5
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@
201201

202202
<h3>Deprecations 👋</h3>
203203

204+
* `qml.operation.WiresEnum`, `qml.operation.AllWires`, and `qml.operation.AnyWires` are deprecated. To indicate that
205+
an operator can act on any number of wires, `Operator.num_wires = None` should be used instead. This is the default
206+
and does not need to be overwritten unless the operator developer wants to add wire number validation.
207+
[(#7313)](https://github.com/PennyLaneAI/pennylane/pull/7313)
208+
204209
* The :func:`qml.QNode.get_gradient_fn` method is now deprecated. Instead, use :func:`~.workflow.get_best_diff_method` to obtain the differentiation method.
205210
[(#7323)](https://github.com/PennyLaneAI/pennylane/pull/7323)
206211

pennylane/operation.py

+39-6
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@
232232

233233
import pennylane as qml
234234
from pennylane.capture import ABCCaptureMeta, create_operator_primitive
235+
from pennylane.exceptions import PennyLaneDeprecationWarning
235236
from pennylane.math import expand_matrix
236237
from pennylane.queuing import QueuingManager
237238
from pennylane.typing import TensorLike
@@ -299,27 +300,38 @@ class ParameterFrequenciesUndefinedError(OperatorPropertyUndefined):
299300
# =============================================================================
300301

301302

302-
class WiresEnum(IntEnum):
303+
class _WiresEnum(IntEnum):
303304
"""Integer enumeration class
304305
to represent the number of wires
305306
an operation acts on.
306307
308+
.. warning::
309+
310+
This class is deprecated ``Operator.num_wires=None`` should now be used to indicate
311+
that an operator can exist on any number of wires.
312+
307313
"""
308314

309315
AnyWires = -1
310316
"""A enumeration that represents that an operator can act on any number of wires.
311317
318+
.. warning::
319+
320+
``AnyWires`` is deprecated ``Operator.num_wires=None`` should now be used to indicate
321+
that an operator can exist on any number of wires.
322+
312323
"""
313324

314325
AllWires = -2
315326
"""A enumeration that represents that an operator acts on all wires in the system.
316327
328+
.. warning::
317329
318-
"""
330+
``AllWires`` is deprecated ``Operator.num_wires=None`` should now be used to indicate
331+
that an operator can exist on any number of wires.
319332
333+
"""
320334

321-
AnyWires = WiresEnum.AnyWires
322-
AllWires = WiresEnum.AllWires
323335

324336
# =============================================================================
325337
# Class property
@@ -971,7 +983,7 @@ def terms(self) -> tuple[list[TensorLike], list["Operation"]]: # pylint: disabl
971983
"""
972984
raise TermsUndefinedError
973985

974-
num_wires: Optional[Union[int, WiresEnum]] = None
986+
num_wires: Optional[Union[int, _WiresEnum]] = None
975987
"""Number of wires the operator acts on."""
976988

977989
@property
@@ -1135,7 +1147,7 @@ def __init__(
11351147
self._wires: Wires = Wires(wires)
11361148

11371149
# check that the number of wires given corresponds to required number
1138-
if (self.num_wires is not None and not isinstance(self.num_wires, WiresEnum)) and len(
1150+
if (self.num_wires is not None and not isinstance(self.num_wires, _WiresEnum)) and len(
11391151
self._wires
11401152
) != self.num_wires:
11411153
raise ValueError(
@@ -2475,6 +2487,27 @@ def gen_is_multi_term_hamiltonian(obj):
24752487

24762488
def __getattr__(name):
24772489
"""To facilitate StatePrep rename"""
2490+
if name == "AnyWires":
2491+
warnings.warn(
2492+
"AnyWires is deprecated and will be removed in v0.43. "
2493+
" If your operation accepts any number of wires, set num_wires=None instead.",
2494+
PennyLaneDeprecationWarning,
2495+
)
2496+
return _WiresEnum.AllWires
2497+
if name == "AllWires":
2498+
warnings.warn(
2499+
"AllWires is deprecated and will be removed in v0.43. "
2500+
" If your operation accepts any number of wires, set num_wires=None instead.",
2501+
PennyLaneDeprecationWarning,
2502+
)
2503+
return _WiresEnum.AllWires
2504+
if name == "WiresEnum":
2505+
warnings.warn(
2506+
"WiresEnum is deprecated and will be removed in v0.43. "
2507+
" If your operation accepts any number of wires, set num_wires=None instead.",
2508+
PennyLaneDeprecationWarning,
2509+
)
2510+
return _WiresEnum
24782511
if name == "StatePrep":
24792512
return StatePrepBase
24802513
raise AttributeError(f"module 'pennylane.operation' has no attribute '{name}'")

pennylane/templates/subroutines/select_pauli_rot.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""
1717

1818
import pennylane as qml
19-
from pennylane.operation import AnyWires, Operation
19+
from pennylane.operation import Operation
2020
from pennylane.templates.state_preparations.mottonen import _apply_uniform_rotation_dagger
2121

2222

@@ -79,7 +79,6 @@ def circuit():
7979
0. +0.j 0. +0.j 0. +0.j 0. +0.j]
8080
"""
8181

82-
num_wires = AnyWires
8382
grad_method = None
8483
ndim_params = (1,)
8584

tests/test_operation.py

+13
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@
4141
I_broadcasted = I[pnp.newaxis]
4242

4343

44+
def test_wires_enum_deprecation():
45+
"""Test that WiresEnum, AllWires, and AnyWires are deprecated."""
46+
47+
with pytest.warns(qml.PennyLaneDeprecationWarning, match="is deprecated"):
48+
_ = qml.operation.WiresEnum
49+
50+
with pytest.warns(qml.PennyLaneDeprecationWarning, match="is deprecated"):
51+
_ = qml.operation.AllWires
52+
53+
with pytest.warns(qml.PennyLaneDeprecationWarning, match="is deprecated"):
54+
_ = qml.operation.AnyWires
55+
56+
4457
class TestOperatorConstruction:
4558
"""Test custom operators' construction."""
4659

0 commit comments

Comments
 (0)