fidelity(state0, state1, check_state=False, c_dtype='complex128')[source]

Compute the fidelity for two states (a state can be a state vector or a density matrix) acting on quantum systems with the same size.

The fidelity for two mixed states given by density matrices \(\rho\) and \(\sigma\) is defined as

\[F( \rho , \sigma ) = \text{Tr}( \sqrt{\sqrt{\rho} \sigma \sqrt{\rho}})^2\]

If one of the states is pure, say \(\rho=\ket{\psi}\bra{\psi}\), then the expression for fidelity simplifies to

\[F( \ket{\psi} , \sigma ) = \bra{\psi} \sigma \ket{\psi}\]

Finally, if both states are pure, \(\sigma=\ket{\phi}\bra{\phi}\), then the fidelity is simply

\[F( \ket{\psi} , \ket{\phi}) = \left|\braket{\psi, \phi}\right|^2\]


It supports all interfaces (Numpy, Autograd, Torch, Tensorflow and Jax). The second state is coerced to the type and dtype of the first state. The fidelity is returned in the type of the interface of the first state.

  • state0 (tensor_like) – 1D state vector or 2D density matrix

  • state1 (tensor_like) – 1D state vector or 2D density matrix

  • check_state (bool) – If True, the function will check the validity of both states; it checks (shape, norm) for state vectors or (shape, trace, positive-definitiveness) for density matrices.

  • c_dtype (str) – Complex floating point precision type.


Fidelity between the two quantum states.

Return type



Two state vectors can be used as arguments and the fidelity (overlap) is returned, e.g.:

>>> state0 = [0.98753537-0.14925137j, 0.00746879-0.04941796j]
>>> state1 = [0.99500417+0.j, 0.09983342+0.j]
>>> qml.math.fidelity(state0, state1)

Alternatively one can give a state vector and a density matrix as arguments, e.g.:

>>> state0 = [0, 1]
>>> state1 = [[0, 0], [0, 1]]
>>> qml.math.fidelity(state0, state1)

It also works with two density matrices, e.g.:

>>> state0 = [[1, 0], [0, 0]]
>>> state1 = [[0, 0], [0, 1]]
>>> qml.math.fidelity(state0, state1)