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

Deprecate inner_transform_program and config arguments to qml.execute #6822

Merged
merged 20 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
6 changes: 6 additions & 0 deletions doc/development/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ deprecations are listed below.
Pending deprecations
--------------------

* The ``inner_transform_program`` and ``config`` keyword arguments in ``qml.execute`` has been deprecated.
andrijapau marked this conversation as resolved.
Show resolved Hide resolved
Instead, use ``qml.run`` with these arguments for a more detailed control over the execution.
andrijapau marked this conversation as resolved.
Show resolved Hide resolved

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

* The ``qsvt_legacy`` function has been deprecated.
Instead, use ``qml.qsvt``. The new functionality takes an input polynomial instead of angles.

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

<h3>Deprecations 👋</h3>

* The ``inner_transform_program`` and ``config`` keyword arguments in ``qml.execute`` has been deprecated.
andrijapau marked this conversation as resolved.
Show resolved Hide resolved
Instead, use ``qml.run`` with these arguments for a more detailed control over the execution.
andrijapau marked this conversation as resolved.
Show resolved Hide resolved
[(#6822)](https://github.com/PennyLaneAI/pennylane/pull/6822)

<h3>Documentation 📝</h3>

* Updated documentation for vibrational Hamiltonians
Expand Down
60 changes: 34 additions & 26 deletions pennylane/workflow/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ def execute(
diff_method: Optional[Union[Callable, str, qml.transforms.core.TransformDispatcher]] = None,
interface: Optional[Union[str, Interface]] = Interface.AUTO,
transform_program=None,
inner_transform=None,
astralcai marked this conversation as resolved.
Show resolved Hide resolved
config=None,
grad_on_execution="best",
gradient_kwargs=None,
cache: Union[None, bool, dict, Cache] = True,
cachesize=10000,
max_diff=1,
device_vjp=False,
mcm_config=None,
config="unset",
inner_transform="unset",
gradient_fn="unset",
) -> ResultBatch:
"""A function for executing a batch of tapes on a device with compatibility for auto-differentiation.
Expand All @@ -70,10 +70,6 @@ def execute(
This affects the types of parameters that can exist on the input tapes.
Available options include ``autograd``, ``torch``, ``tf``, ``jax``, and ``auto``.
transform_program(.TransformProgram): A transform program to be applied to the initial tape.
inner_transform (.TransformProgram): A transform program to be applied to the tapes in
inner execution, inside the ml interface.
config (qml.devices.ExecutionConfig): A data structure describing the parameters
needed to fully describe the execution.
grad_on_execution (bool, str): Whether the gradients should be computed
on the execution or not. It only applies
if the device is queried for the gradient; gradient transform
Expand All @@ -92,6 +88,10 @@ def execute(
product if it is available.
mcm_config (dict): Dictionary containing configuration options for handling
mid-circuit measurements.
config="unset": **DEPRECATED**. This keyword argument has been deprecated and
will be removed in v0.42.
inner_transform="unset": **DEPRECATED**. This keyword argument has been deprecated
and will be removed in v0.42.
gradient_fn="unset": **DEPRECATED**. This keyword argument has been renamed
``diff_method`` and will be removed in v0.41.

Expand Down Expand Up @@ -164,6 +164,20 @@ def cost_fn(params, x):
)
diff_method = gradient_fn

if config != "unset":
warn(
"The config argument has been deprecated and will be removed in v0.42. \
andrijapau marked this conversation as resolved.
Show resolved Hide resolved
Instead, use qml.run with these arguments for a more detailed control over the execution.",
andrijapau marked this conversation as resolved.
Show resolved Hide resolved
qml.PennyLaneDeprecationWarning,
)

if inner_transform != "unset":
warn(
"The inner_transform argument has been deprecated and will be removed in v0.42. \
Instead, use qml.run with these arguments for a more detailed control over the execution.",
andrijapau marked this conversation as resolved.
Show resolved Hide resolved
qml.PennyLaneDeprecationWarning,
)

if logger.isEnabledFor(logging.DEBUG):
logger.debug(
(
Expand Down Expand Up @@ -195,33 +209,27 @@ def cost_fn(params, x):
interface = _resolve_interface(interface, tapes)
# Only need to calculate derivatives with jax when we know it will be executed later.

gradient_kwargs = gradient_kwargs or {}
mcm_config = mcm_config or {}
if not config:
config = qml.devices.ExecutionConfig(
interface=interface,
gradient_method=diff_method,
grad_on_execution=None if grad_on_execution == "best" else grad_on_execution,
use_device_jacobian_product=device_vjp,
mcm_config=mcm_config,
gradient_keyword_arguments=gradient_kwargs,
derivative_order=max_diff,
)
config = _resolve_execution_config(
config, device, tapes, transform_program=transform_program
)
config = qml.devices.ExecutionConfig(
interface=interface,
gradient_method=diff_method,
grad_on_execution=None if grad_on_execution == "best" else grad_on_execution,
use_device_jacobian_product=device_vjp,
mcm_config=mcm_config or {},
gradient_keyword_arguments=gradient_kwargs or {},
derivative_order=max_diff,
)
config = _resolve_execution_config(config, device, tapes, transform_program=transform_program)

config = replace(
config,
interface=interface,
derivative_order=max_diff,
)

if transform_program is None or inner_transform is None:
transform_program = transform_program or qml.transforms.core.TransformProgram()
transform_program, inner_transform = _setup_transform_program(
transform_program, device, config, cache, cachesize
)
transform_program = transform_program or qml.transforms.core.TransformProgram()
transform_program, inner_transform = _setup_transform_program(
transform_program, device, config, cache, cachesize
)

#### Executing the configured setup #####
tapes, post_processing = transform_program(tapes)
Expand Down
22 changes: 22 additions & 0 deletions tests/workflow/interfaces/execute/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ def test_gradient_fn_deprecation():
assert dev.tracker.totals["execute_and_derivative_batches"] == 1 # uses adjoint diff


def test_config_deprecation():
"""Test that the config argument has been deprecated."""

tape = qml.tape.QuantumScript([qml.RX(qml.numpy.array(1.0), 0)], [qml.expval(qml.Z(0))])
dev = qml.device("default.qubit")
with pytest.warns(
qml.PennyLaneDeprecationWarning, match="The config argument has been deprecated"
):
qml.execute((tape,), dev, config=qml.devices.DefaultExecutionConfig)


def test_inner_transform_program_deprecation():
"""Test that the inner_transform argument has been deprecated."""

tape = qml.tape.QuantumScript([qml.RX(qml.numpy.array(1.0), 0)], [qml.expval(qml.Z(0))])
dev = qml.device("default.qubit")
with pytest.warns(
qml.PennyLaneDeprecationWarning, match="The inner_transform argument has been deprecated"
):
qml.execute((tape,), dev, inner_transform=qml.transforms.core.TransformProgram())


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

Expand Down
Loading