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

Rename the pennylane/measure.py file to pennylane/measurements.py #2236

Merged
merged 7 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

<h3>New features since last release</h3>

* 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.

* Transform a circuit from quantum tape, quantum function or quantum node to a pairwise
commutation DAG (directed acyclic graph). The node represents the quantum operations, and the edges represent
non commutation between two operations.
Expand Down Expand Up @@ -51,6 +58,7 @@
>>> second_node_predecessors = second_node.predecessors
[]
```

* The text based drawer accessed via `qml.draw` has been overhauled.
[(#2128)](https://github.com/PennyLaneAI/pennylane/pull/2128)
[(#2198)](https://github.com/PennyLaneAI/pennylane/pull/2198)
Expand Down
2 changes: 1 addition & 1 deletion pennylane/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
2 changes: 1 addition & 1 deletion pennylane/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion pennylane/_qubit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 7 additions & 5 deletions pennylane/circuit_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.measure.MeasurementProcess))
isinstance(
op, (qml.operation.Observable, qml.measurements.MeasurementProcess)
)
and op.return_type is not None
),
operations[wire],
Expand Down Expand Up @@ -540,7 +542,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],
Expand Down
4 changes: 2 additions & 2 deletions pennylane/drawer/representation_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pennylane/gradients/parameter_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -357,7 +357,7 @@ def var_param_shift(tape, argnum, shifts=None, gradient_recipes=None, f0=None):
# We need to calculate d<A^2>/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
)

Expand Down
6 changes: 3 additions & 3 deletions pennylane/gradients/parameter_shift_cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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
)

Expand Down Expand Up @@ -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)
)

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion pennylane/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2443,7 +2443,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
Expand Down
4 changes: 3 additions & 1 deletion pennylane/qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,9 @@ 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."
Expand Down
4 changes: 3 additions & 1 deletion pennylane/qnode_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,9 @@ 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."
Expand Down
2 changes: 1 addition & 1 deletion pennylane/tape/cv_param_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pennylane/tape/qubit_param_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions pennylane/tape/tape.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand All @@ -183,15 +183,15 @@ 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
# new tape, and continue to the next object in the queue
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()
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion pennylane/transforms/hamiltonian_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`"
Expand Down
2 changes: 1 addition & 1 deletion pennylane/transforms/qcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/circuit_graph/test_circuit_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion tests/devices/test_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/drawer/test_circuit_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions tests/drawer/test_representation_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
],
Expand Down Expand Up @@ -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"),
],
Expand Down
2 changes: 1 addition & 1 deletion tests/tape/test_tape.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions tests/test_qubit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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)
)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading