Skip to content

Commit

Permalink
Merge branch 'master' into ad/typehints-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Shiro-Raven authored Jul 23, 2024
2 parents 47efb90 + cdf7810 commit 9070a9f
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 22 deletions.
10 changes: 10 additions & 0 deletions doc/development/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ deprecations are listed below.
Pending deprecations
--------------------

* The ``decomp_depth`` argument in ``qml.device`` is deprecated.

- Deprecated in v0.38
- Will be removed in v0.39

* The ``max_expansion`` argument in ``qml.QNode`` is deprecated.

- Deprecated in v0.38
- Will be removed in v0.39

* The functions ``qml.transforms.sum_expand`` and ``qml.transforms.hamiltonian_expand`` are deprecated.
Instead, ``qml.transforms.split_non_commuting`` can be used for equivalent behaviour.

Expand Down
6 changes: 6 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@

<h3>Deprecations 👋</h3>

* The `decomp_depth` argument in `qml.device` has been deprecated.
[(#6026)](https://github.com/PennyLaneAI/pennylane/pull/6026)

* The `max_expansion` argument in `qml.QNode` has been deprecated.
[(#6026)](https://github.com/PennyLaneAI/pennylane/pull/6026)

* The `expansion_strategy` attribute in the `QNode` class is deprecated.
[(#5989)](https://github.com/PennyLaneAI/pennylane/pull/5989)

Expand Down
16 changes: 15 additions & 1 deletion pennylane/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from importlib import reload, metadata
from sys import version_info

import warnings
import numpy as _np

from semantic_version import SimpleSpec, Version
Expand Down Expand Up @@ -262,6 +263,10 @@ def device(name, *args, **kwargs):
decomp_depth (int): For when custom decompositions are specified,
the maximum expansion depth used by the expansion function.
.. warning::
The ``decomp_depth`` argument is deprecated and will be removed in version 0.39.
All devices must be loaded by specifying their **short-name** as listed above,
followed by the **wires** (subsystems) you wish to initialize. The ``wires``
argument can be an integer, in which case the wires of the device are addressed
Expand Down Expand Up @@ -393,7 +398,15 @@ def run_cnot():
# Pop the custom decomposition keyword argument; we will use it here
# only and not pass it to the device.
custom_decomps = kwargs.pop("custom_decomps", None)
decomp_depth = kwargs.pop("decomp_depth", 10)
decomp_depth = kwargs.pop("decomp_depth", None)

if decomp_depth is not None:
warnings.warn(
"The decomp_depth argument is deprecated and will be removed in version 0.39. ",
PennyLaneDeprecationWarning,
)
else:
decomp_depth = 10

kwargs.pop("config", None)
options.update(kwargs)
Expand All @@ -414,6 +427,7 @@ def run_cnot():

# Once the device is constructed, we set its custom expansion function if
# any custom decompositions were specified.

if custom_decomps is not None:
if isinstance(dev, pennylane.devices.LegacyDevice):
custom_decomp_expand_fn = pennylane.transforms.create_decomp_expand_fn(
Expand Down
11 changes: 9 additions & 2 deletions pennylane/capture/capture_qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
This submodule defines a capture compatible call to QNodes.
"""

import warnings
from copy import copy
from dataclasses import asdict
from functools import lru_cache, partial
Expand All @@ -29,7 +30,7 @@


def _get_shapes_for(*measurements, shots=None, num_device_wires=0):
if jax.config.jax_enable_x64:
if jax.config.jax_enable_x64: # pylint: disable=no-member
dtype_map = {
float: jax.numpy.float64,
int: jax.numpy.int64,
Expand Down Expand Up @@ -65,7 +66,13 @@ def _(*args, qnode, shots, device, qnode_kwargs, qfunc_jaxpr):
def qfunc(*inner_args):
return jax.core.eval_jaxpr(qfunc_jaxpr.jaxpr, qfunc_jaxpr.consts, *inner_args)

qnode = qml.QNode(qfunc, device, **qnode_kwargs)
with warnings.catch_warnings():
warnings.filterwarnings(
action="ignore",
message=r"The max_expansion argument is deprecated and will be removed in version 0.39.",
category=qml.PennyLaneDeprecationWarning,
)
qnode = qml.QNode(qfunc, device, **qnode_kwargs)
return qnode._impl_call(*args, shots=shots) # pylint: disable=protected-access

# pylint: disable=unused-argument
Expand Down
18 changes: 16 additions & 2 deletions pennylane/workflow/qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,12 @@ class QNode:
on supported options for your chosen gradient transform.
.. warning::
The ``expansion_strategy`` attribute is deprecated and will be removed in version 0.39.
The ``expansion_strategy`` argument is deprecated and will be removed in version 0.39.
.. warning::
The ``max_expansion`` argument is deprecated and will be removed in version 0.39.
**Example**
Expand Down Expand Up @@ -452,7 +457,7 @@ def __init__(
interface="auto",
diff_method="best",
expansion_strategy=None,
max_expansion=10,
max_expansion=None,
grad_on_execution="best",
cache="auto",
cachesize=10000,
Expand All @@ -462,6 +467,15 @@ def __init__(
mcm_method=None,
**gradient_kwargs,
):
# Moving it here since the old default value is checked on debugging
if max_expansion is not None:
warnings.warn(
"The max_expansion argument is deprecated and will be removed in version 0.39. ",
qml.PennyLaneDeprecationWarning,
)
else:
max_expansion = 10

if logger.isEnabledFor(logging.DEBUG):
logger.debug(
"""Creating QNode(func=%s, device=%s, interface=%s, diff_method=%s, expansion_strategy=%s, max_expansion=%s, grad_on_execution=%s, cache=%s, cachesize=%s, max_diff=%s, gradient_kwargs=%s""",
Expand Down
9 changes: 9 additions & 0 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,15 @@ def test_shot_vector_property(self):

assert dev.shots == 22

def test_decomp_depth_is_deprecated(self):
"""Test that a warning is raised when using the deprecated decomp_depth argument"""

with pytest.warns(
qml.PennyLaneDeprecationWarning,
match="The decomp_depth argument is deprecated",
):
qml.device("default.qubit", decomp_depth=1)


class TestBatchExecution:
"""Tests for the batch_execute method."""
Expand Down
9 changes: 9 additions & 0 deletions tests/test_qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ def f():

assert f.execute_kwargs["cache"] is True

def test_max_expansion_is_deprecated(self):
"""Test that a warning is raised when using the deprecated max_expansion argument"""
dev = qml.device("default.qubit", wires=1)
with pytest.warns(
qml.PennyLaneDeprecationWarning,
match="The max_expansion argument is deprecated",
):
QNode(dummyfunc, dev, max_expansion=10)


# pylint: disable=too-many-public-methods
class TestValidation:
Expand Down
50 changes: 33 additions & 17 deletions tests/transforms/test_tape_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,15 @@
# pylint: disable=too-few-public-methods, invalid-unary-operand-type, no-member,
# pylint: disable=arguments-differ, arguments-renamed,

import warnings

import numpy as np
import pytest

import pennylane as qml
from pennylane.wires import Wires


@pytest.fixture(scope="function", autouse=True)
def capture_warnings(recwarn):
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "'expansion_strategy' attribute is deprecated" in str(w.message)


class TestCreateExpandFn:
"""Test creating expansion functions from stopping criteria."""

Expand Down Expand Up @@ -438,6 +431,21 @@ def custom_basic_entangler_layers(weights, wires, **kwargs):
class TestCreateCustomDecompExpandFn:
"""Tests for the custom_decomps argument for devices"""

@pytest.fixture(scope="function", autouse=True)
def capture_warnings(self):
with pytest.warns(
qml.PennyLaneDeprecationWarning,
) as record:
yield

assert any(
"'expansion_strategy' attribute is deprecated" in str(w.message) for w in record
)

for w in record:
if "'expansion_strategy' attribute is deprecated" not in str(w.message):
warnings.warn(w.message, w.category)

@pytest.mark.parametrize("device_name", ["default.qubit", "default.qubit.legacy"])
def test_string_and_operator_allowed(self, device_name):
"""Test that the custom_decomps dictionary accepts both strings and operator classes as keys."""
Expand Down Expand Up @@ -544,7 +552,12 @@ def test_no_decomp_with_depth_zero(self, device_name):
"""Test that specifying a single custom decomposition works as expected."""

custom_decomps = {"Hadamard": custom_hadamard, "CNOT": custom_cnot}
decomp_dev = qml.device(device_name, wires=2, custom_decomps=custom_decomps, decomp_depth=0)
with pytest.warns(
qml.PennyLaneDeprecationWarning, match="The decomp_depth argument is deprecated"
):
decomp_dev = qml.device(
device_name, wires=2, custom_decomps=custom_decomps, decomp_depth=0
)

@qml.qnode(decomp_dev, expansion_strategy="device")
def circuit():
Expand Down Expand Up @@ -721,13 +734,16 @@ def test_custom_decomp_different_depth(self, device_name):
# not be further decomposed even though the custom decomposition is specified.
custom_decomps = {"BasicEntanglerLayers": custom_basic_entangler_layers, "RX": custom_rx}

decomp_dev_2 = qml.device(
device_name, wires=2, custom_decomps=custom_decomps, decomp_depth=2
)
with pytest.warns(
qml.PennyLaneDeprecationWarning, match="The decomp_depth argument is deprecated"
):
decomp_dev_2 = qml.device(
device_name, wires=2, custom_decomps=custom_decomps, decomp_depth=2
)

decomp_dev_3 = qml.device(
device_name, wires=2, custom_decomps=custom_decomps, decomp_depth=3
)
decomp_dev_3 = qml.device(
device_name, wires=2, custom_decomps=custom_decomps, decomp_depth=3
)

def circuit():
qml.BasicEntanglerLayers([[0.1, 0.2]], wires=[0, 1])
Expand Down Expand Up @@ -910,7 +926,7 @@ def test_custom_decomp_used_twice(self):
custom_decomps = {"MultiRZ": qml.MultiRZ.compute_decomposition}
dev = qml.device("lightning.qubit", wires=2, custom_decomps=custom_decomps)

@qml.qnode(dev, diff_method="adjoint")
@qml.qnode(dev, diff_method="adjoint", expansion_strategy="device")
def cost(theta):
qml.Hadamard(wires=0)
qml.Hadamard(wires=1)
Expand Down

0 comments on commit 9070a9f

Please sign in to comment.