注釈
このページは 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つの演算子を以下で作成してみましょう。
[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 ├
└─────┘└───┘└────────┘ └────────┘└───┘
ユニタリーを追加した際に、オプションの label に "iswap"
を指定したことに着目してください。これにより、Qiskit Aer NoiseModel
でこのユニタリーを識別することができ、ノイズがある回路シミュレーションでこれらのカスタムユニタリーゲートにエラーを追加することができます。
回路のシミュレーターとして、 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"
のようなカスタムラベルにエラーを追加すると、NoiseModel
はこのラベルが適用されるべきゲートを知らないことに注意してください。 ですから、ノイズモデルの 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回路を取り上げて、最初のアダマールゲートと最後の測定を追加して、理想的な場合とノイズがある場合について AerSimulator
上でシミュレートできるベル状態準備回路を作成しましょう。
[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
理想的な出力¶
最初に理想的な出力を見てみましょう。これは Bell 状態を生成するので、00と11の2つのピークが期待されます。
[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]:

ノイズがある回路の実行¶
最後に、カスタムノイズモデルでシミュレートしましょう。 2量子ビットゲートには小さな振幅減衰誤差があるので、01および10の結果の可能性については、小さなピークが追加されることが予想されます。
[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.
[ ]: