নোট
এই পৃষ্ঠাটি tutorials/algorithms/08_factorizers.ipynb থেকে নেয়া হয়েছে।
ধাপে ধাপে কোয়ান্টাম দশা অনুমানকারী ধারাক্রম (অ্যালোগরিদম)¶
এই টিউটোরিয়ালের উদ্দেশ্য হলো, কিভাবে ইটারেটিভ ফেজ এস্টিমেশন (IPE) অ্যালোগরিদম কাজ করে, কেন আমরা কোয়ান্টাম ফেজ এস্টিমেশন (QPE) অ্যালোগরিদমের বদলে (IPE) অ্যালোগরিদম ব্যবহার করব এবং কিভাবে Qiskit এর সাহায্যে একই বর্তনীতে সার্কিট রিসেট যুক্তিবর্তনী গেইট দিয়ে একে তৈরি করা যায় তা জানা, এবং এরপরেও c_if
পদ্ধতি এর সাহায্যে কিভাবে ক্লাসিক্যাল রেজিস্টারে থাকা আগের মেজারমেন্টের মান শর্ত হিসেবে নিয়ে গেট প্রয়োগ করে আইপিই নির্মাণ (বিল্ড) করা যায় তা জানা।
রেফারেন্স সমূহ:
Conditioned gates: the c_if
method¶
Before starting the IPE algorithm, we will give a brief tutorial about the Qiskit conditional method, c_if
, as it goes into building the IPE circuit.
c_if
হল gate ক্লাসের একটা মেথড, যা পূর্বে ক্লাসিক্যাল রেজিস্টারে সঞ্চিত মানের উপর নির্ভর করে শর্তাধীন ক্রিয়াকলাপ সম্পন্ন করে। এর সাহায্যে আনপারা একই সার্কিটের মধ্যে ফলাফল দ্বারা সীমাবদ্ধ মেজারমেন্টের পর গেটসমূহ ব্যবহার করতে পারেন।
উদাহরণস্বরূপ, ক্লাসিকাল রেজিস্টার যদি \(0\) হয় তাহলে নিম্নলিখিত কোড \(X\) গেট চালাবে।
[1]:
from qiskit import QuantumCircuit
qc = QuantumCircuit(1, 1)
qc.h(0)
qc.measure(0,0)
qc.x(0).c_if(0, 0)
qc.draw(output='mpl')
[1]:

We highlight that the method c_if
expects as the first argument a whole classical register, not a single classical bit (or a list of classical bits), and as the second argument a value in decimal representation (a non-negative integer), not the value of a single bit, 0, or 1 (or a list/string of binary digits).
Let’s make another example. Consider that we want to perform a bit flip on the third qubit after the measurements in the following circuit, when the results of the measurement of \(q_0\) and \(q_1\) are both \(1\).
[2]:
from qiskit import QuantumRegister, ClassicalRegister
q = QuantumRegister(3, 'q')
c = ClassicalRegister(3, 'c')
qc = QuantumCircuit(q, c)
qc.h([0, 1, 2])
qc.barrier()
qc.measure(q, c)
qc.draw('mpl')
[2]:

We want to apply the \(X\) gate, only if both the results of the measurement of \(q_0\) and \(q_1\) are \(1\). We can do this using the c_if
method, conditioning the application of \(X\) depending on the value passed as argument to c_if
.
We will have to encode the value to pass to the c_if
method such that it will check the values 011 and 111 (in binary representation), since it does not matter what \(q_2\) is measured as.
ডেসিমাল রিপ্রেজেন্টেশনে দুটি পূর্ণসংখ্যার মান:
We can check the solutions using the bin()
method in python (the prefix 0b
indicates the binary format).
[3]:
print(bin(3))
print(bin(7))
0b11
0b111
So we have to apply \(X\) to \(q_2\) using c_if
two times, one for each value corresponding to 011 and 111.
[4]:
qc.x(2).c_if(c, 3) # for the 011 case
qc.x(2).c_if(c, 7) # for the 111 case
qc.draw(output='mpl')
[4]:

আইপিই (IPE)¶
The motivation for using the IPE algorithm is that QPE algorithm works fine for short depth circuits but when the circuit starts to grow, it doesn’t work properly due to gate noise and decoherence times.
ধারাক্রম (অ্যালোগরিদম) কিভাবে কাজ করে বিস্তারিত জানতে Iterative Phase Estimation (IPE) Algorithm দেখো। কিউপিই (QPE) ব্যাপারে জানতে Ch.3.6 Quantum Phase Estimation দেখো।
\(U\) -এর জন্য ১-কিউবিট যুক্তিবর্তনী (গেইট) সম্পন্ন আইপিই এর উদাহরণ¶
আমরা আইপিই ধারাক্রম (অ্যালোগরিদম) প্রয়োগ করে ১-কিউবিট অপারেটরের \(U\) দশা অনুমান করব। উদাহরণরূপে এখানে আমরা \(S\) -গেট ব্যবহার করব।
Let’s apply the IPE algorithm to estimate the phase for \(S\)-gate. Its matrix is
অর্থাৎ \(|0\rangle অবস্থার দশা অপরিবর্তিত রেখে :math:\)-গেট, \(|1\rangle\) অবস্থায় \(\pi/2\) দশা যোগ করা হল।
নিচে আমরা অংশ ২ পরীক্ষা ৪ এর নামকরণ ও পদ ব্যবহার করব।
Let’s consider to estimate the phase \(\phi=\frac{\pi}{2}\) for the eigenstate \(|1\rangle\), we should find \(\varphi=\frac{1}{4}\) (where \(\phi = 2 \pi \varphi\)). Therefore to estimate the phase we need exactly 2 phase bits, i.e. \(m=2\), since \(1/2^2=1/4\). So \(\varphi=0.\varphi_1\varphi_2\).
আইপিই ধারাক্রম (অ্যালোগরিদম) এর থেকে মনে থাকবে, \(m\) হলো পুনরাবৃত্তির সংখ্যা, অর্থাৎ আমাদের \(2\) বার পুনরাবৃত্তি করতে হবে।
প্রথমে আমরা বর্তনী (সার্কিট) চালু করব। আইপিই কেবল একটা সহায়ক কিউবিট নিয়ে কাজ করে, যেখানে কিউপিই ধারাক্রম (অ্যালোগরিদম) \(m\) গুলো গণনাকারী কিউবিট ব্যবহার করে। সেজন্য আমাদের ২টি কিউবিট, ১ টা সহায়ক কিউবিট, \(U\) -গেটের আইজেনস্টেটের জন্য ১ টা কিউবিট এবং ২ বিটের একটা প্রথাগত (ক্লাসিক্যাল) রেজিস্টার (দশা বিট \(\varphi_1\), \(\varphi_2\) -এর জন্য) লাগবে।
[5]:
nq = 2
m = 2
q = QuantumRegister(nq, 'q')
c = ClassicalRegister(m, 'c')
qc_S = QuantumCircuit(q,c)
প্রথম ধাপ¶
Now we build the quantum circuit for the first step, that is, the first iteration of the algorithm, to estimate the least significant phase bit \(\varphi_m\), in this case \(\varphi_2\). For the first step we have 3 sub-steps: - initialization - application of the Controlled-\(U\) gates - measure of the auxiliary qubit in X-basis
আরম্ভ¶
আরম্ভের জন্য আমরা আইজেনস্টেট \(|1\rangle\) -এর প্রস্তুতি ও সহায়ক কিউবিটে হ্যাডামার্ড যুক্তিবর্তনী প্রয়োগ করতে হবে।
[6]:
qc_S.h(0)
qc_S.x(1)
qc_S.draw('mpl')
[6]:

Application of the Controlled-\(U\) gates¶
Then we have to apply \(2^t\) times the Controlled-\(U\) operators (see also in the docs Two qubit gates), that, in this example, is the Controlled-\(S\) gate (\(CS\) for short).
বর্তনীতে (সার্কিট) \(CS\) বাস্তবায়নের জন্য আমরা \(\theta=\pi/2\) -এর সাথে নিয়ন্ত্রিত দশা যুক্তিবর্তনী (গেইট) \(\text{CP}(\theta)\) ব্যবহার করতে পারি, যেহেতু \(S\) একটা দশা গেট।
[7]:
from math import pi
cu_circ = QuantumCircuit(2)
cu_circ.cp(pi/2, 0, 1)
cu_circ.draw('mpl')
[7]:

Let’s apply \(2^t\) times \(\text{CP}(\pi/2)\). Since for the first step \(t=m-1\), and \(m=2\), we have \(2^t=2\).
[8]:
for _ in range(2 ** (m - 1)):
qc_S.cp(pi/2, 0, 1)
qc_S.draw('mpl')
[8]:

Measure in X-basis¶
Finally, we perform the measurement of the auxiliary qubit in X-basis. So we will define a function to perform the x_measurement
and then apply it.
[9]:
def x_measurement(qc, qubit, cbit):
"""Measure 'qubit' in the X-basis, and store the result in 'cbit'"""
qc.h(qubit)
qc.measure(qubit, cbit)
এভাবে আমরা দশা বিট \(\varphi_2\) পাবো এবং সেটা প্রথাগত (ক্লাসিক্যাল) বিট \(c_0\) -এ সঞ্চয় করি।
[10]:
x_measurement(qc_S, q[0], c[0])
qc_S.draw('mpl')
[10]:

ক্রমানুসারে (২য় ধাপ)¶
Now we build the quantum circuit for the other remaining steps, in this example, only the second one. In these steps we have 4 sub-steps: the 3 sub-steps as in the first step and, in the middle, the additional step of the phase correction - initialization with reset - phase correction - application of the Control-\(U\) gates - measure of the auxiliary qubit in X-basis
পুনঃস্থাপন (রিসেট) দিয়ে আরম্ভ¶
As we want to perform an iterative algorithm in the same circuit, we need to reset the auxiliary qubit \(q_0\) after the measument gate and initialize it again as before to recycle the qubit.
[11]:
qc_S.reset(0)
qc_S.h(0)
qc_S.draw('mpl')
[11]:

দ্বিতীয় ধাপের জন্য দশা সংশোধন¶
As seen in the theory, in order to extract the phase bit \(\varphi_{1}\), we perform a phase correction of \(-\pi\varphi_2/2\). Of course, we need to apply the phase correction in the circuit only if the phase bit \(\varphi_2=1\), i.e. we have to apply the phase correction of \(-\pi/2\) only if the classical bit \(c_0\) is 1.
সেজন্য পুনঃস্থাপনের (রিসেট) পর c_if
পদ্ধতি ব্যবহার করে প্রথাগত (ক্লাসিক্যাল) বিট \(c_0\) (\(=\varphi_2\)) -এর শর্তাধীনে \(\theta=-\pi/2\) দশাসহ \(P(\theta)\) দশা গেট প্রয়োগ করব। এই পাঠসমূহের (টিউটোরিয়াল) শুরুতে দেখানো ১ মান নিয়ে c_if
পদ্ধতি ব্যবহার করব, যেহেতু \(1_{10} = 001_{2}\) (সাবস্ক্রিপ্ট \(_{10}\) and \(_2\) দশমিক ও দ্বিমিক (বাইনারি) রূপ চিহ্নিত করে)।
[12]:
qc_S.p(-pi/2, 0).c_if(c, 1)
qc_S.draw('mpl')
[12]:

Application of the Control-\(U\) gates and x-measurement (for step 2)¶
We apply the \(CU\) operations as we did in the first step. For the second step we have \(t=m-2\), hence \(2^t=1\). So we apply \(\text{CP}(\pi/2)\) once. And then we perform the X-measurement of the qubit \(q_0\), storing the result, the phase bit \(\varphi_1\), in the bit \(c_1\) of classical register.
[13]:
## 2^t c-U operations (with t=m-2)
for _ in range(2 ** (m - 2)):
qc_S.cp(pi/2, 0, 1)
x_measurement(qc_S, q[0], c[1])
আমরা অন্তিম বর্তনীটা (সার্কিট) পেলাম
[14]:
qc_S.draw('mpl')
[14]:

Let’s sample the circuit with Qiskit Aer’s Sampler
primitive, a local simulator without noise that runs locally.
[15]:
import matplotlib.pyplot as plt
from qiskit.tools.visualization import plot_histogram
from qiskit_aer.primitives import Sampler
sampler = Sampler()
job = sampler.run(qc_S)
result = job.result()
dist0 = result.quasi_dists[0]
key_new = [str(key/2**m) for key in list(dist0.keys())]
dist1 = dict(zip(key_new, dist0.values()))
fig, ax = plt.subplots(1,2)
plot_histogram(dist0, ax=ax[0])
plot_histogram(dist1, ax=ax[1])
plt.tight_layout()

এই ছবিতে একই হিস্টোগ্রাম পেলেও লক্ষ্য করো বাঁদিকে x-অক্ষে \(\varphi_1\), \(\varphi_2\) দশা বিটদুটো এবং ডানদিকে আসল দশা \(\varphi\) দশমিকরূপে আছে।
আশানুরূপভাবে আমরা \(100\%\) সম্ভাবনায় \(\varphi=\frac{1}{4}=0.25\) পেয়েছি।
২-কিউবিট যুক্তিবর্তনী (গেইট) সম্পন্ন আইপিই এর উদাহরণ¶
Now, we want to apply the IPE algorithm to estimate the phase for a 2-qubit gate \(U\). For this example, let’s consider the controlled version of the \(T\) gate, i.e. the gate \(U=\textrm{Controlled-}T\) (that from now we will express more compactly with \(CT\)). Its matrix is
অর্থাৎ \(CT\) গেট \(|11\rangle\) অবস্থায় \(\pi/4\) দশা যোগ করে। এই অবস্থায় অন্যান্য গণনার ভিত্তি অবস্থা \(|00\rangle\), \(|01\rangle\), \(|10\rangle\) -এর দশা অপরিবর্তিত থাকে।
Let’s consider to estimate the phase \(\phi=\pi/4\) for the eigenstate \(|11\rangle\), we should find \(\varphi=1/8\), since \(\phi = 2 \pi \varphi\). Therefore to estimate the phase we need exactly 3 classical bits, i.e. \(m=3\), since \(1/2^3=1/8\). So \(\varphi=0.\varphi_1\varphi_2\varphi_3\).
১-কিউবিট \(U\) অপারেটরের উদাহরণের মতো আমরা একই ধাপ অনুসরণ করব কিন্তু এবার আমাদের \(3\) গুলো ধাপ করবো কারণ \(m=3\)। এই ব্যাপারে বিশদে জানতে ১-কিউবিট \(U\) গেটের উদাহরণ দেখো।
প্রথমে আমরা ৩টি কিউবিট নিয়ে বর্তনী (সার্কিট) চালু করবো, ১টা সহায়ক কিউবিট, ২-কিউবিট গেটের জন্য ২টো। এছাড়াও আমরা ৩টি প্রথাগত (ক্লাসিক্যাল) বিট নেবো যাতে \(\varphi_1\), \(\varphi_2\), \(\varphi_3\) দশা বিটের মান সঞ্চয় হবে।
[16]:
nq = 3 # number of qubits
m = 3 # number of classical bits
q = QuantumRegister(nq,'q')
c = ClassicalRegister(m,'c')
qc = QuantumCircuit(q,c)
প্রথম ধাপ¶
এবার আমরা কম তাৎপর্যপূর্ণ দশা বিট \(\varphi_m=\varphi_3\) অনুমানের জন্য আমরা কোয়ান্টাম বর্তনী (সার্কিট) বানাবো।
আরম্ভ¶
আমরা সহায়ক কিউবিট ও অনান্য কিউবিট \(|11\rangle\) আইজেনস্টেট দিয়ে শুরু করি।
[17]:
qc.h(0)
qc.x([1, 2])
qc.draw('mpl')
[17]:

Application of the Controlled-\(U\) gates¶
Then we have to apply multiple times the \(CU\) operator, that, in this example, is the Controlled-\(CT\) gate (\(CCT\) for short).
বর্তনীতে (সার্কিট) \(CCT\) বাস্তবায়নের জন্য আমরা \(\theta=\pi/4\) -এর সাথে বহুদ্বারা নিয়ন্ত্রিত দশা গেট \(\text{MCP}(\theta)\) ব্যবহার করতে পারি, যেহেতু \(T\) একটা দশা গেট।
[18]:
cu_circ = QuantumCircuit(nq)
cu_circ.mcp(pi/4, [0, 1], 2)
cu_circ.draw('mpl')
[18]:

Let’s apply \(2^t\) times \(\text{MCP}(\pi/4)\). Since for the first step \(t=m-1\) and \(m=3\), we have \(2^t=4\).
[19]:
for _ in range(2 ** (m - 1)):
qc.mcp(pi/4, [0, 1], 2)
qc.draw('mpl')
[19]:

Measure in X-basis¶
Finally, we perform the measurement of the auxiliary qubit in X-basis. We can use the x_measurement
function defined above in the example for 1-qubit gate. In this way we have obtained the phase bit \(\varphi_3\) and stored it in the classical bit \(c_0\).
[20]:
x_measurement(qc, q[0], c[0])
qc.draw('mpl')
[20]:

ক্রমানুসারে (২য় ও ৩য় ধাপ)¶
এখন বাকি ধাপের (২য় ও ৩য়) জন্য কোয়ান্টাম বর্তনী (সার্কিট) বানাবো। প্রথম উদাহরণের মতো এখানেও দশা সংশোধনের অতিরিক্ত ধাপ আছে।
দ্বিতীয় ধাপের জন্য দশা সংশোধন¶
দশা বিট \(\varphi_{2}\) নিষ্কাশনের জন্য আমরা \(-\pi\varphi_3/2\) -এর ত্রুটি সংশোধন করব।
পুনঃস্থাপনের (রিসেট) পর আমরা \(\theta=-\pi/2\) দশা সম্পন্ন দশা গেট \(P(\theta)\) প্রয়োগ করবো, শর্ত হিসেবে প্রথাগত (ক্লাসিক্যাল) বিট \(c_0\) (\(=\varphi_3\))।
[22]:
qc.p(-pi/2, 0).c_if(c, 1)
qc.draw('mpl')
[22]:

Application of the Control-\(U\) gates and x-measurement (for step 2)¶
We apply the \(CU\) operations as we did in the first step. For the second step we have \(t=m-2\), hence \(2^t=2\). So we apply \(\text{MCP}(\pi/4)\) \(2\) times. And then we perform the X-measurement of the qubit \(q_0\), storing the phase bit \(\varphi_2\) in the bit \(c_1\).
[23]:
for _ in range(2 ** (m - 2)):
qc.mcp(pi/4, [0, 1], 2)
x_measurement(qc, q[0], c[1])
qc.draw('mpl')
[23]:

তৃতীয় ধাপের সব আংশিক ধাপ¶
৩য় এবং শেষ ধাপের জন্য আমরা ২য় ধাপের মতো করে সহায়ক কিউবিট পুনঃস্থাপন (রিসেট) এবং আরম্ভ করব।
Then at the 3rd step we have to perform the phase correction of \(-2\pi 0.0\varphi_{2}\varphi_{3}= -2\pi \left(\frac{\varphi_2}{4}+\frac{\varphi_3}{8}\right)=-\frac{\varphi_2\pi}{2}-\frac{ \varphi_3\pi}{4}\), thus we have to apply 2 conditioned phase corrections, one conditioned by \(\varphi_3\) (\(=c_0\)) and the other by \(\varphi_2\)(\(=c_1\)). To do this we have to apply the following: - gate \(P(-\pi/4)\) conditioned by \(c_0=1\), that is, by \(c=001\)
(c_if
with value \(1\)) - gate \(P(-\pi/2)\) conditioned by \(c_1=1\), that is, the gate is applied when \(c=010\) (c_if
with values \(2\)) - gate \(P(-3\pi/4)\) conditioned by \(c_1=1\) and \(c_0=1\) that is, the gate is applied when \(c=011\) (c_if
with values \(3\))
এরপর, \(CU\) ক্রিয়াকলাপ (অপারেশন): আমরা \(2^t\) বার \(\text{MCP}(\pi/4)\) গেট প্রয়োগ করব এবং যেহেতু ৩য় ধাপে \(t=m-3=0\), আমরা গেটটা একবার প্রয়োগ করতে পারবো।
[24]:
# initialization of qubit q0
qc.reset(0)
qc.h(0)
# phase correction
qc.p(-pi/4, 0).c_if(c, 1)
qc.p(-pi/2, 0).c_if(c, 2)
qc.p(-3*pi/2, 0).c_if(c, 3)
# c-U operations
for _ in range(2 ** (m - 3)):
qc.mcp(pi/4, [0, 1], 2)
# X measurement
qc.h(0)
qc.measure(0, 2)
qc.draw('mpl')
[24]:

এবার আমরা ত্রুটি ছাড়া বর্তনীটা (সার্কিট) সিমুলেটরে চালাবো।
[25]:
result = sampler.run(qc).result()
dist0 = result.quasi_dists[0]
key_new = [str(key/2**m) for key in list(dist0.keys())]
dist1 = dict(zip(key_new, dist0.values()))
fig, ax = plt.subplots(1,2)
plot_histogram(dist0, ax=ax[0])
plot_histogram(dist1, ax=ax[1])
plt.tight_layout()

আমরা অনুমানমতো \(\varphi=0.125\) (বা \(1/8\)) পাওয়ার সম্ভাবনা \(100\%\) পেয়েছি।
[26]:
import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright
Version Information
Qiskit Software | Version |
---|---|
qiskit-terra | 0.23.0.dev0+1b4fed3 |
qiskit-aer | 0.11.1 |
qiskit-nature | 0.5.0 |
System information | |
Python version | 3.9.13 |
Python compiler | Clang 12.0.0 |
Python build | main, Oct 13 2022 16:12:30 |
OS | Darwin |
CPUs | 4 |
Memory (Gb) | 32.0 |
Fri Dec 09 16:18:07 2022 CET |
This code is a part of Qiskit
© Copyright IBM 2017, 2022.
This code is licensed under the Apache License, Version 2.0. You may
obtain a copy of this license in the LICENSE.txt file in the root directory
of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
Any modifications or derivative works of this code must retain this
copyright notice, and modified files need to carry a notice indicating
that they have been altered from the originals.