mindquantum.simulator.fidelity
- mindquantum.simulator.fidelity(rho: np.ndarray, sigma: np.ndarray)[source]
Calculate the fidelity of two quantum states.
Definition of quantum state fidelity shown as below.
\[F(\rho, \sigma) = \left( \text{tr} \sqrt{\sqrt{\rho} \sigma \sqrt{\rho}} \right)^2\]where \(\rho\) and \(\sigma\) are density matrices.
If both \(\rho\) and \(\sigma\) are pure, \(\rho=\left|\psi_\rho\right>\!\left<\psi_\rho\right|\) and \(\sigma=\left|\psi_\sigma\right>\!\left<\psi_\sigma\right|\), then
\[F(\rho, \sigma) = \left| \left< \psi_\rho \middle| \psi_\sigma \right> \right|^2\]Besides, mixing state vector with density matrix as input is also supported.
- Parameters
rho (np.ndarray) – One of the quantum state. Support both state vector and density matrix.
sigma (np.ndarray) – Another quantum state. Support both state vector and density matrix.
- Returns
numbers.Number, the fidelity of two quantum states.
Examples
>>> from mindquantum.core.circuit import Circuit >>> from mindquantum.simulator import Simulator, fidelity >>> circ = Circuit().h(0).x(1, 0) >>> sim = Simulator('mqmatrix', 2) >>> sim.apply_circuit(circ) >>> rho = sim.get_qs() >>> sim.reset() >>> sigma = sim.get_qs() >>> fidelity(rho, sigma) 0.5000000000000001