1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Docker, Jupyter-notebookでCirqを使った量子計算の学習

Last updated at Posted at 2022-04-13

Macで確認しましたが、WindowsでもLinuxでもDockerが使える環境なら使えると思います。

適当なディレクトリを作って、

mkdir cirq && cd cirq

適当なテキストエディタでdocker-compose.ymlを作る。

docker-compose.yml
version: "3.8"
services:
  cirq-notebook:
    image: sanori/cirq-notebook:latest
    volumes:
      - ${PWD}/:/home/jovyan/work
    ports:
      - 8888:8888
    container_name: cirq-notebook

下の様にコンテナを立ち上げると、

docker-compose up

こんなのが出てくるので、

cirq-notebook  | [C 2022-04-13 10:34:08.350 ServerApp]
cirq-notebook  |
cirq-notebook  |     To access the server, open this file in a browser:
cirq-notebook  |         file:///home/jovyan/.local/share/jupyter/runtime/jpserver-9-open.html
cirq-notebook  |     Or copy and paste one of these URLs:
cirq-notebook  |         http://[何か英数字列]:8888/lab?token=[何か英数字列]
cirq-notebook  |      or http://127.0.0.1:8888/lab?token=[何か英数字列]

http://127.0.0.1:8888/?token=[何か英数字列]の箇所を選択し、URLとしてブラウザで開きます。
Jupyter Notebookが立ち上がるので、後はこのサイトを見て学習

とは言え、上の記事はバージョン'0.3.1.35'によるもの、こちらで使っているイメージsanori/cirq-notebook:latest'0.14.0'なので、元記事のコードは一部変更が必要です。変更箇所は多いですが、種類としては下の4種です。

# Create a circuit
- circuit = cirq.Circuit.from_ops(
+ circuit = cirq.Circuit(
-    cirq.RotXGate(half_turns=0.2)(qubit),
+    cirq.ops.XPowGate(exponent=0.2)(qubit),
    cirq.measure(qubit, key='m')  # Measurement.
)
# Simulate the circuit several times.
- simulator = cirq.google.XmonSimulator()
+ simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=200)
qubit = cirq.GridQubit(0, 0)
# Create a circuit
- circuit = cirq.Circuit.from_ops(
+ circuit = cirq.Circuit(
    cirq.H(qubit),  
    cirq.measure(qubit, key='m')  # Measurement.
)

print("Circuit:")
print(circuit)
# Simulate the circuit several times.
simulator = cirq.google.XmonSimulator()
result = simulator.simulate(circuit)

- result.final_state
+ result.final_state_vector

ただし、上の変更が元の0.3.1.35と同等と言えるかは確信はありません。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?