qml.measure

measure(wires)[source]

Perform a mid-circuit measurement in the computational basis on the supplied qubit.

Measurement outcomes can be obtained and used to conditionally apply operations.

If a device doesn’t support mid-circuit measurements natively, then the QNode will apply the defer_measurements() transform.

Example:

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

@qml.qnode(dev)
def func(x, y):
    qml.RY(x, wires=0)
    qml.CNOT(wires=[0, 1])
    m_0 = qml.measure(1)

    qml.cond(m_0, qml.RY)(y, wires=0)
    return qml.probs(wires=[0])

Executing this QNode:

>>> pars = np.array([0.643, 0.246], requires_grad=True)
>>> func(*pars)
tensor([0.90165331, 0.09834669], requires_grad=True)

Mid circuit measurements can be manipulated using the following dunder methods +, -, *, /, ~ (not), & (and), | (or), ==, <=, >=, <, >. With other mid-circuit measurements or scalars.

Note

python not, and, or, do not work since these do not have dunder methods. Instead use ~, &, |.

Parameters

wires (Wires) – The wire of the qubit the measurement process applies to.

Returns

measurement process instance

Return type

MidMeasureMP

Raises

QuantumFunctionError – if multiple wires were specified

Contents

Using PennyLane

Development

API

Internals