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} ||}_{tr}$ = max $|trace({P}^{1/2}U{Q}^{1/2})|$

where the trace norm $||.||_{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:

maximize 1/2 trace(Z+Z*)

subject to $\left[\begin{array}{cc}P&Z\\{Z}^{*}&Q\end{array}\right] \succeq 0$

where $Z \in \mathbf {C}^{n \times n}$

In :
using Convex
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(trace(Z+Z'))
constraint = [P Z;Z' Q] ⪰ 0
problem = maximize(objective,constraint)
solve!(problem)
computed_fidelity = evaluate(objective)

----------------------------------------------------------------------------
SCS v1.1.8 - Splitting Conic Solver
(c) Brendan O'Donoghue, Stanford University, 2012-2015
----------------------------------------------------------------------------
Lin-sys: sparse-direct, nnz in A = 19
eps = 1.00e-04, alpha = 1.80, max_iters = 20000, normalize = 1, scale = 5.00
Variables n = 9, constraints m = 65
Cones:	primal zero / dual free vars: 29
sd vars: 36, sd blks: 1
Setup time: 1.45e-04s
----------------------------------------------------------------------------
Iter | pri res | dua res | rel gap | pri obj | dua obj | kap/tau | time (s)
----------------------------------------------------------------------------
0|      inf       inf      -nan      -inf      -nan       inf  1.39e-04
100| 2.65e-04  2.64e-02  2.29e-03 -5.19e+00 -5.16e+00  1.37e-15  8.48e-03
200| 2.07e-06  2.08e-04  1.42e-05 -5.19e+00 -5.19e+00  1.52e-16  1.77e-02
220| 7.84e-07  7.89e-05  5.38e-06 -5.19e+00 -5.19e+00  2.30e-15  1.96e-02
----------------------------------------------------------------------------
Status: Solved
Timing: Solve time: 1.96e-02s
Lin-sys: nnz in L factor: 93, avg solve time: 2.19e-06s
Cones: avg projection time: 8.26e-05s
----------------------------------------------------------------------------
Error metrics:
dist(s, K) = 1.1202e-09, dist(y, K*) = 1.7323e-09, s'y/m = -4.9828e-12
|Ax + s - b|_2 / (1 + |b|_2) = 7.8427e-07
|A'y + c|_2 / (1 + |c|_2) = 7.8923e-05
|c'x + b'y| / (1 + |c'x| + |b'y|) = 5.3752e-06
----------------------------------------------------------------------------
c'x = -5.1910, -b'y = -5.1910
============================================================================

Out:
5.191031665238333
In :
# Verify that computer fidelity is equal to actual fidelity
P1,P2 = eig(P)
sqP = P2 * diagm([p1^0.5 for p1 in P1]) * P2'
Q1,Q2 = eig(Q)
sqQ = Q2 * diagm([q1^0.5 for q1 in Q1]) * Q2'

Out:
2×2 Array{Complex{Float64},2}:
1.73143-5.55112e-17im        -0.047971+0.182073im
-0.047971-0.182073im   0.951632+0.0im     
In :
actual_fidelity = sum(svd(sqP * sqQ))

Out:
5.1910314597465606

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