From 5f8619fa1b1060a898f729dfe5c5e538564a53d8 Mon Sep 17 00:00:00 2001 From: PietropaoloFrisoni Date: Fri, 10 May 2024 15:54:58 -0400 Subject: [PATCH 01/13] E.C. [ci skip] From 1bda7f7331389e7b3868c3378fd0539d30d4d729 Mon Sep 17 00:00:00 2001 From: PietropaoloFrisoni Date: Mon, 13 May 2024 10:05:19 -0400 Subject: [PATCH 02/13] added test for deprecation and warning in `map_batch_transform` [ci skip] --- pennylane/transforms/batch_transform.py | 12 ++++++++++++ tests/transforms/test_batch_transform.py | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/pennylane/transforms/batch_transform.py b/pennylane/transforms/batch_transform.py index 43aeede21bb..fe366b10421 100644 --- a/pennylane/transforms/batch_transform.py +++ b/pennylane/transforms/batch_transform.py @@ -18,6 +18,7 @@ import pennylane as qml from pennylane.typing import ResultBatch +import warnings PostprocessingFn = Callable[[ResultBatch], ResultBatch] QuantumTapeBatch = Tuple[qml.tape.QuantumScript] @@ -64,7 +65,18 @@ def map_batch_transform( >>> dev = qml.device("default.qubit", wires=2) >>> fn(qml.execute(tapes, dev, qml.gradients.param_shift)) [array(0.99500417), array(0.8150893)] + + .. warning:: + qml.transforms.map_batch_transform is deprecated and will be removed in a future release. + Please ... . """ + + warnings.warn( + "qml.transforms.map_batch_transform is deprecated." + "Please ... .", + qml.PennyLaneDeprecationWarning, + ) + execution_tapes = [] batch_fns = [] tape_counts = [] diff --git a/tests/transforms/test_batch_transform.py b/tests/transforms/test_batch_transform.py index aaba0a8148a..e6a647b43f7 100644 --- a/tests/transforms/test_batch_transform.py +++ b/tests/transforms/test_batch_transform.py @@ -18,11 +18,25 @@ import pennylane as qml from pennylane import numpy as np +import pytest class TestMapBatchTransform: """Tests for the map_batch_transform function""" + def test_map_batch_transform_is_deprecated(self): + """Test that map_batch_transform is deprecated.""" + ops = [qml.RX(1.2345, wires=0)] + measurements = [qml.expval(qml.Z(0))] + tape = qml.tape.QuantumTape(ops, measurements) + transform = qml.transforms.compile + with pytest.warns( + qml.PennyLaneDeprecationWarning, + match="deprecated", + ): + _ = qml.transforms.map_batch_transform(transform, [tape]) + + @pytest.mark.filterwarnings("ignore") def test_result(self, mocker): """Test that it correctly applies the transform to be mapped""" dev = qml.device("default.qubit.legacy", wires=2) @@ -57,6 +71,7 @@ def test_result(self, mocker): assert np.allclose(fn(res), expected) + @pytest.mark.filterwarnings("ignore") def test_differentiation(self): """Test that an execution using map_batch_transform can be differentiated""" dev = qml.device("default.qubit.legacy", wires=2) From 26d156d5e293383c66d7b07410af0688063705b1 Mon Sep 17 00:00:00 2001 From: PietropaoloFrisoni Date: Mon, 13 May 2024 10:06:53 -0400 Subject: [PATCH 03/13] Formatting [ci skip] --- pennylane/transforms/batch_transform.py | 5 ++--- tests/transforms/test_batch_transform.py | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pennylane/transforms/batch_transform.py b/pennylane/transforms/batch_transform.py index fe366b10421..df9e76408e0 100644 --- a/pennylane/transforms/batch_transform.py +++ b/pennylane/transforms/batch_transform.py @@ -14,11 +14,11 @@ """Contains tools and decorators for registering batch transforms.""" # pylint: disable=too-few-public-methods +import warnings from typing import Callable, Tuple import pennylane as qml from pennylane.typing import ResultBatch -import warnings PostprocessingFn = Callable[[ResultBatch], ResultBatch] QuantumTapeBatch = Tuple[qml.tape.QuantumScript] @@ -72,8 +72,7 @@ def map_batch_transform( """ warnings.warn( - "qml.transforms.map_batch_transform is deprecated." - "Please ... .", + "qml.transforms.map_batch_transform is deprecated." "Please ... .", qml.PennyLaneDeprecationWarning, ) diff --git a/tests/transforms/test_batch_transform.py b/tests/transforms/test_batch_transform.py index e6a647b43f7..1af6175e501 100644 --- a/tests/transforms/test_batch_transform.py +++ b/tests/transforms/test_batch_transform.py @@ -16,9 +16,10 @@ """ # pylint: disable=too-few-public-methods,not-callable +import pytest + import pennylane as qml from pennylane import numpy as np -import pytest class TestMapBatchTransform: From 3ec63a7ddae1963166ea9b3d5c6bd2974a9568cc Mon Sep 17 00:00:00 2001 From: PietropaoloFrisoni Date: Mon, 13 May 2024 15:49:09 -0400 Subject: [PATCH 04/13] doc test [ci skip] --- pennylane/transforms/batch_transform.py | 3 ++- pennylane/transforms/core/transform.py | 10 ++++++++++ pennylane/transforms/core/transform_dispatcher.py | 8 ++++---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pennylane/transforms/batch_transform.py b/pennylane/transforms/batch_transform.py index df9e76408e0..7e3ed058ed7 100644 --- a/pennylane/transforms/batch_transform.py +++ b/pennylane/transforms/batch_transform.py @@ -72,7 +72,8 @@ def map_batch_transform( """ warnings.warn( - "qml.transforms.map_batch_transform is deprecated." "Please ... .", + "qml.transforms.map_batch_transform is deprecated." + "Instead, you can directly apply a transform to a batch of tapes. See ...", qml.PennyLaneDeprecationWarning, ) diff --git a/pennylane/transforms/core/transform.py b/pennylane/transforms/core/transform.py index f2327bd1d70..bf5dcc8b210 100644 --- a/pennylane/transforms/core/transform.py +++ b/pennylane/transforms/core/transform.py @@ -126,6 +126,11 @@ def qnode_circuit(a): and creates a sequence of tapes to be executed. The execution results are then post-processed in the reverse order of the transform program to obtain the final results. + .. details:: + :title: Applying a transform to a batch of tapes + + + .. details:: :title: Signature of a transform @@ -134,6 +139,7 @@ def qnode_circuit(a): - :class:`pennylane.QNode` - a quantum function (callable) - :class:`pennylane.tape.QuantumTape` + - a batch of :class:`pennylane.tape.QuantumTape` - :class:`pennylane.devices.Device`. For each object, the transform will be applied in a different way, but it always preserves the underlying @@ -157,6 +163,10 @@ def qnode_circuit(a): :class:`~.QuantumTape`. It returns a sequence of :class:`~.QuantumTape` and a processing function to be applied after execution. + - For a batch of :class:`pennylane.tape.QuantumTape`, the quantum transform is mapped across all the tapes. + It returns a sequence of :class:`~.QuantumTape` and a processing function to be applied after execution. + Each tape in the sequence is transformed by the transform. + - For a :class:`~.devices.Device`, the transform is added to the device's transform program and a transformed :class:`pennylane.devices.Device` is returned. The transform is added to the end of the device program and will be last in the overall transform program. diff --git a/pennylane/transforms/core/transform_dispatcher.py b/pennylane/transforms/core/transform_dispatcher.py index f8263fa42e2..bd216d6f5b9 100644 --- a/pennylane/transforms/core/transform_dispatcher.py +++ b/pennylane/transforms/core/transform_dispatcher.py @@ -324,7 +324,7 @@ def original_device(self): return TransformedDevice(original_device, self._transform) def _batch_transform(self, original_batch, targs, tkwargs): - """Apply the transform on a batch of tapes""" + """Apply the transform on a batch of tapes.""" execution_tapes = [] batch_fns = [] tape_counts = [] @@ -342,14 +342,14 @@ def processing_fn(res: ResultBatch) -> ResultBatch: """Applies a batch of post-processing functions to results. Args: - res (ResultBatch): the results of executing a batch of circuits + res (ResultBatch): the results of executing a batch of circuits. Returns: - ResultBatch : results that have undergone classical post processing + ResultBatch: results that have undergone classical post processing. Closure variables: tape_counts: the number of tapes outputted from each application of the transform - batch_fns: the post processing functions to apply to each sub-batch + batch_fns: the post processing functions to apply to each sub-batch. """ count = 0 From 286227ccee5f5387083f855fece575805311f941 Mon Sep 17 00:00:00 2001 From: PietropaoloFrisoni Date: Mon, 13 May 2024 16:20:26 -0400 Subject: [PATCH 05/13] doc check --- pennylane/transforms/core/transform.py | 42 ++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/pennylane/transforms/core/transform.py b/pennylane/transforms/core/transform.py index bf5dcc8b210..49d53e71b30 100644 --- a/pennylane/transforms/core/transform.py +++ b/pennylane/transforms/core/transform.py @@ -127,9 +127,47 @@ def qnode_circuit(a): reverse order of the transform program to obtain the final results. .. details:: - :title: Applying a transform to a batch of tapes + :title: Dispatch a transform onto a batch of tapes + + We can compose multiple transforms when working in the tape paradigm. + + **Example** + + In this example, we first apply two transforms to a batch of tapes. The first transform expands the Hamiltonian, + and the second merge the rotations. We then execute the transformed tapes on a device and post-process the results. + + .. code-block:: python + + H = qml.PauliY(2) @ qml.PauliZ(1) + 0.5 * qml.PauliZ(2) + qml.PauliZ(1) + measur = [qml.expval(H)] + ops = [qml.Hadamard(0), qml.RX(0.2, 0), qml.RX(0.6, 0), qml.CNOT((0, 1))] + tape = qml.tape.QuantumTape(ops, measur) + + batch1, fn1 = qml.transforms.hamiltonian_expand(tape) + batch2, fn2 = qml.transforms.merge_rotations(batch1) + + dev = qml.device("default.qubit", wires=3) + result = dev.execute(batch2) + post_processed_result = fn1(fn2(result)) + + The first transform returns a batch of tapes and a processing function. + + >>> batch1 + [, + ] + + The second transform is applied to the batch of tapes returned by the first transform. + + >>> batch2 + (, + ) + + The post-processing function is applied to the results of the execution of the transformed tapes. + + >>> post_processed_result + [array(0.5)] + - .. details:: :title: Signature of a transform From 2383253ca0ec6d54a59b397938f5e0a5e626c967 Mon Sep 17 00:00:00 2001 From: PietropaoloFrisoni Date: Mon, 13 May 2024 17:01:55 -0400 Subject: [PATCH 06/13] Doc check --- pennylane/transforms/core/transform.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/pennylane/transforms/core/transform.py b/pennylane/transforms/core/transform.py index 49d53e71b30..ec005cc9d49 100644 --- a/pennylane/transforms/core/transform.py +++ b/pennylane/transforms/core/transform.py @@ -129,12 +129,13 @@ def qnode_circuit(a): .. details:: :title: Dispatch a transform onto a batch of tapes - We can compose multiple transforms when working in the tape paradigm. + We can compose multiple transforms when working in the tape paradigm and apply them to more than one tape. + The following example demonstrates how to apply a transform to a batch of tapes. **Example** - In this example, we first apply two transforms to a batch of tapes. The first transform expands the Hamiltonian, - and the second merge the rotations. We then execute the transformed tapes on a device and post-process the results. + In this example, we first apply a transform to a tape and another one to a batch of tapes. + We then execute the transformed tapes on a device and post-process the results. .. code-block:: python @@ -148,26 +149,22 @@ def qnode_circuit(a): dev = qml.device("default.qubit", wires=3) result = dev.execute(batch2) - post_processed_result = fn1(fn2(result)) - - The first transform returns a batch of tapes and a processing function. - - >>> batch1 - [, - ] + The first transform splits the original tape. It returns a batch of tapes and a processing function. The second transform is applied to the batch of tapes returned by the first transform. + It returns a new batch of tapes, each of which has been transformed by the second transform, and a processing function. >>> batch2 (, ) - The post-processing function is applied to the results of the execution of the transformed tapes. - - >>> post_processed_result - [array(0.5)] + >>> type(fn2) + function + We can combine the processing functions to post-process the results of the execution. + >>> fn1(fn2(result)) + [array(0.5)] .. details:: :title: Signature of a transform From 6d1e29474d9ae828761beaf7fa14a537f22c95ff Mon Sep 17 00:00:00 2001 From: PietropaoloFrisoni Date: Mon, 13 May 2024 17:52:48 -0400 Subject: [PATCH 07/13] Need to link the new expandable section --- doc/development/deprecations.rst | 4 ++++ doc/releases/changelog-dev.md | 3 +++ pennylane/transforms/batch_transform.py | 5 +++-- .../transforms/core/transform_dispatcher.py | 2 +- tests/transforms/test_batch_transform.py | 22 ++++++++++++------- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/doc/development/deprecations.rst b/doc/development/deprecations.rst index 35936e6a6af..69402a34992 100644 --- a/doc/development/deprecations.rst +++ b/doc/development/deprecations.rst @@ -9,6 +9,10 @@ deprecations are listed below. Pending deprecations -------------------- +* ``qml.transforms.map_batch_transform`` is deprecated, since transforms can be applied directly to a batch of tapes. + + - Deprecated in v0.37 + New operator arithmetic deprecations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index ed88f98fcb4..c22592ad113 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -95,6 +95,9 @@

Deprecations 👋

+* ``qml.transforms.map_batch_transform`` is deprecated, since a transform can be applied directly to a batch of tapes. + [(#5676)](https://github.com/PennyLaneAI/pennylane/pull/5676) +

Documentation 📝

Bug fixes 🐛

diff --git a/pennylane/transforms/batch_transform.py b/pennylane/transforms/batch_transform.py index 7e3ed058ed7..cc85ef8582d 100644 --- a/pennylane/transforms/batch_transform.py +++ b/pennylane/transforms/batch_transform.py @@ -68,12 +68,13 @@ def map_batch_transform( .. warning:: qml.transforms.map_batch_transform is deprecated and will be removed in a future release. - Please ... . + Instead, a transform can be applied directly to a batch of tapes. See ... for more details. """ warnings.warn( "qml.transforms.map_batch_transform is deprecated." - "Instead, you can directly apply a transform to a batch of tapes. See ...", + "Instead, a transform can be applied directly to a batch of tapes." + "See ... for more details.", qml.PennyLaneDeprecationWarning, ) diff --git a/pennylane/transforms/core/transform_dispatcher.py b/pennylane/transforms/core/transform_dispatcher.py index bd216d6f5b9..a8ffda58554 100644 --- a/pennylane/transforms/core/transform_dispatcher.py +++ b/pennylane/transforms/core/transform_dispatcher.py @@ -330,7 +330,7 @@ def _batch_transform(self, original_batch, targs, tkwargs): tape_counts = [] for t in original_batch: - # Preprocess the tapes by applying batch transforms + # Preprocess the tapes by applying transforms # to each tape, and storing corresponding tapes # for execution, processing functions, and list of tape lengths. new_tapes, fn = self(t, *targs, **tkwargs) diff --git a/tests/transforms/test_batch_transform.py b/tests/transforms/test_batch_transform.py index 1af6175e501..7ce2f07e47b 100644 --- a/tests/transforms/test_batch_transform.py +++ b/tests/transforms/test_batch_transform.py @@ -37,7 +37,6 @@ def test_map_batch_transform_is_deprecated(self): ): _ = qml.transforms.map_batch_transform(transform, [tape]) - @pytest.mark.filterwarnings("ignore") def test_result(self, mocker): """Test that it correctly applies the transform to be mapped""" dev = qml.device("default.qubit.legacy", wires=2) @@ -60,9 +59,13 @@ def test_result(self, mocker): tape2 = qml.tape.QuantumScript.from_queue(q2) spy = mocker.spy(qml.transforms, "hamiltonian_expand") - tapes, fn = qml.transforms.map_batch_transform( - qml.transforms.hamiltonian_expand, [tape1, tape2] - ) + with pytest.warns( + qml.PennyLaneDeprecationWarning, + match="deprecated", + ): + tapes, fn = qml.transforms.map_batch_transform( + qml.transforms.hamiltonian_expand, [tape1, tape2] + ) spy.assert_called() assert len(tapes) == 5 @@ -72,7 +75,6 @@ def test_result(self, mocker): assert np.allclose(fn(res), expected) - @pytest.mark.filterwarnings("ignore") def test_differentiation(self): """Test that an execution using map_batch_transform can be differentiated""" dev = qml.device("default.qubit.legacy", wires=2) @@ -95,9 +97,13 @@ def cost(weights): qml.expval(H + qml.Hamiltonian([0.5], [qml.PauliY(0)])) tape2 = qml.tape.QuantumScript.from_queue(q2) - tapes, fn = qml.transforms.map_batch_transform( - qml.transforms.hamiltonian_expand, [tape1, tape2] - ) + with pytest.warns( + qml.PennyLaneDeprecationWarning, + match="deprecated", + ): + tapes, fn = qml.transforms.map_batch_transform( + qml.transforms.hamiltonian_expand, [tape1, tape2] + ) res = qml.execute(tapes, dev, qml.gradients.param_shift, device_batch_transform=False) return np.sum(fn(res)) From d0e2f2edc9ee6c8eb2c5a37b714c55ba0371fb85 Mon Sep 17 00:00:00 2001 From: PietropaoloFrisoni Date: Mon, 13 May 2024 18:13:35 -0400 Subject: [PATCH 08/13] Providing link to doc --- doc/development/deprecations.rst | 1 + pennylane/transforms/batch_transform.py | 4 ++-- pennylane/transforms/core/transform.py | 2 +- pennylane/transforms/core/transform_dispatcher.py | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/doc/development/deprecations.rst b/doc/development/deprecations.rst index 69402a34992..5a8f0cbac9e 100644 --- a/doc/development/deprecations.rst +++ b/doc/development/deprecations.rst @@ -10,6 +10,7 @@ Pending deprecations -------------------- * ``qml.transforms.map_batch_transform`` is deprecated, since transforms can be applied directly to a batch of tapes. + See :func:`~.pennylane.transform` for more information. - Deprecated in v0.37 diff --git a/pennylane/transforms/batch_transform.py b/pennylane/transforms/batch_transform.py index cc85ef8582d..9d07f1d5ea6 100644 --- a/pennylane/transforms/batch_transform.py +++ b/pennylane/transforms/batch_transform.py @@ -68,13 +68,13 @@ def map_batch_transform( .. warning:: qml.transforms.map_batch_transform is deprecated and will be removed in a future release. - Instead, a transform can be applied directly to a batch of tapes. See ... for more details. + Instead, a transform can be applied directly to a batch of tapes. See :func:`~.pennylane.transform` for more details. """ warnings.warn( "qml.transforms.map_batch_transform is deprecated." "Instead, a transform can be applied directly to a batch of tapes." - "See ... for more details.", + "See :func:`~.pennylane.transform` for more details.", qml.PennyLaneDeprecationWarning, ) diff --git a/pennylane/transforms/core/transform.py b/pennylane/transforms/core/transform.py index ec005cc9d49..4e5e87bdee5 100644 --- a/pennylane/transforms/core/transform.py +++ b/pennylane/transforms/core/transform.py @@ -150,7 +150,7 @@ def qnode_circuit(a): dev = qml.device("default.qubit", wires=3) result = dev.execute(batch2) - The first transform splits the original tape. It returns a batch of tapes and a processing function. + The first transform splits the original tape, returning a batch of tapes and a processing function. The second transform is applied to the batch of tapes returned by the first transform. It returns a new batch of tapes, each of which has been transformed by the second transform, and a processing function. diff --git a/pennylane/transforms/core/transform_dispatcher.py b/pennylane/transforms/core/transform_dispatcher.py index a8ffda58554..b9a4f4a7165 100644 --- a/pennylane/transforms/core/transform_dispatcher.py +++ b/pennylane/transforms/core/transform_dispatcher.py @@ -348,7 +348,7 @@ def processing_fn(res: ResultBatch) -> ResultBatch: ResultBatch: results that have undergone classical post processing. Closure variables: - tape_counts: the number of tapes outputted from each application of the transform + tape_counts: the number of tapes outputted from each application of the transform. batch_fns: the post processing functions to apply to each sub-batch. """ From 65da4f3ba927e79a750bbb8e5d7b0bd6409e3598 Mon Sep 17 00:00:00 2001 From: PietropaoloFrisoni Date: Tue, 14 May 2024 09:33:00 -0400 Subject: [PATCH 09/13] Improving doc example [ci skip] --- pennylane/transforms/core/transform.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pennylane/transforms/core/transform.py b/pennylane/transforms/core/transform.py index 4e5e87bdee5..a3b2d3d29b1 100644 --- a/pennylane/transforms/core/transform.py +++ b/pennylane/transforms/core/transform.py @@ -134,36 +134,38 @@ def qnode_circuit(a): **Example** - In this example, we first apply a transform to a tape and another one to a batch of tapes. + In this example, we apply sequentially a transform to a tape and another one to a batch of tapes. We then execute the transformed tapes on a device and post-process the results. .. code-block:: python + import pennylane as qml + H = qml.PauliY(2) @ qml.PauliZ(1) + 0.5 * qml.PauliZ(2) + qml.PauliZ(1) measur = [qml.expval(H)] ops = [qml.Hadamard(0), qml.RX(0.2, 0), qml.RX(0.6, 0), qml.CNOT((0, 1))] tape = qml.tape.QuantumTape(ops, measur) - batch1, fn1 = qml.transforms.hamiltonian_expand(tape) - batch2, fn2 = qml.transforms.merge_rotations(batch1) + batch1, function1 = qml.transforms.hamiltonian_expand(tape) + batch2, function2 = qml.transforms.merge_rotations(batch1) dev = qml.device("default.qubit", wires=3) result = dev.execute(batch2) - The first transform splits the original tape, returning a batch of tapes and a processing function. - The second transform is applied to the batch of tapes returned by the first transform. - It returns a new batch of tapes, each of which has been transformed by the second transform, and a processing function. + The first ``hamiltonian_expand`` transform splits the original tape, returning a batch of tapes ``batch1`` and a processing function ``function1``. + The second ``merge_rotations`` transform is applied to the batch of tapes returned by the first transform. + It returns a new batch of tapes ``batch2``, each of which has been transformed by the second transform, and a processing function ``function2``. >>> batch2 (, ) - >>> type(fn2) + >>> type(function2) function We can combine the processing functions to post-process the results of the execution. - >>> fn1(fn2(result)) + >>> function1(function2(result)) [array(0.5)] .. details:: From fd473ef41fc82f244257463b939865c2c97b2646 Mon Sep 17 00:00:00 2001 From: PietropaoloFrisoni Date: Tue, 14 May 2024 09:37:15 -0400 Subject: [PATCH 10/13] Testing doc From 784b7f5c5bccb085000773bfb0bf1af5388c571b Mon Sep 17 00:00:00 2001 From: PietropaoloFrisoni Date: Tue, 14 May 2024 14:51:39 -0400 Subject: [PATCH 11/13] Changing back from map_batch_transf --- tests/param_shift_dev.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/param_shift_dev.py b/tests/param_shift_dev.py index 4454b6ead17..9668880f219 100644 --- a/tests/param_shift_dev.py +++ b/tests/param_shift_dev.py @@ -52,7 +52,7 @@ def compute_derivatives(self, circuits, execution_config=None): self.tracker.update(derivative_batches=1, derivatives=len(circuits)) self.tracker.record() - diff_batch, fn = qml.transforms.map_batch_transform(qml.gradients.param_shift, circuits) + diff_batch, fn = qml.transforms(qml.gradients.param_shift, circuits) diff_results = self.execute(diff_batch) jacs = fn(diff_results) @@ -73,7 +73,7 @@ def execute_and_compute_derivatives(self, circuits, execution_config=None): ) self.tracker.record() - diff_batch, fn = qml.transforms.map_batch_transform(qml.gradients.param_shift, circuits) + diff_batch, fn = qml.transforms(qml.gradients.param_shift, circuits) combined_batch = tuple(circuits) + tuple(diff_batch) all_results = self.execute(combined_batch) results = all_results[: len(circuits)] From e8515f6e167d4b4b1ef73efa38bf35d3253afb5a Mon Sep 17 00:00:00 2001 From: PietropaoloFrisoni Date: Tue, 14 May 2024 14:55:06 -0400 Subject: [PATCH 12/13] Replacing map_batch_transf --- tests/param_shift_dev.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/param_shift_dev.py b/tests/param_shift_dev.py index 9668880f219..12fd11eea16 100644 --- a/tests/param_shift_dev.py +++ b/tests/param_shift_dev.py @@ -52,7 +52,7 @@ def compute_derivatives(self, circuits, execution_config=None): self.tracker.update(derivative_batches=1, derivatives=len(circuits)) self.tracker.record() - diff_batch, fn = qml.transforms(qml.gradients.param_shift, circuits) + diff_batch, fn = qml.gradients.param_shift(circuits) diff_results = self.execute(diff_batch) jacs = fn(diff_results) @@ -73,7 +73,7 @@ def execute_and_compute_derivatives(self, circuits, execution_config=None): ) self.tracker.record() - diff_batch, fn = qml.transforms(qml.gradients.param_shift, circuits) + diff_batch, fn = qml.gradients.param_shift(circuits) combined_batch = tuple(circuits) + tuple(diff_batch) all_results = self.execute(combined_batch) results = all_results[: len(circuits)] From 810f7d91920d7155b91b9379b8f2a36de03716eb Mon Sep 17 00:00:00 2001 From: PietropaoloFrisoni Date: Wed, 15 May 2024 14:07:59 -0400 Subject: [PATCH 13/13] Suggestions from code review --- pennylane/transforms/core/transform.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pennylane/transforms/core/transform.py b/pennylane/transforms/core/transform.py index a3b2d3d29b1..fc4c3b72198 100644 --- a/pennylane/transforms/core/transform.py +++ b/pennylane/transforms/core/transform.py @@ -142,9 +142,9 @@ def qnode_circuit(a): import pennylane as qml H = qml.PauliY(2) @ qml.PauliZ(1) + 0.5 * qml.PauliZ(2) + qml.PauliZ(1) - measur = [qml.expval(H)] - ops = [qml.Hadamard(0), qml.RX(0.2, 0), qml.RX(0.6, 0), qml.CNOT((0, 1))] - tape = qml.tape.QuantumTape(ops, measur) + measurement = [qml.expval(H)] + operations = [qml.Hadamard(0), qml.RX(0.2, 0), qml.RX(0.6, 0), qml.CNOT((0, 1))] + tape = qml.tape.QuantumTape(operations, measurement) batch1, function1 = qml.transforms.hamiltonian_expand(tape) batch2, function2 = qml.transforms.merge_rotations(batch1)