Last active
December 28, 2018 02:08
-
-
Save gyu-don/b1930ce7ad46ce39525b5b9fae298f8b to your computer and use it in GitHub Desktop.
はじめてのBlueqat.ipynb
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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "はじめてのBlueqat.ipynb", | |
"version": "0.3.2", | |
"provenance": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/gyu-don/b1930ce7ad46ce39525b5b9fae298f8b/-blueqat.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "gJlkp16yEA5q", | |
"colab_type": "code", | |
"outputId": "570e5dd2-e855-43b8-829e-f8ad1768c56f", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 86 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"!pip3 install blueqat" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Collecting blueqat\n", | |
" Downloading https://files.pythonhosted.org/packages/c5/6a/6853e8e6fb54e4fd34cccbfae3773b4e23163f69efae116bc07a511dab96/blueqat-0.3.1-py3-none-any.whl\n", | |
"Installing collected packages: blueqat\n", | |
"Successfully installed blueqat-0.3.1\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "Q6M4xQmpKzHv", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# 初めての量子回路\n", | |
"やること\n", | |
"\n", | |
"\n", | |
"1. 量子回路を作る\n", | |
"2. 回路(の0ビット目)にアダマールゲートをひとつ付け加える\n", | |
"3. 実行して状態ベクトルを得る\n", | |
"\n", | |
"量子回路は通常、|0>に初期化されている。なので、\n", | |
"\n", | |
"|0> ---H--- $\\frac{1}{\\sqrt{2}}$(|0> + |1>)となる" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "17xEu2dXFJUg", | |
"colab_type": "code", | |
"outputId": "e21ba78f-d94f-4bd2-cede-7054ee76f76b", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"from blueqat import Circuit\n", | |
"c = Circuit()\n", | |
"c.h[0]\n", | |
"c.run()" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([0.70710678+0.j, 0.70710678+0.j])" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 2 | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "YQhk1LMuJ-mB", | |
"colab_type": "code", | |
"outputId": "69ee8f6b-c674-492b-e69c-f389e3bb4feb", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"0.70710678 ** 2 # 1/√2のはずなので、2乗して1/2になるか確認" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.49999999832196845" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 3 | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "xajPDWDLOBa6", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"|1> ---H--- $\\frac{1}{\\sqrt{2}}$(|0> - |1>)\n", | |
"となるが、それを確認。\n", | |
"\n", | |
"Xゲートはビットを反転するゲート。\n", | |
"なので、\n", | |
"|0> --- X --- |1>\n", | |
"となり、\n", | |
"|0> --- X --- H --- $\\frac{1}{\\sqrt{2}}$(|0> - |1>)\n", | |
"となる。" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "P7iP2zjxLiDW", | |
"colab_type": "code", | |
"outputId": "21312708-c252-41dd-a1df-5cde10c99f82", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"c = Circuit()\n", | |
"c.x[0].h[0].run()" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([ 0.70710678+0.j, -0.70710678+0.j])" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 5 | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "ogKcIgWfOZpt", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# 観測してみる\n", | |
"観測はmゲートで行う。runの中にshots=数字を入れて、何度か回路を動かす。" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "GP2iYIaONCQl", | |
"colab_type": "code", | |
"outputId": "a9cff587-f91c-47ef-f0f3-b02ba448b7c1", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"c = Circuit()\n", | |
"c.h[0].m[0].run(shots=100)" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"Counter({'0': 49, '1': 51})" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 7 | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "QM-2mgnsP0rN", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"演習問題:\n", | |
"|1>にHをかけた回路も、|0>にHをかけた回路と同様の観測結果になることを確認して下さい。" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "KnNN0S4fPzwH", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "YXp2Yo95QXph", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# Hをいっぱい並べてみる" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "-5dTYQpQOv5U", | |
"colab_type": "code", | |
"outputId": "924b731f-77a0-4bf3-a795-15afb146e50d", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 538 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"c = Circuit()\n", | |
"c.h[:5].m[:].run(shots=100)" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"Counter({'00000': 3,\n", | |
" '00001': 3,\n", | |
" '00010': 5,\n", | |
" '00011': 2,\n", | |
" '00100': 4,\n", | |
" '00101': 5,\n", | |
" '00110': 2,\n", | |
" '00111': 4,\n", | |
" '01000': 1,\n", | |
" '01001': 6,\n", | |
" '01010': 2,\n", | |
" '01011': 2,\n", | |
" '01100': 1,\n", | |
" '01101': 5,\n", | |
" '01110': 3,\n", | |
" '01111': 7,\n", | |
" '10000': 3,\n", | |
" '10001': 3,\n", | |
" '10011': 2,\n", | |
" '10100': 3,\n", | |
" '10101': 3,\n", | |
" '10110': 2,\n", | |
" '10111': 1,\n", | |
" '11000': 3,\n", | |
" '11001': 4,\n", | |
" '11011': 2,\n", | |
" '11100': 1,\n", | |
" '11101': 2,\n", | |
" '11110': 9,\n", | |
" '11111': 7})" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 10 | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "1wNniY9-WNK9", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# 量子もつれを作ろう\n", | |
"HゲートとCX (CNOT)ゲートを使うと、量子もつれ状態を作ることができる。\n", | |
"量子もつれ状態を作って、観測してみよう" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "YOvUoX2RQeNf", | |
"colab_type": "code", | |
"outputId": "0a879105-b02d-4fba-eb4d-b80addc4dec2", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"c = Circuit()\n", | |
"c.h[0].cx[0, 1].m[:].run(shots=100)" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"Counter({'00': 43, '11': 57})" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 12 | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "7GqMeDCBWf_s", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment