Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Torch connector #12

Merged
merged 216 commits into from
Mar 30, 2021

Conversation

stefan-woerner
Copy link
Contributor

@stefan-woerner stefan-woerner commented Mar 16, 2021

Summary

This PR adds the TorchConnector to use a (Quantum) NeuralNetwork form Qiskit Machine Learning as a PyTorch Module. Further, it adds corresponding tutorials.

Details and comments

TODOs:

  • unit tests
  • include batching

Comment on lines 52 to 54
dense: Whether to return a dense (array with 'output_shape') or sparse (dict)
probabilities. Dense probabilities require "interpret == 'int'" where the integer
will be the index in the array of probabilities.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code below suggests that 'callable' is also valid, given certain output from it, for dense. That would be nice to state here too

"source": [
"import numpy as np\n",
"\n",
"from qiskit import Aer, QuantumCircuit\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is a case where we really want to use Aer (for its snapshot capability i.e. using AerPauliExpectatio) and using BasicAer would not suffice.

@stefan-woerner stefan-woerner changed the title [WIP] Adding Torch connector Adding Torch connector Mar 30, 2021
@stefan-woerner stefan-woerner marked this pull request as ready for review March 30, 2021 19:08
Copy link
Collaborator

@adekusar-drl adekusar-drl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking care the review! Looks good!

@stefan-woerner stefan-woerner merged commit 6451a76 into qiskit-community:master Mar 30, 2021
gentinettagian pushed a commit to gentinettagian/qiskit-machine-learning that referenced this pull request Dec 14, 2021
* add torch connector

* Update 99_vqc.ipynb

this tutorial will be removed eventually

* updating circuit and two layer QNNs

* update copyright headers

* Update 01_neural_networks.ipynb

* Update torch_connector.py

* initial version of classifier and regressor

* fix output dimensions for networks and torch connector

* check whether torch is installed or not in torch connector

* start unit tests for torch connector

* update two layer QNN

* Update 03_torch_connector.ipynb

* update structure

* fix l1 and l2 losses

* clean imports

* Test torch optional

* fix mypy

* fix notebooks

* include torchvision

* add `dense` to QNNs and update NNClassifier/Regressor

* Update 03_torch_connector.ipynb

* fix notebook

* fix notebook

* fix notebook

* fix notebook

* Increase notebook cell timeout

* shelved changes

* batching

* enable SparseArray for neural networks

* fix style, lint

* fix tutorials

* more on batching

* Update circuit_qnn.py

* add unit tests

* add gradient unit tests

* Update test_circuit_qnn.py

* fix lint

* fix typos

* fix mypy

* fix opflow qnn output shape

output should always be a batch, possibly with a single element

* fix sampling output shape to `(num_samples, *sample_shape)`

* more on batching

* more on batching

* clean circuit QNN and fix backward output shape

* Update circuit_qnn.py

* update circuit QNN and torch connector

* Update torch_connector.py

* fix issue with np.int64 / int type check

* fix lint and types

* consistent batching

* some spelling

* add copyright

* some docstrings.

* rename dense to sparse

* Update circuit_qnn.py

* add sparse logic

* and rename `returns_samples` to `sampling`

* fix typing and clean code

* fix spelling

* add torch connector unit tests

* fix some lint errors

* Add CI step unit test without torch installed

* fix unit tests when torch is not installed

* add unit tests and fix output shape

* Update test_torch_connector.py

* fix circuit qnn tests

* fix typing

* fix typing

* more batching

* more batching

* fix output shapes and tutorials

* Update circuit_qnn.py

* Update opflow_qnn.py

* fix lint

* use sparse probability gradients in case of sample based evaluation

* update neural network classifier

* fix style/spelling/mypy

* fix tests

* Update neural_network_classifier.py

* more batching

* removed duplicate code

* gradients reshaping and more tests.

* fix circuit QNN tests

* fix typing

* add batching logic to `sample()`, `probabilities()`, and `probability_gradients()`

* fix lint

* add gradients for circuit QNN classification + tests

* add initial draft for VQC

* gradient tests.

* add tests with cross entropy

* fix lint, style, mypy

* cross_entropy gradient added

* add cross entropy test with gradients

* fix style, spell

* Update test_torch_connector.py

* update torch connector

* Update torch_connector.py

* removed _sample double definition

* use new backend in tests

* make a clean git mv instead of just renaming (1)

* make a clean git mv instead of just renaming (2)

* add vqc + test

* update vqc and tests

* update tutorials

* restructure tutorials

* cross_entropy sigmod, softmax, softmax_stable added

* cross_entropy sigmod, softmax, softmax_stable added

* cross_entropy sigmod added to NN classifier

* cross_entropy and sigmod_cross_entropy has been changed to "CrossEntryLoss" and "CrossEntropySigmodLoss" in both classifier and regression; initial version of NN regressor

* fix loss + minor changes

* revert changes in raw_feature_vector as non-required.

* update torch connector tests

* minor fixes

* Update 02_neural_network_classifier_and_regressor.ipynb

* Update 02_neural_network_classifier_and_regressor.ipynb

* use given gradient in circuit qnn

* remove double output_shape handling

* style, lint

* update tutorial to use regressor

* Create 05_torch_connector.ipynb

* fix mypy

* merged with new RawFeatureVector, some tests fail

* fix test randomness, disable some of test_raw_feature_vector

* One hot encoding has been removed from NNregression

* randomness, tests

* fix tutorial

* fix tutorial

* fix vqc test with bfgs and qasm simulator

* fix raw feature vector tests

* import fix

* Update 02_neural_network_classifier_and_regressor.ipynb

* test neural network regressor

* 1) 1 bug in NNregressor has been fixed
2) add VQR

* revert notebook to python 3

* 1. VQR updated with TwoLayerQNN
2. fit, predict and score functions were removed from VQR

* new readme test, fix style, lint

* updated vqc notebook, refactored readme test

* update circuit QNN to remove final circuit measurements for gradient

* remove old tutorials

* update TwoLayerQNN and VQR to use default feature map and var form

* update init files

* update NNC/NNR tutorial (02)

* update vqc and vqr

* Update 02_neural_network_classifier_and_regressor.ipynb

* fix lint, mypy

* Update 02_neural_network_classifier_and_regressor.ipynb

* delete 03_vqc

* fix merging problems

* update tests and dict

* Delete 05_torch_connector.ipynb

* added test_vqr.py

* updated readme

* update tutorials

* fix notebook 04

* fix 05 notebook title level inconsistent

* Update test_torch_connector.py

Co-authored-by: beichensinn <beichen2@ie.ibm.com>
Co-authored-by: Manoel Marques <manoel.marques@ibm.com>
Co-authored-by: Anton Dekusar <adekusar@ie.ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants