From 1e897a210d628c9071daf27171e3c51529170062 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Fri, 25 Feb 2022 19:16:45 -0500 Subject: [PATCH 1/3] pennylane/measure.py -> pennylane/measurements.py --- pennylane/__init__.py | 2 +- pennylane/_device.py | 2 +- pennylane/_qubit_device.py | 2 +- pennylane/circuit_graph.py | 10 +++++----- pennylane/drawer/representation_resolver.py | 4 ++-- pennylane/gradients/parameter_shift.py | 4 ++-- pennylane/gradients/parameter_shift_cv.py | 6 +++--- pennylane/{measure.py => measurements.py} | 0 pennylane/operation.py | 2 +- pennylane/qnode.py | 2 +- pennylane/qnode_old.py | 2 +- pennylane/tape/cv_param_shift.py | 2 +- pennylane/tape/qubit_param_shift.py | 2 +- pennylane/tape/tape.py | 8 ++++---- pennylane/transforms/hamiltonian_expand.py | 2 +- pennylane/transforms/qcut.py | 2 +- tests/circuit_graph/test_circuit_graph.py | 8 ++++---- tests/devices/test_caching.py | 2 +- tests/drawer/test_circuit_drawer.py | 2 +- tests/drawer/test_representation_resolver.py | 10 +++++----- tests/tape/test_tape.py | 2 +- tests/test_measure.py | 2 +- tests/test_qubit_device.py | 8 ++++---- tests/transforms/test_qcut.py | 4 ++-- 24 files changed, 45 insertions(+), 45 deletions(-) rename pennylane/{measure.py => measurements.py} (100%) diff --git a/pennylane/__init__.py b/pennylane/__init__.py index 994b56d30bf..8b726fb0709 100644 --- a/pennylane/__init__.py +++ b/pennylane/__init__.py @@ -41,7 +41,7 @@ from pennylane.configuration import Configuration from pennylane.tracker import Tracker from pennylane.io import * -from pennylane.measure import density_matrix, expval, probs, sample, state, var +from pennylane.measurements import density_matrix, expval, probs, sample, state, var from pennylane.ops import * from pennylane.templates import broadcast, layer from pennylane.templates.embeddings import * diff --git a/pennylane/_device.py b/pennylane/_device.py index 1081e995c86..fce6241d6fa 100644 --- a/pennylane/_device.py +++ b/pennylane/_device.py @@ -895,7 +895,7 @@ def check_validity(self, queue, observables): ) for o in observables: - if isinstance(o, qml.measure.MeasurementProcess) and o.obs is not None: + if isinstance(o, qml.measurements.MeasurementProcess) and o.obs is not None: o = o.obs if isinstance(o, Tensor): diff --git a/pennylane/_qubit_device.py b/pennylane/_qubit_device.py index f1547089146..450f56210d6 100644 --- a/pennylane/_qubit_device.py +++ b/pennylane/_qubit_device.py @@ -38,7 +38,7 @@ from pennylane.math import sum as qmlsum from pennylane.wires import Wires -from pennylane.measure import MeasurementProcess +from pennylane.measurements import MeasurementProcess class QubitDevice(Device): diff --git a/pennylane/circuit_graph.py b/pennylane/circuit_graph.py index 31e60997f11..dd7090b7af0 100644 --- a/pennylane/circuit_graph.py +++ b/pennylane/circuit_graph.py @@ -77,7 +77,7 @@ def _is_returned_observable(op): bool: whether or not the observable or measurement process is in the return statement """ - is_obs = isinstance(op, (qml.operation.Observable, qml.measure.MeasurementProcess)) + is_obs = isinstance(op, (qml.operation.Observable, qml.measurements.MeasurementProcess)) return is_obs and op.return_type is not None @@ -151,11 +151,11 @@ def __init__(self, ops, obs, wires, par_info=None, trainable_params=None): # State measurements contain no wires by default, but wires are # required for the circuit drawer, so we recreate the state # measurement with all wires - op = qml.measure.MeasurementProcess(qml.operation.State, wires=wires) + op = qml.measurements.MeasurementProcess(qml.operation.State, wires=wires) elif op.return_type is qml.operation.Sample and op.wires == Wires([]): # Sampling without specifying wires is treated as sampling all wires - op = qml.measure.MeasurementProcess(qml.operation.Sample, wires=wires) + op = qml.measurements.MeasurementProcess(qml.operation.Sample, wires=wires) op.queue_idx = k @@ -506,7 +506,7 @@ def greedy_layers(self, wire_order=None, show_all_wires=False): operations[wire] = list( filter( lambda op: not ( - isinstance(op, (qml.operation.Observable, qml.measure.MeasurementProcess)) + isinstance(op, (qml.operation.Observable, qml.measurements.MeasurementProcess)) and op.return_type is not None ), operations[wire], @@ -540,7 +540,7 @@ def greedy_layers(self, wire_order=None, show_all_wires=False): observables[wire] = list( filter( lambda op: isinstance( - op, (qml.operation.Observable, qml.measure.MeasurementProcess) + op, (qml.operation.Observable, qml.measurements.MeasurementProcess) ) and op.return_type is not None, self._grid[wire], diff --git a/pennylane/drawer/representation_resolver.py b/pennylane/drawer/representation_resolver.py index 949482a5c4a..ccf9a3d2234 100644 --- a/pennylane/drawer/representation_resolver.py +++ b/pennylane/drawer/representation_resolver.py @@ -328,7 +328,7 @@ def operator_representation(self, op, wire): Returns: str: String representation of the Operator """ - if isinstance(op, qml.measure.MeasurementProcess): + if isinstance(op, qml.measurements.MeasurementProcess): if op.obs is not None: op = op.obs else: @@ -511,7 +511,7 @@ def element_representation(self, element, wire): if isinstance(element, str): return element if ( - isinstance(element, (qml.operation.Observable, qml.measure.MeasurementProcess)) + isinstance(element, (qml.operation.Observable, qml.measurements.MeasurementProcess)) and element.return_type is not None ): return self.output_representation(element, wire) diff --git a/pennylane/gradients/parameter_shift.py b/pennylane/gradients/parameter_shift.py index 120bd956c81..4cd1401dcd5 100644 --- a/pennylane/gradients/parameter_shift.py +++ b/pennylane/gradients/parameter_shift.py @@ -317,7 +317,7 @@ def var_param_shift(tape, argnum, shifts=None, gradient_recipes=None, f0=None): # Convert all variance measurements on the tape into expectation values for i in var_idx: obs = expval_tape._measurements[i].obs - expval_tape._measurements[i] = qml.measure.MeasurementProcess( + expval_tape._measurements[i] = qml.measurements.MeasurementProcess( qml.operation.Expectation, obs=obs ) @@ -357,7 +357,7 @@ def var_param_shift(tape, argnum, shifts=None, gradient_recipes=None, f0=None): # We need to calculate d/dp; to do so, we replace the # involutory observables A in the queue with A^2. obs = _square_observable(expval_sq_tape._measurements[i].obs) - expval_sq_tape._measurements[i] = qml.measure.MeasurementProcess( + expval_sq_tape._measurements[i] = qml.measurements.MeasurementProcess( qml.operation.Expectation, obs=obs ) diff --git a/pennylane/gradients/parameter_shift_cv.py b/pennylane/gradients/parameter_shift_cv.py index 66ef1ad71f7..8b48081d1d0 100644 --- a/pennylane/gradients/parameter_shift_cv.py +++ b/pennylane/gradients/parameter_shift_cv.py @@ -215,7 +215,7 @@ def var_param_shift(tape, dev_wires, argnum=None, shifts=None, gradient_recipes= # Convert all variance measurements on the tape into expectation values for i in var_idx: obs = expval_tape._measurements[i].obs - expval_tape._measurements[i] = qml.measure.MeasurementProcess( + expval_tape._measurements[i] = qml.measurements.MeasurementProcess( qml.operation.Expectation, obs=obs ) @@ -245,7 +245,7 @@ def var_param_shift(tape, dev_wires, argnum=None, shifts=None, gradient_recipes= # with itself, to get a square symmetric matrix representing # the square of the observable obs = qml.PolyXP(np.outer(A, A), wires=obs.wires) - expval_sq_tape._measurements[i] = qml.measure.MeasurementProcess( + expval_sq_tape._measurements[i] = qml.measurements.MeasurementProcess( qml.operation.Expectation, obs=obs ) @@ -402,7 +402,7 @@ def second_order_param_shift(tape, dev_wires, argnum=None, shifts=None, gradient constants.append(constant) - g_tape._measurements[idx] = qml.measure.MeasurementProcess( + g_tape._measurements[idx] = qml.measurements.MeasurementProcess( qml.operation.Expectation, _transform_observable(obs, Z, dev_wires) ) diff --git a/pennylane/measure.py b/pennylane/measurements.py similarity index 100% rename from pennylane/measure.py rename to pennylane/measurements.py diff --git a/pennylane/operation.py b/pennylane/operation.py index 800b9471a91..f74319d8dd0 100644 --- a/pennylane/operation.py +++ b/pennylane/operation.py @@ -2399,7 +2399,7 @@ def has_unitary_gen(obj): @qml.BooleanFn def is_measurement(obj): """Returns ``True`` if an operator is a ``MeasurementProcess`` instance.""" - return isinstance(obj, qml.measure.MeasurementProcess) + return isinstance(obj, qml.measurements.MeasurementProcess) @qml.BooleanFn diff --git a/pennylane/qnode.py b/pennylane/qnode.py index 0a0a7da2720..533764d3f6c 100644 --- a/pennylane/qnode.py +++ b/pennylane/qnode.py @@ -495,7 +495,7 @@ def construct(self, args, kwargs): else: measurement_processes = self._qfunc_output - if not all(isinstance(m, qml.measure.MeasurementProcess) for m in measurement_processes): + if not all(isinstance(m, qml.measurements.MeasurementProcess) for m in measurement_processes): raise qml.QuantumFunctionError( "A quantum function must return either a single measurement, " "or a nonempty sequence of measurements." diff --git a/pennylane/qnode_old.py b/pennylane/qnode_old.py index 3ebc201f78c..276827930d3 100644 --- a/pennylane/qnode_old.py +++ b/pennylane/qnode_old.py @@ -586,7 +586,7 @@ def construct(self, args, kwargs): else: measurement_processes = self.qfunc_output - if not all(isinstance(m, qml.measure.MeasurementProcess) for m in measurement_processes): + if not all(isinstance(m, qml.measurements.MeasurementProcess) for m in measurement_processes): raise qml.QuantumFunctionError( "A quantum function must return either a single measurement, " "or a nonempty sequence of measurements." diff --git a/pennylane/tape/cv_param_shift.py b/pennylane/tape/cv_param_shift.py index 8ab80a0c4a4..82949a0cf56 100644 --- a/pennylane/tape/cv_param_shift.py +++ b/pennylane/tape/cv_param_shift.py @@ -24,7 +24,7 @@ import numpy as np import pennylane as qml -from pennylane.measure import MeasurementProcess +from pennylane.measurements import MeasurementProcess from pennylane.tape import QuantumTape from .qubit_param_shift import QubitParamShiftTape diff --git a/pennylane/tape/qubit_param_shift.py b/pennylane/tape/qubit_param_shift.py index 25b2762921a..f5cb9452bdc 100644 --- a/pennylane/tape/qubit_param_shift.py +++ b/pennylane/tape/qubit_param_shift.py @@ -22,7 +22,7 @@ import numpy as np import pennylane as qml -from pennylane.measure import MeasurementProcess +from pennylane.measurements import MeasurementProcess from pennylane.tape import QuantumTape from pennylane.math import toarray diff --git a/pennylane/tape/tape.py b/pennylane/tape/tape.py index 1fe3607ce3e..5ba28aa92e8 100644 --- a/pennylane/tape/tape.py +++ b/pennylane/tape/tape.py @@ -172,7 +172,7 @@ def expand_tape(tape, depth=1, stop_at=None, expand_measurements=False): tape._ops.extend(rotations) for o, i in zip(diag_obs, tape._obs_sharing_wires_id): - new_m = qml.measure.MeasurementProcess(tape.measurements[i].return_type, obs=o) + new_m = qml.measurements.MeasurementProcess(tape.measurements[i].return_type, obs=o) tape._measurements[i] = new_m for queue in ("_prep", "_ops", "_measurements"): @@ -183,7 +183,7 @@ def expand_tape(tape, depth=1, stop_at=None, expand_measurements=False): if not expand_measurements: # Measurements should not be expanded; treat measurements # as a stopping condition - stop = stop or isinstance(obj, qml.measure.MeasurementProcess) + stop = stop or isinstance(obj, qml.measurements.MeasurementProcess) if stop: # do not expand out the object; append it to the @@ -191,7 +191,7 @@ def expand_tape(tape, depth=1, stop_at=None, expand_measurements=False): getattr(new_tape, queue).append(obj) continue - if isinstance(obj, (qml.operation.Operation, qml.measure.MeasurementProcess)): + if isinstance(obj, (qml.operation.Operation, qml.measurements.MeasurementProcess)): # Object is an operation; query it for its expansion try: obj = obj.expand() @@ -434,7 +434,7 @@ def _process_queue(self): else: self._ops.append(obj) - elif isinstance(obj, qml.measure.MeasurementProcess): + elif isinstance(obj, qml.measurements.MeasurementProcess): # measurement process self._measurements.append(obj) diff --git a/pennylane/transforms/hamiltonian_expand.py b/pennylane/transforms/hamiltonian_expand.py index 127fc978971..548f46f2fe3 100644 --- a/pennylane/transforms/hamiltonian_expand.py +++ b/pennylane/transforms/hamiltonian_expand.py @@ -123,7 +123,7 @@ def hamiltonian_expand(tape, group=True): if ( not isinstance(hamiltonian, qml.Hamiltonian) or len(tape.measurements) > 1 - or tape.measurements[0].return_type != qml.measure.Expectation + or tape.measurements[0].return_type != qml.measurements.Expectation ): raise ValueError( "Passed tape must end in `qml.expval(H)`, where H is of type `qml.Hamiltonian`" diff --git a/pennylane/transforms/qcut.py b/pennylane/transforms/qcut.py index d800ca8e1f4..90312621e24 100644 --- a/pennylane/transforms/qcut.py +++ b/pennylane/transforms/qcut.py @@ -30,7 +30,7 @@ import pennylane as qml from pennylane import apply, expval from pennylane.grouping import string_to_pauli_word -from pennylane.measure import MeasurementProcess +from pennylane.measurements import MeasurementProcess from pennylane.operation import Expectation, Operation, Operator, Tensor from pennylane.ops.qubit.non_parametric_ops import WireCut from pennylane.tape import QuantumTape diff --git a/tests/circuit_graph/test_circuit_graph.py b/tests/circuit_graph/test_circuit_graph.py index 6b7803fae1a..483b8031e69 100644 --- a/tests/circuit_graph/test_circuit_graph.py +++ b/tests/circuit_graph/test_circuit_graph.py @@ -119,7 +119,7 @@ def lst_mp_equality(lst1, lst2): if not isinstance(lst1[index], type(lst2[index])): # if they are different types return False # then return false - elif isinstance(lst1[index], qml.measure.MeasurementProcess): + elif isinstance(lst1[index], qml.measurements.MeasurementProcess): lsts_same = lsts_same and measurement_process_equality(lst1[index], lst2[index]) else: @@ -308,13 +308,13 @@ def test_grid_when_sample_no_wires(self): none are explicitly provided.""" ops = [qml.Hadamard(wires=0), qml.CNOT(wires=[0, 1])] - obs_no_wires = [qml.measure.sample(op=None, wires=None)] - obs_w_wires = [qml.measure.sample(op=None, wires=[0, 1, 2])] + obs_no_wires = [qml.measurements.sample(op=None, wires=None)] + obs_w_wires = [qml.measurements.sample(op=None, wires=[0, 1, 2])] circuit_no_wires = CircuitGraph(ops, obs_no_wires, wires=Wires([0, 1, 2])) circuit_w_wires = CircuitGraph(ops, obs_w_wires, wires=Wires([0, 1, 2])) - sample_w_wires_op = qml.measure.sample(op=None, wires=[0, 1, 2]) + sample_w_wires_op = qml.measurements.sample(op=None, wires=[0, 1, 2]) expected_grid = { 0: [ops[0], ops[1], sample_w_wires_op], 1: [ops[1], sample_w_wires_op], diff --git a/tests/devices/test_caching.py b/tests/devices/test_caching.py index 7ce56954f82..60b554b4ab0 100644 --- a/tests/devices/test_caching.py +++ b/tests/devices/test_caching.py @@ -15,7 +15,7 @@ import pytest import pennylane as qml -from pennylane.measure import expval +from pennylane.measurements import expval from pennylane import qnode, QNode from pennylane.devices import DefaultQubit from pennylane import numpy as np diff --git a/tests/drawer/test_circuit_drawer.py b/tests/drawer/test_circuit_drawer.py index 8e2d3bb925e..d9976c0166e 100644 --- a/tests/drawer/test_circuit_drawer.py +++ b/tests/drawer/test_circuit_drawer.py @@ -25,7 +25,7 @@ from pennylane.drawer.charsets import CHARSETS, UnicodeCharSet, AsciiCharSet from pennylane.wires import Wires -from pennylane.measure import state +from pennylane.measurements import state class TestFunctions: diff --git a/tests/drawer/test_representation_resolver.py b/tests/drawer/test_representation_resolver.py index bb5ba76d6c5..f53aaa6d12d 100644 --- a/tests/drawer/test_representation_resolver.py +++ b/tests/drawer/test_representation_resolver.py @@ -20,7 +20,7 @@ import pennylane as qml from pennylane.drawer.representation_resolver import RepresentationResolver -from pennylane.measure import state +from pennylane.measurements import state @pytest.fixture @@ -211,8 +211,8 @@ def test_single_parameter_representation(self, unicode_representation_resolver, (qml.Toffoli(wires=[0, 2, 1]).inv(), 1, "X⁻¹"), (qml.Toffoli(wires=[0, 2, 1]).inv(), 0, "C"), (qml.Toffoli(wires=[0, 2, 1]).inv(), 2, "C"), - (qml.measure.sample(wires=[0, 1]), 0, "basis"), # not providing an observable in - (qml.measure.sample(wires=[0, 1]), 1, "basis"), # sample gets displayed as raw + (qml.measurements.sample(wires=[0, 1]), 0, "basis"), # not providing an observable in + (qml.measurements.sample(wires=[0, 1]), 1, "basis"), # sample gets displayed as raw (two_wire_quantum_tape(), 0, "QuantumTape:T0"), (two_wire_quantum_tape(), 1, "QuantumTape:T0"), ], @@ -355,8 +355,8 @@ def test_operator_representation_unicode( (qml.Toffoli(wires=[0, 2, 1]).inv(), 1, "X^-1"), (qml.Toffoli(wires=[0, 2, 1]).inv(), 0, "C"), (qml.Toffoli(wires=[0, 2, 1]).inv(), 2, "C"), - (qml.measure.sample(wires=[0, 1]), 0, "basis"), # not providing an observable in - (qml.measure.sample(wires=[0, 1]), 1, "basis"), # sample gets displayed as raw + (qml.measurements.sample(wires=[0, 1]), 0, "basis"), # not providing an observable in + (qml.measurements.sample(wires=[0, 1]), 1, "basis"), # sample gets displayed as raw (two_wire_quantum_tape(), 0, "QuantumTape:T0"), (two_wire_quantum_tape(), 1, "QuantumTape:T0"), ], diff --git a/tests/tape/test_tape.py b/tests/tape/test_tape.py index 902b5b1e9f8..35a2a538bcb 100644 --- a/tests/tape/test_tape.py +++ b/tests/tape/test_tape.py @@ -22,7 +22,7 @@ import pennylane as qml from pennylane import CircuitGraph from pennylane.tape import QuantumTape -from pennylane.measure import MeasurementProcess, expval, sample, var +from pennylane.measurements import MeasurementProcess, expval, sample, var def TestOperationMonkeypatching(): diff --git a/tests/test_measure.py b/tests/test_measure.py index d67bcc157f9..8eccb668f96 100644 --- a/tests/test_measure.py +++ b/tests/test_measure.py @@ -22,7 +22,7 @@ from pennylane.operation import DecompositionUndefinedError from pennylane.queuing import AnnotatedQueue -from pennylane.measure import ( +from pennylane.measurements import ( expval, var, sample, diff --git a/tests/test_qubit_device.py b/tests/test_qubit_device.py index 5c4738a1dbb..97b6f48f56b 100644 --- a/tests/test_qubit_device.py +++ b/tests/test_qubit_device.py @@ -25,7 +25,7 @@ from pennylane.circuit_graph import CircuitGraph from pennylane.wires import Wires from pennylane.tape import QuantumTape -from pennylane.measure import state +from pennylane.measurements import state mock_qubit_device_paulis = ["PauliX", "PauliY", "PauliZ"] mock_qubit_device_rotations = ["RX", "RY", "RZ"] @@ -268,7 +268,7 @@ def test_unsupported_observable_return_type_raise_error( with qml.tape.QuantumTape() as tape: qml.PauliX(wires=0) - qml.measure.MeasurementProcess( + qml.measurements.MeasurementProcess( return_type="SomeUnsupportedReturnType", obs=qml.PauliZ(0) ) @@ -615,7 +615,7 @@ def test_sample_with_no_observable_and_no_wires( ): """Test that when we sample a device without providing an observable or wires then it will return the raw samples""" - obs = qml.measure.sample(op=None, wires=None) + obs = qml.measurements.sample(op=None, wires=None) dev = mock_qubit_device_with_original_statistics(wires=2) generated_samples = np.array([[1, 0], [1, 1]]) dev._samples = generated_samples @@ -628,7 +628,7 @@ def test_sample_with_no_observable_and_with_wires( ): """Test that when we sample a device without providing an observable but we specify wires then it returns the generated samples for only those wires""" - obs = qml.measure.sample(op=None, wires=[1]) + obs = qml.measurements.sample(op=None, wires=[1]) dev = mock_qubit_device_with_original_statistics(wires=2) generated_samples = np.array([[1, 0], [1, 1]]) dev._samples = generated_samples diff --git a/tests/transforms/test_qcut.py b/tests/transforms/test_qcut.py index 172789d492b..4fe5f2a4934 100644 --- a/tests/transforms/test_qcut.py +++ b/tests/transforms/test_qcut.py @@ -1018,7 +1018,7 @@ def test_no_measurements(self): out = qcut._get_measurements(group, meas) assert len(out) == len(group) - assert out[0].return_type is qml.measure.Expectation + assert out[0].return_type is qml.measurements.Expectation assert out[0].obs.name == "Identity" assert out[0].obs.wires[0] == 0 @@ -1030,7 +1030,7 @@ def test_single_measurement(self): out = qcut._get_measurements(group, meas) assert len(out) == 2 - assert [o.return_type for o in out] == [qml.measure.Expectation, qml.measure.Expectation] + assert [o.return_type for o in out] == [qml.measurements.Expectation, qml.measurements.Expectation] obs = [o.obs for o in out] From 61060f01224f0942ea0b826aa6e898bb9f90aee1 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Fri, 25 Feb 2022 19:21:14 -0500 Subject: [PATCH 2/3] changelog --- doc/releases/changelog-dev.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index d19296bc3f4..fff82ed6fea 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -4,6 +4,9 @@

New features since last release

+* Added the user-interface for mid-circuit measurements. + [(#2236)](https://github.com/PennyLaneAI/pennylane/pull/2236) + * The text based drawer accessed via `qml.draw` has been overhauled. The new drawer has a `decimals` keyword for controlling parameter rounding, a different algorithm for determining positions, deprecation of the `charset` keyword, and minor cosmetic changes. From b6bb7ef75633cc9846b8ccb94778c2d00e003d76 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Fri, 25 Feb 2022 19:52:34 -0500 Subject: [PATCH 3/3] format --- pennylane/circuit_graph.py | 4 +++- pennylane/qnode.py | 4 +++- pennylane/qnode_old.py | 4 +++- tests/transforms/test_qcut.py | 5 ++++- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/pennylane/circuit_graph.py b/pennylane/circuit_graph.py index dd7090b7af0..91ed4a58860 100644 --- a/pennylane/circuit_graph.py +++ b/pennylane/circuit_graph.py @@ -506,7 +506,9 @@ def greedy_layers(self, wire_order=None, show_all_wires=False): operations[wire] = list( filter( lambda op: not ( - isinstance(op, (qml.operation.Observable, qml.measurements.MeasurementProcess)) + isinstance( + op, (qml.operation.Observable, qml.measurements.MeasurementProcess) + ) and op.return_type is not None ), operations[wire], diff --git a/pennylane/qnode.py b/pennylane/qnode.py index 533764d3f6c..4cfb0201d55 100644 --- a/pennylane/qnode.py +++ b/pennylane/qnode.py @@ -495,7 +495,9 @@ def construct(self, args, kwargs): else: measurement_processes = self._qfunc_output - if not all(isinstance(m, qml.measurements.MeasurementProcess) for m in measurement_processes): + if not all( + isinstance(m, qml.measurements.MeasurementProcess) for m in measurement_processes + ): raise qml.QuantumFunctionError( "A quantum function must return either a single measurement, " "or a nonempty sequence of measurements." diff --git a/pennylane/qnode_old.py b/pennylane/qnode_old.py index 276827930d3..45196ac0a19 100644 --- a/pennylane/qnode_old.py +++ b/pennylane/qnode_old.py @@ -586,7 +586,9 @@ def construct(self, args, kwargs): else: measurement_processes = self.qfunc_output - if not all(isinstance(m, qml.measurements.MeasurementProcess) for m in measurement_processes): + if not all( + isinstance(m, qml.measurements.MeasurementProcess) for m in measurement_processes + ): raise qml.QuantumFunctionError( "A quantum function must return either a single measurement, " "or a nonempty sequence of measurements." diff --git a/tests/transforms/test_qcut.py b/tests/transforms/test_qcut.py index 4fe5f2a4934..3d5dad73bc5 100644 --- a/tests/transforms/test_qcut.py +++ b/tests/transforms/test_qcut.py @@ -1030,7 +1030,10 @@ def test_single_measurement(self): out = qcut._get_measurements(group, meas) assert len(out) == 2 - assert [o.return_type for o in out] == [qml.measurements.Expectation, qml.measurements.Expectation] + assert [o.return_type for o in out] == [ + qml.measurements.Expectation, + qml.measurements.Expectation, + ] obs = [o.obs for o in out]