ベンチマーク関数 標準誤差機能 の使い方

Open in Colab

ベンチマーク関数に追加した標準誤差機能の使い方を解説します。

ライブラリのインストール、モデルの準備

OpenJij のリポジトリから Fork した自分のリポジトリ内に、さらに feature/error_bar リポジトリを branch しました。 標準誤差関数が実装されるまでは、自分のGitHubから、 OpenJij をインストールして、実行ができます。

反強磁性1次元イジングモデルの準備

[1]:
import random
import numpy as np
import matplotlib.pyplot as plt
import openjij as oj

# 反強磁性1次元イジングモデル を作る
N = 30
h = {0: -10}
J = {(i, i+1): 1 for i in range(N-1)}

ベンチマーク関数

呼び出し方は、今まで通りです。

引数に変化はありません。
返り値として、以下の計算結果が新たに辞書型で格納されるようにしました。
  • se_success_prob: 成功確率の標準誤差のリスト
    iteration 回アニーリング時、成功確率の期待値の標準偏差
    step_num ごとに、格納されている
  • se_residual_energy: 残留エネルギーの標準誤差のリスト
    iteration 回アニーリング時の、残留エネルギー値の平均の標準偏差
    step_num ごとに、格納されている
  • se_lower_tts: TTSの下位誤差のリスト 成功確率の上位誤差を基に算出したTTSの下位誤差

  • se_upper_tts: TTSの上位誤差のリスト 成功確率の下位誤差を基に算出したTTSの下位誤差

[2]:
# 最適解
correct_state = [(-1)**i for i in range(N)]

# ステップ数とアニーリングの反復数を与えます
step_num_list = list(range(10, 101, 10))  # [10, 20,,,, 100]
iteration = 100

# benchmark 関数で TTS 残留エネルギー 成功確率 標本平均 標準誤差を計算
sampler = oj.SASampler(num_reads=iteration)
result = oj.solver_benchmark(
                      solver=lambda time, **args: sampler.sample_ising(h,J,num_sweeps=time),
                      time_list=step_num_list, solutions=[correct_state], p_r=0.99
            )
[ ]:
# seaborn を使うとグラフが見やすくなる
!pip install seaborn
import seaborn as sns
sns.set()

TTS 残留エネルギー 成功確率

TTS 残留エネルギー 成功確率 の可視化は今まで通りできます。

[4]:
fig, (axL,axC,axR) = plt.subplots(ncols=3, figsize=(15,3))
plt.subplots_adjust(wspace=0.4)

fontsize = 10
axL.plot(result['time'], result['tts'], color = 'black')
axL.set_xlabel('annealing time', fontsize=fontsize)
axL.set_ylabel('TTS', fontsize=fontsize)

axC.plot(result['time'], result['residual_energy'], color = 'black')
axC.set_xlabel('annealing time', fontsize=fontsize)
axC.set_ylabel('Residual energy', fontsize=fontsize)

axR.plot(result['time'], result['success_prob'], color = 'black')
axR.set_xlabel('annealing time', fontsize=fontsize)
axR.set_ylabel('Success probability', fontsize=fontsize)
[4]:
Text(0, 0.5, 'Success probability')
../_images/ja_002-Evaluation_errorbar_9_1.png

標準誤差

標準誤差は、se_success_probとse_residual_energyに格納されています。
これらを使えば、標準誤差のエラーバーを表示できます。
[5]:
# 標準誤差の確認
print(result['se_upper_tts'])
print(result['se_lower_tts'])
print(result['se_residual_energy'])
print(result['se_success_prob'])
[inf, inf, inf, inf, 7858.819354838525, 2654.190893716449, 2178.0486375615496, 5615.492293981748, 1464.0152746680208, 468.88364094269514]
[inf, inf, inf, inf, 5513.0440804857535, 1185.3970674363136, 427.5985307963406, 325.07613576824417, 279.1623010043531, 201.6331109905467]
[4.480755708267492, 4.224937988905992, 4.116436627114585, 3.6505984012033816, 3.400475308428562, 2.6654542698471615, 2.2271057451320084, 1.8134180015883952, 1.435340384787532, 1.0490977364412055]
[0.0, 0.0, 0.0, 0.0, 0.1705872210923198, 0.33630343441600474, 0.46647615158762396, 0.49839743177508444, 0.4828043081829324, 0.41424630354415964]
[6]:
fig, (axL,axC,axR) = plt.subplots(ncols=3, figsize=(15,3))
plt.subplots_adjust(wspace=0.4)

fontsize = 10
axL.plot(result['time'], result['tts'])
axL.errorbar(result['time'], result['tts'], yerr = (result['se_lower_tts'],result['se_upper_tts']), capsize=5, fmt='o', markersize=5, ecolor='black', markeredgecolor = "black", color='w')
axL.set_xlabel('annealing time', fontsize=fontsize)
axL.set_ylabel('TTS', fontsize=fontsize)

axC.plot(result['time'], result['residual_energy'])
axC.errorbar(result['time'], result['residual_energy'], yerr = result['se_residual_energy'], capsize=5, fmt='o', markersize=5, ecolor='black', markeredgecolor = "black", color='w')
axC.set_xlabel('annealing time', fontsize=fontsize)
axC.set_ylabel('Residual energy', fontsize=fontsize)

axR.plot(result['time'], result['success_prob'])
axR.errorbar(result['time'], result['success_prob'], yerr = result['se_success_prob'], capsize=5, fmt='o', markersize=5, ecolor='black', markeredgecolor = "black", color='w')
axR.set_xlabel('annealing time', fontsize=fontsize)
axR.set_ylabel('Success probability', fontsize=fontsize)
[6]:
Text(0, 0.5, 'Success probability')
../_images/ja_002-Evaluation_errorbar_12_1.png

1回のアニーリング時間が延びると、成功確率と残留エネルギーについて標準誤差のエラーバーが短くなっていくことが確認できました。