Skip to content

dq.fidelity

fidelity(x: ArrayLike, y: ArrayLike) -> Array

Returns the fidelity of two states, kets or density matrices.

The fidelity is computed

  • as \(F(\ket\psi,\ket\varphi)=\left|\braket{\psi|\varphi}\right|^2\) if both arguments are kets,
  • as \(F(\ket\psi,\rho)=\lvert\braket{\psi|\rho|\psi}\rvert\) if one arguments is a ket and the other is a density matrix,
  • as \(F(\rho,\sigma)=\tr{\sqrt{\sqrt\rho\sigma\sqrt\rho}}^2\) if both arguments are density matrices.
Warning

This definition is different from qutip.fidelity() which uses the square root fidelity \(F_\text{qutip} = \sqrt{F}\).

Parameters

  • x (array_like of shape (..., n, 1) or (..., n, n))

    Ket or density matrix.

  • y (array_like of shape (..., n, 1) or (..., n, n))

    Ket or density matrix.

Returns

(array of shape (...)) Real-valued fidelity.

Examples

>>> fock0 = dq.fock(3, 0)
>>> dq.fidelity(fock0, fock0)
Array(1., dtype=float32)
>>> fock01_dm = 0.5 * (dq.fock_dm(3, 1) + dq.fock_dm(3, 0))
>>> dq.fidelity(fock01_dm, fock01_dm)
Array(1., dtype=float32)
>>> dq.fidelity(fock0, fock01_dm)
Array(0.5, dtype=float32)