참고
이 페이지는 tutorials/simulators/4_custom_gate_noise.ipynb 에서 생성되었다.
맞춤 유니터리 게이트를 노이즈로 적용하기¶
소개¶
이 노트북에서는 양자 회로에 맞춤 유니터리 게이트를 추가하는 방법을 살펴보고 이를 Qiskit Aer의 노이즈 시뮬레이션에 사용해 본다.
[1]:
from qiskit import transpile, QuantumCircuit
import qiskit.quantum_info as qi
from qiskit_aer import AerSimulator
from qiskit_aer.noise import NoiseModel, amplitude_damping_error
from qiskit.tools.visualization import plot_histogram
행렬 연산자 만들기¶
우리는 임의의 행렬 연산자를 사용할 때 qiskit.quantum_info
안의 Operator
클래스를 사용할 수 있다. 그 연산자가 유니터리이면 양자 회로에 추가하여 Qiskit Aer의 시뮬레이션에 사용할 수 있다.
다음과 같은 CNOT 게이트와 iSWAP 게이트를 만들어 보자.
[2]:
# CNOT matrix operator with qubit-0 as control and qubit-1 as target
cx_op = qi.Operator([[1, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0],
[0, 1, 0, 0]])
# iSWAP matrix operator
iswap_op = qi.Operator([[1, 0, 0, 0],
[0, 0, 1j, 0],
[0, 1j, 0, 0],
[0, 0, 0, 1]])
참고: 행렬은 목록 [a, b]
에 의해 지정된 큐비트에 대한 텐서 곱 \(U_{b}\otimes U_{a}\) 과 관련하여 지정된다.
회로에서 연산자 사용하기¶
회로에서 이러한 기능을 사용할 수 있는 방법을 시연해 보겠다. 우리는 단일 쿼비트 게이트와 iSWAP 게이트 측면에서 분해된 CNOT 게이트를 구현하는 예를 다음과 같이 고려할 것이다.
[3]:
# CNOT in terms of iSWAP and single-qubit gates
cx_circ = QuantumCircuit(2, name='cx<iSWAP>')
# Add gates
cx_circ.sdg(1)
cx_circ.h(1)
cx_circ.sdg(0)
cx_circ.unitary(iswap_op, [0, 1], label='iswap')
cx_circ.sdg(0)
cx_circ.h(0)
cx_circ.sdg(0)
cx_circ.unitary(iswap_op, [0, 1], label='iswap')
cx_circ.s(1)
print(cx_circ)
┌─────┐ ┌────────┐┌─────┐┌───┐┌─────┐┌────────┐
q_0: ┤ SDG ├─────┤0 ├┤ SDG ├┤ H ├┤ SDG ├┤0 ├─────
├─────┤┌───┐│ iswap │└─────┘└───┘└─────┘│ iswap │┌───┐
q_1: ┤ SDG ├┤ H ├┤1 ├───────────────────┤1 ├┤ S ├
└─────┘└───┘└────────┘ └────────┘└───┘
단위가 삽입될 때 iswap
의 선택적인 레이블 을 유니테리에 할당했다는 점에 유의한다. 이를 통해 우리는 Qiskit Air Noise Model
에서 이 유니테리를 식별하여 소음 회로 시뮬레이션에서 이러한 맞춤형 유니터리 게이트에 오류를 추가할 수 있다.
Operator
클래스를 회로의 시뮬레이터로 사용하여 이 회로가 올바른 출력을 반환하는지 확인한다.
[4]:
# Simulate the unitary for the circuit using Operator:
unitary = qi.Operator(cx_circ)
print(unitary)
Operator([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
[0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],
[0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
[0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]],
input_dims=(2, 2), output_dims=(2, 2))
또한 출력이 올바른지 확인하기 위해 평균 게이트 신뢰도를 계산할 수 있다.
[5]:
f_ave = qi.average_gate_fidelity(cx_op, unitary)
print("Average Gate Fidelity: F = {:f}".format(f_ave))
Average Gate Fidelity: F = 1.000000
노이즈 모델에서 맞춤 유니터리 만들기¶
Qiskit Aer의 AerSimulator
는 기저 게이트에서 "unitary"
로 지정된 임의의 단위 상태 연산자의 시뮬레이션을 직접 지원한다.
[6]:
'unitary' in AerSimulator().configuration().basis_gates
[6]:
True
이를 통해 QuantumCircuit.unitary
의 선택적인 label
인수를 사용하여 이를 식별할 때 시뮬레이션에서 임의 유니터리에 노이즈 모델을 추가할 수 있다.
이제 사용자 지정 iSWAP 게이트에 양자 오류 채널이 포함된 NoiseModel
을 만들어 이를 수행할 것이다. 이 예에서는 서로 다른 감폭 매개 변수를 가진 2개의 단일 큐비트 진폭 감폭 채널로 구성된 2-큐비트 오류를 생성한다. 지금은 다른 모든 회로 지침 이상적인 것으로 가정한다.
[7]:
# Error parameters
param_q0 = 0.05 # damping parameter for qubit-0
param_q1 = 0.1 # damping parameter for qubit-1
# Construct the error
qerror_q0 = amplitude_damping_error(param_q0)
qerror_q1 = amplitude_damping_error(param_q1)
iswap_error = qerror_q1.tensor(qerror_q0)
# Build the noise model by adding the error to the "iswap" gate
noise_model = NoiseModel()
noise_model.add_all_qubit_quantum_error(iswap_error, 'iswap')
참고로 "iswap"
과 같은 사용자 정의 레이블에 오류를 추가할 때 Noise Model
은 이 레이블이 적용되는 게이트를 모르므로 원하는 게이트 문자열을 노이즈 모델 basis_gates
에 수동으로 추가해야 한다. 이를 통해 컴파일러가 노이즈 모델 시뮬레이션을 위한 올바른 기본 게이트로 롤아웃할 수 있다. 이 작업은 〈NoiseModel.add_basis_gates
〉 함수를 사용하여 수행할 수 있다.
[8]:
noise_model.add_basis_gates(['unitary'])
print(noise_model.basis_gates)
['cx', 'id', 'u3', 'unitary']
기본적으로 노이즈 모델의 기저 게이트는 ['cx','id','u3']
과 노이즈 모델에 추가된 기본 AerSimulator
기저 게이트이다.
맞춤 유니터리 노이즈 모델의 시뮬레이션¶
먼저 이전 CX 회로를 사용하고 초기 하다마드 게이트와 최종 측정을 추가하여 이상적인 경우와 시끄러운 경우 모두를 Aer Simulator
에서 시뮬레이터로 사용할 수 있는 벨 상태 준비 회로를 만들자.
[9]:
# Bell state circuit where iSWAPS should be inserted at barrier locations
bell_circ = QuantumCircuit(2, 2, name='bell')
bell_circ.h(0)
bell_circ.append(cx_circ, [0, 1])
bell_circ.measure([0,1], [0,1])
print(bell_circ)
┌───┐┌────────────┐┌─┐
q_0: ┤ H ├┤0 ├┤M├───
└───┘│ cx<iSWAP> │└╥┘┌─┐
q_1: ─────┤1 ├─╫─┤M├
└────────────┘ ║ └╥┘
c: 2/════════════════════╩══╩═
0 1
이상적인 출력¶
Let’s first see the ideal output. Since this generates a Bell-state we expect two peaks for 00 and 11.
[10]:
# Create ideal simulator backend and transpile circuit
sim_ideal = AerSimulator()
tbell_circ = transpile(bell_circ, sim_ideal)
ideal_result = sim_ideal.run(tbell_circ).result()
ideal_counts = ideal_result.get_counts(0)
plot_histogram(ideal_counts,
title='Ideal output for iSWAP bell-state preparation')
[10]:

노이즈가 있는 회로의 실행¶
Finally, let’s now simulate it with our custom noise model. Since there is a small amplitude damping error on the two-qubit gates we expect small additional peaks for the 01 and 10 outcome probabilities.
[11]:
# Create noisy simulator and transpile circuit
sim_noise = AerSimulator(noise_model=noise_model)
tbell_circ_noise = transpile(bell_circ, sim_noise)
# Run on the simulator without noise
noise_result = sim_noise.run(tbell_circ_noise).result()
noise_counts = noise_result.get_counts(bell_circ)
plot_histogram(noise_counts,
title='Noisy output for iSWAP bell-state preparation')
[11]:

[12]:
import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright
Version Information
Qiskit Software | Version |
---|---|
Qiskit | 0.25.0 |
Terra | 0.17.0 |
Aer | 0.8.0 |
Ignis | 0.6.0 |
Aqua | 0.9.0 |
IBM Q Provider | 0.12.2 |
System information | |
Python | 3.7.7 (default, May 6 2020, 04:59:01) [Clang 4.0.1 (tags/RELEASE_401/final)] |
OS | Darwin |
CPUs | 6 |
Memory (Gb) | 32.0 |
Fri Apr 02 12:12:41 2021 EDT |
This code is a part of Qiskit
© Copyright IBM 2017, 2021.
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.
[ ]: