Measurements

Module name: pennylane.measure

This module contains the functions for computing expectation values, variances, and measurement samples of quantum observables.

These are used to indicate to the quantum device how to measure and return the requested observables. For example, the following QNode returns the expectation value of observable PauliZ on wire 1, and the variance of observable PauliX on wire 2.

import pennylane as qml
from pennylane import expval, var

dev = qml.device('default.qubit', wires=2)

@qml.qnode(dev)
def circuit(x, y):
    qml.RX(x, wires=0)
    qml.CNOT(wires=[0,1])
    qml.RY(y, wires=1)
    return expval(qml.PauliZ(0)), var(qml.PauliX(1))

Note that all returned observables must be within a measurement function; they cannot be ‘bare’.

Summary

expval

Expectation value of the supplied observable.

var(op)

Variance of the supplied observable.

sample(op)

Sample from the supplied observable, with the number of shots determined from the dev.shots attribute of the corresponding device.

Code details

expval = <pennylane.measure.ExpvalFactory object>

Expectation value of the supplied observable.

Parameters

op (Observable) – a quantum observable object

var(op)[source]

Variance of the supplied observable.

Parameters

op (Observable) – a quantum observable object

sample(op)[source]

Sample from the supplied observable, with the number of shots determined from the dev.shots attribute of the corresponding device.

Parameters

op (Observable) – a quantum observable object