Mixed-State Fidelity: Why Matrix Overlap Is Not Enough

Mixed-State Fidelity: Why Matrix Overlap Is Not Enough

Two identical mixed states can have a matrix overlap below one. Learn squared fidelity, root fidelity, and a NumPy calculation that keeps the distinction clear.

You compare two density matrices with np.trace(rho @ sigma) and get 0.5. Does that mean the states are only halfway alike?

Not necessarily. They could be exactly the same state.

Take rho = sigma = I/2, the maximally mixed qubit. Their matrix overlap is Tr(rho sigma) = 1/2, but their quantum fidelity is 1. Nothing is wrong with either calculation. They answer different questions, and confusing them is an easy way to misread a noisy simulation.

First, choose a fidelity convention

This article uses the squared convention:

F(rho, sigma) = [Tr sqrt(sqrt(rho) sigma sqrt(rho))]^2.

Every square root here is a positive semidefinite matrix square root, not an element-by-element square root. For valid density matrices of the same size, F lies between 0 and 1. It is 1 exactly when the states are identical, and 0 exactly when their supports are orthogonal.

You will also encounter the same definition without the outer square. That convention is often called root fidelity. The IBM Quantum fidelity lesson uses the unsquared version and explicitly warns about this naming difference. Qiskit's state_fidelity implementation uses the squared version used here.

Neither convention is an error. A numerical comparison becomes an error when one source reports root fidelity and another reports its square without saying so.

Why the familiar overlap sometimes works

If the target is pure, rho = |psi><psi|, the squared fidelity simplifies to

F(rho, sigma) = <psi|sigma|psi> = Tr(rho sigma).

With two pure states it becomes |<psi|phi>|^2. This is why overlap feels like the obvious fidelity formula after working with statevectors. It really is correct in that setting.

The problem appears when both inputs are mixed. Setting sigma equal to rho then gives

Tr(rho sigma) = Tr(rho^2).

That is the purity of rho, not a general measure of how closely a state matches itself. A mixed state's purity is less than 1 even though it is, of course, identical to itself. For the maximally mixed state in dimension d, this self-overlap is 1/d, whereas its fidelity with itself remains 1.

The Hilbert-Schmidt overlap is still a useful quantity. It just should not silently replace mixed-state fidelity.

A noncommuting qubit example

Consider these two valid density matrices:

rho   = [[0.8, 0.0],
         [0.0, 0.2]]

sigma = [[0.5, 0.3],
         [0.3, 0.5]]

Both have eigenvalues 0.8 and 0.2. On the Bloch sphere their vectors have length 0.6, with rho pointing along +Z and sigma along +X. They have the same purity but are different states, and they do not commute.

For two qubit density matrices, the squared fidelity has a convenient formula:

F(rho, sigma) = Tr(rho sigma) + 2 sqrt(det(rho) det(sigma)).

To see why, let A be the positive matrix sqrt(rho) sigma sqrt(rho), with eigenvalues a and b. Then (sqrt(a) + sqrt(b))^2 = Tr(A) + 2 sqrt(det(A)). Cyclicity of the trace gives Tr(A) = Tr(rho sigma), and multiplicativity of determinants gives det(A) = det(rho) det(sigma).

For our example, the overlap is 0.5 and both determinants are 0.16. The correction contributes 0.32, giving a squared fidelity of 0.82. The root fidelity is approximately 0.905539. Those are three distinct numbers, not three software packages disagreeing about the same calculation.

The determinant shortcut is specific to two-dimensional states. The matrix-square-root definition works in any finite dimension.

Calculate it without taking entrywise square roots

An equivalent expression is

F(rho, sigma) = ||sqrt(rho) sqrt(sigma)||_1^2.

The trace norm here is the sum of singular values. It is not the sum of absolute entries and not the Frobenius norm. We can therefore compute positive matrix square roots with a Hermitian eigendecomposition, then use a singular-value decomposition for the final norm.

import numpy as np

def sqrt_density(matrix, tol=1e-10):
    matrix = np.asarray(matrix, dtype=complex)
    if matrix.ndim != 2 or matrix.shape[0] != matrix.shape[1]:
        raise ValueError("A density matrix must be square")
    if not np.all(np.isfinite(matrix)):
        raise ValueError("Nonfinite matrix entry")
    if not np.allclose(matrix, matrix.conj().T, atol=tol, rtol=0):
        raise ValueError("Matrix must be Hermitian")
    if not np.isclose(np.trace(matrix), 1, atol=tol, rtol=0):
        raise ValueError("Trace must equal one")
    values, vectors = np.linalg.eigh(matrix)
    if values.min() < -tol:
        raise ValueError("Matrix is not positive semidefinite")
    values = np.maximum(values, 0)  # Only tolerate tiny roundoff negatives.
    return (vectors * np.sqrt(values)) @ vectors.conj().T

def fidelity(rho, sigma):
    left, right = sqrt_density(rho), sqrt_density(sigma)
    if left.shape != right.shape:
        raise ValueError("States must have matching dimensions")
    singular_values = np.linalg.svd(left @ right, compute_uv=False)
    return float(singular_values.sum() ** 2)

rho = np.diag([0.8, 0.2]).astype(complex)
H = np.array([[1, 1], [1, -1]], dtype=complex) / np.sqrt(2)
sigma = H @ rho @ H.conj().T
mixed = np.eye(2) / 2
F = fidelity(rho, sigma)

print(f"Matrix overlap: {np.trace(rho @ sigma).real:.6f}")
print(f"Squared fidelity: {F:.6f}")
print(f"Root fidelity: {np.sqrt(F):.6f}")
print(f"Identical mixed states, overlap: {np.trace(mixed @ mixed):.6f}")
print(f"Identical mixed states, fidelity: {fidelity(mixed, mixed):.6f}")

# Outcome distributions from measuring Z and retaining only the result.
p, q = np.diag(rho).real, np.diag(sigma).real
classical_F = np.sum(np.sqrt(p * q)) ** 2
print(f"Z-outcome squared fidelity: {classical_F:.6f}")
assert np.isclose(F, 0.82)
assert np.isclose(classical_F, 0.9)

The output is

Matrix overlap: 0.500000
Squared fidelity: 0.820000
Root fidelity: 0.905539
Identical mixed states, overlap: 0.500000
Identical mixed states, fidelity: 1.000000
Z-outcome squared fidelity: 0.900000

The helper rejects materially nonphysical inputs. Clipping tiny negative eigenvalues is only a floating-point safeguard, not a method for repairing a bad tomography estimate. This is a dense-matrix educational implementation, not a claim that full-state fidelity is cheap to estimate on large quantum hardware.

A measurement can hide some of the difference

The final line compares the classical Z-measurement distributions: (0.8, 0.2) for rho and (0.5, 0.5) for sigma. Their squared classical fidelity is

(sqrt(0.8 * 0.5) + sqrt(0.2 * 0.5))^2 = 0.9.

That is larger than the quantum fidelity of 0.82. Recording only Z outcomes has discarded information relevant to distinguishing the original states.

This illustrates a general property: applying the same trace-preserving quantum channel to both states cannot decrease their fidelity. Measurement with only its classical result retained is one such channel. The IBM lesson states this monotonicity for root fidelity; squaring preserves the inequality because all values are nonnegative. This statement does not cover normalizing a selected postselection branch.

A higher fidelity after processing therefore does not necessarily mean you improved state preparation. You may simply have removed information that revealed a mismatch. Completely replacing both inputs with the same fixed state would give fidelity 1 while preserving none of their original differences.

When comparing noisy quantum states, label the quantity you report. Say whether it is squared fidelity, root fidelity, or matrix overlap, and whether you compared full states or only measurement distributions. That small bit of precision can prevent a very convincing wrong conclusion.