From 5ae20d0917ff23c66dce5dfc41f7446345ae414a Mon Sep 17 00:00:00 2001 From: Matthew Silverman Date: Thu, 9 Nov 2023 16:40:40 -0500 Subject: [PATCH 1/2] create a PL-specific deprecation warning --- pennylane/__init__.py | 4 ++++ pennylane/operation.py | 4 ++-- pennylane/ops/functions/generator.py | 2 +- pennylane/ops/op_math/controlled.py | 2 +- pennylane/tape/qscript.py | 4 ++-- pennylane/transforms/__init__.py | 6 +++--- pennylane/transforms/batch_transform.py | 2 +- pennylane/transforms/core/transform_dispatcher.py | 2 +- pennylane/transforms/op_transforms.py | 2 +- pennylane/transforms/qfunc_transforms.py | 6 +++--- pennylane/vqe/vqe.py | 2 +- 11 files changed, 20 insertions(+), 16 deletions(-) diff --git a/pennylane/__init__.py b/pennylane/__init__.py index 9a3398a3dc1..b6b8fbc71c4 100644 --- a/pennylane/__init__.py +++ b/pennylane/__init__.py @@ -139,6 +139,10 @@ class QuantumFunctionError(Exception): """Exception raised when an illegal operation is defined in a quantum function.""" +class PennyLaneDeprecationWarning(UserWarning): + """Warning raised when a PennyLane feature is being deprecated.""" + + def _get_device_entrypoints(): """Returns a dictionary mapping the device short name to the loadable entrypoint""" diff --git a/pennylane/operation.py b/pennylane/operation.py index 846f153108d..a18e06ceb5e 100644 --- a/pennylane/operation.py +++ b/pennylane/operation.py @@ -1883,7 +1883,7 @@ def return_type(self): warnings.warn( "`Observable.return_type` is deprecated. Instead, you should " "inspect the type of the surrounding measurement process.", - UserWarning, + qml.PennyLaneDeprecationWarning, ) return self._return_type @@ -1893,7 +1893,7 @@ def return_type(self, value): warnings.warn( "`Observable.return_type` is deprecated. Instead, you should " "create a measurement process containing this Observable.", - UserWarning, + qml.PennyLaneDeprecationWarning, ) self._return_type = value diff --git a/pennylane/ops/functions/generator.py b/pennylane/ops/functions/generator.py index f98569ffd7d..61cce2c6764 100644 --- a/pennylane/ops/functions/generator.py +++ b/pennylane/ops/functions/generator.py @@ -88,7 +88,7 @@ def _generator_backcompatibility(op): "The Operator.generator property is deprecated. Please update the operator so that " "\n\t1. Operator.generator() is a method, and" "\n\t2. Operator.generator() returns an Operator instance representing the operator.", - UserWarning, + qml.PennyLaneDeprecationWarning, ) gen = op.generator diff --git a/pennylane/ops/op_math/controlled.py b/pennylane/ops/op_math/controlled.py index de3212140e5..284d5951c0e 100644 --- a/pennylane/ops/op_math/controlled.py +++ b/pennylane/ops/op_math/controlled.py @@ -268,7 +268,7 @@ def __init__(self, base, control_wires, control_values=None, work_wires=None, id if isinstance(control_values, str): warnings.warn( "Specifying control values as a string is deprecated. Please use Sequence[Bool]", - UserWarning, + qml.PennyLaneDeprecationWarning, ) # All values not 0 are cast as true. Assumes a string of 1s and 0s. control_values = [(x != "0") for x in control_values] diff --git a/pennylane/tape/qscript.py b/pennylane/tape/qscript.py index 1b2951b53fa..695ad65c8b4 100644 --- a/pennylane/tape/qscript.py +++ b/pennylane/tape/qscript.py @@ -405,7 +405,7 @@ def is_sampled(self) -> bool: ">>> from pennylane.measurements import *\n" ">>> sample_types = (SampleMP, CountsMP, ClassicalShadowMP, ShadowExpvalMP)\n" ">>> is_sampled = any(isinstance(m, sample_types) for m in tape.measurements)\n", - UserWarning, + qml.PennyLaneDeprecationWarning, ) sample_type = (SampleMP, CountsMP, ClassicalShadowMP, ShadowExpvalMP) return any(isinstance(m, sample_type) for m in self.measurements) @@ -419,7 +419,7 @@ def all_sampled(self) -> bool: ">>> from pennylane.measurements import *\n" ">>> sample_types = (SampleMP, CountsMP, ClassicalShadowMP, ShadowExpvalMP)\n" ">>> all_sampled = all(isinstance(m, sample_types) for m in tape.measurements)\n", - UserWarning, + qml.PennyLaneDeprecationWarning, ) sample_type = (SampleMP, CountsMP, ClassicalShadowMP, ShadowExpvalMP) return all(isinstance(m, sample_type) for m in self.measurements) diff --git a/pennylane/transforms/__init__.py b/pennylane/transforms/__init__.py index 4405fa58fe3..28ab66ddf66 100644 --- a/pennylane/transforms/__init__.py +++ b/pennylane/transforms/__init__.py @@ -215,9 +215,9 @@ Old transforms framework ------------------------ -These utility functions were previously used to create transforms in PennyLane and will be -deprecated soon. It is now recommended to use :class:`qml.transform ` -for creation of custom transforms. +These utility functions were previously used to create transforms in PennyLane and are now +deprecated. It is now recommended to use :class:`qml.transform ` +for the creation of custom transforms. .. autosummary:: :toctree: api diff --git a/pennylane/transforms/batch_transform.py b/pennylane/transforms/batch_transform.py index 201d1529b18..60bdc55fb8a 100644 --- a/pennylane/transforms/batch_transform.py +++ b/pennylane/transforms/batch_transform.py @@ -224,7 +224,7 @@ def __init__(self, transform_fn, expand_fn=None, differentiable=True): "Use of `batch_transform` to create a custom transform is deprecated. Instead " "switch to using the new qml.transform function. Follow the instructions here for " "further details: https://docs.pennylane.ai/en/stable/code/qml_transforms.html#custom-transforms.", - UserWarning, + qml.PennyLaneDeprecationWarning, ) self.transform_fn = transform_fn self.expand_fn = expand_fn diff --git a/pennylane/transforms/core/transform_dispatcher.py b/pennylane/transforms/core/transform_dispatcher.py index 87a22029336..f284f549f12 100644 --- a/pennylane/transforms/core/transform_dispatcher.py +++ b/pennylane/transforms/core/transform_dispatcher.py @@ -134,7 +134,7 @@ def processing_fn(results): "or call the transform directly using qnode = transform_fn(qnode, " "**transform_kwargs). Visit the deprecations page for more details: " "https://docs.pennylane.ai/en/stable/development/deprecations.html", - UserWarning, + qml.PennyLaneDeprecationWarning, ) if obj is not None: diff --git a/pennylane/transforms/op_transforms.py b/pennylane/transforms/op_transforms.py index 492ec8c5df2..fa8d4f040fc 100644 --- a/pennylane/transforms/op_transforms.py +++ b/pennylane/transforms/op_transforms.py @@ -203,7 +203,7 @@ def __init__(self, fn): "Use of `op_transform` to create a custom transform is deprecated. Instead " "switch to using the new qml.transform function. Follow the instructions here for " "further details: https://docs.pennylane.ai/en/stable/code/qml_transforms.html#custom-transforms.", - UserWarning, + qml.PennyLaneDeprecationWarning, ) self._fn = fn self._sig = inspect.signature(fn).parameters diff --git a/pennylane/transforms/qfunc_transforms.py b/pennylane/transforms/qfunc_transforms.py index 2cbcfbcbaf3..efe6dfe0637 100644 --- a/pennylane/transforms/qfunc_transforms.py +++ b/pennylane/transforms/qfunc_transforms.py @@ -150,7 +150,7 @@ def __init__(self, transform_fn): "Use of `single_tape_transform` to create a custom transform is deprecated. Instead " "switch to using the new qml.transform function. Follow the instructions here for " "further details: https://docs.pennylane.ai/en/stable/code/qml_transforms.html#custom-transforms.", - UserWarning, + qml.PennyLaneDeprecationWarning, ) self.transform_fn = transform_fn functools.update_wrapper(self, transform_fn) @@ -405,11 +405,11 @@ def new_qfunc(*args, **kwargs): "Use of `qfunc_transform` to create a custom transform is deprecated. Instead " "switch to using the new qml.transform function. Follow the instructions here for " "further details: https://docs.pennylane.ai/en/stable/code/qml_transforms.html#custom-transforms.", - UserWarning, + qml.PennyLaneDeprecationWarning, ) if not isinstance(tape_transform, single_tape_transform): with warnings.catch_warnings(): - warnings.simplefilter("ignore", UserWarning) + warnings.simplefilter("ignore", qml.PennyLaneDeprecationWarning) tape_transform = single_tape_transform(tape_transform) sig = inspect.signature(tape_transform) diff --git a/pennylane/vqe/vqe.py b/pennylane/vqe/vqe.py index 3dca1bcca96..3286180860a 100644 --- a/pennylane/vqe/vqe.py +++ b/pennylane/vqe/vqe.py @@ -156,7 +156,7 @@ def __init__( "ExpvalCost is deprecated, use qml.expval() instead. " "For optimizing Hamiltonian measurements with measuring commuting " "terms in parallel, use the grouping_type keyword in qml.Hamiltonian.", - UserWarning, + qml.PennyLaneDeprecationWarning, ) if kwargs.get("measure", "expval") != "expval": From a273577f614d1a51dfcd1921864fafb48915bb97 Mon Sep 17 00:00:00 2001 From: Matthew Silverman Date: Thu, 9 Nov 2023 18:48:25 -0500 Subject: [PATCH 2/2] add docs --- doc/development/deprecations.rst | 3 +++ doc/releases/changelog-dev.md | 3 +++ 2 files changed, 6 insertions(+) diff --git a/doc/development/deprecations.rst b/doc/development/deprecations.rst index 1a5bcf31114..75b1ff4809a 100644 --- a/doc/development/deprecations.rst +++ b/doc/development/deprecations.rst @@ -3,6 +3,9 @@ Deprecations ============ +All PennyLane deprecations will raise a ``qml.PennyLaneDeprecationWarning``. Pending and completed +deprecations are listed below. + Pending deprecations -------------------- diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 0fa5a007d9f..156a317b281 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -48,6 +48,9 @@

Deprecations 👋

+* All deprecations now raise a `qml.PennyLaneDeprecationWarning` instead of a `UserWarning`. + [(#4814)](https://github.com/PennyLaneAI/pennylane/pull/4814) + * `QuantumScript.is_sampled` and `QuantumScript.all_sampled` are deprecated. Users should now validate these properties manually. [(#4773)](https://github.com/PennyLaneAI/pennylane/pull/4773)