Note
இந்தப் பக்கம் tutorials/algorithms/09_IQPE.ipynb இலிருந்து உருவாக்கப்பட்டது.
மறுபயன்பாட்டு குவாண்டம் கட்ட மதிப்பீடு அல்காரிதம்¶
இந்த பயிற்சிகளின் குறிக்கோள், படிநிலை கட்ட மதிப்பீடு (IPE) வழிமுறை எவ்வாறு செயல்படுகிறது, QPE (Quantum Phase Estimation) அல்காரிதத்திற்குப் பதிலாக நாம் ஏன் IPE அல்காரிதத்தைப் பயன்படுத்துவோம் மற்றும் அதே சர்க்யூட் சுரண்டல் மீட்டமைப்பு வாயிலைப் பயன்படுத்தி அதை எப்படி உருவாக்கலாம் முந்தைய அளவீடுகளின் விளைவாக கிளாசிக்கல் பதிவேட்டில் சேமிக்கப்பட்ட மதிப்புகளால் நிபந்தனைக்குட்பட்ட வாயில்களைப் பயன்படுத்த அனுமதிக்கும் 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
என்பது ஒரு பாரம்பரிய பதிவேட்டில் முன்பு சேமிக்கப்பட்ட மதிப்பின் அடிப்படையில் நிபந்தனைக்குட்பட்ட செயல்பாடுகளைச் செய்வதற்கான ஒரு செயல்பாடு (உண்மையில் கேட் வகுப்பின் முறை) ஆகும். இந்த அம்சத்தின் மூலம் அளவீட்டு முடிவுகளால் நிபந்தனைக்குட்பட்ட அதே சுற்றில் ஒரு அளவீட்டுக்குப் பிறகு நீங்கள் வாயில்களைப் பயன்படுத்தலாம்.
எடுத்துக்காட்டாக, பின்வரும் code கிளாசிக்கல் பதிவின் மதிப்பு என்றால் \(X\) கேட் \(0\).
[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.
தசம பிரதிநிதித்துவத்தில் 2 முழு எண் மதிப்புகள்:
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 ஐயும் காணலாம்.
IPE உதாரணம் :math:` U ` க்கு 1-க்யூபிட் வாய்ப்பாடு¶
1-குவிட் ஆபரேட்டருக்கான கட்டத்தை மதிப்பிடுவதற்கு IPE வழிமுறையைப் பயன்படுத்த விரும்புகிறோம் \(U\) எடுத்துக்காட்டாக, இங்கே நாம் \(S\)-கேட் பயன்படுத்துகிறோம்.
Let’s apply the IPE algorithm to estimate the phase for \(S\)-gate. Its matrix is
அதாவது, \(S\)-கேட் ஒரு கட்டத்தை சேர்க்கிறது \(\pi/2\) மாநிலத்திற்கு \(|1\rangle\), மாநிலத்தின் கட்டத்தை மாற்றாமல் விட்டுவிடுகிறது :math:`|0rangle
பின்வருவனவற்றில், Section 2 of lab 4 இல் பயன்படுத்தப்படும் குறியீடு மற்றும் சொற்களைப் பயன்படுத்துவோம்.
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\).
IPE வழிமுறையைப் பொறுத்தவரை, \(m\) என்பது மறு செய்கைகளின் எண்ணிக்கையாகும், எனவே நமக்கு மட்டும் தேவை \(2\) மறு செய்கைகள் அல்லது படிகள்.
முதலில், நாங்கள் சுற்று துவக்குகிறோம். IPE வழிமுறையின் \(m\) எண்ணும் குவிட்களுக்குப் பதிலாக, 1 துணை துணைக் கருவியுடன் மட்டுமே IPE செயல்படுகிறது. ஆகையால், கட்டத்தின் பிட்களுக்கு \(\varphi_1\) , \(U\)- கேட் மற்றும் 2 பிட்களின் கிளாசிக்கல் பதிவு \(\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\) சுற்றுவட்டத்தில், என்பதால் \(S\) என்பது ஒரு கட்ட வாயில், நாம் கட்டுப்படுத்தப்பட்ட கட்ட வாயிலைப் பயன்படுத்தலாம் \(\text{CP}(\theta)\), உடன் \(\theta=\pi/2\).
[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]:

அடுத்தடுத்த படிகள் (2 வது படி)¶
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]:

கட்ட திருத்தம் (படி 2 க்கு)¶
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.
எனவே, மீட்டமைத்த பின் கட்டம் \(P(\theta)\) கட்டம் \(\theta=-\pi/2\) கிளாசிக்கல் பிட் \(c_0\) (\(=\varphi_2\)) c_if
முறையைப் பயன்படுத்துகிறது. இந்த டுடோரியலின் முதல் பகுதியில் நாம் பார்த்தது போல், 1 மதிப்புடன் c_if
முறையைப் பயன்படுத்த வேண்டும் \(1_{10} = 001_{2}\) (சந்தாக்கள் \(_{10}\) மற்றும் \(_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])
Et voilà, எங்கள் இறுதி சுற்று உள்ளது
[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\).
நாங்கள் எதிர்பார்த்தபடி \(\varphi=\frac{1}{4}=0.25\) உடன் \(100\%\) நிகழ்தகவு.
IPE உதாரணம் 2-கிபிட் வாயில்¶
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\) கேட் \(\pi/4\) என கட்டத்தை \(|11\rangle\) உடன் சேர்க்கிறது, அதை போல \(|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\).
1-குவிட் எடுத்துக்காட்டுடன் செய்யப்பட்டுள்ளபடி \(U\) ஆபரேட்டர் நாங்கள் அதே படிகளைச் செல்வோம், ஆனால் இந்த முறை நமக்கு இருக்கும் \(3\) படிகள் \(m=3\), மற்றும் நாங்கள் எல்லா விளக்கங்களையும் மீண்டும் செய்ய வேண்டாம். எனவே விவரங்களுக்கு 1-குவிட் \(U\) கேட் மேலே உள்ள உதாரணத்தைக் காண்க.
முதலில், 3 க்யூபிட், 1 துணை க்யூபிட் மற்றும் 2-க்யூபிட் கேட், மற்றும் 3 கிளாசிக்கல் பிட்கள், கட்ட பிட்களை சேமித்து வைப்பதற்கான சர்க்யூட்டை துவக்குகிறோம் \(\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\).
துவக்கம்¶
Eigenstate \(|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\) சுற்று, என்பதால் \(T\) என்பது ஒரு கட்ட வாயில், நாம் பல கட்டுப்பாட்டு கட்ட வாயிலைப் பயன்படுத்தலாம் \(\text{MCP}(\theta)\), உடன் \(\theta=\pi/4\).
[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]:

அடுத்தடுத்த படிகள் (2 வது, 3 வது)¶
இப்போது மீதமுள்ள மற்ற படிகளுக்கு குவாண்டம் சர்க்யூட்டை உருவாக்குகிறோம், இரண்டாவது மற்றும் மூன்றாவது. முதல் எடுத்துக்காட்டில் கூறியது போல், இந்த படிகளில் கட்ட திருத்தத்தின் கூடுதல் துணை-படி உள்ளது.
கட்ட திருத்தம் (படி 2 க்கு)¶
கட்ட பிட் பிரித்தெடுப்பதற்காக \(\varphi_{2}\), \(-\pi\varphi_3/2\) இன் ஒரு கட்ட திருத்தம் செய்கிறோம்.
எனவே, மீட்டமைத்த பின் கட்டம் \(P(\theta)\) கட்டம் \(\theta=-\pi/2\) கிளாசிக்கல் பிட் \(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]:

3 வது படியின் அனைத்து மாற்றுகளும்¶
3 வது மற்றும் கடைசி கட்டத்திற்கு, இரண்டாவது கட்டத்தில் செய்ததைப் போல துணை குவிட்டின் மீட்டமைப்பு மற்றும் துவக்கத்தை நாங்கள் செய்கிறோம்.
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)\) கேட் மற்றும் 3 வது கட்டத்திலிருந்து \(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()

நாங்கள் பெற்றுள்ளோம் \(100\%\) கண்டுபிடிக்க நிகழ்தகவு \(\varphi=0.125\), அதாவது, \(1/8\), எதிர்பார்த்தபடி.
[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.