注釈
このページは tutorials/simulators/6_extended_stabilizer_tutorial.ipynb から生成されました。
拡張スタビライザーシミュレーション¶
はじめに¶
Extended Simulatorは、Qiskit-Aer の最新リリースで利用可能な量子回路を古典的にシミュレートするための新しいメソッドです。
本手法は、Bravyi, Browne, Calpin, Campbell, Gosset & Howard 2018 らによる Simulation of quantum circuits by low-rank stabilizer decompositions by Bravyi, Browne, Calpin, Campbell, Gosset & Howard, 2018, arXiv:1808.00128 論文に掲載されたアイデアを実装したものです。
それは量子回路の異なる表現を使用し、それはいくつかのユニークな機能を与えます。 このノートブックは、拡張スタビライザ方式ができることのいくつかの例を示します。
例:
[1]:
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from qiskit.tools.visualization import plot_histogram
import random
[2]:
circ = QuantumCircuit(40, 40)
# Initialize with a Hadamard layer
circ.h(range(40))
# Apply some random CNOT and T gates
qubit_indices = [i for i in range(40)]
for i in range(10):
control, target, t = random.sample(qubit_indices, 3)
circ.cx(control, target)
circ.t(t)
circ.measure(range(40), range(40))
[2]:
<qiskit.circuit.instructionset.InstructionSet at 0x7fbd18a23310>
私たちは、わずか60ゲートのランダム回路を作成し、40量子ビットに対して動作します。 しかし量子ビットの数のために、状態ベクトルシミュレーターなどでこれを実行したい場合は、テラバイトのRAMにアクセスできることが期待されています。
[3]:
# Create statevector method simulator
statevector_simulator = AerSimulator(method='statevector')
# Transpile circuit for backend
tcirc = transpile(circ, statevector_simulator)
# Try and run circuit
statevector_result = statevector_simulator.run(tcirc, shots=1).result()
print('This succeeded?: {}'.format(statevector_result.success))
print('Why not? {}'.format(statevector_result.status))
Simulation failed and returned the following error message:
ERROR: [Experiment 0] Insufficient memory to run circuit "circuit-2" using the statevector simulator.
This succeeded?: False
Why not? ERROR: [Experiment 0] Insufficient memory to run circuit "circuit-2" using the statevector simulator.
対照的に、Extended Stabilizerメソッドはこの回路をうまく処理します(数分かかりますが)。
[4]:
# Create extended stabilizer method simulator
extended_stabilizer_simulator = AerSimulator(method='extended_stabilizer')
# Transpile circuit for backend
tcirc = transpile(circ, extended_stabilizer_simulator)
extended_stabilizer_result = extended_stabilizer_simulator.run(tcirc, shots=1).result()
print('This succeeded?: {}'.format(extended_stabilizer_result.success))
This succeeded?: True
これはどのように動作しているのでしょうか?¶
このような大きな回路をどのように正確に扱えるか興味があるなら、詳細な説明をするために 論文を読むことができます。
しかし回路を動作させるためには、基本を理解することが重要です。
Extended Stabilizerメソッドは2つの部分から構成されています。 1つ目は、量子回路を スタビライザ回路 に分解する方法で、古典的に効率的にシミュレーションできる特別なクラスの回路です。 次に、これらの回路を組み合わせて測定を行う方法です。
あなたが必要とする項目数は、私たちが 非Clifford Gate と呼ぶものの数に比例します。 現時点では、メソッドは次のメソッドを処理する方法を知っています。
circ.t(qr[qubit])
circ.tdg(qr[qubit])
circ.ccx(qr[control_1], qr[control_2], qr[target])
circ.u1(rotation_angle, qr[qubit])
シミュレータは最大63量子ビットの回路を処理することもできます。
注意すべき重要な点は、これらの分解は近似であるということです。 これは、状態ベクトルシミュレーターと同じ結果ではないことを意味します。
[5]:
small_circ = QuantumCircuit(2, 2)
small_circ.h(0)
small_circ.cx(0, 1)
small_circ.t(0)
small_circ.measure([0, 1], [0, 1])
# This circuit should give 00 or 11 with equal probability...
expected_results ={'00': 50, '11': 50}
[6]:
tsmall_circ = transpile(small_circ, extended_stabilizer_simulator)
result = extended_stabilizer_simulator.run(
tsmall_circ, shots=100).result()
counts = result.get_counts(0)
print('100 shots in {}s'.format(result.time_taken))
100 shots in 0.4958779811859131s
[7]:
plot_histogram([expected_results, counts],
legend=['Expected', 'Extended Stabilizer'])
[7]:

Qiskit Aerの extended_stabilizer_approximation_error
を使用して、この近似エラーを制御できます。 デフォルトのエラーは0.05です。 エラーが小さいほど、結果はより正確になりますが、シミュレーションにかかる時間が長くなり、より多くのメモリが必要になります。
[8]:
# Add runtime options for extended stabilizer simulator
opts = {'extended_stabilizer_approximation_error': 0.03}
reduced_error = extended_stabilizer_simulator.run(
tsmall_circ, shots=100, **opts).result()
reduced_error_counts = reduced_error.get_counts(0)
print('100 shots in {}s'.format(reduced_error.time_taken))
plot_histogram([expected_results, reduced_error_counts],
legend=['Expected', 'Extended Stabilizer'])
100 shots in 1.404871940612793s
[8]:

シミュレーターオプション¶
拡張スタビライザメソッドがどのように機能するかを制御するために微調整することができるいくつかの他のオプションがあります。 これらのオプションとその説明はすべて、Qiskit Aer のドキュメントに記載されています。 しかし、シミュレーションを最適化するのに役立つ2つの重要なものを強調したいと思います。
測定を行うために、拡張スタビライザ法はマルコフ連鎖法を使用してランダムに結果をサンプルします。 このマルコフ連鎖は、サンプリングが開始される前に「混合時間」と呼ばれるしばらくの間実行する必要があります。 回路ショットごとに再混合する必要があります。
回路出力がわずか数個の出力状態に集中することを期待している場合、extended_stabilizer_mixing_time
オプションを減らすことでシミュレーションを最適化できます。
[9]:
print("The circuit above, with 100 shots at precision 0.03 "
"and default mixing time, needed {}s".format(int(reduced_error.time_taken)))
opts = {
'extended_stabilizer_approximation_error': 0.03,
'extended_stabilizer_mixing_time': 100
}
optimized = extended_stabilizer_simulator.run(
tsmall_circ, shots=100, **opts).result()
print('Dialing down the mixing time, we completed in just {}s'.format(optimized.time_taken))
The circuit above, with 100 shots at precision 0.03 and default mixing time, needed 1s
Dialing down the mixing time, we completed in just 1.4710919857025146s
同様に、回路のすべての振幅でゼロ以外の確率がある場合 (たとえば、ランダム回路の場合) 、出力から一度に複数のショットを取得するこのコストの高い再混合ステップを回避できます。 これは、 extended_stabilizer_measure_sampling=True
を設定することで有効にできます。
例としてチュートリアルの最初にあるランダムな回路をもう一度見てみましょう。ここでは、100ショットが実行されています。
[10]:
# We set these options here only to make the example run more quickly.
opts = {'extended_stabilizer_mixing_time': 100}
multishot = extended_stabilizer_simulator.run(
tcirc, shots=100, **opts).result()
print("100 shots took {} s".format(multishot.time_taken))
100 shots took 29.634929895401 s
[11]:
opts = {
'extended_stabilizer_measure_sampling': True,
'extended_stabilizer_mixing_time': 100
}
measure_sampling = extended_stabilizer_simulator.run(
circ, shots=100, **opts).result()
print("With the optimization, 100 shots took {} s".format(result.time_taken))
With the optimization, 100 shots took 0.4958779811859131 s
いつ使用しますか?¶
クリフォード以外のゲートが多数ある小さな回路の場合、statevectorメソッドは拡張スタビライザーよりもパフォーマンスが向上する可能性があります。 ただし、ハイパフォーマンスな計算にアクセスすることなく、多くの量子ビットの回路を調べたい場合は、この方法を試してみてください。
[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:28:14 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.
[ ]: