Path: blob/main/notebooks/ch-demos/piday-code.ipynb
3855 views
Estimating pi () using Quantum Phase Estimation Algorithm
1. Quick overview of the Quantum Phase Estimation Algorithm
Quantum Phase Estimation (QPE) is a quantum algorithm that forms the building block of many more complex quantum algorithms. At its core, QPE solves a fairly straightforward problem: given an operator and a quantum state that is an eigenvalue of with , can we obtain an estimate of ?
The answer is yes. The QPE algorithm gives us , where is the number of qubits we use to estimate the phase .
2. Estimating
In this demo, we choose where is one of the quantum gates available in Qiskit, and
By choosing the phase for our gate to be , we can solve for using the following two relations:
From the output of the QPE algorithm, we measure an estimate for . Then,
From the definition of the gate above, we know that
Combining these two relations, .
For detailed understanding of the QPE algorithm, please refer to the chapter dedicated to it in the Qiskit Textbook located at qiskit.org/textbook.
3. Time to write code
We begin by importing the necessary libraries.
The function qft_dagger computes the inverse Quantum Fourier Transform. For a detailed understanding of this algorithm, see the dedicated chapter for it in the Qiskit Textbook.
The next function, qpe_pre, prepares the initial state for the estimation. Note that the starting state is created by applying a Hadamard gate on the all but the last qubit, and setting the last qubit to .
Next, we write a quick function, run_job, to run a quantum circuit and return the results.
Then, load your account to use the cloud simulator or real devices.
Finally, we bring everything together in a function called get_pi_estimate that uses n_qubits to get an estimate for .
Now, run the get_pi_estimate function with different numbers of qubits and print the estimates.
Job Status: job has successfully run
2 qubits, pi ≈ 2.0
Job Status: job has successfully run
3 qubits, pi ≈ 4.0
Job Status: job has successfully run
4 qubits, pi ≈ 2.6666666666666665
Job Status: job has successfully run
5 qubits, pi ≈ 3.2
Job Status: job has successfully run
6 qubits, pi ≈ 3.2
Job Status: job has successfully run
7 qubits, pi ≈ 3.2
Job Status: job has successfully run
8 qubits, pi ≈ 3.1219512195121952
Job Status: job has successfully run
9 qubits, pi ≈ 3.1604938271604937
Job Status: job has successfully run
10 qubits, pi ≈ 3.1411042944785277
Job Status: job has successfully run
11 qubits, pi ≈ 3.1411042944785277
Job Status: job has successfully run
12 qubits, pi ≈ 3.1411042944785277
And plot all the results.