# Fidelity in quantum information theory

This example is inspired from a lecture of John Watrous in the course on Theory of Quantum Information.

The Fidelity between two Hermitian semidefinite matrices P and Q is defined as:

$$$F(P, Q) = \|P^{1/2}Q^{1/2}\|_{\text{tr}} = \max_U \mathrm{tr}(P^{1/2}U Q^{1/2})$$$

where the trace norm $\|\cdot\|_{\text{tr}}$ is the sum of the singular values, and the maximization goes over the set of all unitary matrices U. This quantity can be expressed as the optimal value of the following complex-valued SDP:

$$$\begin{array}{ll} \text{maximize} & \frac{1}{2}\text{tr}(Z+Z^\dagger) \\ \text{subject to} &\\ & \left[\begin{array}{cc}P&Z\\{Z}^{\dagger}&Q\end{array}\right] \succeq 0\\ & Z \in \mathbf {C}^{n \times n}\\ \end{array}$$$
using Convex, SCS, LinearAlgebra

n = 20
P = randn(n, n) + im * randn(n, n)
P = P * P'
Q = randn(n, n) + im * randn(n, n)
Q = Q * Q'
Z = ComplexVariable(n, n)
objective = 0.5 * real(tr(Z + Z'))
constraint = [P Z; Z' Q] ⪰ 0
problem = maximize(objective, constraint)
solve!(problem, SCS.Optimizer; silent = true)
Problem statistics
problem is DCP         : true
number of variables    : 1 (800 scalar elements)
number of constraints  : 1 (6_400 scalar elements)
number of coefficients : 2_401
number of atoms        : 24

Solution summary
termination status : OPTIMAL
primal status      : FEASIBLE_POINT
dual status        : FEASIBLE_POINT
objective value    : 594.7495

Expression graph
maximize
└─ * (affine; real)
├─ [0.5;;]
└─ real (affine; real)
└─ sum (affine; complex)
└─ …
subject to
└─ PSD constraint (convex)
└─ vcat (affine; real)
├─ hcat (affine; real)
│  ├─ …
│  └─ …
└─ hcat (affine; real)
├─ …
└─ …

computed_fidelity = evaluate(objective)
594.7494990013275
# Verify that computer fidelity is equal to actual fidelity
P1, P2 = eigen(P)
sqP = P2 * diagm([p1^0.5 for p1 in P1]) * P2'
Q1, Q2 = eigen(Q)
sqQ = Q2 * diagm([q1^0.5 for q1 in Q1]) * Q2'
20×20 Matrix{ComplexF64}:
5.38006+2.50657e-17im  …  0.00764943+0.18069im
-1.09495+0.92107im          0.0809456-0.155984im
-0.550562+0.439034im         -0.526088-0.639864im
-0.649293-0.0308768im        -0.064919+0.0401416im
0.228963-0.387478im         -0.295176-0.362662im
0.341702-0.695126im     …   -0.952577-0.931465im
-0.407313+0.665933im         -0.958451-0.331556im
0.32162-0.280273im          0.547943-0.90029im
0.635895-0.200483im          0.688909-0.168503im
1.01216+0.148413im          -0.15593-0.00733035im
0.765091+0.34633im      …    0.595993-0.255228im
-1.01904-0.0603644im        0.0375434-0.0471595im
-0.112082+0.0227483im        -0.731198+0.231123im
0.0525511+0.298068im           0.29118+0.697098im
0.703363-0.804591im         -0.278485-0.00596324im
0.0158827+0.495351im     …    0.998033-0.371941im
-0.908249-0.437721im         -0.813657-0.0610957im
-0.0927775+0.306206im         -0.481457+0.476183im
-0.242561+0.0848857im         0.117876+0.27859im
0.00764943-0.18069im            4.55598+0.0im
actual_fidelity = sum(svdvals(sqP * sqQ))
594.7979633637808

We can see that the actual fidelity value is very close the computed fidelity value.