qml.state

state()[source]

Quantum state in the computational basis.

This function accepts no observables and instead instructs the QNode to return its state. A wires argument should not be provided since state() always returns a pure state describing all wires in the device.

Note that the output shape of this measurement process depends on the number of wires defined for the device.

Returns

Measurement process instance

Return type

StateMP

Example:

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

@qml.qnode(dev)
def circuit():
    qml.Hadamard(wires=1)
    return qml.state()

Executing this QNode:

>>> circuit()
array([0.70710678+0.j, 0.70710678+0.j, 0.        +0.j, 0.        +0.j])

The returned array is in lexicographic order. Hence, we have a \(1/\sqrt{2}\) amplitude in both \(|00\rangle\) and \(|01\rangle\).

Note

Differentiating state() is currently only supported when using the classical backpropagation differentiation method (diff_method="backprop") with a compatible device.

A QNode with the qml.state output can be used in a cost function which is then differentiated:

>>> dev = qml.device('default.qubit', wires=2)
>>> @qml.qnode(dev, diff_method="backprop")
... def test(x):
...     qml.RY(x, wires=[0])
...     return qml.state()
>>> def cost(x):
...     return np.abs(test(x)[0])
>>> cost(x)
0.9987502603949663
>>> qml.grad(cost)(x)
tensor(-0.02498958, requires_grad=True)

Contents

Using PennyLane

Development

API

Internals