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

Improve LearningCircuit class and Regressor, add GD solver, add test file #254

Closed Gruntrexpewrus closed 1 year ago

Gruntrexpewrus commented 1 year ago

Hi, it's Leonardo and this is a list of what I would like to pull: 1) In skqulacs/circuit/circuit.py I added a function to perform any multiqubit gate using qulacs base function. 2) In skqulacs/qnn/solver I added gradient descent method. 3) In skqulacs/qnn/regressor I added: _func_grad which computes the gradients of the circuit without the cost, I added observables_str: List[str] = field(init=True, default_factory=list) for the class and modified around to make it possible to use. The latter allows to arbitrary select the observable writing them as a string (e.g. "Z 3"). I think it was a bit more complex. Since is initialized as empty list, it does not cause problems in any test code. 4) In skqulacs/test I added test_qnn_regressor_grad_descent.py which in the style of the other tests shows that is possible to learn the sin (noisy or not) with batch gradient. Careful, it seems that stochastic gradient does not work, and it may be a simple theoretical reason.

Feel free to contact me if there is any error in the codes or PR!

Gruntrexpewrus commented 1 year ago

There is a little bug related to the operators, I will fix.

codecov[bot] commented 1 year ago

Codecov Report

Merging #254 (79f17c1) into main (61c3366) will increase coverage by 0.28%. The diff coverage is 98.36%.

@@            Coverage Diff             @@
##             main     #254      +/-   ##
==========================================
+ Coverage   92.25%   92.54%   +0.28%     
==========================================
  Files          12       12              
  Lines         943      993      +50     
  Branches      180      189       +9     
==========================================
+ Hits          870      919      +49     
  Misses         41       41              
- Partials       32       33       +1     
Impacted Files Coverage Δ
skqulacs/circuit/__init__.py 100.00% <ø> (ø)
skqulacs/circuit/circuit.py 83.06% <90.90%> (+0.29%) :arrow_up:
skqulacs/circuit/pre_defined.py 97.54% <100.00%> (+0.10%) :arrow_up:
skqulacs/qnn/regressor.py 84.76% <100.00%> (+3.15%) :arrow_up:
skqulacs/qnn/solver.py 90.27% <100.00%> (+1.21%) :arrow_up:

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

Gruntrexpewrus commented 1 year ago

⚠️⚠️⚠️ There still is a bug related to the observables, I will update here when is solved. Maybe I will also modify the test file to show a circuit where I manually select the observables.

Gruntrexpewrus commented 1 year ago

I don't understand why codecov says that a function that I explicitly use in the test_qnn_regressor_grad_descent.py is not used. Do tests not include my new test file?

Gruntrexpewrus commented 1 year ago

It seems that the PR passed all the checks 🥹

Gruntrexpewrus commented 1 year ago

I think I followed your advices @ikanago, let me know if I should change other things 🤓

forest1040 commented 1 year ago

Merged.