Skip to content

Commit

Permalink
Merge branch 'master' into no-grad-on-diff-method-none
Browse files Browse the repository at this point in the history
  • Loading branch information
albi3ro authored Jan 15, 2025
2 parents 9c785ce + d90d539 commit 0cd911c
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 133 deletions.
22 changes: 11 additions & 11 deletions doc/development/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ deprecations are listed below.
Pending deprecations
--------------------

* The ``tape`` and ``qtape`` properties of ``QNode`` have been deprecated.
Instead, use the ``qml.workflow.construct_tape`` function.

- Deprecated in v0.40
- Will be removed in v0.41

* The ``max_expansion`` argument in :func:`~pennylane.devices.preprocess.decompose` is deprecated.

- Deprecated in v0.40
Expand All @@ -25,11 +19,6 @@ Pending deprecations
- Deprecated in v0.40
- Will be removed in v0.41

* The ``gradient_fn`` keyword argument to ``qml.execute`` has been renamed ``diff_method``.

- Deprecated in v0.40
- Will be removed in v0.41

* ``op.ops`` and ``op.coeffs`` for ``Sum`` and ``Prod`` will be removed in the future. Use
:meth:`~.Operator.terms` instead.

Expand Down Expand Up @@ -73,6 +62,17 @@ for details on how to port your legacy code to the new system. The following fun
Completed deprecation cycles
----------------------------

* The ``tape`` and ``qtape`` properties of ``QNode`` have been removed.
Instead, use the ``qml.workflow.construct_tape`` function.

- Deprecated in v0.40
- Removed in v0.41

* The ``gradient_fn`` keyword argument to ``qml.execute`` has been removed. Instead, it has been replaced with ``diff_method``.

- Deprecated in v0.40
- Removed in v0.41

* The ``QNode.get_best_method`` and ``QNode.best_method_str`` methods have been removed.
Instead, use the ``qml.workflow.get_best_diff_method`` function.

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

<h3>Breaking changes 💔</h3>

* The ``tape`` and ``qtape`` properties of ``QNode`` have been removed.
Instead, use the ``qml.workflow.construct_tape`` function.
[(#6825)](https://github.com/PennyLaneAI/pennylane/pull/6825)

* The ``gradient_fn`` keyword argument to ``qml.execute`` has been removed. Instead, it has been replaced with ``diff_method``.
[(#6830)](https://github.com/PennyLaneAI/pennylane/pull/6830)

* The ``QNode.get_best_method`` and ``QNode.best_method_str`` methods have been removed.
Instead, use the ``qml.workflow.get_best_diff_method`` function.
[(#6823)](https://github.com/PennyLaneAI/pennylane/pull/6823)
Expand All @@ -46,3 +53,4 @@ This release contains contributions from (in alphabetical order):
Yushao Chen,
Diksha Dhawan,
Christina Lee,
Andrija Paurevic
13 changes: 1 addition & 12 deletions pennylane/workflow/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from collections.abc import Callable
from dataclasses import replace
from typing import Optional, Union
from warnings import warn

from cachetools import Cache

Expand Down Expand Up @@ -55,7 +54,6 @@ def execute(
max_diff=1,
device_vjp=False,
mcm_config=None,
gradient_fn="unset",
) -> ResultBatch:
"""A function for executing a batch of tapes on a device with compatibility for auto-differentiation.
Expand Down Expand Up @@ -85,16 +83,14 @@ def execute(
cache (None, bool, dict, Cache): Whether to cache evaluations. This can result in
a significant reduction in quantum evaluations during gradient computations.
cachesize (int): the size of the cache.
max_diff (int): If ``gradient_fn`` is a gradient transform, this option specifies
max_diff (int): If ``diff_method`` is a gradient transform, this option specifies
the maximum number of derivatives to support. Increasing this value allows
for higher-order derivatives to be extracted, at the cost of additional
(classical) computational overhead during the backward pass.
device_vjp=False (Optional[bool]): whether or not to use the device-provided Jacobian
product if it is available.
mcm_config (dict): Dictionary containing configuration options for handling
mid-circuit measurements.
gradient_fn="unset": **DEPRECATED**. This keyword argument has been renamed
``diff_method`` and will be removed in v0.41.
Returns:
list[tensor_like[float]]: A nested list of tape results. Each element in
Expand Down Expand Up @@ -158,13 +154,6 @@ def cost_fn(params, x):
if not isinstance(device, qml.devices.Device):
device = qml.devices.LegacyDeviceFacade(device)

if gradient_fn != "unset":
warn(
"gradient_fn has been renamed to diff_method in qml.execute",
qml.PennyLaneDeprecationWarning,
)
diff_method = gradient_fn

if logger.isEnabledFor(logging.DEBUG):
logger.debug(
(
Expand Down
21 changes: 1 addition & 20 deletions pennylane/workflow/qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from pennylane.logging import debug_logger
from pennylane.math import Interface, SupportedInterfaceUserInput, get_canonical_interface_name
from pennylane.measurements import MidMeasureMP
from pennylane.tape import QuantumScript, QuantumTape
from pennylane.tape import QuantumScript
from pennylane.transforms.core import TransformContainer, TransformDispatcher, TransformProgram

from ._capture_qnode import capture_qnode
Expand Down Expand Up @@ -693,25 +693,6 @@ def get_gradient_fn(
f"options are {tuple(get_args(SupportedDiffMethods))}."
)

@property
def tape(self) -> QuantumTape:
"""The quantum tape
.. warning::
This property is deprecated in v0.40 and will be removed in v0.41.
Instead, use the :func:`qml.workflow.construct_tape <.workflow.construct_tape>` function.
"""

warnings.warn(
"The tape/qtape property is deprecated and will be removed in v0.41. "
"Instead, use the qml.workflow.construct_tape function.",
qml.PennyLaneDeprecationWarning,
)
return self._tape

qtape = tape # for backwards compatibility

@debug_logger
def construct(self, args, kwargs) -> qml.tape.QuantumScript:
"""Call the quantum function with a tape context, ensuring the operations get queued."""
Expand Down
4 changes: 2 additions & 2 deletions tests/devices/test_default_clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,9 @@ def circuit_fn():
return qml.expval(qml.PauliZ(0))

qnode_clfrd = qml.QNode(circuit_fn, dev_c)
qnode_clfrd()
tape = qml.workflow.construct_tape(qnode_clfrd)()

conf_c, tape_c = dev_c.setup_execution_config(), qnode_clfrd.tape
conf_c, tape_c = dev_c.setup_execution_config(), tape

with pytest.raises(
NotImplementedError,
Expand Down
4 changes: 2 additions & 2 deletions tests/ops/functions/test_map_wires.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ def qfunc():
assert isinstance(m_ops[1], Prod)
qml.assert_equal(m_ops[0], mapped_op)
qml.assert_equal(m_ops[1], mapped_op_2)
assert m_qnode.tape.observables[0].wires == Wires(wire_map[0])
assert m_qnode.tape.observables[1].wires == Wires(wire_map[1])
assert m_tape.observables[0].wires == Wires(wire_map[0])
assert m_tape.observables[1].wires == Wires(wire_map[1])

@pytest.mark.jax
def test_jitting_simplified_qfunc(self):
Expand Down
56 changes: 0 additions & 56 deletions tests/test_qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,6 @@
from pennylane.typing import PostprocessingFn


def test_tape_property_is_deprecated():
"""Test that the tape property is deprecated."""
dev = qml.device("default.qubit")

@qml.qnode(dev)
def circuit(x):
qml.RX(x, wires=0)
return qml.PauliY(0)

with pytest.warns(
qml.PennyLaneDeprecationWarning, match="The tape/qtape property is deprecated"
):
_ = circuit.tape


def dummyfunc():
"""dummy func."""
return None
Expand Down Expand Up @@ -482,47 +467,6 @@ def circuit():
class TestTapeConstruction:
"""Tests for the tape construction"""

def test_basic_tape_construction(self, tol):
"""Test that a quantum tape is properly constructed"""
dev = qml.device("default.qubit", wires=2)

def func(x, y):
qml.RX(x, wires=0)
qml.RY(y, wires=1)
qml.CNOT(wires=[0, 1])
return qml.expval(qml.PauliZ(0))

qn = QNode(func, dev)

x = pnp.array(0.12, requires_grad=True)
y = pnp.array(0.54, requires_grad=True)

res = qn(x, y)
with pytest.warns(
qml.PennyLaneDeprecationWarning, match="tape/qtape property is deprecated"
):
tape = qn.tape

assert isinstance(tape, QuantumScript)
assert len(tape.operations) == 3
assert len(tape.observables) == 1
assert tape.num_params == 2
assert tape.shots.total_shots is None

expected = qml.execute([tape], dev, None)
assert np.allclose(res, expected, atol=tol, rtol=0)

# when called, a new quantum tape is constructed
old_tape = tape
res2 = qn(x, y)
with pytest.warns(
qml.PennyLaneDeprecationWarning, match="tape/qtape property is deprecated"
):
new_tape = qn.tape

assert np.allclose(res, res2, atol=tol, rtol=0)
assert new_tape is not old_tape

def test_returning_non_measurements(self):
"""Test that an exception is raised if a non-measurement
is returned from the QNode."""
Expand Down
15 changes: 0 additions & 15 deletions tests/test_qnode_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,6 @@
from pennylane.typing import PostprocessingFn


def test_legacy_qtape_property_is_deprecated():
"""Test that the legacy qtape property is deprecated."""
dev = qml.device("default.qubit")

@qml.qnode(dev)
def circuit(x):
qml.RX(x, wires=0)
return qml.PauliY(0)

with pytest.warns(
qml.PennyLaneDeprecationWarning, match="The tape/qtape property is deprecated"
):
_ = circuit.qtape


def dummyfunc():
"""dummy func."""
return None
Expand Down
15 changes: 0 additions & 15 deletions tests/workflow/interfaces/execute/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,6 @@ def test_execute_legacy_device():
assert qml.math.allclose(res[0], np.cos(0.1))


def test_gradient_fn_deprecation():
"""Test that gradient_fn has been renamed to diff_method."""

tape = qml.tape.QuantumScript([qml.RX(qml.numpy.array(1.0), 0)], [qml.expval(qml.Z(0))])
dev = qml.device("default.qubit")

with dev.tracker:
with pytest.warns(
qml.PennyLaneDeprecationWarning, match=r"gradient_fn has been renamed to diff_method"
):
qml.execute((tape,), dev, gradient_fn="adjoint")

assert dev.tracker.totals["execute_and_derivative_batches"] == 1 # uses adjoint diff


def test_execution_with_empty_batch():
"""Test that qml.execute can be used with an empty batch."""

Expand Down

0 comments on commit 0cd911c

Please sign in to comment.