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

Remove top-level access to Device, QubitDevice and QutritDevice #6537

Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
dc2ab21
Remove top-level access to `Device`, `QubitDevice` and `QutritDevice`
JerryChen97 Nov 6, 2024
ccb1408
update rst
JerryChen97 Nov 8, 2024
06cf851
rm warns from init
JerryChen97 Nov 8, 2024
cd67f57
typo qml.Device -> qml.device
JerryChen97 Nov 8, 2024
566a67a
rm dep test
JerryChen97 Nov 8, 2024
b68c671
rm dep test
JerryChen97 Nov 8, 2024
c0e6f6b
rm depr test
JerryChen97 Nov 8, 2024
6c8ac27
batch str rp pennylane.Device -> pennylane.devices.Device
JerryChen97 Nov 8, 2024
edbd5cf
batch rp Qubit and Qutrit
JerryChen97 Nov 8, 2024
ddbee26
Revert "batch str rp pennylane.Device -> pennylane.devices.Device"
JerryChen97 Nov 8, 2024
81f490f
batch rp pennylane.Device -> pennylane.devices.LegacyDevice
JerryChen97 Nov 8, 2024
0b82d3a
update changelog
JerryChen97 Nov 8, 2024
918324d
Merge branch 'master' into Remove-top-level-access-to-`Device`,-`Qubi…
JerryChen97 Nov 8, 2024
48688a0
recover two inits
JerryChen97 Nov 8, 2024
7132bb7
Merge branch 'master' into Remove-top-level-access-to-`Device`,-`Qubi…
JerryChen97 Nov 8, 2024
def27cc
restore unblacked
JerryChen97 Nov 8, 2024
d8d93d1
revert black
JerryChen97 Nov 8, 2024
2177de4
Merge branch 'master' into Remove-top-level-access-to-`Device`,-`Qubi…
JerryChen97 Nov 8, 2024
1499d4f
Update pennylane/__init__.py
JerryChen97 Nov 8, 2024
dec8514
Merge branch 'master' into Remove-top-level-access-to-`Device`,-`Qubi…
JerryChen97 Nov 8, 2024
c8c7466
Update doc/development/guide/architecture.rst
JerryChen97 Nov 8, 2024
8bdc594
Update doc/introduction/circuits.rst
JerryChen97 Nov 8, 2024
e9051f4
Merge branch 'master' into Remove-top-level-access-to-`Device`,-`Qubi…
JerryChen97 Nov 8, 2024
cf0a457
Merge branch 'master' into Remove-top-level-access-to-`Device`,-`Qubi…
JerryChen97 Nov 11, 2024
87bd051
Merge branch 'master' into Remove-top-level-access-to-`Device`,-`Qubi…
JerryChen97 Nov 11, 2024
e09c743
Update pennylane/devices/__init__.py
JerryChen97 Nov 12, 2024
d935ad4
Update pennylane/qcut/utils.py
JerryChen97 Nov 12, 2024
e578ba8
rm unecessary docstr explaining legacy stuff
JerryChen97 Nov 12, 2024
3268bbb
Revert "rm unecessary docstr explaining legacy stuff"
JerryChen97 Nov 12, 2024
da0a417
rm unnecessary sentence explaining the legacy replacement
JerryChen97 Nov 12, 2024
eb3c3d5
LegacyDevice -> ...Facade
JerryChen97 Nov 12, 2024
667fb52
Merge branch 'master' into Remove-top-level-access-to-`Device`,-`Qubi…
JerryChen97 Nov 12, 2024
1ecd905
Update pennylane/workflow/jacobian_products.py
JerryChen97 Nov 12, 2024
e3cffc3
Update pennylane/workflow/jacobian_products.py
JerryChen97 Nov 12, 2024
ed47742
rm the unnecessary test
JerryChen97 Nov 12, 2024
ea04c82
Merge branch 'Remove-top-level-access-to-`Device`,-`QubitDevice`-and-…
JerryChen97 Nov 12, 2024
0837144
Update pennylane/workflow/interfaces/jax_jit.py
JerryChen97 Nov 12, 2024
e537cec
Update pennylane/workflow/interfaces/tensorflow_autograph.py
JerryChen97 Nov 12, 2024
462fab9
Update pennylane/workflow/interfaces/jax_jit.py
JerryChen97 Nov 12, 2024
9218936
Merge branch 'master' into Remove-top-level-access-to-`Device`,-`Qubi…
JerryChen97 Nov 13, 2024
5dbed2d
Merge branch 'master' into Remove-top-level-access-to-`Device`,-`Qubi…
JerryChen97 Nov 13, 2024
e7543cb
pylint
JerryChen97 Nov 13, 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
14 changes: 7 additions & 7 deletions doc/development/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ Pending deprecations
- Deprecated in v0.39
- Will be removed in v0.40

* ``Device``, ``QubitDevice``, and ``QutritDevice`` will no longer be imported top level in v0.40. They instead
will be available as ``qml.devices.LegacyDevice``, ``qml.devices.QubitDevice``, and ``qml.devices.QutritDevice``
respectively.

- Deprecated top level access in v0.39
- Top level access will be removed in v0.40

* ``QNode.gradient_fn`` is deprecated. Please use ``QNode.diff_method`` instead. ``QNode.get_gradient_fn`` can also be used to
process the diff method.

Expand Down Expand Up @@ -123,6 +116,13 @@ Other deprecations
Completed deprecation cycles
----------------------------

* Top level access to ``Device``, ``QubitDevice``, and ``QutritDevice`` have been removed. Instead, they
are available as ``qml.devices.LegacyDevice``, ``qml.devices.QubitDevice``, and ``qml.devices.QutritDevice``
respectively.

- Deprecated in v0.39
- Removed in v0.40

* The ``simplify`` argument in ``qml.Hamiltonian`` and ``qml.ops.LinearCombination`` has been removed.
Instead, ``qml.simplify()`` can be called on the constructed operator.

Expand Down
2 changes: 1 addition & 1 deletion doc/development/guide/architecture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Devices
*******

In PennyLane, the abstraction of a quantum computation device is encompassed
within the :class:`pennylane.Device` class. The main job of devices is to
within the :class:`pennylane.devices.LegacyDevice` class. The main job of devices is to
JerryChen97 marked this conversation as resolved.
Show resolved Hide resolved
interpret and execute tapes. The most important method is ``batch_execute``,
which executes a list of tapes, such as a list of the single tape created above:

Expand Down
4 changes: 2 additions & 2 deletions doc/development/legacy_plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ as well as potential further capabilities, by providing the following class attr
return obj.name in {'CNOT', 'PauliX', 'PauliY', 'PauliZ'}
return qml.BooleanFn(accepts_obj)

Supported operations can also be determined by the :attr:`pennylane.Device.operations` property.
Supported operations can also be determined by the :attr:`pennylane.devices.LegacyDevice.operations` property.
This property is a list of string names for supported operations.

.. code-block:: python
Expand Down Expand Up @@ -422,7 +422,7 @@ and similar code in the ``batch_execute`` method:
self.tracker.update(batches=1, batch_len=len(circuits))
self.tracker.record()

These functions are called in base :class:`pennylane.Device` and :class:`~.QubitDevice` devices. Unless you are
These functions are called in base :class:`pennylane.devices.LegacyDevice` and :class:`~.QubitDevice` devices. Unless you are
overriding the ``execute`` and ``batch_execute`` methods or want to customize the stored
information, you do not need to add any new code.

Expand Down
2 changes: 1 addition & 1 deletion doc/introduction/circuits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Defining a device

To run---and later optimize---a quantum circuit, one needs to first specify a *computational device*.

The device is an instance of the :class:`~.pennylane.Device`
The device is an instance of the :class:`~.pennylane.devices.LegacyDevice`
JerryChen97 marked this conversation as resolved.
Show resolved Hide resolved
class, and can represent either a simulator or hardware device. They can be
instantiated using the :func:`device <pennylane.device>` loader.

Expand Down
6 changes: 5 additions & 1 deletion doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@

<h3>Breaking changes 💔</h3>

* Top level access to `Device`, `QubitDevice`, and `QutritDevice` have been removed. Instead, they
are available as `qml.devices.LegacyDevice`, `qml.devices.QubitDevice`, and `qml.devices.QutritDevice`
respectively.

<h3>Deprecations 👋</h3>

<h3>Documentation 📝</h3>
Expand All @@ -68,7 +72,7 @@

This release contains contributions from (in alphabetical order):

Shiwen An
Shiwen An,
Astral Cai,
Yushao Chen,
Pietropaolo Frisoni,
Expand Down
27 changes: 1 addition & 26 deletions pennylane/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,35 +178,10 @@
if name == "plugin_devices":
return pennylane.devices.device_constructor.plugin_devices

from warnings import warn # pylint: disable=import-outside-toplevel

if name == "QubitDevice":
warn(
"QubitDevice will no longer be accessible top level. Please access "
"the class as pennylane.devices.QubitDevice",
PennyLaneDeprecationWarning,
)
return pennylane.devices._qubit_device.QubitDevice # pylint:disable=protected-access

if name == "QutritDevice":
warn(
"QutritDevice will no longer be accessible top level. Please access "
"the class as pennylane.devices.QutritDevice",
PennyLaneDeprecationWarning,
)
return pennylane.devices._qutrit_device.QutritDevice # pylint:disable=protected-access

if name == "Device":
warn(
"Device will no longer be accessible top level. Please access "
"the class as pennylane.devices.LegacyDevice",
PennyLaneDeprecationWarning,
)
return pennylane.devices._legacy_device.Device # pylint:disable=protected-access

raise AttributeError(f"module 'pennylane' has no attribute '{name}'")


def version():
"""Returns the PennyLane version number."""
return __version__

Check notice on line 187 in pennylane/__init__.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/__init__.py#L187

Trailing newlines (trailing-newlines)
JerryChen97 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion pennylane/devices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
:class:`pennylane.devices.Device` is the latest interface for the next generation of devices that
replaces :class:`pennylane.Device` and :class:`pennylane.QubitDevice`.
JerryChen97 marked this conversation as resolved.
Show resolved Hide resolved

While the previous interface :class:`pennylane.Device` is imported top level, the new :class:`pennylane.devices.Device` is
The previous interface :class:`pennylane.Device` has been removed from the top level, and the new :class:`pennylane.devices.Device` is
JerryChen97 marked this conversation as resolved.
Show resolved Hide resolved
accessible from the ``pennylane.devices`` submodule.

.. currentmodule:: pennylane.devices
Expand Down
2 changes: 1 addition & 1 deletion pennylane/devices/default_mixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
r"""
The default.mixed device is PennyLane's standard qubit simulator for mixed-state computations.

It implements the necessary :class:`~pennylane.Device` methods as well as some built-in
It implements the necessary :class:`~pennylane.devices.LegacyDevice` methods as well as some built-in
qubit :doc:`operations </introduction/operations>`, providing a simple mixed-state simulation of
qubit-based quantum circuits.
"""
Expand Down
2 changes: 1 addition & 1 deletion pennylane/devices/device_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Device(abc.ABC):
.. details::
:title: Porting from the old interface

:meth:`pennylane.Device.batch_execute` and :meth:`~pennylane.Device.execute` are now a single method, :meth:`~.Device.execute`
:meth:`pennylane.devices.LegacyDevice.batch_execute` and :meth:`~pennylane.devices.LegacyDevice.execute` are now a single method, :meth:`~.Device.execute`

:meth:`~.Device.batch_transform` and :meth:`~.Device.expand_fn` are now a single method, :meth:`~.Device.preprocess`

Expand Down
2 changes: 1 addition & 1 deletion pennylane/gradients/parameter_shift_cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def param_shift_cv(

Args:
tape (QNode or QuantumTape): quantum circuit to differentiate
dev (pennylane.Device): device the parameter-shift method is to be computed on
dev (pennylane.devices.LegacyDevice): device the parameter-shift method is to be computed on
JerryChen97 marked this conversation as resolved.
Show resolved Hide resolved
argnum (int or list[int] or None): Trainable parameter indices to differentiate
with respect to. If not provided, the derivative with respect to all
trainable indices are returned.
Expand Down
2 changes: 1 addition & 1 deletion pennylane/measurements/classical_shadow.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def process(self, tape, device):

Args:
tape (QuantumTape): the quantum tape to be processed
device (pennylane.Device): the device used to process the quantum tape
device (pennylane.devices.LegacyDevice): the device used to process the quantum tape

Returns:
tensor_like[int]: A tensor with shape ``(2, T, n)``, where the first row represents
Expand Down
4 changes: 2 additions & 2 deletions pennylane/measurements/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ class MeasurementTransform(MeasurementProcess):
which should have the following arguments:

* tape (QuantumTape): quantum tape to transform
* device (pennylane.Device): device used to transform the quantum tape
* device (pennylane.devices.LegacyDevice): device used to transform the quantum tape
"""

@abstractmethod
Expand All @@ -685,5 +685,5 @@ def process(self, tape, device):

Args:
tape (QuantumTape): quantum tape to transform
device (pennylane.Device): device used to transform the quantum tape
device (pennylane.devices.LegacyDevice): device used to transform the quantum tape
"""
2 changes: 1 addition & 1 deletion pennylane/qcut/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def find_and_place_cuts(
device, a :class:`~.CutStrategy` can be used to populate the necessary explorations of cutting
parameters. As an extreme example, if the only device at our disposal is a 2-qubit device, a
simple cut strategy is to simply specify the the ``max_free_wires`` argument (or equivalently
directly passing a :class:`pennylane.Device` to the ``device`` argument):
directly passing a :class:`pennylane.devices.LegacyDevice` to the ``device`` argument):
JerryChen97 marked this conversation as resolved.
Show resolved Hide resolved

>>> cut_strategy = qml.qcut.CutStrategy(max_free_wires=2)
>>> cut_strategy.get_cut_kwargs(graph)
Expand Down
2 changes: 1 addition & 1 deletion pennylane/transforms/defer_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def defer_measurements(

.. note::

Devices that inherit from :class:`~pennylane.QubitDevice` **must** be initialized
Devices that inherit from :class:`~pennylane.devices.QubitDevice` **must** be initialized
with an additional wire for each mid-circuit measurement after which the measured
wire is reused or reset for ``defer_measurements`` to transform the quantum tape
correctly.
Expand Down
6 changes: 3 additions & 3 deletions pennylane/transforms/tape_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def create_expand_fn(depth, stop_at=None, device=None, docstring=None):
``stop_at(obj)``, where ``obj`` is a *queueable* PennyLane object such as
:class:`~.Operation` or :class:`~.MeasurementProcess`. It must return a
boolean, indicating if the expansion should stop at this object.
device (pennylane.Device): Ensure that the expanded tape only uses native gates of the
device (pennylane.devices.LegacyDevice): Ensure that the expanded tape only uses native gates of the
given device.
docstring (str): docstring for the generated expansion function

Expand Down Expand Up @@ -331,7 +331,7 @@ def create_decomp_expand_fn(custom_decomps, dev, decomp_depth=10):
Args:
custom_decomps (Dict[Union(str, qml.operation.Operation), Callable]): Custom
decompositions to be applied by the device at runtime.
dev (pennylane.Device): A quantum device.
dev (pennylane.devices.LegacyDevice): A quantum device.
decomp_depth: The maximum depth of the expansion.

Returns:
Expand Down Expand Up @@ -464,7 +464,7 @@ def set_decomposition(custom_decomps, dev, decomp_depth=10):
Args:
custom_decomps (Dict[Union(str, qml.operation.Operation), Callable]): Custom
decompositions to be applied by the device at runtime.
dev (pennylane.Device): A quantum device.
dev (pennylane.devices.LegacyDevice): A quantum device.
decomp_depth: The maximum depth of the expansion.

**Example**
Expand Down
2 changes: 1 addition & 1 deletion pennylane/workflow/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def execute(

Args:
tapes (Sequence[.QuantumTape]): batch of tapes to execute
device (pennylane.Device): Device to use to execute the batch of tapes.
device (pennylane.devices.LegacyDevice): Device to use to execute the batch of tapes.
If the device does not provide a ``batch_execute`` method,
by default the tapes will be executed in serial.
gradient_fn (None or callable): The gradient transform function to use
Expand Down
4 changes: 2 additions & 2 deletions pennylane/workflow/interfaces/jax_jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def jax_jit_jvp_execute(tapes, execute_fn, jpc, device):
tapes (Sequence[.QuantumTape]): batch of tapes to execute
execute_fn (Callable[[Sequence[.QuantumTape]], ResultBatch]): a function that turns a batch of circuits into results
jpc (JacobianProductCalculator): a class that can compute the Jacobian for the input tapes.
device (pennylane.Device, pennylane.devices.Device): The device used for execution. Used to determine the shapes of outputs for
device (pennylane.devices.LegacyDevice, pennylane.devices.Device): The device used for execution. Used to determine the shapes of outputs for
JerryChen97 marked this conversation as resolved.
Show resolved Hide resolved
pure callback calls.

Returns:
Expand All @@ -267,7 +267,7 @@ def jax_jit_vjp_execute(tapes, execute_fn, jpc, device=None):
execute_fn (Callable[[Sequence[.QuantumTape]], ResultBatch]): a function that turns a batch of circuits into results
jpc (JacobianProductCalculator): a class that can compute the vector Jacobian product (VJP)
for the input tapes.
device (pennylane.Device, pennylane.devices.Device): The device used for execution. Used to determine the shapes of outputs for
device (pennylane.devices.LegacyDevice, pennylane.devices.Device): The device used for execution. Used to determine the shapes of outputs for
JerryChen97 marked this conversation as resolved.
Show resolved Hide resolved
pure callback calls.

Returns:
Expand Down
2 changes: 1 addition & 1 deletion pennylane/workflow/interfaces/tensorflow_autograph.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def execute(

Args:
tapes (Sequence[.QuantumTape]): batch of tapes to execute
device (pennylane.Device): Device to use to execute the batch of tapes.
device (pennylane.devices.LegacyDevice): Device to use to execute the batch of tapes.
JerryChen97 marked this conversation as resolved.
Show resolved Hide resolved
If the device does not provide a ``batch_execute`` method,
by default the tapes will be executed in serial.
execute_fn (callable): The execution function used to execute the tapes
Expand Down
4 changes: 2 additions & 2 deletions pennylane/workflow/jacobian_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,11 @@ class DeviceDerivatives(JacobianProductCalculator):

Args:

device (Union[pennylane.Device, pennylane.devices.Device]): the device for execution and derivatives.
device (Union[pennylane.devices.LegacyDevice, pennylane.devices.Device]): the device for execution and derivatives.
Must support first order gradients with the requested configuration.
JerryChen97 marked this conversation as resolved.
Show resolved Hide resolved
execution_config (pennylane.devices.ExecutionConfig): a datastructure containing the options needed to fully
describe the execution. Only used with :class:`pennylane.devices.Device` from the new device interface.
gradient_kwargs (dict): a dictionary of keyword arguments for the gradients. Only used with a :class:`~.pennylane.Device`
gradient_kwargs (dict): a dictionary of keyword arguments for the gradients. Only used with a :class:`~.pennylane.devices.LegacyDevice`
from the old device interface.
JerryChen97 marked this conversation as resolved.
Show resolved Hide resolved

**Examples:**
Expand Down
6 changes: 0 additions & 6 deletions tests/devices/test_legacy_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,6 @@ def get_device(wires=1):
yield get_device


def test_deprecated_access():
"""Test that accessing via top-level is deprecated."""
with pytest.warns(qml.PennyLaneDeprecationWarning, match="Device will no longer be accessible"):
qml.Device # pylint: disable=pointless-statement


# pylint: disable=pointless-statement
def test_invalid_attribute_in_devices_raises_error():
with pytest.raises(AttributeError, match="'pennylane.devices' has no attribute 'blabla'"):
Expand Down
6 changes: 0 additions & 6 deletions tests/devices/test_qubit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,6 @@ def _working_get_batch_size(tensor, expected_shape, expected_size):
return None


def test_deprecated_access():
"""Test that accessing via top-level is deprecated."""
with pytest.warns(qml.PennyLaneDeprecationWarning, match="Device will no longer be accessible"):
qml.QubitDevice # pylint: disable=pointless-statement


def test_notimplemented_circuit_hash(mock_qubit_device):
"""Test that the circuit hash property is not implemented"""
dev = mock_qubit_device()
Expand Down
10 changes: 2 additions & 8 deletions tests/devices/test_qutrit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,6 @@ def get_qutrit_device(wires=1):
# TODO: Add tests for expval, var after observables are added


def test_deprecated_access():
"""Test that accessing via top-level is deprecated."""
with pytest.warns(qml.PennyLaneDeprecationWarning, match="Device will no longer be accessible"):
qml.QutritDevice # pylint: disable=pointless-statement


class TestOperations:
"""Tests the logic related to operations"""

Expand Down Expand Up @@ -1196,8 +1190,8 @@ def circuit(U):
class TestUnimplemented:
"""Tests for class methods that aren't implemented

These tests are for reaching 100% coverage of :class:`pennylane.QutritDevice`, as the
methods/properties being tested here have been overriden from :class:`pennylane.QubitDevice`
These tests are for reaching 100% coverage of :class:`pennylane.devices.QutritDevice`, as the
methods/properties being tested here have been overriden from :class:`pennylane.devices.QubitDevice`
to avoid unexpected behaviour, but do not yet have working implementations.
"""

Expand Down
2 changes: 1 addition & 1 deletion tests/test_qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def test_best_method_is_param_shift(self):

# pylint: disable=protected-access
@pytest.mark.xfail(
reason="qml.Device will always work with 'parameter-shift' and never falls back on 'finite-diff'"
reason="qml.device will always work with 'parameter-shift' and never falls back on 'finite-diff'"
JerryChen97 marked this conversation as resolved.
Show resolved Hide resolved
)
def test_best_method_is_finite_diff(self, monkeypatch):
"""Test that the method for determining the best diff method
Expand Down
Loading