# Demystifying Quantum Gate Fidelity for Electronics Engineers
## 4 Quantum Gate Fidelity in Matrix Form
The comparison of Equation (14) with Equation (16) leads to the following reformulation of Equation (15):  
\[F=\int_{S^{2n-1}}\lvert\langle\psi_{IN}\rvert D_{H}\lvert\psi_{IN}\rangle\rvert ^{2}dV+\int_{S^{2n-1}}\lvert\langle\psi_{IN}\rvert D_{A}\lvert\psi_{IN}\rangle \rvert^{2}dV \tag{17}\]  
Since \(d_{Ai}\) is wholly imaginary, \(d_{Ai}/j\) is a real number and, since \(\lvert j\rvert=1\), Equation (17) can be reformulated by replacing \(D_{A}\), whose entries are \(d_{Ai}\), with \(D_{A}/j\), whose entries are \(d_{Ai}/j\):  
\[F=\int_{S^{2n-1}}\lvert\langle\psi_{IN}\rvert D_{H}\lvert\psi_{IN}\rangle \rvert^{2}dV+\int_{S^{2n-1}}\lvert\langle\psi_{IN}\rvert D_{A}/j\lvert\psi_{IN }\rangle\rvert^{2}dV \tag{18}\]  
In this way, the fidelity \(F\) has been expressed by means of the two real diagonal matrices \(D_{H}\) and \(D_{A}/j\). For the sake of brevity, it therefore makes sense to address the following integral \(\mathfrak{H}\):  
\[\mathfrak{H}=\int_{S^{2n-1}}\lvert\langle\psi_{IN}\rvert D\lvert\psi_{IN} \rangle\rvert^{2}dV \tag{19}\]  
where \(D\) is a real diagonal matrix of real-valued eigenvalues \(d_{i}\). The expectation value \(\langle\psi_{IN}\rvert D\lvert\psi_{IN}\rangle\) is thus also real-valued, because of the usual expression:  
\[\langle\psi_{IN}\rvert D\lvert\psi_{IN}\rangle=\sum_{i}\lvert\alpha_{i}\rvert ^{2}d_{i} \tag{20}\]  
Since the squared module of a real number is the squared number \(\lvert\langle\psi_{IN}\rvert D\lvert\psi_{IN}\rangle\rvert^{2}=\langle\psi_{ IN}\rvert D\lvert\psi_{IN}\rangle^{2}\), Equation (20) yields:  
\[\lvert\langle\psi_{IN}\rvert D\lvert\psi_{IN}\rangle\rvert^{2}=\left(\sum_{i }\lvert\alpha_{i}\rvert^{2}d_{i}\right)^{2}=\sum_{i}\lvert\alpha_{i}\rvert ^{4}d_{i}^{2}+2\sum_{i,j\
eq i}\lvert\alpha_{i}\rvert^{2}\lvert\alpha_{j} \rvert^{2}d_{i}d_{j} \tag{21}\]  
Equation (21) is the generalization to \(n\) terms of the Leibniz trinomial for n = 2 \((a+b+c)^{2}=a^{2}+b^{2}+c^{2}+2 ab+2 ac+2 bc\). The substitution of Equation (21) into Equation (19) yields:  
\[\mathfrak{H}=\int_{S^{2n-1}}\sum_{i}\lvert\alpha_{i}\rvert^{4}d_{i}^{2}dV+ \int_{S^{2n-1}}2\sum_{i,j\
eq i}\lvert\alpha_{i}\rvert^{2}\lvert\alpha_{j} \rvert^{2}d_{i}d_{j}dV \tag{22}\]  
from which:  
\[\mathfrak{H}=\sum_{i}\biggl{(}\int_{S^{2n-1}}\lvert\alpha_{i}\rvert^{4}dV \biggr{)}d_{i}^{2}+\sum_{i}\biggl{(}\int_{S^{2n-1}}\lvert\alpha_{i}\rvert^{2} \lvert\alpha_{j}\rvert^{2}dV\biggr{)}2d_{i}d_{j} \tag{23}\]  
The eigenvectors can be factored out from the integral because the integral is calculated on the (2n - 1)-dimensional Bloch hypersphere by varying the vector state \(\lvert\psi_{IN}\rangle\), i.e., by varying its components. Appendix B details the mathematical proof of the following pair of integrals:  
\[ \begin{array}{l}\int_{S^{2n-1}}\lvert\alpha_{i}\rvert^{4}dV=\frac{2}{n(n+1) }\\ \int_{S^{2n-1}}\lvert\alpha_{i}\rvert^{2}\lvert\alpha_{j}\rvert^{2}dV=\frac{ 1}{n(n+1)}\end{array}  \tag{24}\]  
The substitution of Equation (24) into Equation (23) yields:  
\[\mathfrak{H}=\frac{2}{n(n+1)}\sum_{i}d_{i}^{2}+\frac{1}{n(n+1)}\sum_{i}2d_{i}d _{j} \tag{25}\]By remarking that the trace \(\mathit{Tr}\{D\}\) of the matrix \(D\) is also a real-valued number, you can write:  
\[|\mathit{Tr}\{D\}|^{2}=\mathit{Tr}^{2}\{D\}=\left(\sum_{i}d_{i}\right)^{2}=\sum_ {i}d_{i}^{2}+2\sum_{i,j\
eq i}d_{i}d_{j}=\mathit{Tr}\Big{\{}D^{2}\Big{\}}+2\sum _{i,j\
eq i}d_{i}d_{j} \tag{26}\]  
where it has been remarked that the first summation on the right-hand side of Equation (26) is the trace \(\mathit{Tr}\big{\{}D^{2}\big{\}}\) of \(D^{2}\).