Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecating Legacy Devices #5997

Merged
merged 52 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
e548bd9
added warnings for legacy devices and warning capture fixture
Shiro-Raven Jul 13, 2024
bd31a81
minor fixes to failing tests
Shiro-Raven Jul 13, 2024
8302975
Update pennylane/devices/default_qubit_autograd.py
Shiro-Raven Jul 15, 2024
dc89473
improved warnings messages for the deprecated devices
Shiro-Raven Jul 15, 2024
632c006
more fixes to failing tests
Shiro-Raven Jul 15, 2024
e98becb
update device to default.qubit in pulse examples
Shiro-Raven Jul 16, 2024
e803847
update tests to use default.qubit and add missing tests to new device…
Shiro-Raven Jul 16, 2024
11571c2
Merge branch 'master' into ad/deprecate-legacy-devices
Shiro-Raven Jul 16, 2024
13eeea3
add changelog and deprecation note
Shiro-Raven Jul 16, 2024
e257c29
printing failing warning checks
Shiro-Raven Jul 16, 2024
b9f405c
maybe this time
Shiro-Raven Jul 16, 2024
0a1f9f9
fix typo in deprecations doc
Shiro-Raven Jul 17, 2024
3a63fad
remove no longer needed comment
Shiro-Raven Jul 17, 2024
d145941
more robust classical control draw tests
Shiro-Raven Jul 17, 2024
a611917
what
Shiro-Raven Jul 17, 2024
ba6ff26
another one
Shiro-Raven Jul 17, 2024
836527d
Merge branch 'master' into ad/deprecate-legacy-devices
Shiro-Raven Jul 17, 2024
3e4dc46
fix
Shiro-Raven Jul 17, 2024
c4d4bb0
Merge branch 'master' into ad/deprecate-legacy-devices
Shiro-Raven Jul 17, 2024
c8d4475
fix
Shiro-Raven Jul 17, 2024
ca3ee22
Update doc/releases/changelog-dev.md
Shiro-Raven Jul 19, 2024
1b2bbfe
Update doc/releases/changelog-dev.md
Shiro-Raven Jul 19, 2024
5c30389
Update doc/development/deprecations.rst
Shiro-Raven Jul 19, 2024
85ed884
Merge branch 'master' into ad/deprecate-legacy-devices
Shiro-Raven Jul 19, 2024
7c14991
consolidate warning capture fixtures of devs and {ham,sum}_expand
Shiro-Raven Jul 19, 2024
5ced67f
consolidate warning capture fixtures of devs and tape_expand tests
Shiro-Raven Jul 19, 2024
4778f7f
minor typo in changelog
Shiro-Raven Jul 19, 2024
6339bea
test fix
Shiro-Raven Jul 19, 2024
624167a
fix tape_mpl test
Shiro-Raven Jul 19, 2024
c64ac29
Merge branch 'master' into ad/deprecate-legacy-devices
Shiro-Raven Jul 19, 2024
fd42815
more tape_mpl fixes
Shiro-Raven Jul 19, 2024
73d151a
more robust fixture setup and undoing draw test changes
Shiro-Raven Jul 21, 2024
0247cf7
fix minor formatting typo
Shiro-Raven Jul 21, 2024
7925f8f
revert changes to hardware_hamiltonian example
Shiro-Raven Jul 22, 2024
1b0ce0e
Update pennylane/devices/default_qubit_legacy.py
Shiro-Raven Jul 22, 2024
bfc3148
Merge branch 'master' into ad/deprecate-legacy-devices
Shiro-Raven Jul 23, 2024
4b10db6
restructuring and bringing back a test
Shiro-Raven Jul 23, 2024
43ceda8
Merge branch 'master' into ad/deprecate-legacy-devices
Shiro-Raven Jul 23, 2024
2d6784a
computationally less expensive shots for TF shots tests
Shiro-Raven Jul 23, 2024
a3150f4
Merge branch 'master' into ad/deprecate-legacy-devices
Shiro-Raven Jul 23, 2024
1043ed6
Merge branch 'master' into ad/deprecate-legacy-devices
Shiro-Raven Jul 23, 2024
dad606b
Merge branch 'master' into ad/deprecate-legacy-devices
Shiro-Raven Jul 23, 2024
d4eaf6c
add print statements to try and debug failing drawer test
albi3ro Jul 23, 2024
7681429
Merge branch 'master' into ad/deprecate-legacy-devices
Shiro-Raven Jul 24, 2024
67f0944
hardcode expected linewidth value, remove print statements
albi3ro Jul 24, 2024
7572bda
fix typo in style name
Shiro-Raven Jul 24, 2024
b69a7a8
Merge branch 'master' into ad/deprecate-legacy-devices
Shiro-Raven Jul 24, 2024
7a889f3
Merge branch 'master' into ad/deprecate-legacy-devices
Shiro-Raven Jul 24, 2024
1eb9d89
hardcode eraser color in test_tape_mpl
Shiro-Raven Jul 24, 2024
1ef3729
fixes to tf tests and runtime improvements
Shiro-Raven Jul 24, 2024
4a2d5e8
more shots
Shiro-Raven Jul 24, 2024
fa8d10a
Merge branch 'master' into ad/deprecate-legacy-devices
Shiro-Raven Jul 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pennylane/devices/default_qubit_autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"""This module contains an autograd implementation of the :class:`~.DefaultQubitLegacy`
reference plugin.
"""
import warnings

from pennylane import PennyLaneDeprecationWarning
from pennylane import numpy as pnp
from pennylane.devices import DefaultQubitLegacy

Expand All @@ -34,6 +37,9 @@ class DefaultQubitAutograd(DefaultQubitLegacy):

pip install autograd

.. warning::
This device is deprecated. Use :class:`~.devices.DefaultQubit` instead; for example through ``qml.device("default.qubit")``.
Shiro-Raven marked this conversation as resolved.
Show resolved Hide resolved

**Example**

The ``default.qubit.autograd`` is designed to be used with end-to-end classical backpropagation
Expand Down Expand Up @@ -104,6 +110,13 @@ def _const_mul(constant, array):
return constant * array

def __init__(self, wires, *, shots=None, analytic=None):
warnings.warn(
f"Use of '{self.short_name}' is deprecated. Instead, use 'default.qubit'. "
"If you experience issues, reach out to the PennyLane team on "
"the discussion forum: https://discuss.pennylane.ai/",
Shiro-Raven marked this conversation as resolved.
Show resolved Hide resolved
PennyLaneDeprecationWarning,
)

r_dtype = pnp.float64
c_dtype = pnp.complex128
super().__init__(wires, shots=shots, r_dtype=r_dtype, c_dtype=c_dtype, analytic=analytic)
Expand Down
12 changes: 12 additions & 0 deletions pennylane/devices/default_qubit_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"""This module contains a jax implementation of the :class:`~.DefaultQubitLegacy`
reference plugin.
"""
import warnings

# pylint: disable=ungrouped-imports
import numpy as np

Expand Down Expand Up @@ -49,6 +51,9 @@ class DefaultQubitJax(DefaultQubitLegacy):

pip install jax jaxlib

.. warning::
This device is deprecated. Use :class:`~.devices.DefaultQubit` instead; for example through ``qml.device("default.qubit")``.

**Example**

The ``default.qubit.jax`` device is designed to be used with end-to-end classical backpropagation
Expand Down Expand Up @@ -165,6 +170,13 @@ def circuit():
operations = DefaultQubitLegacy.operations.union({"ParametrizedEvolution"})

def __init__(self, wires, *, shots=None, prng_key=None, analytic=None):
warnings.warn(
f"Use of '{self.short_name}' is deprecated. Instead, use 'default.qubit'. "
"If you experience issues, reach out to the PennyLane team on "
"the discussion forum: https://discuss.pennylane.ai/",
qml.PennyLaneDeprecationWarning,
)

if jax.config.read("jax_enable_x64"):
c_dtype = jnp.complex128
r_dtype = jnp.float64
Expand Down
12 changes: 10 additions & 2 deletions pennylane/devices/default_qubit_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""
import functools
import itertools
import warnings
from string import ascii_letters as ABC
from typing import List

Expand Down Expand Up @@ -82,8 +83,8 @@ class DefaultQubitLegacy(QubitDevice):

.. warning::

This is the legacy implementation of DefaultQubit. It has been replaced by
``qml.devices.DefaultQubit``, which can be accessed with the familiar constructor,
This is the legacy implementation of DefaultQubit and is deprecated. It has been replaced by
:class:`~.devices.DefaultQubit`, which can be accessed with the familiar constructor,
Shiro-Raven marked this conversation as resolved.
Show resolved Hide resolved
``qml.device("default.qubit")``.

This change will not alter device behaviour for most workflows, but may have implications for
Expand Down Expand Up @@ -207,6 +208,13 @@ class DefaultQubitLegacy(QubitDevice):
def __init__(
self, wires, *, r_dtype=np.float64, c_dtype=np.complex128, shots=None, analytic=None
):
warnings.warn(
f"Use of '{self.short_name}' is deprecated. Instead, use 'default.qubit'. "
"If you experience issues, reach out to the PennyLane team on "
"the discussion forum: https://discuss.pennylane.ai/",
qml.PennyLaneDeprecationWarning,
)

super().__init__(wires, shots, r_dtype=r_dtype, c_dtype=c_dtype, analytic=analytic)
self._debugger = None

Expand Down
13 changes: 12 additions & 1 deletion pennylane/devices/default_qubit_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
reference plugin.
"""
import itertools
import warnings

import numpy as np
from packaging.version import Version
Expand Down Expand Up @@ -42,7 +43,7 @@


class DefaultQubitTF(DefaultQubitLegacy):
"""Simulator plugin based on ``"default.qubit.legacy"``, written using TensorFlow.
r"""Simulator plugin based on ``"default.qubit.legacy"``, written using TensorFlow.

**Short name:** ``default.qubit.tf``

Expand All @@ -57,6 +58,9 @@ class DefaultQubitTF(DefaultQubitLegacy):

pip install tensorflow>=2.0

.. warning::
This device is deprecated. Use :class:`~.devices.DefaultQubit` instead; for example through ``qml.device("default.qubit")``.

**Example**

The ``default.qubit.tf`` is designed to be used with end-to-end classical backpropagation
Expand Down Expand Up @@ -162,6 +166,13 @@ def _asarray(array, dtype=None):
return res

def __init__(self, wires, *, shots=None, analytic=None):
warnings.warn(
f"Use of '{self.short_name}' is deprecated. Instead, use 'default.qubit'. "
"If you experience issues, reach out to the PennyLane team on "
"the discussion forum: https://discuss.pennylane.ai/",
qml.PennyLaneDeprecationWarning,
)

r_dtype = tf.float64
c_dtype = tf.complex128

Expand Down
13 changes: 12 additions & 1 deletion pennylane/devices/default_qubit_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import numpy as np

from pennylane import PennyLaneDeprecationWarning
from pennylane.ops.qubit.attributes import diagonal_in_z_basis

from . import DefaultQubitLegacy
Expand All @@ -43,7 +44,7 @@


class DefaultQubitTorch(DefaultQubitLegacy):
"""Simulator plugin based on ``"default.qubit.legacy"``, written using PyTorch.
r"""Simulator plugin based on ``"default.qubit.legacy"``, written using PyTorch.

**Short name:** ``default.qubit.torch``

Expand All @@ -58,6 +59,10 @@ class DefaultQubitTorch(DefaultQubitLegacy):

pip install torch>=1.8.0

.. warning::
This device is deprecated. Use :class:`~.devices.DefaultQubit` instead; for example through ``qml.device("default.qubit")``.


**Example**

The ``default.qubit.torch`` is designed to be used with end-to-end classical backpropagation
Expand Down Expand Up @@ -165,6 +170,12 @@ def circuit(x):
_ndim = staticmethod(lambda tensor: tensor.ndim)

def __init__(self, wires, *, shots=None, analytic=None, torch_device=None):
warnings.warn(
f"Use of '{self.short_name}' is deprecated. Instead, use 'default.qubit'. "
"If you experience issues, reach out to the PennyLane team on "
"the discussion forum: https://discuss.pennylane.ai/",
PennyLaneDeprecationWarning,
)
# Store if the user specified a Torch device. Otherwise the execute
# method attempts to infer the Torch device from the gate parameters.
self._torch_device_specified = torch_device is not None
Expand Down
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ def set_numpy_seed():
yield


@pytest.fixture(scope="function", autouse=True)
def capture_legacy_device_deprecation_warnings(recwarn):
yield

for w in recwarn:
if isinstance(w.message, qml.PennyLaneDeprecationWarning):
assert "Use of 'default.qubit." in str(w.message)
assert "is deprecated" in str(w.message)
assert "use 'default.qubit'" in str(w.message)


@pytest.fixture(scope="session")
def tol():
"""Numerical tolerance for equality tests."""
Expand Down
3 changes: 2 additions & 1 deletion tests/devices/test_default_qubit_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,8 @@ class TestApplyOps:
gates in DefaultQubitLegacy."""

state = np.arange(2**4, dtype=np.complex128).reshape((2, 2, 2, 2))
dev = qml.device("default.qubit.legacy", wires=4)
with pytest.warns(qml.PennyLaneDeprecationWarning):
dev = qml.device("default.qubit.legacy", wires=4)

single_qubit_ops = [
(qml.PauliX, dev._apply_x),
Expand Down
3 changes: 2 additions & 1 deletion tests/devices/test_default_qubit_legacy_broadcasting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,8 @@ class TestApplyOpsBroadcasted:
gates in DefaultQubitLegacy."""

broadcasted_state = np.arange(2**4 * 3, dtype=np.complex128).reshape((3, 2, 2, 2, 2))
dev = qml.device("default.qubit.legacy", wires=4)
with pytest.warns(qml.PennyLaneDeprecationWarning):
dev = qml.device("default.qubit.legacy", wires=4)

single_qubit_ops = [
(qml.PauliX, dev._apply_x),
Expand Down
16 changes: 5 additions & 11 deletions tests/drawer/test_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,13 +878,11 @@ def circ():


class TestLevelExpansionStrategy:
@pytest.fixture(
params=[qml.device("default.qubit.legacy", wires=3), qml.devices.DefaultQubit()],
)
def transforms_circuit(self, request):
@pytest.fixture
def transforms_circuit(self):
@qml.transforms.merge_rotations
@qml.transforms.cancel_inverses
@qml.qnode(request.param, diff_method="parameter-shift")
@qml.qnode(qml.device("default.qubit"), diff_method="parameter-shift")
def circ(weights, order):
qml.RandomLayers(weights, wires=(0, 1))
qml.Permute(order, wires=(0, 1, 2))
Expand Down Expand Up @@ -1015,11 +1013,7 @@ def circ():
assert draw(circ)() == expected


@pytest.mark.parametrize(
"device",
[qml.device("default.qubit.legacy", wires=2), qml.device("default.qubit", wires=2)],
)
def test_applied_transforms(device):
def test_applied_transforms():
"""Test that any transforms applied to the qnode are included in the output."""

@qml.transform
Expand All @@ -1028,7 +1022,7 @@ def just_pauli_x(_):
return (new_tape,), lambda res: res[0]

@just_pauli_x
@qml.qnode(device)
@qml.qnode(qml.device("default.qubit", wires=2))
def my_circuit(x):
qml.RX(x, wires=0)
qml.SWAP(wires=(0, 1))
Expand Down
24 changes: 7 additions & 17 deletions tests/drawer/test_draw_mpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,11 @@ def test_fig_argument():


class TestLevelExpansionStrategy:
@pytest.fixture(
params=[qml.device("default.qubit.legacy", wires=3), qml.devices.DefaultQubit()],
)
def transforms_circuit(self, request):
@pytest.fixture
def transforms_circuit(self):
@qml.transforms.merge_rotations
@qml.transforms.cancel_inverses
@qml.qnode(request.param, diff_method="parameter-shift")
@qml.qnode(qml.device("default.qubit"), diff_method="parameter-shift")
def circ(weights, order):
qml.RandomLayers(weights, wires=(0, 1))
qml.Permute(order, wires=(0, 1, 2))
Expand Down Expand Up @@ -125,18 +123,14 @@ def test_equivalent_levels(self, transforms_circuit, levels, expected_metadata):

plt.close("all")

@pytest.mark.parametrize(
"device",
[qml.device("default.qubit.legacy", wires=3), qml.devices.DefaultQubit(wires=3)],
)
@pytest.mark.parametrize(
"strategy, initial_strategy, n_lines",
[("gradient", "device", 3), ("device", "gradient", 13)],
)
def test_expansion_strategy(self, device, strategy, initial_strategy, n_lines):
def test_expansion_strategy(self, strategy, initial_strategy, n_lines):
"""Test that the expansion strategy keyword controls what operations are drawn."""

@qml.qnode(device, expansion_strategy=initial_strategy)
@qml.qnode(qml.device("default.qubit"), expansion_strategy=initial_strategy)
def circuit():
qml.Permute([2, 0, 1], wires=(0, 1, 2))
return qml.expval(qml.PauliZ(0))
Expand Down Expand Up @@ -499,11 +493,7 @@ def circuit():
assert ax.texts[-1].get_text() == "X†"


@pytest.mark.parametrize(
"device",
[qml.device("default.qubit.legacy", wires=2), qml.device("default.qubit", wires=2)],
)
def test_applied_transforms(device):
def test_applied_transforms():
"""Test that any transforms applied to the qnode are included in the output."""

@qml.transform
Expand All @@ -512,7 +502,7 @@ def just_pauli_x(_):
return (new_tape,), lambda res: res[0]

@just_pauli_x
@qml.qnode(device)
@qml.qnode(qml.device("default.qubit", wires=2))
def my_circuit():
qml.SWAP(wires=(0, 1))
qml.CNOT(wires=(0, 1))
Expand Down
3 changes: 2 additions & 1 deletion tests/interfaces/test_jacobian_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
)

dev = qml.device("default.qubit")
dev_old = qml.device("default.qubit.legacy", wires=5)
with pytest.warns(qml.PennyLaneDeprecationWarning):
dev_old = qml.device("default.qubit.legacy", wires=5)
dev_lightning = qml.device("lightning.qubit", wires=5)
adjoint_config = qml.devices.ExecutionConfig(gradient_method="adjoint")
dev_ps = ParamShiftDerivativesDevice()
Expand Down
11 changes: 6 additions & 5 deletions tests/interfaces/test_transform_program_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@

import pennylane as qml

device_suite = (
qml.device("default.qubit.legacy", wires=5),
qml.devices.DefaultQubit(),
qml.device("lightning.qubit", wires=5),
)
with pytest.warns(qml.PennyLaneDeprecationWarning):
device_suite = (
qml.device("default.qubit.legacy", wires=5),
qml.devices.DefaultQubit(),
qml.device("lightning.qubit", wires=5),
)


@pytest.mark.all_interfaces
Expand Down
8 changes: 4 additions & 4 deletions tests/ops/qubit/test_observables.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,10 @@ def test_matrix_representation(self, basis_state, expected, n_wires, tol):
assert np.allclose(res_dynamic, expected, atol=tol)
assert np.allclose(res_static, expected, atol=tol)

@pytest.mark.parametrize(
"dev", (qml.device("default.qubit"), qml.device("default.qubit.legacy", wires=1))
)
def test_integration_batched_state(self, dev):
@pytest.mark.parametrize("dev_name", ("default.qubit", "default.qubit.legacy"))
def test_integration_batched_state(self, dev_name):
dev = qml.device(dev_name, wires=1)

@qml.qnode(dev)
def circuit(x):
qml.RX(x, wires=0)
Expand Down
13 changes: 8 additions & 5 deletions tests/resource/test_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
import pennylane as qml
from pennylane import numpy as pnp

with pytest.warns(qml.PennyLaneDeprecationWarning):
devices_list = [
(qml.device("default.qubit"), 1),
(qml.device("default.qubit", wires=2), 2),
(qml.device("default.qubit.legacy", wires=2), 2),
]


class TestSpecsTransform:
"""Tests for the transform specs using the QNode"""
Expand Down Expand Up @@ -317,11 +324,7 @@ def circuit():

@pytest.mark.parametrize(
"device,num_wires",
[
(qml.device("default.qubit"), 1),
(qml.device("default.qubit", wires=2), 2),
(qml.device("default.qubit.legacy", wires=2), 2),
],
devices_list,
)
def test_num_wires_source_of_truth(self, device, num_wires):
"""Tests that num_wires behaves differently on old and new devices."""
Expand Down
Loading
Loading