The new Qiskit Textbook beta is now available. Try it out now
Lab 4. Iterative Quantum Phase Estimation

# Lab 4 Iterative Phase Estimation Algorithm

from qiskit import *
import numpy as np
from qiskit.visualization import plot_histogram
import qiskit.tools.jupyter
import matplotlib.pyplot as plt

sim = Aer.get_backend('aer_simulator')


## Part 1: Implementation of Iterative Phase Estimation algorithm

Goal

Estimate a phase value on a system of two qubits through Iterative Phase Estimation (IPE) algorithm.

Having gone through previous labs, you should have noticed that the "length" of a quantum circuit is the primary factor when determining the magnitude of the errors in the resulting output distribution; quantum circuits with greater depth have decreased fidelity. Therefore, implementing algorithms based on shallow depth circuits is of the great importance in near-term quantum computing. In Lab 4, we learn one such algorithm for estimating quantum phase called the Iterative Phase Estimation (IPE) algorithm which requires a system comprised of only a single auxiliary qubit and evaluate the phase through a repetitive process.

### 1. Understand a circuit with non-unitary operations.

Before we learn how the IPE algorithm works, lets review reset and conditional operations in Qiskit that go into building a IPE circuit. Read the Qiskit tutorial here ( go to Non-unitary operations section ) to understand how to build a circuit that performs conditional operations and reset.

#### 📓Step A. Run the following cell and predict the outcome of the circuit.

q = QuantumRegister(1)
c = ClassicalRegister(2)

qc0 = QuantumCircuit(q, c)
qc0.h(q[0])
qc0.measure(q[0], c[0])
qc0.reset(q[0])
qc0.h(q[0])
qc0.p(np.pi/3, q[0]).c_if(c,1)
qc0.h(q[0])
qc0.measure(q[0],c[1])

qc0.draw()


Execute the cell below to see the result.

qc0 = transpile(qc0, sim)
count0 = sim.run(qc0).result().get_counts()
plot_histogram(count0)


#### 📓Step B. Complete the rest of the circuit so that the auxiliary qubit ( top qubit ) after the reset would be in the state $\frac{1}{\sqrt2}(|0\rangle + e^{-i\frac{\pi}{2}}|1\rangle)$ if the value of the classical bit is one or remains zero state otherwise.

q = QuantumRegister(2)
c = ClassicalRegister(2)

qc1 = QuantumCircuit(q,c)
qc1.h(q[0])
qc1.x(q[1])
qc1.cp(np.pi/5, q[0], q[1])
qc1.measure(q[0], c[0])
qc1.reset(q[0])

###### your code goes here #####

##########################
qc1.h(q[0])
qc1.measure(q[0],c[1])

qc1.draw()


Running the following cell to display the result.

qc1 = transpile(qc1, sim)
count1 = sim.run(qc1).result().get_counts()
plot_histogram(count1)


### 2. Iterative Phase Estimation (IPE) Algorithm.

The Quantum Phase Estimation (QPE) circuit that we have learned and used previously is limited by the number of qubits necessary for the algorithm’s precision. Every additional qubit has added costs in terms of noise and hardware requirements; noisy results that we obtained from executing the QPE circuit on a real quantum device in Lab 3 would get worse as the number of the qubits on the circuit increases. The IPE algorithm implements quantum phase estimation with only a single auxiliary qubit, and the accuracy of the algorithm is restricted by the number of iterations rather than the number of counting qubits. Therefore, IPE circuits are of practical interest and are of foremost importance for near-term quantum computing as QPE is an essential element in many quantum algorithms.

Consider the problem of finding $\varphi$ given $|\Psi\rangle$ and $U$ such that $U |\Psi\rangle = e^{i \phi} | \Psi \rangle$, with $\phi = 2 \pi \varphi$. Let's assume for now that $\varphi$ can be written as $\varphi = \varphi_1/2 + \varphi_2/4 + ... + \varphi_m/2^m = 0.\varphi_1 \varphi_2 ... \varphi_m$, where we have previously defined the notation $0.\varphi_1 \varphi_2 ... \varphi_m$.

Assume that $U$ is a unitary operator acting on one qubit. We therefore need a system of two qubits, $q_0$ and $q_1$, where $q_0$ is auxiliary qubit and the qubit $q_1$ represents the physical system on which $U$ operates. Having them initialized as $q_0 \rightarrow |+\rangle$ and $q_1 \rightarrow |\Psi \rangle$, application of control-U between $q_0$ and $q_1$ $2^t$ times would change the state of $q_0$ to $|0\rangle + e^{i 2 \pi 2^{t} \varphi} | 1 \rangle$. That is, the phase of $U$ has been kicked back into $q_0$ as many times as the control operation has been performed.

Therefore,

for $t=0$, the phase encoded into $q_0$ would be $e^{i 2 \pi 2^{0} \varphi} = e^{i 2 \pi \varphi} = e^{i 2 \pi 0.\varphi_1 \varphi_2 ... \varphi_m}$ and

for $t=1$, the phase would be $e^{i 2 \pi 2^{1} \varphi} = e^{i 2 \pi \varphi_1} e^{i 2 \pi 0.\varphi_2 \varphi_3 ... \varphi_m}$ and

for $t=2$, $e^{i 2 \pi 2^{2} \varphi} = e^{i 2 \pi 2 \varphi_1} e^{i 2 \pi \varphi_2} e^{i 2 \pi 0.\varphi_3 \varphi_4 ... \varphi_m}$ and

for $t=m-1$, $e^{i 2 \pi 2^{m-1} \varphi} = e^{i 2 \pi 2^{m-2} \varphi_1} e^{i 2 \pi 2^{m-3} \varphi_2} ... e^{i 2 \pi 2^{-1} \varphi_m} = e^{i 2 \pi 0.\varphi_m}$.

Note that for the last case with $t=m-1$, the state of $q_0$ is $|0\rangle + e^{i 2 \pi 0.\varphi_m}|1\rangle$; $|+\rangle$ if $\varphi_m = 0$ and $|-\rangle$ if $\varphi_m = 1$ which would produce outcomes $|0\rangle$ and $|1\rangle$ respectively when it gets measured in x-basis.

In the first step of the IPE algorithm, we directly measure the least significant bit of the phase $\varphi$, $\varphi_m$, by initializing the 2-qubit registers as described above ( $q_0 \rightarrow |+\rangle$ and $q_1 \rightarrow |\Psi \rangle$ ), performing $2^{m-1}$ control-$U$ operations between the qubits, and measuring $q_0$ in the x-basis.

For the second step, we initialize the systems in the same way and apply $2^{m-2}$ control-$U$ operations. The relative phase in $q_0$ after these operations is now $e^{i 2 \pi 0.\varphi_{m-1}\varphi_{m}}= e^{i 2 \pi 0.\varphi_{m-1}} e^{i 2 \pi \varphi_m/4}$. To extract the phase bit $\varphi_{m-1}$, first perform a phase correction by rotating around the $Z-$axis of angle $-2 \pi \varphi_m/4=-\pi \varphi_m/2$, which results in the state of $q_0$ to be $|0\rangle + e^{i 2 \pi 0.\varphi_{m-1}} | 1 \rangle$. Perform a measurement on $q_0$ in x-basis to obtain the phase bit $\varphi_{m-1}$.

Therefore, the $k$th step of the IPE, getting $\varphi_{m-k+1}$, consists of the register initialization ($q_0$ in $|+\rangle$, $q_1$ in $|\Psi\rangle$), the application of control-$U$ $2^{m-k}$ times, a rotation around $Z$ of angle $\omega_k = -2 \pi 0.0\varphi_{m-k+2} ... \varphi_m$, and a measurement of $q_0$ in x-basis: a Hadamard transform to $q_0$, and a measurement of $q_0$ in the standard basis. Note that $q_1$ remains in the state $|\Psi\rangle$ throughout the algorithm.

### 3. Estimate the phase of the $T$-gate implementing IPE algorithm.

Review the section 2. Example: T-gate in Ch.3.8 Quantum Phase Estimation and the section 4. Measuring in Different Bases in Ch.1.4 Single Qubit Gates

As we already learned the Qiskit textbook, the phase of a T-gate is exactly expressed using three bits.

#### 📓Step A. Obtain the least significant phase bit of the $T$-gate by setting up the circuit T_x3 properly and assign the value to the variable x_3.

In the previous section, the first step explains how to construct the circuit to extract the least significant phase bit.

q = QuantumRegister(2)
c = ClassicalRegister(1)

T_x3 = QuantumCircuit(q,c)

########## your code goes here #######

##1 Initialization

##2 Apply control-U operator as many times as needed to get the least significant phase bit

##3 measure the anscillar qubit in x-basis

########## Simulate the circuit and assign the output value to the variable 'x_3'
T_x3 = transpile(T_x3)
job = sim.run(T_x3, shots=1, memory=True)
x_3 = int(job.result().get_memory()[0])

print('x_3 = {}'.format(x_3))
T_x3.draw('mpl')


#### 📓Step B. Extract the middle phase bit of the $T$-gate by creating the circuit T_x2 with phase correction using x_3 value from Step A. Assign the outcome bit to the variable x_2.

Read the the second step in the previous section.

q = QuantumRegister(2)
c = ClassicalRegister(1)

T_x2 = QuantumCircuit(q,c)

########### your code goes here ##########

##1 Initialization

##2 Apply control-U operator as many times as needed

##3 phase correction

##4 measure the anscillar qubit in x-basis

######## Simulate the circuit and assign the output value to the variable 'x_2'
T_x2 = transpile(T_x2, sim)
job = sim.run(T_x2, shots=1, memory=True)
x_2 = int(job.result().get_memory()[0])

print('x_2 = {}'.format(x_2))
T_x2.draw('mpl')


#### 📓Step C. Find the most significant phase bit of the $T$-gate and assign it to the variable x_1.

q = QuantumRegister(2)
c = ClassicalRegister(1)

T_x1 = QuantumCircuit(q,c)

########### your code goes here #########

##1 Initialization

##2 Apply control-U operator as many times as needed to get the least significant phase bit

##3 phase correction

##4 measure the anscillar qubit in x-basis

########## Simulate the circuit and assign the output value to the variable 'x_1'
T_x1 = transpile(T_x1, sim)
job = sim.run(T_x1, shots=1, memory=True)
x_1 = int(job.result().get_memory()[0])

print('x_1 = {}'.format(x_1))
T_x1.draw('mpl')


Therefore, the $T$-gate phase bit that you found is 0.x_1x_2x_3. Run the following cell to check if your answer is correct by comparing your phase bit x_1x_2x_3 with 001, the answer in the Qiskit textbook, which corresponds to $\frac{1}{8}$ ( = 0.125), the phase of the $T$-gate.

T_phase_bits = '{}{}{}'.format(x_1, x_2, x_3)
T_phase_bits == '001'


#### 📓Step D. Construct the full IPE circuit and pass it to the variable qc_T ; Put the all steps that you performed into one circuit utilizing conditional operations and reset.

Instead of using three separate circuits to extract each phase bit value, build one circuit; perform a reset operation on the auxiliary qubit after each bit gets measured into a classical register. Therefore, the circuit requires three classical registers for this example; the least significant bit measured into the classical register, c[0] and the most significant bit measured into c[2]. Implement conditional operator between the auxiliary qubit and the classical register for the phase correction.

##### your code goes here ######

################
qc_T.draw()


#### Step E. Execute the following cell to perform the simulation and display the result.

qc_T = transpile(qc_T, sim)
count0 = sim.run(qc_T).result().get_counts()

key_new = [str(int(key,2)/2**3) for key in list(count0.keys())]
count1 = dict(zip(key_new, count0.values()))

fig, ax = plt.subplots(1,2)
plot_histogram(count0, ax=ax[0])
plot_histogram(count1, ax=ax[1])
plt.tight_layout()


## Part 2: Comparison between QPE and IPE results in the presence of noise

Goal

Understand the importance of implementing shallow circuit algorithms on current noisy quantum computers.

In Part 2 of Lab 3, we executed a Quantum Phase Estimation (QPE) circuit on a real quantum device. Having recognized the limits of the performance due to noise that presents in current quantum system, we utilized several techniques to reduce its influence on the outcome. However, the final result that was obtained, even after all these procedures, is still far from ideal. Here, we implement Iterative Phase Estimation (IPE) algorithm to overcome the effect of noise in phase estimation to a great extent and compare the result with the QPE outcome.

To investigate the impact of the noise from real quantum system on the outcome, we will perform noisy simulations of IPE circuit employing the Qiskit Aer noise module which produces a simplified noise model for an IBM quantum system. To learn more about noisy simulation, read here.

As in Lab 3, we consider to estimate the phase of p gate with $\theta = \frac{1}{3}$. Suppose that the accuracy of the estimation that we desire here is same as when the QPE circuit has four counting qubits, which determines the number of iteration and classical registers required for the IPE circuit.

#### 📓Step A. How many classical registers is needed? Assign the value to the variable n.

## your answer goes here
n =


#### 📓Step B. Construct the IPE circuit in the following cell.

q = QuantumRegister(2)
c = ClassicalRegister(n)

IPE = QuantumCircuit(q,c)

########## your code goes here ############

#####################
IPE.draw()


Run the cell below to obtain the result.

IPE = transpile(IPE, sim)
count_tmp= sim.run(IPE).result().get_counts()
key_new = [str(int(key,2)/2**n) for key in list(count_tmp.keys())]
count_new = dict(zip(key_new, count_tmp.values()))

fig, ax = plt.subplots(1,2, figsize=(15, 4))
plot_histogram(count_tmp, ax=ax[0])
plot_histogram(count_new, ax=ax[1])


#### Step C. Run the cell below to create the QPE circuit for the comparison.

# define the parameters
t = 4

# building a circuit
QPE = QuantumCircuit(t+1,t)
QPE.h(range(t))
QPE.x(t)
for idx in range(t):
QPE.cp(psi*2**idx, idx, t)

QPE.append(QFT(t).inverse(), range(t))
QPE.measure(range(t), range(t))

QPE.draw()


#### 📓Step D. Transpile the circuits for the backend ibmq_vigo.

Run the following cell to check the properties of the backend, ibmq_vigo. Pick an initial_layout, and transpile the IPE circuit setting optimization_level = 3, and save the transpiled circuit to the variable IPE_trans. Print out the depth of the transpiled circuit.

from qiskit.test.mock import FakeVigo
import qiskit.tools.jupyter

backend = FakeVigo()
backend;

######## your code to transpile IPE circuit goes here ########

#####################


Execute the cell below to transpile QPE circuit.

num = 500
QPE_trans = transpile([QPE]*num, backend, optimization_level=3)
QPE_trans_depth = np.array([QPE_trans[idx].depth() for idx in range(num)])
print(min(QPE_trans_depth), max(QPE_trans_depth))
best_arg = np.argmin(QPE_trans_depth)
QPE_trans_best = QPE_trans[best_arg]


#### Step E. Run the following cells to perform the noise simulation of the transpiled circuits.

from qiskit.providers.aer import AerSimulator

sim_vigo = AerSimulator.from_backend(backend)
counts = sim_vigo.run([IPE_trans, QPE_trans], shots=8192).result().get_counts()


#### Step F. Execute the cell below to compute the exact phase estimation results.

from qiskit.quantum_info import Statevector

QPE_exact = QuantumCircuit(t+1)
QPE_exact.h(range(t))
QPE_exact.x(t)
for idx in range(t):
QPE_exact.cp(psi*2**idx, idx, t)

QPE_exact.append(QFT(t).inverse(), range(t))

print(QPE_exact)

state = Statevector.from_instruction(QPE_exact)
pmf = state.probabilities_dict(range(4))


#### Step G. Show the comparison figure by running the following cell.

def count_new(count):
phi_est = np.array([round(int(key, 2)/2**t, 3) for key in list(count.keys())])
key_new = list(map(str, phi_est))
count_new = dict(zip(key_new, count.values()))
return count_new

pmf_new = count_new(pmf)
count_IPE = count_new(counts[0])
count_QPE = count_new(counts[1])

fig, ax = plt.subplots(1, 2, figsize=(32,10))
fig.suptitle('QPE .vs. IPE for estimating $\\theta=1/3$', fontsize=23)
plot_histogram([pmf_new, count_QPE], ax=ax[0], legend=['No_noise', 'vigo_sim'])
plot_histogram([pmf_new, count_IPE], ax=ax[1], legend=['No_noise', 'vigo_sim'])
ax[0].set_title('QPE circuit result', fontsize=16)
ax[0].set_xlabel('$\\theta$', fontsize=16)
ax[1].set_title('IPE circuit result', fontsize=16)
ax[1].set_xlabel('$\\theta$', fontsize=16)
plt.show()


If you create the IPE circuit successfully to estimate the phase, $\theta = \frac{1}{3}$, you would get the similar plots as shown below.