Python の 練習を兼ねて, 量子コンピュータのフレームワーク(Google社)のチュートリアルを確認してみた
動作環境は, Google Colaboratory で行った
■感想.
1. 量子論に関する知識がないので, Cirq を 操作しながら, 物理に関する知識も少しずつ確認していく必要があると感じた.
2. 時間を見つけて, 今後も, チュートリアルの残りの部分を進めていこうと思う.
■Cirq の install.
1 2 3 4 5 6 7 |
[編集内容] try: import cirq except ImportError: print("installing cirq...") !pip install --quiet cirq print("installed cirq.") |
1 2 3 4 |
[出力結果] installing cirq... |████████████████████████████████| 1.6MB 18.8MB/s installed cirq. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
[参考(出力結果)] [--quiet]オプションを外すと, 以下のような, installログが出力された. installing cirq... Collecting cirq Downloading https://files.pythonhosted.org/packages/18/05/39c24828744b91f658fd1e5d105a9d168da43698cfaec006179c7646c71c/cirq-0.9.1-py3-none-any.whl (1.6MB) |████████████████████████████████| 1.6MB 5.5MB/s Requirement already satisfied: sortedcontainers~=2.0 in /usr/local/lib/python3.7/dist-packages (from cirq) (2.3.0) Requirement already satisfied: networkx~=2.4 in /usr/local/lib/python3.7/dist-packages (from cirq) (2.5) Requirement already satisfied: sympy in /usr/local/lib/python3.7/dist-packages (from cirq) (1.1.1) Requirement already satisfied: google-api-core[grpc]<2.0.0dev,>=1.14.0 in /usr/local/lib/python3.7/dist-packages (from cirq) (1.16.0) Requirement already satisfied: protobuf~=3.12.0 in /usr/local/lib/python3.7/dist-packages (from cirq) (3.12.4) Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from cirq) (3.7.4.3) Requirement already satisfied: matplotlib~=3.0 in /usr/local/lib/python3.7/dist-packages (from cirq) (3.2.2) Requirement already satisfied: requests~=2.18 in /usr/local/lib/python3.7/dist-packages (from cirq) (2.23.0) Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from cirq) (1.1.5) Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from cirq) (1.4.1) Collecting freezegun~=0.3.15 Downloading https://files.pythonhosted.org/packages/17/5d/1b9d6d3c7995fff473f35861d674e0113a5f0bd5a72fe0199c3f254665c7/freezegun-0.3.15-py2.py3-none-any.whl Requirement already satisfied: numpy~=1.16 in /usr/local/lib/python3.7/dist-packages (from cirq) (1.19.5) Requirement already satisfied: decorator>=4.3.0 in /usr/local/lib/python3.7/dist-packages (from networkx~=2.4->cirq) (4.4.2) Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.7/dist-packages (from sympy->cirq) (1.2.1) Requirement already satisfied: google-auth<2.0dev,>=0.4.0 in /usr/local/lib/python3.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq) (1.27.0) Requirement already satisfied: googleapis-common-protos<2.0dev,>=1.6.0 in /usr/local/lib/python3.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq) (1.52.0) Requirement already satisfied: pytz in /usr/local/lib/python3.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq) (2018.9) Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq) (1.15.0) Requirement already satisfied: setuptools>=34.0.0 in /usr/local/lib/python3.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq) (53.0.0) Requirement already satisfied: grpcio<2.0dev,>=1.8.2; extra == "grpc" in /usr/local/lib/python3.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq) (1.32.0) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib~=3.0->cirq) (0.10.0) Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib~=3.0->cirq) (2.8.1) Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib~=3.0->cirq) (2.4.7) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib~=3.0->cirq) (1.3.1) Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests~=2.18->cirq) (1.24.3) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests~=2.18->cirq) (2020.12.5) Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests~=2.18->cirq) (3.0.4) Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests~=2.18->cirq) (2.10) Requirement already satisfied: cachetools<5.0,>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from google-auth<2.0dev,>=0.4.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq) (4.2.1) Requirement already satisfied: rsa<5,>=3.1.4; python_version >= "3.6" in /usr/local/lib/python3.7/dist-packages (from google-auth<2.0dev,>=0.4.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq) (4.7.1) Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.7/dist-packages (from google-auth<2.0dev,>=0.4.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq) (0.2.8) Requirement already satisfied: pyasn1>=0.1.3 in /usr/local/lib/python3.7/dist-packages (from rsa<5,>=3.1.4; python_version >= "3.6"->google-auth<2.0dev,>=0.4.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq) (0.4.8) Installing collected packages: freezegun, cirq Successfully installed cirq-0.9.1 freezegun-0.3.15 installed cirq. |
■qubits.
定義方法に, 以下の三パターンあるとのこと.
・cirq.NamedQubit
・cirq.LineQubit
・cirq.GridQubit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[編集内容(NamedQubit)] import cirq q0 = cirq.NamedQubit('source') q1 = cirq.NamedQubit('target') # チュートリアル上は, source と記載されているが, 変更可らしい. q2 = cirq.NamedQubit('Cirq') # チュートリアル上は, target と記載されているが, 変更可らしい. q3 = cirq.NamedQubit('basics') print(cirq.X(q0), cirq.Y(q0), cirq.Z(q0)) print(cirq.X(q1), cirq.Y(q1), cirq.Z(q1)) print(cirq.X(q2), cirq.Y(q2), cirq.Z(q2)) print(cirq.X(q3), cirq.Y(q3), cirq.Z(q3)) |
1 2 3 4 5 |
[出力結果] X(source) Y(source) Z(source) X(target) Y(target) Z(target) X(Cirq) Y(Cirq) Z(Cirq) X(basics) Y(basics) Z(basics) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[編集内容(LineQubit)] import cirq q0 = cirq.LineQubit(0) q1, q2 = cirq.LineQubit.range(2) q3 = cirq.LineQubit(5) q4, q5, q6 = cirq.LineQubit.range(3) print(cirq.X(q0), cirq.Y(q0), cirq.Z(q0)) print(cirq.X(q1), cirq.Y(q1), cirq.Z(q1)) print(cirq.X(q2), cirq.Y(q2), cirq.Z(q2)) print(cirq.X(q3), cirq.Y(q3), cirq.Z(q3)) print(cirq.X(q4), cirq.Y(q4), cirq.Z(q4)) print(cirq.X(q5), cirq.Y(q5), cirq.Z(q5)) print(cirq.X(q6), cirq.Y(q6), cirq.Z(q6)) |
1 2 3 4 5 6 7 8 |
[出力結果] X(0) Y(0) Z(0) X(0) Y(0) Z(0) X(1) Y(1) Z(1) X(5) Y(5) Z(5) X(0) Y(0) Z(0) X(1) Y(1) Z(1) X(2) Y(2) Z(2) |
1 2 3 4 5 6 7 8 9 10 11 |
[編集内容(GridQubit)] import cirq q0_2 = cirq.GridQubit(3, 5) qubits = cirq.GridQubit.square(3) print(cirq.X(q0_2), cirq.Y(q0_2), cirq.Z(q0_2)) print(cirq.X(qubits[0]), cirq.Y(qubits[1]), cirq.Z(qubits[2])) print(cirq.X(qubits[3]), cirq.Y(qubits[3]), cirq.Z(qubits[3])) print(cirq.X(qubits[5]), cirq.Y(qubits[6]), cirq.Z(qubits[7])) print(qubits) |
1 2 3 4 5 6 |
[出力結果] X((3, 5)) Y((3, 5)) Z((3, 5)) X((0, 0)) Y((0, 1)) Z((0, 2)) X((1, 0)) Y((1, 0)) Z((1, 0)) X((1, 2)) Y((2, 0)) Z((2, 1)) [cirq.GridQubit(0, 0), cirq.GridQubit(0, 1), cirq.GridQubit(0, 2), cirq.GridQubit(1, 0), cirq.GridQubit(1, 1), cirq.GridQubit(1, 2), cirq.GridQubit(2, 0), cirq.GridQubit(2, 1), cirq.GridQubit(2, 2)] |
■hardware.
チュートリアル上, Foxtail が紹介されているが, 他のパターンも確認可能らしい.
1 2 3 4 |
[編集内容(Foxtail)] import cirq print(cirq.google.Foxtail) |
1 2 3 4 5 |
[出力結果] (0, 0)───(0, 1)───(0, 2)───(0, 3)───(0, 4)───(0, 5)───(0, 6)───(0, 7)───(0, 8)───(0, 9)───(0, 10) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (1, 0)───(1, 1)───(1, 2)───(1, 3)───(1, 4)───(1, 5)───(1, 6)───(1, 7)───(1, 8)───(1, 9)───(1, 10) |
1 2 3 4 |
[編集内容(Bristlecone)] import cirq print(cirq.google.Bristlecone) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
[出力結果] (0, 5)────(0, 6) │ │ │ │ (1, 4)───(1, 5)────(1, 6)────(1, 7) │ │ │ │ │ │ │ │ (2, 3)───(2, 4)───(2, 5)────(2, 6)────(2, 7)───(2, 8) │ │ │ │ │ │ │ │ │ │ │ │ (3, 2)───(3, 3)───(3, 4)───(3, 5)────(3, 6)────(3, 7)───(3, 8)───(3, 9) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (4, 1)───(4, 2)───(4, 3)───(4, 4)───(4, 5)────(4, 6)────(4, 7)───(4, 8)───(4, 9)───(4, 10) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (5, 0)───(5, 1)───(5, 2)───(5, 3)───(5, 4)───(5, 5)────(5, 6)────(5, 7)───(5, 8)───(5, 9)───(5, 10)───(5, 11) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (6, 1)───(6, 2)───(6, 3)───(6, 4)───(6, 5)────(6, 6)────(6, 7)───(6, 8)───(6, 9)───(6, 10) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (7, 2)───(7, 3)───(7, 4)───(7, 5)────(7, 6)────(7, 7)───(7, 8)───(7, 9) │ │ │ │ │ │ │ │ │ │ │ │ (8, 3)───(8, 4)───(8, 5)────(8, 6)────(8, 7)───(8, 8) │ │ │ │ │ │ │ │ (9, 4)───(9, 5)────(9, 6)────(9, 7) │ │ │ │ (10, 5)───(10, 6) |
1 2 3 4 |
[編集内容(Sycamore)] import cirq print(cirq.google.Sycamore) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
[出力結果] (0, 5)───(0, 6) │ │ │ │ (1, 4)───(1, 5)───(1, 6)───(1, 7) │ │ │ │ │ │ │ │ (2, 3)───(2, 4)───(2, 5)───(2, 6)───(2, 7)───(2, 8) │ │ │ │ │ │ │ │ │ │ │ │ (3, 2)───(3, 3)───(3, 4)───(3, 5)───(3, 6)───(3, 7)───(3, 8)───(3, 9) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (4, 1)───(4, 2)───(4, 3)───(4, 4)───(4, 5)───(4, 6)───(4, 7)───(4, 8)───(4, 9) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (5, 0)───(5, 1)───(5, 2)───(5, 3)───(5, 4)───(5, 5)───(5, 6)───(5, 7)───(5, 8) │ │ │ │ │ │ │ │ │ │ │ │ │ │ (6, 1)───(6, 2)───(6, 3)───(6, 4)───(6, 5)───(6, 6)───(6, 7) │ │ │ │ │ │ │ │ │ │ (7, 2)───(7, 3)───(7, 4)───(7, 5)───(7, 6) │ │ │ │ │ │ (8, 3)───(8, 4)───(8, 5) │ │ (9, 4) |