Quantum Phase Estimation Algorithm
Translator: unseenme
Overview
Quantum Phase Estimation Algorithm, or QPE for short, is the key to many quantum algorithms. Suppose a unitary operator
Algorithm Analysis
The implementation of the quantum phase estimation algorithm requires two registers, the first register contains
Step One
Perform Hadamard gate operations on all qubits in the first register, and continuously perform control U
gate operations on the second register, where the powers of
where
Step Two
Perform the inverse quantum Fourier transform on the first register, which is expressed as
where
is the probability amplitude corresponding to the eigenbasis vector
Step Three
Measure the qubits of the first register to obtain the final state of the first register
QPE Code Implementation
The following is an example to demonstrate how to implement the quantum phase estimation algorithm in MindSpore Quantum. The T gate is selected as the unitary operator for estimation, from the definition of
it can be known that the phase angle to be estimated is
Now suppose we don’t know the phase information of the T gate, but only know that the unitary operator
First import the relevant dependencies.
[1]:
from mindquantum.core.gates import T, H, X, Power, BARRIER
from mindquantum.core.circuit import Circuit, UN
from mindquantum.simulator import Simulator
from mindquantum.algorithm.library import qft
import numpy as np
UN can specify quantum gates, target bits and control bits to build gate operations in the circuit; Power can get the exponential form of the specified quantum gate. Because we know that the eigenstate of the
T gate is
So we need to build a 5-bit circuit,
Use UN to perform Hadamard gate operation on
[2]:
# pylint: disable=W0104
n = 4
circ = Circuit()
circ += UN(H, n) # Act h gate on the first 4 bits
circ += X.on(n) # Act X gate on q4
circ.svg()
[2]:
With
[3]:
# pylint: disable=W0104
for i in range(n):
circ += Power(T, 2**i).on(n, n - i - 1) # Add T^2^i gate, where q4 is the target bit and n-i-1 is the control bit
circ.svg()
[3]:
Perform an inverse quantum Fourier transform on the bits in the first register.
[4]:
# pylint: disable=W0104
circ += BARRIER
circ += qft(range(n)).hermitian() # Inverse transform of quantum Fourier transform applied to the first 4 bits
circ.svg()
[4]:
Select the backend, pass in the total number of bits to create a simulator, evolve the quantum circuit, and get the final state.
[5]:
# pylint: disable=W0104
from mindquantum.core.gates import Measure
sim = Simulator('mqvector', circ.n_qubits) # Create an emulator
sim.apply_circuit(circ) # Evolving the circuit with the simulator
qs = sim.get_qs() # Obtain the evolved quantum state
res = sim.sampling(UN(Measure(), circ.n_qubits - 1), shots=100) # Add a measurement gate to register 1 and sample the circuit 100 times to obtain statistical results
res.svg()
[5]:
It should be noted that the reading order of the measurement result as a binary string should be 0010
, the probability amplitude is 1, and the final state can accurately reflect the phase 0010
is a binary result, so we convert it back to decimal and divide by
We can also find out the position of the amplitude maximum qs
obtained from the circuit evolution, and then obtain the corresponding eigenbasis vector
[6]:
index = np.argmax(np.abs(qs))
print(bin(index)[2:])
10100
It should be noted that qs
corresponds to the final state of the entire quantum circuit, so the obtained index
also includes the bits in the second register, and the index
into binary and remove the bits corresponding to
[7]:
bit_string = bin(index)[2:].zfill(circ.n_qubits)[1:] # Convert index to 01 string and remove q4
bit_string = bit_string[::-1] # Adjust the bit string order to q0q1q2q3
print(bit_string)
0010
Convert binary back to decimal again to get our final estimate.
[8]:
# pylint: disable=W0104
theta_exp = int(bit_string, 2) / 2**n
theta_exp
[8]:
0.125
It can be seen that the estimated phase obtained is approximately equal to
[9]:
from mindquantum.utils.show_info import InfoTable
InfoTable('mindquantum', 'scipy', 'numpy')
[9]:
Software | Version |
---|---|
mindquantum | 0.9.11 |
scipy | 1.10.1 |
numpy | 1.23.5 |
System | Info |
Python | 3.9.16 |
OS | Linux x86_64 |
Memory | 8.3 GB |
CPU Max Thread | 8 |
Date | Sun Dec 31 23:56:30 2023 |
Reference
[1] Michael A. Nielsen and Isaac L. Chuang. Quantum computation and quantum information