Quantum Phase Estimation with AWS Amazon Braket
In the previous articles, we covered the following topics:
- Understanding Quantum Fourier Transform Through Concrete Examples
- Inverse Quantum Fourier Transform with Qiskit 2.0
- Inverse Quantum Fourier Transform with the Amazon Braket SDK
In this article, we will build on those foundations to explore Quantum Phase Estimation.
Amazon Braket Learning Course
You can efficiently learn the basic knowledge of quantum computing—including quantum gates and quantum circuits introduced in this article—as well as how to use Amazon Braket through this course.
This course is designed for those with no prior knowledge of quantum computing or AWS, and by the end, you’ll even be able to learn about quantum machine learning. Take advantage of this opportunity to build your skills in quantum technologies!
Regarding the IBM Quantum Developer Certification Exam
IBM offers a certification exam related to Qiskit, which is introduced in this blog.
By obtaining this certification, you can prove your knowledge of Qiskit and quantum programming. For those aiming to achieve this certification, I have created a practice exams on the Udemy platform! I have made the explanations as clear as possible, so please make sure to take full advantage of the following practice exams as extra study material for exam preparation.
Practice exams
Quantum Phase Estimation with AWS Amazon Braket
Through the previous articles, we have learned that the inverse quantum Fourier transform allows us to identify quantum states that include phase information.
Could we leverage this property to design a useful quantum algorithm?
In fact, this property is utilized in the Quantum Phase Estimation (QPE) algorithm.
Given a unitary operator $U$ and its eigenstate $|\psi\rangle$,
the algorithm estimates $\phi$ in the form $\phi \approx 0.\phi_1\phi_2\ldots\phi_n = \sum_{k=1}^{n} \frac{\phi_k}{2^k}$ where $n$ is the number of qubits in the readout register.
$$
U |\psi\rangle = e^{2\pi i \phi} |\psi\rangle \quad (\phi \in [0, 1))
$$
The steps of this algorithm are as follows.
Prepare the Quantum Fourier Transformed state that carries the information of $\phi$
$\downarrow$
By applying the inverse quantum Fourier transform to the resulting state, we can extract the information about $\phi$
Let’s walk through the theoretical steps.
We begin by considering how to prepare the Quantum Fourier Transformed state that encodes the information of $\phi$.
As usual, the initial state of the $n+1$ qubit system is given as follows.
$$
|0\rangle^{\otimes n+1}
$$
Since we cannot obtain the value of $\phi$ without preparing the eigenstate,
let’s transform the last qubit into the eigenstate $|\psi\rangle$ of the unitary operator $U$.
$$
|0\rangle^{\otimes n} \otimes |\psi\rangle
$$
The goal this time is to prepare a Quantum Fourier Transformed state that encodes the information of $\phi$.
To proceed, let’s recall the definition of the Quantum Fourier Transform $\mathcal{F}$.
$$
\mathcal{F} \ket{j} = \frac{1}{\sqrt{2^n}} \sum_{k=0}^{2^n-1} e^{2\pi i jk / 2^n} \ket{k}
$$
The bit string $|0\rangle^{\otimes n}$ is referred to as the readout register in this case.
By applying H gates to it, the state becomes closer to the Quantum Fourier Transformed state, as shown below.
$$
\left( H^{\otimes n} |0\rangle^{\otimes n} \right) \otimes |\psi\rangle = \frac{1}{\sqrt{2^n}} \sum_{k=0}^{2^n - 1} |k\rangle \otimes |\psi\rangle
$$
By comparing the formula of the quantum Fourier transform with the current state, we can see that in the QFT expression, a phase factor dependent on $k$ is applied to each $|k\rangle$.
This can be achieved by applying a controlled-$U$ gate $\mathrm{C}\text{-}U$ with $|k\rangle$ as the control qubit.
$$
\begin{align}
\mathrm{C}\text{-}U \left( \frac{1}{\sqrt{2^n}} \sum_{k=0}^{2^n - 1} |k\rangle \otimes |\psi\rangle \right) &= \frac{1}{\sqrt{2^n}} \sum_{k=0}^{2^n - 1} |k\rangle \otimes U^k |\psi\rangle \\
&= \frac{1}{\sqrt{2^n}} \sum_{k=0}^{2^n - 1} e^{2\pi i \phi k} |k\rangle \otimes |\psi\rangle
\end{align}
\tag{1}
$$
We’re now very close to the form of the Quantum Fourier Transform!
At this point, let’s also recall the definition of the inverse Quantum Fourier Transform $\mathcal{F}^{-1}$.
$$
\mathcal{F}^{-1} \left( \frac{1}{\sqrt{2^n}} \sum_{k=0}^{2^n-1} e^{2\pi i jk / 2^n} \ket{k} \right) = \ket{j}
$$
Applying the inverse Quantum Fourier Transform simplifies the form significantly.
In the same way, if we apply the inverse Quantum Fourier Transform to the readout register part of the expression we derived, it should result in a simpler form.
This can be expressed as follows:
$$
\left( \mathcal{F}^{-1} \left( \frac{1}{\sqrt{2^n}} \sum_{k=0}^{2^n - 1} e^{2\pi i \phi k} |k\rangle \right) \right) \otimes |\psi\rangle = |2^n\phi\rangle \otimes |\psi\rangle
$$
After this transformation, measuring the readout register should allow us to observe $\phi$ in the form of $|2^n\phi\rangle$!
However, note that in Quantum Phase Estimation, if $\phi$ cannot be exactly represented with $n$ qubits in binary, the resulting state will be a superposition of nearby values as an approximation.
Implementation with the Amazon Braket SDK
Now, let’s implement the theory we’ve developed. In this example, we’ll use the $T$ gate as the target unitary matrix and $\ket{1}$ as its eigenstate. In other words, we aim to estimate the eigenvalue of the $T$ gate with respect to $\ket{1}$ using Quantum Phase Estimation. While it might be possible to compute this eigenvalue mentally, let’s pretend we don’t know it and proceed with the implementation. First, we’ll import the necessary functions.
from braket.circuits import Circuit
from braket.devices import LocalSimulator
from braket.experimental.algorithms.quantum_fourier_transform import (
quantum_fourier_transform as qft_module
)
In this example, we set the number of qubits in the readout register to 3.
n_readout_qubits = 3
circ = Circuit()
As explained earlier, we can apply Hadamard gates to the qubits in the readout register based on the theory.
for qubit in list(range(n_readout_qubits)):
circ.h(qubit)
Additionally, the fourth qubit, which is the last qubit, is prepared in the eigenstate $\ket{1}$.
circ.x(n_readout_qubits)
Next, we apply the controlled-$T$ gate. As shown in equation (1), note that the controlled-$T$ gate must be applied $2^{n-1}$ times depending on the qubit.
for ctl_bit in list(range(n_readout_qubits)):
for i in list(range(2**(ctl_bit))):
circ.cphaseshift(ctl_bit, n_readout_qubits, np.pi/4)
Next, we will perform the inverse Quantum Fourier Transform, but before that, let’s take a moment to review. When defined in the standard way, the inverse Quantum Fourier Transform for 3 qubits using the Amazon Braket Algorithm Library results in the following circuit:
T : │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │
┌──────────────┐ ┌──────────────┐ ┌───┐
q0 : ────x──────────────────────────────────┤ PHASE(-0.79) ├─┤ PHASE(-1.57) ├─┤ H ├─
│ └──────┬───────┘ └──────┬───────┘ └───┘
│ ┌──────────────┐ ┌───┐ │ │
q1 : ────┼───────────┤ PHASE(-1.57) ├─┤ H ├────────┼────────────────●───────────────
│ └──────┬───────┘ └───┘ │
│ ┌───┐ │ │
q2 : ────x─────┤ H ├────────●──────────────────────●────────────────────────────────
└───┘
T : │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │
Since the state of the readout register needs to be arranged according to the order of this transformation, we apply SWAP gates.
circ.swap(0,n_readout_qubits - 1)
When implementing the Quantum Fourier Transform and its inverse, it is important to pay close attention to the order in which operations are applied. In this case, by constructing the inverse Quantum Fourier Transform circuit while taking into account the pattern of controlled-$T$ gates applied to each qubit, we can obtain the expected result.
Printing the circuit up to this point yields the following:
T : │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │
┌───┐
q0 : ─┤ H ├────────●───────────────────────────────────────────────────────────────────────────────────────────────────────────x─────
└───┘ │ │
┌───┐ │ │
q1 : ─┤ H ├────────┼───────────────●───────────────●───────────────────────────────────────────────────────────────────────────┼─────
└───┘ │ │ │ │
┌───┐ │ │ │ │
q2 : ─┤ H ├────────┼───────────────┼───────────────┼───────────────●───────────────●───────────────●───────────────●───────────x─────
└───┘ │ │ │ │ │ │ │
┌───┐ ┌──────┴──────┐ ┌──────┴──────┐ ┌──────┴──────┐ ┌──────┴──────┐ ┌──────┴──────┐ ┌──────┴──────┐ ┌──────┴──────┐
q3 : ─┤ X ├─┤ PHASE(0.79) ├─┤ PHASE(0.79) ├─┤ PHASE(0.79) ├─┤ PHASE(0.79) ├─┤ PHASE(0.79) ├─┤ PHASE(0.79) ├─┤ PHASE(0.79) ├──────────
└───┘ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘
T : │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │
Now, let’s add the inverse Quantum Fourier Transform circuit that we’ve already constructed, and perform the measurement.
circ.iqft(range(n_readout_qubits))
device = LocalSimulator()
result = device.run(circ, shots=1000).result()
counts = result.measurement_counts
print(counts)
# Output
# Counter({'0011': 1000})
By measuring only the readout register, we obtain 001. Since the number of qubits in the readout register was $3$, this means that $\phi = 1/2^3 = 1/8$ was estimated by this circuit.
When reading out the result, it is necessary to consider the pattern of controlled-$T$ gates and the inverse Quantum Fourier Transform circuit. In this case, the correct result corresponds to the binary number 0.001, not 0.100.
In other words, the eigenvalue of the $T$ gate with respect to $\ket{1}$ is estimated as $e^{2\pi i \phi} = e^{\pi i /4}$. This is indeed consistent with the theoretical expression below.
Quantum phase estimation has been successfully executed!
$$ T |1\rangle = \begin{bmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{bmatrix} \begin{bmatrix} 0 \\ 1 \end{bmatrix} = e^{i\pi/4} |1\rangle $$
Amazon Braket Learning Course
You can efficiently learn the basic knowledge of quantum computing—including quantum gates and quantum circuits introduced in this article—as well as how to use Amazon Braket through this course.
This course is designed for those with no prior knowledge of quantum computing or AWS, and by the end, you’ll even be able to learn about quantum machine learning. Take advantage of this opportunity to build your skills in quantum technologies!
Regarding the IBM Quantum Developer Certification Exam
IBM offers a certification exam related to Qiskit, which is introduced in this blog.
By obtaining this certification, you can prove your knowledge of Qiskit and quantum programming. For those aiming to achieve this certification, I have created a practice exams on the Udemy platform! I have made the explanations as clear as possible, so please make sure to take full advantage of the following practice exams as extra study material for exam preparation.