qml.tape.state

state()[source]

Quantum state in the computational basis.

Note

The quantum state can only be returned in tape mode:

>>> qml.enable_tape()

For more details on tape mode, see pennylane.tape.

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.

Example:

qml.enable_tape()

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

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