From 341f3e243d60c8397740dd20e8778a0913f85a15 Mon Sep 17 00:00:00 2001 From: albi3ro Date: Tue, 19 Nov 2024 17:31:56 -0500 Subject: [PATCH 1/5] enforce passing by keyword argument --- doc/releases/changelog-dev.md | 2 ++ pennylane/workflow/execution.py | 1 + pennylane/workflow/qnode.py | 1 + 3 files changed, 4 insertions(+) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index f6abe3c5002..91d33418d45 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -37,6 +37,8 @@ visualizations, allowing global and per-wire customization with options like `color`, `linestyle`, and `linewidth`. [(#6486)](https://github.com/PennyLaneAI/pennylane/pull/6486) +* `QNode` and `qml.execute` now forbid certain keyword arguments from being passed positionally. + * Shortened the string representation for the `qml.S`, `qml.T`, and `qml.SX` operators. [(#6542)](https://github.com/PennyLaneAI/pennylane/pull/6542) diff --git a/pennylane/workflow/execution.py b/pennylane/workflow/execution.py index deb58d12634..656c034b85b 100644 --- a/pennylane/workflow/execution.py +++ b/pennylane/workflow/execution.py @@ -313,6 +313,7 @@ def execute( device: SupportedDeviceAPIs, diff_method: Optional[Union[Callable, str, qml.transforms.core.TransformDispatcher]] = None, interface: Optional[str] = "auto", + *, transform_program=None, inner_transform=None, config=None, diff --git a/pennylane/workflow/qnode.py b/pennylane/workflow/qnode.py index f7007df30b7..4b30aae0dc2 100644 --- a/pennylane/workflow/qnode.py +++ b/pennylane/workflow/qnode.py @@ -501,6 +501,7 @@ def __init__( device: SupportedDeviceAPIs, interface: SupportedInterfaceUserInput = "auto", diff_method: Union[TransformDispatcher, SupportedDiffMethods] = "best", + *, grad_on_execution: Literal[True, False, "best"] = "best", cache: Union[Cache, Literal["auto", True, False]] = "auto", cachesize: int = 10000, From 0efae5519fab41eaaa5792f05dd7a7b499534ae6 Mon Sep 17 00:00:00 2001 From: Christina Lee Date: Tue, 19 Nov 2024 17:34:21 -0500 Subject: [PATCH 2/5] Update doc/releases/changelog-dev.md --- doc/releases/changelog-dev.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 91d33418d45..36b5ad4f11e 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -38,6 +38,7 @@ [(#6486)](https://github.com/PennyLaneAI/pennylane/pull/6486) * `QNode` and `qml.execute` now forbid certain keyword arguments from being passed positionally. + [(#6610)](https://github.com/PennyLaneAI/pennylane/pull/6610) * Shortened the string representation for the `qml.S`, `qml.T`, and `qml.SX` operators. [(#6542)](https://github.com/PennyLaneAI/pennylane/pull/6542) From 8eb3a9a52ea47f2abffc6b6c2ffc469b6b85aee9 Mon Sep 17 00:00:00 2001 From: albi3ro Date: Wed, 20 Nov 2024 09:35:07 -0500 Subject: [PATCH 3/5] final silencing, cbreaking change entry --- doc/releases/changelog-dev.md | 4 ++++ pennylane/workflow/execution.py | 35 ++++++++++----------------------- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 36b5ad4f11e..9684b48ffbc 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -78,6 +78,10 @@

Breaking changes 💔

+* `QNode` and `qml.execute` now forbid certain keyword arguments from being passed positionally. + This helps prevent keyword arguments from being passed in the wrong order. + [(#6610)](https://github.com/PennyLaneAI/pennylane/pull/6610) + * Gradient transforms are now applied after the user's transform program. [(#6590)](https://github.com/PennyLaneAI/pennylane/pull/6590) diff --git a/pennylane/workflow/execution.py b/pennylane/workflow/execution.py index 656c034b85b..1b00b9e12c5 100644 --- a/pennylane/workflow/execution.py +++ b/pennylane/workflow/execution.py @@ -469,9 +469,16 @@ def cost_fn(params, x): gradient_kwargs = gradient_kwargs or {} mcm_config = mcm_config or {} - config = config or _get_execution_config( - diff_method, grad_on_execution, interface, device, device_vjp, mcm_config, gradient_kwargs - ) + 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, + ) + config = device.preprocess(config)[1] # Mid-circuit measurement configuration validation # If the user specifies `interface=None`, regular execution considers it numpy, but the mcm @@ -661,25 +668,3 @@ def _make_transform_programs( transform_program = device.preprocess(config)[0] return transform_program, inner_transform - - -def _get_execution_config( - diff_method, grad_on_execution, interface, device, device_vjp, mcm_config, gradient_kwargs -): - """Helper function to get the execution config.""" - if diff_method is None: - _gradient_method = None - elif isinstance(diff_method, str): - _gradient_method = diff_method - else: - _gradient_method = "gradient-transform" - config = qml.devices.ExecutionConfig( - interface=interface, - gradient_method=_gradient_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, - ) - - return device.preprocess(config)[1] From c959e2fe7326f19925fe84d64de516c56e16f540 Mon Sep 17 00:00:00 2001 From: albi3ro Date: Wed, 20 Nov 2024 10:29:02 -0500 Subject: [PATCH 4/5] fix buggy tests --- tests/transforms/test_add_noise.py | 2 +- tests/transforms/test_insert_ops.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/transforms/test_add_noise.py b/tests/transforms/test_add_noise.py index 082f8ce6b1b..1835b613d13 100644 --- a/tests/transforms/test_add_noise.py +++ b/tests/transforms/test_add_noise.py @@ -215,7 +215,7 @@ def test_add_noise_dev(self, dev_name): new_program, _ = new_dev.preprocess() [tape], _ = new_program([in_tape]) res_with_noise = qml.execute( - [in_tape], new_dev, qml.gradients, transform_program=new_program + [in_tape], new_dev, qml.gradients.param_shift, transform_program=new_program ) with qml.queuing.AnnotatedQueue() as q_tape_exp: diff --git a/tests/transforms/test_insert_ops.py b/tests/transforms/test_insert_ops.py index bf03d9b7c5c..a56fea89787 100644 --- a/tests/transforms/test_insert_ops.py +++ b/tests/transforms/test_insert_ops.py @@ -435,7 +435,9 @@ def test_insert_dev(dev_name): new_program, _ = new_dev.preprocess() tapes, _ = new_program([in_tape]) tape = tapes[0] - res_with_noise = qml.execute([in_tape], new_dev, qml.gradients, transform_program=new_program) + res_with_noise = qml.execute( + [in_tape], new_dev, qml.gradients.param_shift, transform_program=new_program + ) with qml.queuing.AnnotatedQueue() as q_tape_exp: qml.RX(0.9, wires=0) From 59cc66f67e88834d993cc961cd8e93e916737b18 Mon Sep 17 00:00:00 2001 From: Christina Lee Date: Wed, 27 Nov 2024 11:29:08 -0500 Subject: [PATCH 5/5] Update doc/releases/changelog-dev.md --- doc/releases/changelog-dev.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index b808c6c3a38..8498517d199 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -159,10 +159,6 @@

Breaking changes 💔

-* `QNode` and `qml.execute` now forbid certain keyword arguments from being passed positionally. - This helps prevent keyword arguments from being passed in the wrong order. - [(#6610)](https://github.com/PennyLaneAI/pennylane/pull/6610) - * `qml.fourier.qnode_spectrum` no longer automatically converts pure numpy parameters to the Autograd framework. As the function uses automatic differentiation for validation, parameters from an autodiff framework have to be used.