Korean
언어
English
Bengali
French
German
Japanese
Korean
Portuguese
Spanish
Tamil

참고

이 페이지는 tutorials/algorithms/09_IQPE.ipynb 로부터 생성되었다.

Iterative Quantum Phase Estimation (반복 양자 위상 추정) 알고리즘

이 튜토리얼의 목표는 Iterative Phase Estimation (IPE, 반복 위상 추정) 알고리즘이 어떻게 작동되는지, 왜 우리가 QPE (Quantum Phase Estimation) 가 아니라 IPE 알고리즘을 사용해야 하는지, 그리고 어떻게 Qiskit을 사용해서 이 알고리즘을 생성할 수 있는지를 이해하는 것이다. 이 알고리즘을 생성할 때에는 초기화 게이트와 c_if 메서드를 포함한 회로를 사용하면 되는데, 이 c_if 메서드는 이전의 측정들에서 비롯된 고전 레지스터에 저장된 값들로 조절된 게이트들을 적용할 수 있게 한다.

참조

조건부 게이트: c_if 메서드

IPE 알고리즘을 시작하기 전에, IPE 회로를 만들어 내는 Qiskit 조건부 메서드 c_if 에 관한 간단한 튜토리얼을 제공할 것이다.

(사실상 게이트 클래스의 메서드인) c_if 는 고전 레지스터에 저장된 값을 기반으로 조건부 연산을 하는 함수이다. 이 기능으로 회로의 측정 이후에 측정 결과를 조건부로 가지는 게이트를 적용할 수 있다.

예를 들어, 다음의 코드는 고전 레지스터의 값이 \(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]:
../../_images/tutorials_algorithms_09_IQPE_3_0.png

강조할만한 것은 메서드 c_if 는 하나의 고전 비트 (또는 고전 비트의 리스트) 가 아닌, 전체 고전 레지스터를 첫 번째 인수로 가지고, 하나의 비트값 0 또는 1 (또는 이진수 리스트/문자열) 이 아닌 (음수가 아닌) 십진수 표현 값을 두 번째 인수로 가질 것으로 기대한다는 것이다.

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]:
../../_images/tutorials_algorithms_09_IQPE_5_0.png

\(q_0\)\(q_1\) 인 경우에만 \(X\) 게이트를 적용할 것이다. 이것은 c_if 메서드를 이용하여 c_if 에 전달되는 인자 값에 따라 조건부로 \(X\) 를 적용하도록 할 수 있다.

\(q_2\) 가 무엇으로 측정되는지는 중요하지 않기 때문에 (이진 표현에서) 011 및 111의 값을 확인하도록 c_if 메서드에 전달될 값을 암호화해야 한다.

두 정숫값의 십진수 표현은 다음과 같다:

8477356533704be095d54288da2f6948

Python의 bin() 메서드를 통해 답을 확인할 수 있다 (접두어 0b 는 이진수 형식을 나타낸다).

[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]:
../../_images/tutorials_algorithms_09_IQPE_11_0.png

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, 반복 위상 추정) 알고리즘 에서 찾을 수 있다. QPE를 심도 있게 이해하려면 Ch.3.6 Quantum Phase Estimation (양자 위상 추정) 에서 볼 수 있다.

\(U\) 에 대한 1-큐비트 게이트의 IPE 예제

1-큐비트 연산자 \(U\) 의 위상을 추정하고자 IPE 알고리즘을 적용하고자 한다. 예를 들어, 여기서는 \(S\)-게이트를 사용한다.

Let’s apply the IPE algorithm to estimate the phase for \(S\)-gate. Its matrix is

\[\begin{split} S = \begin{bmatrix} 1 & 0\\ 0 & e^\frac{i\pi}{2}\\ \end{bmatrix}\end{split}\]

이 말은 \(S\)-게이트는 \(|1\rangle\) 상태에 위상 \(\pi/2\) 을 더하고, \(|0\rangle\) 상태의 위상은 변하지 않게 내버려 둔다

\[S|1\rangle = e^\frac{i\pi}{2}|1\rangle\]

다음부터는 랩 4의 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\).

IPE 알고리즘에서 \(m\) 는 반복 회수이기도 한 점을 기억하라. 따라서 \(2\) 번의 반복 또는 단계가 필요하다.

먼저 회로를 초기화한다. IPE는 QPE 알고리즘에서 \(m\) 개의 카운팅 큐비트 (counting qubit) 대신, 1개의 보조 큐비트로 동작한다. 그러므로 1개의 보조 큐비트와 1개의 \(U\)-게이트의 고유 상태인 총 2개의 큐비트와 페이즈 비트 \(\varphi_1\), :math:`varphi_2`를 위한 고전 레지스터 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

초기화

초기화는 Hadamard 게이트를 보조 큐비트에 적용하는 것과 고유 상태 \(|1\rangle\) 를 준비하는 것으로 구성되어 있다.

[6]:
qc_S.h(0)
qc_S.x(1)
qc_S.draw('mpl')
[6]:
../../_images/tutorials_algorithms_09_IQPE_15_0.png

Controlled-\(U\) 게이트의 적용

그러면 Controlled-\(U\) 연산자들을 \(2^t\) 번 적용해야 한다 (문서 두 큐비트 게이트 를 참조하라). 여기서는 Controlled-\(S\) 게이트 (줄여서 \(CS\)) 를 적용한다.

\(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]:
../../_images/tutorials_algorithms_09_IQPE_17_0.png

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]:
../../_images/tutorials_algorithms_09_IQPE_19_0.png

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]:
../../_images/tutorials_algorithms_09_IQPE_23_0.png

후속 단계 (두 번째 단계)

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]:
../../_images/tutorials_algorithms_09_IQPE_25_0.png

위상 보정(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.

따라서 초기화 후에는 c_if 매서드를 사용하여 고전 비트 \(c_0\) (\(=\varphi_2\)) 로 조절된 위상 \(\theta=-\pi/2\) 를 사용하여 위상 게이트 \(P(\theta)\) 를 적용한다. 이 튜토리얼의 첫 번째 부분에서 확인한 것처럼, 값이 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]:
../../_images/tutorials_algorithms_09_IQPE_27_0.png

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]:
../../_images/tutorials_algorithms_09_IQPE_31_0.png

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()
../../_images/tutorials_algorithms_09_IQPE_33_0.png

그림에서 히스토그램은 동일하지만 왼쪽에는 X축에 위상 비트 \(\varphi_1\) , \(\varphi_2\) 가 있는 문자열이 있고 오른쪽에는 10진수 표시의 실제 위상 \(\varphi\) 가 있다.

예상했던 대로 \(100\%\) 확률을 가진 \(\varphi=\frac{1}{4}=0.25\) 를 찾았다.

2-큐비트 게이트의 IPE 예제

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

\[\begin{split} CT = \begin{bmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & e^\frac{i\pi}{4}\\ \end{bmatrix}\end{split}\]

즉, \(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\).

1큐비트 \(U\) 연산자의 예와 마찬가지로 동일한 단계를 거치지만 \(m=3\) 로 인한 \(3\) 단계를 거치게 되며 모든 설명을 반복하지는 않을 것이다. 자세한 내용은 위의 1큐비트 \(U\) 게이트 예를 참조하자.

먼저, 3개의 큐비트 (보조 큐비트용 1개 및 2-큐비트 게이트용 2개)와 위상 비트 \(\varphi_1\), \(\varphi_2\), \(\varphi_3\) 를 저장하는 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]:
../../_images/tutorials_algorithms_09_IQPE_38_0.png

Controlled-\(U\) 게이트의 적용

그런 다음 \(CU\) 연산자를 여러 번 적용해야 한다. 이 예에서는 Controlled-\(CT\) 게이트(줄여서 \(CCT\)) 이다.

\(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]:
../../_images/tutorials_algorithms_09_IQPE_40_0.png

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]:
../../_images/tutorials_algorithms_09_IQPE_42_0.png

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]:
../../_images/tutorials_algorithms_09_IQPE_44_0.png

후속 단계 (2차, 3차)

이제 나머지 단계, 즉 두 번째 단계와 세 번째 단계를 위한 양자 회로를 구축한다. 첫 번째 예에서 말했듯이 이 단계에는 위상 보정의 추가 하위 단계가 있다.

재설정을 통한 초기화

[21]:
qc.reset(0)
qc.h(0)
qc.draw('mpl')
[21]:
../../_images/tutorials_algorithms_09_IQPE_46_0.png

위상 보정(2단계)

위상 비트 \(\varphi_{2}\) 를 추출하기 위해 \(-\pi\varphi_3/2\) 의 위상 보정을 실시한다.

따라서 재설정 후 고전 비트 \(c_0\) (\(=\varphi_3\)) 로 조절된 위상 \(\theta=-\pi/2\) 를 사용하여 위상 게이트 \(P(\theta)\) 를 적용한다.

[22]:
qc.p(-pi/2, 0).c_if(c, 1)
qc.draw('mpl')
[22]:
../../_images/tutorials_algorithms_09_IQPE_48_0.png

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]:
../../_images/tutorials_algorithms_09_IQPE_50_0.png

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\) 연산자: \(\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]:
../../_images/tutorials_algorithms_09_IQPE_52_0.png

이제 잡음 없이 시뮬레이터로 회로를 실행한다.

[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()
../../_images/tutorials_algorithms_09_IQPE_54_0.png

예상대로 \(\varphi=0.125\), 즉 \(1/8\) 를 찾을 수 있는 \(100\%\) 확률을 얻었다.

[26]:
import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright

Version Information

Qiskit SoftwareVersion
qiskit-terra0.23.0.dev0+1b4fed3
qiskit-aer0.11.1
qiskit-nature0.5.0
System information
Python version3.9.13
Python compilerClang 12.0.0
Python buildmain, Oct 13 2022 16:12:30
OSDarwin
CPUs4
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.