# Fidelity

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
 Other toolboxes required Fidelity Computes the fidelity of two density matrices none TraceNorm Norms and distance measures

Fidelity is a function that computes the fidelity between two quantum states $\rho$ and $\sigma$, defined as follows: $F(\rho,\sigma) := \mathrm{Tr}\Big( \sqrt{ \sqrt{\rho}\sigma\sqrt{\rho}}\Big).$

Note that, in some sources, "fidelity" refers to the square of this quantity.

## Syntax

• FID = Fidelity(RHO,SIGMA)

## Argument descriptions

• RHO: A density matrix.
• SIGMA: A density matrix.

## Examples

### Pure states

If $\rho = |v\rangle\langle v|$ and $\sigma = |w\rangle\langle w|$ are both pure states then their fidelity simply equals $\big|\langle v|w \rangle\big|$:

>> v = RandomStateVector(4);
>> w = RandomStateVector(4);
>> Fidelity(v*v',w*w')

ans =

0.6486

>> abs(v'*w)

ans =

0.6486

### Can be used with CVX

The fidelity function is a jointly concave function, and it can be used in the objective function or constraints of a CVX optimization problem. For example, the following code computes the maximum output fidelity of two quantum channels:

>> Phi = RandomSuperoperator(3); % generate a random channel
>> Psi = RandomSuperoperator(3); % generate another one
>> cvx_begin sdp quiet
variable rho(3,3) hermitian;
variable sigma(3,3) hermitian;

maximize Fidelity(ApplyMap(rho,Phi),ApplyMap(sigma,Psi))

subject to % the constraints here just force rho and sigma to be density matrices
trace(rho) == 1;
trace(sigma) == 1;
rho >= 0;
sigma >= 0;
cvx_end
>> cvx_optval

cvx_optval =

0.9829

Of course, in this case it is more convenient to just use the MaximumOutputFidelity function directly:

>> MaximumOutputFidelity(Phi,Psi)

ans =

0.9829

## Source code

Click on "expand" to the right to view the MATLAB source code for this function.

1. %%  FIDELITY    Computes the fidelity of two density matrices
2. %   This function has two required input arguments:
3. %     RHO,SIGMA: density matrices
4. %
5. %   FID = Fidelity(RHO,SIGMA) is the fidelity between the two density
6. %   matrices RHO and SIGMA, defined by ||sqrt(RHO)*sqrt(SIGMA)||_1, where
7. %   ||.||_1 denotes the trace norm. FID is a value between 0 and 1, with 0
8. %   corresponding to matrices RHO and SIGMA with orthogonal support, and 1
9. %   corresponding to the case RHO = SIGMA.
10. %
11. %   URL: http://www.qetlab.com/Fidelity
12. 
13. %   requires: nothing (only uses CVX if called as an input to CVX)
14. %   authors: Vincent Russo (vrusso@uwaterloo.ca)
15. %            Nathaniel Johnston (nathaniel@njohnston.ca)
16. %   package: QETLAB
17. %   last updated: November 21, 2014
18. 
19. function fid = Fidelity(rho,sigma)
20. 
21. sz_rho = size(rho);
22. 
23. % Do some error checking.
24. if(~all(sz_rho == size(sigma)))
25.     error('Fidelity:InvalidDims','RHO and SIGMA must be matrices of the same size.');
26. elseif(sz_rho(1) ~= sz_rho(2))
27.     error('Fidelity:InvalidDims','RHO and SIGMA must be square.');
28. end
29. 
30. % If rho or sigma is a CVX variable then compute fidelity via semidefinite
31. % programming, so that this function can be used in the objective function
32. % or constraints of other CVX optimization problems.
33. if(isa(rho,'cvx') || isa(sigma,'cvx'))
34.     cvx_begin sdp quiet
35.         variable X(sz_rho(1),sz_rho(1)) complex;
36.         maximize trace(X) + trace(X');
37.         subject to
38.             cons = [rho,X;X',sigma];
39.             cons + cons' >= 0; % avoid some numerical problems: CVX often thinks things aren't symmetric without this
40.     cvx_end
41. 
42.     fid = cvx_optval/2;
43. 
44. % If rho and sigma are *not* CVX variables, compute fidelity normally,
45. % since this is much faster.
46. else
47.     [sq_rho,res] = sqrtm(rho); % need "res" parameter to suppress MATLAB singularity warning
48.     [sq_fid,res] = sqrtm(sq_rho*sigma*sq_rho);
49.     fid = real(trace(sq_fid)); % finally, compute the fidelity
50. end