# Demystifying Quantum Gate Fidelity for Electronics Engineers
## 4 Quantum Gate Fidelity in Matrix Form
Eventually, the substitution of Equation (26) into Equation (25) yields:  
\[\mathfrak{H}=\frac{2}{n(n+1)}\sum_{i}d_{i}^{2}+\frac{1}{n(n+1)}\Big{(}| \mathit{Tr}\{D\}|^{2}-\mathit{Tr}\Big{\{}D^{2}\Big{\}}\Big{)} \tag{27}\]  
Again, the summation in Equation (27) is \(\mathit{Tr}\big{\{}D^{2}\big{\}}\), so that the Equation can be rearranged by factoring out \(\mathit{Tr}\big{\{}D^{2}\big{\}}\):  
\[\mathfrak{H}=\mathit{Tr}\Big{\{}D^{2}\Big{\}}\left[\frac{2}{n(n+1)}-\frac{1}{ n(n+1)}\right]+\frac{1}{n(n+1)}|\mathit{Tr}\{D\}|^{2}=\frac{\mathit{Tr}\big{\{}D^{2} \big{\}}+|\mathit{Tr}\{D\}|^{2}}{n(n+1)} \tag{28}\]  
The application of Equation (28) to the real diagonal matrices \(D_{H}\) and \(D_{A}/j\) in Equation (18) leads to:  
\[ \begin{array}{ll}F&=\frac{\mathit{Tr}\big{\{}D_{H}^{2}\big{\}}+|\mathit{Tr} \{D_{H}\}|^{2}}{n(n+1)}+\frac{\mathit{Tr}\big{\{}(D_{A}/j)^{2}\big{\}}+| \mathit{Tr}\{D_{A}/j\}|^{2}}{n(n+1)}\\ &=\frac{\mathit{Tr}\{D_{H}^{2}\big{\}}+|\mathit{Tr}\{D_{H}\}|^{2}}{n(n+1)}+ \frac{-\mathit{Tr}\{D_{A}^{2}\big{\}}+|\mathit{Tr}\{D_{A}\}|^{2}}{n(n+1)}\\ \end{array}  \tag{29}\]  
Since \(D_{H}\) (\(D_{A}\)) is a diagonal matrix, whose entries are pure real (imaginary), \(D_{H}=D_{H}^{\ddagger}\) (\(D_{A}=-D_{A}^{\ddagger}\)) and thus:  
\[ \begin{array}{ll}D_{H}^{2}=D_{H}D_{H}^{\ddagger}\\ D_{A}^{2}=-D_{A}D_{A}^{\ddagger}\end{array}  \tag{30}\]  
Since \(H\) and \(D_{H}\) (\(D_{A}\)) are unitarily similar, by definition, \(D_{H}\) (\(D_{A}\)) is obtained from \(H\) (\(A\)) by means of the following similarity transformation:  
\[ \begin{array}{ll}D_{H}=U_{H}^{-1}HU_{H}\\ D_{A}=U_{A}^{-1}AU_{A}\end{array}  \tag{31}\]  
where \(U_{H}\) (\(U_{A}\)) is the diagonalizing unitary matrix. The substitution of Equation (31) into Equation (30) yields:  
\[ \begin{array}{ll}D_{H}^{2}=U_{H}^{-1}HU_{H}\Big{(}U_{H}^{-1}HU_{H}\Big{)}^{ \ddagger}=U_{H}^{-1}HU_{H}\Big{[}U_{H}^{-1}(HU_{H})\Big{]}^{\ddagger}\\ D_{A}^{2}=-U_{A}^{-1}AU_{A}\left(U_{A}^{-1}AU_{A}\right)^{\ddagger}=-U_{A}^{- 1}AU_{A}\Big{[}U_{A}^{-1}(AU_{A})\Big{]}^{\ddagger}\end{array}  \tag{32}\]  
Since, for a unitary matrix, \(U^{-1}=U^{\ddagger}\), and, for a couple of matrices \(A\) and \(B\), \((AB)^{\ddagger}=B^{\ddagger}A^{\ddagger}\), Equation (32) can be reformulated as follows:  
\[ \begin{array}{ll}D_{H}^{2}=U_{H}^{-1}HU_{H}\left(HU_{H}\right)^{\ddagger}U_ {H}^{-1\ddagger}=U_{H}^{-1}HU_{H}U_{H}^{\ddagger}H^{\ddagger}U_{H}=U_{H}^{-1} HH^{\ddagger}U_{H}\\ D_{A}^{2}=-U_{A}^{-1}AU_{A}\left(AU_{A}\right)^{\ddagger}U_{A}^{-1\ddagger}= -U_{A}^{-1}AU_{A}U_{A}^{\ddagger}A^{\ddagger}U_{A}=-U_{A}^{-1}AA^{\ddagger}U_{A} \end{array}  \tag{33}\]By emphasizing that, for a pair of matrices \(A\) and \(B\), \(\mathit{Tr}\{AB\}=\mathit{Tr}\{BA\}\), from Equation (33), you get:  
\[ \begin{array}{l}\mathit{Tr}\{D_{H}^{2}\}=\mathit{Tr}\Big{\{}\left(U_{H}^{-1}H \right)\left(H^{\dagger}U_{H}\right)\Big{\}}=\mathit{Tr}\Big{\{}\left(H^{ \dagger}U_{H}\right)\left(U_{H}^{-1}H\right)\Big{\}}=\mathit{Tr}\{H^{\dagger} H\}\\ \mathit{Tr}\{D_{A}^{2}\}=-\mathit{Tr}\Big{\{}\left(U_{A}^{-1}A\right)\left(A ^{\dagger}U_{A}\right)\Big{\}}-\mathit{Tr}\Big{\{}\left(A^{\dagger}U_{A} \right)\left(U_{A}^{-1}A\right)\Big{\}}=-\mathit{Tr}\big{\{}A^{\dagger}A\} \end{array}  \tag{34}\]  
and from Equation (31):  
\[ \begin{array}{l}\mathit{Tr}\{D_{H}\}=\mathit{Tr}\Big{\{}\left(U_{H}^{-1}H \right)U_{H}\Big{\}}=\mathit{Tr}\Big{\{}U_{H}\Big{(}U_{H}^{-1}H\Big{)}\Big{\}}= \mathit{Tr}\Big{\{}U_{H}U_{H}^{-1}H\Big{\}}=\mathit{Tr}\{H\}\\ \mathit{Tr}\{D_{A}\}=\mathit{Tr}\{A\}\end{array}