# Fidelity and Metrics **Fidelity** measures how close two quantum states (or unitaries) are. For states $\rho$ and $\sigma$: $$F(\rho, \sigma) = \text{Tr}(\sqrt{\sqrt{\rho} \sigma \sqrt{\rho}})$$ For pure states, this simplifies to $F = |\langle\psi|\phi\rangle|^2$. Fidelity ranges from 0 (orthogonal) to 1 (identical). ```python from qutip import * import numpy as np # Two quantum states psi1 = basis(2, 0) psi2 = (basis(2, 0) + basis(2, 1)).unit() rho1 = psi1 * psi1.dag() rho2 = psi2 * psi2.dag() # Fidelity between states F = fidelity(rho1, rho2) print(F) # 0.5 for these states # Fidelity between unitaries U1 = sigmax() U2 = np.sqrt(sigmax()) # √X gate F_unitary = average_gate_fidelity(U1, U2) ``` ## Distance Metrics - **Trace distance**: $D(\rho, \sigma) = \frac{1}{2}\text{Tr}|\rho - \sigma|$ (bounded by 1) - **Hilbert-Schmidt distance**: $D_{\text{HS}} = \sqrt{\text{Tr}[(\rho - \sigma)^2]}$ - **Bures distance**: $D_B = \sqrt{2(1 - F)}$ ```python D = tracedist(rho1, rho2) D_hs = (rho1 - rho2).norm() # Hilbert-Schmidt ``` Fidelity is the key metric for assessing quantum gate accuracy and simulation accuracy. Higher fidelity means closer to target.