Created
June 24, 2021 14:13
-
-
Save shokout/a4b38c4aa336614b3245c073de079097 to your computer and use it in GitHub Desktop.
Amazon Braket のサンプルノートブックです(日本語版)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"* このノートブックは、Amazon Braket のスタート時にクローンされるノートブック\n", | |
"Braket examples/getting_started/0_Getting_started.ipynb\n", | |
"に含まれるものを日本語訳と編集を加えたものです。" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Amazon Braket 入門\n", | |
"\n", | |
"このhello-worldチュートリアルでは、2つのキュービット間で最大限に絡み合ったベル状態を準備します。 次に、ローカルシミュレータで回路を実行し、結果を取得します。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# general imports\n", | |
"import matplotlib.pyplot as plt\n", | |
"%matplotlib inline\n", | |
"\n", | |
"# AWS imports: Import Braket SDK modules\n", | |
"from braket.circuits import Circuit\n", | |
"from braket.devices import LocalSimulator" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 1. Local Simulator で回路を構築する\n", | |
"\n", | |
"2つの量子ビットで「ベル状態」を構築しましょう。 `Circuit()` を呼び出すことにより、空の回路を作成し、回路にゲートを追加するだけです。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# build a Bell state with two qubits. Here 'cnot(control=0, target=1)' can be simplified as 'cnot(0,1)'\n", | |
"bell = Circuit().h(0).cnot(control=0, target=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"print(bell)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 回路をローカルシミュレータに送信し、結果を取得します\n", | |
"\n", | |
"ここでは、回路をローカルシミュレータに送信して、結果を取得します。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# set up device\n", | |
"device = LocalSimulator()\n", | |
"\n", | |
"# run circuit\n", | |
"result = device.run(bell, shots=10).result()\n", | |
"# get measurement shots\n", | |
"counts = result.measurement_counts\n", | |
"# print counts\n", | |
"print(counts)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# plot using Counter\n", | |
"plt.bar(counts.keys(), counts.values());\n", | |
"plt.xlabel('bitstrings');\n", | |
"plt.ylabel('counts');" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## [演習] shot 数を増やして 量子計算の挙動の違いを確認する\n", | |
"\n", | |
"`result = device.run(bell, shots= 10).result()`\n", | |
"shot の数 を 1 -> 100 -> 1000 -> と増やしてみてください。\n", | |
"shot 数を増やすと、\n", | |
"(Local Simulator で shot 数を増やしても、shot 数に対して課金される料金に変化はありません)\n", | |
"\n", | |
"* shot 数 100,000 以上になると、計算時間が急に増えてきますので、ご注意ください。目安の shot 数は、最大 10,000,000程度まで" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%%time\n", | |
"# run circuit\n", | |
"result = device.run(bell, shots= 10).result()\n", | |
"# get measurement shots\n", | |
"counts = result.measurement_probabilities\n", | |
"# print counts\n", | |
"print(counts)\n", | |
"\n", | |
"# plot using Counter\n", | |
"plt.bar(counts.keys(), counts.values());\n", | |
"plt.xlabel('bitstrings');\n", | |
"plt.ylabel('probabilities');" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"shot の数を10, 100, 1000と増やしていくと、|00>状態と|11>状態の観測確率がほぼ等しく50%に近づいていくことがわかりましたか?\n", | |
"\n", | |
"このように、統計誤差を無くすために(理想的な計算をするために)どの程度のショット数が必要かを見積もることができます。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "conda_braket", | |
"language": "python", | |
"name": "conda_braket" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment