Fidelity

Fidelity is a function that computes the fidelity between two quantum states $\rho$ and $\sigma$, defined as follows:
 * $$F(\rho,\sigma) := \mathrm{Tr}\Big( \sqrt{ \sqrt{\rho}\sigma\sqrt{\rho}}\Big).$$

Note that, in some sources, "fidelity" refers to the square of this quantity.

Syntax

 * FID = Fidelity(RHO,SIGMA)

Argument descriptions

 * RHO: A density matrix.
 * SIGMA: A density matrix.

Pure states
If $\rho = |v\rangle\langle v|$ and $\sigma = |w\rangle\langle w|$ are both pure states then their fidelity simply equals $\big|\langle v|w \rangle\big|$:

Can be used with CVX
The fidelity function is a jointly concave function, and it can be used in the objective function or constraints of a CVX optimization problem. For example, the following code computes the maximum output fidelity of two quantum channels:

Of course, in this case it is more convenient to just use the MaximumOutputFidelity function directly: