qml.probs

probs(wires=None, op=None)[source]

Probability of each computational basis state.

This measurement function accepts either a wire specification or an observable. Passing wires to the function instructs the QNode to return a flat array containing the probabilities \(|\langle i | \psi \rangle |^2\) of measuring the computational basis state \(| i \rangle\) given the current state \(| \psi \rangle\).

Marginal probabilities may also be requested by restricting the wires to a subset of the full system; the size of the returned array will be [2**len(wires)].

Note

If no wires or observable are given, the probability of all wires is returned.

Parameters
  • wires (Sequence[int] or int) – the wire the operation acts on

  • op (Observable or MeasurementValue) – Observable (with a diagonalizing_gates attribute) that rotates the computational basis, or a MeasurementValue corresponding to mid-circuit measurements.

Returns

Measurement process instance

Return type

ProbabilityMP

Example:

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

@qml.qnode(dev)
def circuit():
    qml.Hadamard(wires=1)
    return qml.probs(wires=[0, 1])

Executing this QNode:

>>> circuit()
array([0.5, 0.5, 0. , 0. ])

The returned array is in lexicographic order, so corresponds to a \(50\%\) chance of measuring either \(|00\rangle\) or \(|01\rangle\).

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

H = 1 / np.sqrt(2) * np.array([[1, 1], [1, -1]])

@qml.qnode(dev)
def circuit():
    qml.Z(0)
    qml.X(1)
    return qml.probs(op=qml.Hermitian(H, wires=0))
>>> circuit()
array([0.14644661 0.85355339])

The returned array is in lexicographic order, so corresponds to a \(14.6\%\) chance of measuring the rotated \(|0\rangle\) state and \(85.4\%\) of measuring the rotated \(|1\rangle\) state.

Note that the output shape of this measurement process depends on whether the device simulates qubit or continuous variable quantum systems.

Contents

Using PennyLane

Development

API

Internals