LoginSignup
2
0

More than 1 year has passed since last update.

PennyLaneをC++バックエンドで高速に動かす

Last updated at Posted at 2021-06-19

PennyLaneとバックエンド

PennyLane はいろいろなバックエンドを自由に使い分けられます。
実機はともかく、シミュレーターの場合、速度が重要になりますよね。

Pennylaneの標準のバックエンド default.qubit
dev = qml.device("default.qubit", wires=n_qubits)
は、速度の面では特筆した性能は有りません。しかし、勾配計算としてadjointが使えます。
このアルゴリズム、標準の parameter-shift1に比べて体感で3倍程度早くなります。いいですね。
https://qiita.com/notori48/items/9cfe49c79b42389db0e1
(早くなる度合いは、パラメータ数にも依存します。)

アルゴリズム計算量に加えて、バックエンド自体の計算の速さも重要です。
Pythonはインタプリタなので基本遅くて、できればC++やJuliaが使いたい。
素晴らしいことに、C++が裏で走る(が、記述はpythonな)qulacsをPennyLaneから呼び出す事ができます。
回路実行はdefault.qubitのおよそ3倍速になります。

!pip install pennylane-qulacs["cpu"]
dev = qml.device("qulacs.simulator", wires=n_qubits)

よし、これで勾配計算にadjointを使えば、3x3 = 9 倍速だ!・・・と、なればいいのですが、
残念ながらqulacs.simulatorparameter-shiftしか使えません。
C++かつadjointを使う方法はないのでしょうか??

実は、ありました。
紹介します。

Pennylane-lightning plugin

どうやら最近出たプラグインで Pennylane-lightning plugin というものがあります。

Release: 0.16.0-dev
The PennyLane-Lightning plugin provides a fast state-vector simulator written in C++.

PennyLane is a cross-platform Python library for quantum machine learning, automatic differentiation, and optimization of hybrid quantum-classical computations.

公式にはpennylaneのver0.16.0が推奨となっていますが、0.15.0でも動くようです。
プラグインのインストールは、
!pip install PennyLane-Lightning

です。
そして、
dev = qml.device("lightning.qubit", wires=n_qubits)

で使えます。
単純な回路2で時間を測ってみると、
default.qubit : 6 sec
qulacas.simulator : 2 sec
lightning.qubit : 2 sec
となり、確かにqulacs並の速度が出ています。

勾配計算についても、adjointが(エラー無く)指定できました。
実際の量子機械学習タスク(Tensorflow.keras + pennylane)でやったところ、
default.qubit w/adjoint : 3min 15sec /epoch
qulacas.simulator w/parameter-shift : 9min 10sec /epoch
lightning.qubit w/adjoint : 2min 45sec / epoch

このようになり、やはりlightning.qubit w/adjointが最速となります。
しかし残念ながら、keras + pennylane で量子NNを組んだ場合では単純な回路実行以外のオーバーヘッドに律速があるようで
default.qubitの3倍速にはなっていません。それでも、たった1行で速度が20%ぐらいは早くなっています。

まぁ、それでも古典NNに比べるとめっっちゃくちゃ遅いんですけどね。
Communiityでも、速度が遅すぎて驚いたという記事がたくさん上がっています。それな。3

まとめ

ちょっとでも早くする工夫をしてみた。
PennyLane-Lightningはすぐ使えて速いぞ!


  1. 例えばqiskit に入っている勾配計算ライブラリも、この方法です。 

  2. 24qubitのGHZ状態生成回路, shots=10 

  3. みなさんもぜひ量子機械学習(変分回路)をやってみてください。驚くほど遅いと思います。 

2
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
2
0