Measurements¶
PennyLane can extract different types of measurement results from quantum devices: the expectation of an observable, its variance, samples of a single measurement, or computational basis state probabilities.
For example, the following circuit returns the expectation value of the
PauliZ
observable on wire 1:
def my_quantum_function(x, y):
qml.RZ(x, wires=0)
qml.CNOT(wires=[0, 1])
qml.RY(y, wires=1)
return qml.expval(qml.PauliZ(1))
The available measurement functions are
Expectation value of the supplied observable. Sample from the supplied observable, with the number of shots determined from the Sample from the supplied observable, with the number of shots determined from the Variance of the supplied observable. Probability of each computational basis state. Quantum state in the computational basis. Quantum density matrix in the computational basis. Von Neumann entropy of the system prior to measurement. Mutual information between the subsystems prior to measurement: The purity of the system prior to measurement. The classical shadow measurement protocol. Compute expectation values using classical shadows in a differentiable manner.
expval
(op)sample
([op, wires])dev.shots
attribute of the corresponding device, returning raw samples.counts
([op, wires, all_outcomes])dev.shots
attribute of the corresponding device, returning the number of counts for each sample.var
(op)probs
([wires, op])state
()density_matrix
(wires)vn_entropy
(wires[, log_base])mutual_info
(wires0, wires1[, log_base])purity
(wires)classical_shadow
(wires[, seed])shadow_expval
(H[, k, seed])
Note
All measurement functions support analytic differentiation, with the
exception of sample()
, counts()
, and
classical_shadow()
, as they return stochastic
results.
Combined measurements¶
Quantum functions can also return combined measurements of multiple observables. If the observables are not
qubit-wise-commuting, then multiple device executions may occur behind the scenes. Non-commuting oberservables
can not be simultaneously measured in conjunction with non-observable type measurements such as sample()
,
counts()
, probs()
, state()
, and density_matrix()
.
def my_quantum_function(x, y):
qml.RZ(x, wires=0)
qml.CNOT(wires=[0, 1])
qml.RY(y, wires=1)
return qml.expval(qml.PauliZ(0)), qml.var(qml.PauliX(0))
You can also use list comprehensions, and other common Python patterns:
def my_quantum_function(x, y):
qml.RZ(x, wires=0)
qml.CNOT(wires=[0, 1])
qml.RY(y, wires=1)
return [qml.expval(qml.PauliZ(i)) for i in range(2)]
As a full example of combined measurements, let us look at
a Bell state \((|00\rangle + |11\rangle)/\sqrt{2}\), prepared
by a Hadamard
and CNOT
gate.
import pennylane as qml
from pennylane import numpy as np
dev = qml.device("default.qubit", wires=2, shots=1000)
@qml.qnode(dev)
def circuit():
qml.Hadamard(wires=0)
qml.CNOT(wires=[0, 1])
return qml.sample(qml.PauliZ(0)), qml.sample(qml.PauliZ(1))
The combined PauliZ-measurement of the
first and second qubit returns a tuple of two arrays, each containing
the measurement results of the respective qubit. sample()
returns 1000 samples per observable as defined on the device.
>>> results = circuit()
>>> results[0].shape
(1000,)
>>> results[1].shape
(1000,)
Since the two qubits are maximally entangled, the measurement results always coincide, and the lists are therefore equal:
>>> np.all(result[0] == result[1])
True
Tensor observables¶
PennyLane supports measuring the tensor product of observables, by using
the @
notation. For example, to measure the expectation value of
\(Z\otimes I \otimes X\):
def my_quantum_function(x, y):
qml.RZ(x, wires=0)
qml.CNOT(wires=[0, 1])
qml.RY(y, wires=1)
qml.CNOT(wires=[0, 2])
return qml.expval(qml.PauliZ(0) @ qml.PauliX(2))
Note that we don’t need to declare the identity observable on wire 1; this is implicitly assumed.
The tensor observable notation can be used inside all measurement functions that
accept observables as arguments,
including expval()
, var()
,
and sample()
.
Counts¶
To avoid dealing with long arrays for the larger numbers of shots, one can use counts()
rather than
sample()
. This performs the same measurement as sampling, but returns a dictionary containing the
possible measurement outcomes and the number of occurrences for each, rather than a list of all outcomes.
The previous example will be modified as follows:
dev = qml.device("default.qubit", wires=2, shots=1000)
@qml.qnode(dev)
def circuit():
qml.Hadamard(wires=0)
qml.CNOT(wires=[0, 1])
return qml.counts(qml.PauliZ(0)), qml.counts(qml.PauliZ(1))
After executing the circuit, we can directly see how many times each measurement outcome occurred:
>>> circuit()
({-1: 496, 1: 504}, {-1: 496, 1: 504})
Similarly, if the observable is not provided, the count of the observed computational basis state is returned.
dev = qml.device("default.qubit", wires=2, shots=1000)
@qml.qnode(dev)
def circuit():
qml.Hadamard(wires=0)
qml.CNOT(wires=[0, 1])
return qml.counts()
And the result is:
>>> circuit()
{'00': 495, '11': 505}
By default, only observed outcomes are included in the dictionary. The kwarg all_outcomes=True
can
be used to display all possible outcomes, including those that were observed 0
times in sampling.
For example, we could run the previous circuit with all_outcomes=True
:
dev = qml.device("default.qubit", wires=2, shots=1000)
@qml.qnode(dev)
def circuit():
qml.Hadamard(wires=0)
qml.CNOT(wires=[0, 1])
return qml.counts(all_outcomes=True)
>>> result = circuit()
>>> print(result)
{'00': 518, '01': 0, '10': 0, '11': 482}
Note: For complicated Hamiltonians, this can add considerable overhead time (due to the cost of calculating eigenvalues to determine possible outcomes), and as the number of qubits increases, the length of the output dictionary showing possible computational basis states grows rapidly.
If counts are obtained along with a measurement function other than sample()
,
a tuple is returned to provide differentiability for the outputs of QNodes.
@qml.qnode(dev)
def circuit():
qml.Hadamard(wires=0)
qml.CNOT(wires=[0,1])
qml.X(1)
return qml.expval(qml.PauliZ(0)),qml.expval(qml.PauliZ(1)), qml.counts()
>>> circuit()
(-0.036, 0.036, {'01': 482, '10': 518})
Probability¶
You can also train QNodes on computational basis probabilities, by using
the probs()
measurement function. The function can
accept either specified wires
or an observable that rotates the
computational basis.
def my_quantum_function(x, y):
qml.RZ(x, wires=0)
qml.CNOT(wires=[0, 1])
qml.RY(y, wires=1)
qml.CNOT(wires=[0, 2])
return qml.probs(wires=[0, 1])
For example:
>>> dev = qml.device("default.qubit", wires=3)
>>> qnode = qml.QNode(my_quantum_function, dev)
>>> qnode(0.56, 0.1)
array([0.99750208, 0.00249792, 0. , 0. ])
The returned probability array uses lexicographical ordering, so corresponds to a \(99.75\%\) probability of measuring state \(|00\rangle\), and a \(0.25\%\) probability of measuring state \(|01\rangle\).
Changing the number of shots¶
For hardware devices where the number of shots determines the accuracy of the expectation value and variance, as well as the number of samples returned, it can sometimes be convenient to execute the same QNode with differing number of shots.
For simulators like default.qubit
, finite shots will be simulated if
we set shots
to a positive integer.
The shot number can be changed on the device itself, or temporarily altered
by the shots
keyword argument when executing the QNode:
dev = qml.device("default.qubit", wires=1, shots=10)
@qml.qnode(dev)
def circuit(x, y):
qml.RX(x, wires=0)
qml.RY(y, wires=0)
return qml.expval(qml.PauliZ(0))
# execute the QNode using 10 shots
result = circuit(0.54, 0.1)
# execute the QNode again, now using 1 shot
result = circuit(0.54, 0.1, shots=1)
With an increasing number of shots, the average over measurement samples converges to the exact expectation of an observable. Consider the following circuit:
# fix seed to make results reproducible
np.random.seed(1)
dev = qml.device("default.qubit", wires=1)
@qml.qnode(dev)
def circuit():
qml.Hadamard(wires=0)
return qml.expval(qml.PauliZ(0))
Running the simulator with shots=None
returns the exact expectation.
>>> circuit(shots=None)
0.0
Now we set the device to return stochastic results, and increase the number of shots starting from 10
.
>>> circuit(shots=10)
0.2
>>> circuit(shots=1000)
-0.062
>>> circuit(shots=100000)
0.00056
The result converges to the exact expectation.