Measurements

PennyLane can extract different types of measurement results from quantum devices: the expectation of an observable, its variance, or samples of a single measurement.

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

expval Expectation value of the supplied observable.
sample Sample from the supplied observable, with the number of shots determined from the dev.shots attribute of the corresponding device.
var Variance of the supplied observable.

Note

Both expval() and var() support analytic differentiation. sample(), however, returns stochastic results, and does not support differentiation.

Multiple measurements

Quantum functions can also return multiple measurements, as long as each wire is not measured more than once:

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)), 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)]

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, including expval(), var(), and sample().

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. This can be done by modifying the value of Device.shots:

dev = qml.device("default.qubit", wires=1, shots=10, analytic=False)

@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
dev.shots = 1
result = circuit(0.54, 0.1)