Qulacs-Osaka / scikit-qulacs

scikit-qulacs is a library for quantum neural network. This library is based on qulacs and named after scikit-learn.
https://qulacs-osaka.github.io/scikit-qulacs/index.html
MIT License
19 stars 6 forks source link

Save parameter #230

Closed ikanago closed 1 year ago

ikanago commented 1 year ago

モデルを pickle でバイナリファイルに保存しようとしたのですが、モデルのメンバのうち ParametricQuantumCircuit が pickle に対応していませんでした. そこでモデルそのものではなく学習したパラメータを保存することにしました. モデルのパラメータ以外の部分(回路やソルバなど)が決定的に初期化されていれば,パラメータを保存するだけで問題ないと考えてそうしました.

@wmizukami test/test_save_parameter.py に今回実装した機能のテストがあり,使用例にもなっています. 改善点などあればコメントお願いします.

dataclass について

pickle にしたあとそこから復元できるかをテストする必要がありました. そこで Regressor と Classifier 比較できるようにするために dataclass 化したのですが,上述の通りモデルではなくパラメータを保存することにしたのでこのテストは必要なくなりました. dataclass にすれば attribute を一覧できて便利だと思うので,戻さないでおくことにします.

forest1040 commented 1 year ago

https://github.com/Qulacs-Osaka/qulacs-osaka/pull/387 で、ParametricQuantumCircuit が変更されるので @KowerKoint にも確認頂いた方がよい気がします。

ParametricQuantumCircuit が pickle に対応できない理由がちょっと気になりました。。(循環参照とかの問題?) それ以外はLGTMです!

codecov[bot] commented 1 year ago

Codecov Report

Merging #230 (02a8536) into main (c0e8aa0) will increase coverage by 0.08%. The diff coverage is 94.44%.

@@            Coverage Diff             @@
##             main     #230      +/-   ##
==========================================
+ Coverage   91.77%   91.85%   +0.08%     
==========================================
  Files          11       12       +1     
  Lines         863      884      +21     
  Branches      167      169       +2     
==========================================
+ Hits          792      812      +20     
+ Misses         42       41       -1     
- Partials       29       31       +2     
Impacted Files Coverage Δ
skqulacs/qnn/regressor.py 81.60% <90.47%> (+0.88%) :arrow_up:
skqulacs/qnn/classifier.py 90.90% <93.75%> (-0.65%) :arrow_down:
skqulacs/circuit/circuit.py 82.77% <100.00%> (+0.19%) :arrow_up:
skqulacs/save.py 100.00% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

ikanago commented 1 year ago

C++ で定義されたクラスを pickle するには,pybind のほうで適切に対応する必要があるみたいです: https://pybind11.readthedocs.io/en/latest/advanced/classes.html#pickling-support

KowerKoint commented 1 year ago

Qulacsのpybind11もいずれpickleに対応したいですね。

KowerKoint commented 1 year ago

https://github.com/Qulacs-Osaka/qulacs-osaka/pull/387 ですが、このPRの方針ではパラメータの配列しか保存していなくてサーキット自体はpickleしていないので変更されても問題なさそうです。 scikit-qulacs自身の問題としてnew-styleのParametricQuantumCircuitを採用するかどうかで中身は変わりそうですが

forest1040 commented 1 year ago

scikit-qulacs自身の問題としてnew-styleのParametricQuantumCircuitを採用するかどうかで中身は変わりそうですが

一旦これでマージしてもらって、そしてParametricQuantumCircuitのnew-styleが反映されたら、 scikit-qulacsも別チケットでnew-styleに対応したいですね!