# Improving 2-5 Qubit Quantum Phase Estimation Circuits Using Machine Learning
## 1 Introduction
We apply a Hadamard gate \(H\) to each qubit in the first register to yield the state in Equation (2):  
\[H|0\rangle=\frac{1}{\sqrt{2}}(\ket{0}+\ket{1})\qquad H|1\rangle=\frac{1}{ \sqrt{2}}(\ket{0}-\ket{1}) \tag{1}\]  
\[\ket{\psi_{2}}=2^{-\frac{n}{2}}(\ket{0}+\ket{1})^{\otimes n}\otimes\ket{u}_{2} \tag{2}\]  
Following this operation, we apply a controlled unitary operation \(2^{j-1}\) times for the \(j^{th}\) qubit in the first register. A controlled unitary operation, denoted \(CU\), will apply the unitary operation to the second register if the control qubit is \(\ket{1}\) and not apply it if the qubit is \(\ket{0}\), with the result shown in Equation (3). This concludes the operations that involve the second register. \[ \begin{array}{ll}\ket{\psi_{3}}&=2^{-\frac{n}{2}}\left\{\bigotimes_{j=1}^{n }CU^{2^{j-1}}(\ket{0}+\ket{1})\right\}\ket{u}_{2}\\ &=2^{-\frac{n}{2}}\left\{\bigotimes_{j=1}^{n}\big{(}\ket{0}+\ket{1}U^{2^{j-1}} \big{)}\right\}\ket{u}_{2}\\ &=2^{-\frac{n}{2}}\left\{\bigotimes_{j=1}^{n}\big{(}\ket{0}+e^{2\pi i\theta ^{2^{j-1}}}\ket{1})\right\}\ket{u}_{2}\\ &=2^{-\frac{n}{2}}\sum\limits_{k=0}^{2^{k}-1}e^{2\pi ik\theta}\ket{k}\otimes \ket{u}_{2}\end{array}  \tag{3}\]  
The next step in the derivation is to apply the inverse quantum Fourier transform \(F^{\dagger}\). The \(F^{\dagger}\) is based on the inverse discrete Fourier transform (IDFT), which takes a (normalized) vector of complex numbers, say \(x_{0},\ x_{1},\ldots,\ x_{N-1}\), and outputs another vector of complex numbers \(y_{0},\ y_{1},\ldots,\ y_{N-1}\), where \(y_{k}=\frac{1}{\sqrt{N}}\sum_{j=0}^{N-1}x_{j}e^{-2\pi i\tilde{\mu}j/N}\). The \(F^{\dagger}\) takes the quantum state in the numerical basis and transforms it to a superposition of all states in the numerical basis with the coefficient of each basis state in the transformation being the IDFT value. The effect of \(F^{\dagger}\) is to transform the \(n\) qubit state \(\ket{j}\) to the state \(F^{\dagger}\ket{j}\) shown in Equation (4). The \(F^{\dagger}\) operator is applied to the first register to obtain the final state of the circuit \(|\psi_{4}\rangle\) before measurement shown in Equation (5):  
\[F^{\dagger}|j\rangle=2^{-n/2}\sum_{x=0}^{2^{n}-1}e^{-2\pi i|jx/2^{n}}|x\rangle \tag{4}\]  
\[|\psi_{4}\rangle=F^{\dagger}|\psi_{3}\rangle=2^{-n}\sum_{k=0}^{2^{n}-1}\sum_{x =0}^{2^{n}-1}e^{-2\pi i|k2^{-n}(x-2^{n}\theta)}\big{|}x\rangle\otimes|u\rangle_ {2} \tag{5}\]  
Measurement in QPE is performed only on the first register. Each of the qubits are measured to produce a string of bits, which is then converted into a decimal number between 0 and \(2^{n-1}\). The probability of measuring a given number \(x\) is the probability of measuring the state \(|x\rangle\) in the \(|\psi_{4}\rangle\) equation above, and the probability of such a measurement is the modulus squared of the coefficient of state \(|x\rangle\). Thus, the probability of \(x\) given \(\theta\), denoted \(P(x|\theta)=\mid\langle x|\psi_{4}\rangle|^{2}\), is shown in Equation (6):  
\[P(x|\theta)=\left|2^{-n}\sum_{k=0}^{2^{n}-1}\sum_{x^{\prime}=0}^{2^{n}-1}e^{- 2\pi i|k2^{-n}(x^{\prime}-2^{n}\theta)}\big{\langle}x\big{|}x^{\prime}\right| ^{2}\;=\;\left|2^{-n}\sum_{k=0}^{2^{n}-1}e^{-2\pi i|k2^{-n}(x-2^{n}\theta)} \right|^{2} \tag{6}\]  
When \(\theta\) can be represented exactly by a decimal string of \(n\) qubits (say \(0.\theta_{1}\theta_{2}\cdots\theta_{n}\)), then this probability distribution \(P(x|\theta)\) becomes 1 for the numerical value associated with the string of bits representing \(\theta\) (in this case \(P\Big{(}\sum_{d=1}^{n}\theta_{d}2^{d}\Big{|}\theta\Big{)}=1\) with 0 otherwise). However, when this is not possible (like for \(\theta=1/3\)), \(P(x|\theta)\) will have non-zero values for all \(x\). An example of the final circuit diagram is shown in Figure 1, which depicts a 3-qubit QPE circuit diagram where the unitary operator is a phase gate corresponding to \(\theta=1/3\) in the probability distribution.