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 (Operator or MeasurementValue or Sequence[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\).

Warning

qml.probs is not compatible with Hermitian. When using qml.probs with a Hermitian observable, the output might be different than expected as the lexicographical ordering of eigenvalues is not guaranteed and the diagonalizing gates may exist in a degenerate subspace.

Example:

The order of the output might be different when using qml.Hermitian, as in the following example:

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

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

Example:

The output might also be different than expected when using qml.Hermitian, because the probability vector can be expressed in the eigenbasis obtained from diagonalizing the matrix of the observable, as in the following example:

ob = qml.X(0) @ qml.Y(1)
h = qml.Hermitian(ob.matrix(), wires=[0, 1])

@qml.qnode(dev)
def circuit():
    return qml.probs(op=h), qml.probs(op=ob)
>>> circuit()
(array([0.5, 0. , 0. , 0.5]), array([0.25, 0.25, 0.25, 0.25]))

Both outputs are in the eigenbasis of the observable, but at different locations in a degenerate subspace. Both correspond to half in the -1 eigenvalue and half in the +1 eigenvalue.

Contents

Using PennyLane

Release news

Development

API

Internals