複数回に渡って実際の量子アルゴリズムを実装していきます。
その際に Google Quantum AIチームが発表した誤り訂正機能のないアナログの性質の強いゲートモデル (Noisy Intermediate-Scale Quantum) 向けフレームワークの Cirq を利用するので、Docker 上で Cirq をさわれる環境を構築したいと思います。
※ Cirq に関しての記事はこちら

構築方法

docker 及び docker-compose をインストール

docker 及び docker-compose を利用して構築するのでそれぞれをインストールする必要があります。Mac の場合は、下記の記事が参考になると思います。

    • DockerをMacにインストールする (更新:2018/5/14)

 

    Docker Compose – インストール

Dockerfile を作成する

今回は機械学習やデータサイエンスでお決まりの jupyter を利用したいので、Dockerfile に以下を記載します。

FROM jupyter/datascience-notebook:latest

WORKDIR /app

ADD . .

RUN pip install -r requirements.txt

EXPOSE 8888

jupyter のイメージは base-notebook でも良いですが、数値計算用のライブラリーが入っている datascience-notebook を使用します。また、requirements.txt にはインストールしたい cirq を記載します。

cirq==0.3.1.35

docker-compose.yml を作成する

Dockerfile ができたので docker のコマンドを使用して cirq の入った jupyter 環境はできるのですが、コマンドを覚えるのが面倒なので docker-compose.yml を以下のように作成します。

version: '2'
services:
  jupyter:
    build: .
    ports:
      - 8888:8888
    volumes:
      - .:/app

Jupyter を Docker 上で起動する

Dockerfile 及び docker-compose.yml ができたので以下のコマンドを実行して jupyter を docker 上で起動させます。

$ docker-compose build
Building jupyter
Step 1/5 : FROM jupyter/datascience-notebook:latest
 ---> ae4c1f6633af
Step 2/5 : WORKDIR /app
 ---> Using cache
 ---> 18ca7d85f90e
Step 3/5 : ADD . .
 ---> c04f53d29de7
Step 4/5 : RUN pip install -r requirements.txt
 ---> Running in a7c1bc5b42b2
Collecting cirq==0.3.1.35 (from -r requirements.txt (line 1))
  Downloading
...
 ---> 8c517f34e385
Step 5/5 : EXPOSE 8888
 ---> Running in 57af18110d9f
Removing intermediate container 57af18110d9f
 ---> fc6809c9486e
Successfully built fc6809c9486e
Successfully tagged docker-jupyter-criq_jupyter:latest

$ docker-compose up
Creating network "docker-jupyter-criq_default" with the default driver
Creating docker-jupyter-criq_jupyter_1 ... done
Attaching to docker-jupyter-criq_jupyter_1
jupyter_1  | Container must be run with group "root" to update passwd file
jupyter_1  | Executing the command: jupyter notebook
jupyter_1  | [I 05:18:29.813 NotebookApp] Writing notebook server cookie secret to /home/jovyan/.local/share/jupyter/runtime/notebook_cookie_secret
jupyter_1  | [I 05:18:30.168 NotebookApp] JupyterLab extension loaded from /opt/conda/lib/python3.6/site-packages/jupyterlab
jupyter_1  | [I 05:18:30.169 NotebookApp] JupyterLab application directory is /opt/conda/share/jupyter/lab
jupyter_1  | [I 05:18:30.177 NotebookApp] Serving notebooks from local directory: /app
jupyter_1  | [I 05:18:30.178 NotebookApp] The Jupyter Notebook is running at:
jupyter_1  | [I 05:18:30.178 NotebookApp] http://(87022d2d1013 or 127.0.0.1):8888/?token=some_token
jupyter_1  | [I 05:18:30.179 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

jupyter が起動したので http://localhost:8888 にアクセスするとログイン画面が表示されるので some_token の部分に書かれている文字列を Password or Token に入力し、ログインができたら OK です。

スクリーンショット 2018-11-01 14.23.01.png

サンプルを実行してみる

次に Cirq が正しくインストールされているかを見るために cirq のサンプルコード を jupyter に入力してみます。

スクリーンショット 2018-11-01 14.31.45.png

こんな感じで Circuit と Results が表示されれば、成功です。

まとめ

今回は Google が NISQ 向けに開発しているフレームワーク cirq を docker 上で動作させるまでをやりました。
※ 今回の内容は GitHub にのせました!

また、次回以降は cirq を使用して実際に量子アルゴリズムを実装していきたいと思います。

追記

アルゴリズム実装しました!

    1. superdense coding

 

    grover アルゴリズム