본문 바로가기

Quantum Computing

Qiskit을 이용한 양자 컴퓨팅 예제 (3) - 아다마르 게이트 (Hadamard Gate)

반응형

이번 포스팅에서는 양자 컴퓨팅에서 아주 주용한 역할을 하는 아다마르 게이트 (Hadamard Gate)에 대해 알아보겠습니다.

 

아다마르 게이트는 다음과 같은 $2\times 2$ 행렬로 표현이 됩니다:

$$ {\pmb H} = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}$$

 

이러한 게이트를 큐비트에 적용해 보면 다음과 같은 결과가 나옵니다:

$$ {\pmb H} | 0 \rangle = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} \begin{pmatrix} 1 \\ 0 \end{pmatrix} = \frac{1}{\sqrt{2}} \left[ \begin{pmatrix} 1 \\ 0 \end{pmatrix} + \begin{pmatrix} 0 \\ 1 \end{pmatrix} \right] = \frac{1}{\sqrt{2}} \left[ | 0 \rangle + | 1 \rangle \right] \equiv | + \rangle $$

또는

$$ {\pmb H} | 1 \rangle = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} \begin{pmatrix} 0 \\ 1 \end{pmatrix} = \frac{1}{\sqrt{2}} \left[ \begin{pmatrix} 1 \\ 0 \end{pmatrix} - \begin{pmatrix} 0 \\ 1 \end{pmatrix} \right] = \frac{1}{\sqrt{2}} \left[ | 0 \rangle - | 1 \rangle \right] \equiv | - \rangle .$$

 

즉, 아다마르 게이트는 양자 중첩 상태를 형성한다는 점에서 양자 컴퓨팅에서 매우 중요한 역할을 합니다. 

 

Qiskit을 이용해 아마다르 게이트를 생성해 보겠습니다:

from qiskit import QuantumCircuit, transpile
from qiskit.visualization import plot_bloch_multivector, plot_histogram
from qiskit_aer import AerSimulator
import matplotlib.pyplot as plt
 
# 양자 회로 생성
qc = QuantumCircuit(1)  # 1개의 큐비트 생성
 
# Hadamard 게이트 적용
qc.h(0)  
 
# 상태 벡터 저장
qc.save_statevector()
 
# 시뮬레이터 초기화
simulator = AerSimulator()
 
# 회로 컴파일 및 실행
compiled_circuit = transpile(qc, simulator)
result = simulator.run(compiled_circuit).result()

# 상태 시각화
bloch_sphere = plot_bloch_multivector(statevector)  # Matplotlib 객체 반환
bloch_sphere.savefig("bloch_sphere.png", dpi=300)  # PNG로 저장
 
# Matplotlib 스타일로 회로 플롯
fig = qc.draw(output="mpl")  # Matplotlib 객체 반환
fig.savefig("quantum_circuit.png", dpi=300)  # PNG로 저장

 

코드를 실행하면, 생성된 큐비트 $|0\rangle$ 상태를 아마다르 게이트에 의해 $|0\rangle$과 $\ 1 \rangle$ 상태의 중첩된 값으로 변하는 것을 확인할 수 있습니다. 코드 실행으로부터 얻은 결과들은 아래와 같습니다:

아마다르 게이트가 적용된 양자 회로도
아다마르 게이트로 인해 양자 중첩 상태로 변화한 모습을 블로흐 구 상에서 표현한 그림.

 

반응형