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

Migrating Pytorch backend to Qiboml #1510

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
41e0b85
feat: migration of pytorch backend effects in qibo source code
MatteoRobbiati Oct 30, 2024
c1eef23
feat: adapt tests to pytorch migration
MatteoRobbiati Oct 30, 2024
d3255c1
chore: point to qiboml torch migration branch
MatteoRobbiati Oct 30, 2024
fdaf4c8
fix: import torch backend from qiboml
MatteoRobbiati Oct 30, 2024
86e1574
fix: adapt vqe and qaoa optimization tests to pytorch migration
MatteoRobbiati Oct 30, 2024
cbc6ed9
Merge branch 'master' into torch_migration
MatteoRobbiati Nov 1, 2024
ce623b1
docs: update docs after moving pytorch to qiboml
MatteoRobbiati Nov 1, 2024
77008a8
Update doc/source/code-examples/advancedexamples.rst
MatteoRobbiati Nov 12, 2024
b744521
Merge branch 'master' into torch_migration
MatteoRobbiati Nov 12, 2024
299d78a
docs: correct backends subection in api-reference documentation section
MatteoRobbiati Nov 12, 2024
1c38069
Merge branch 'torch_migration' of github.com:qiboteam/qibo into torch…
MatteoRobbiati Nov 12, 2024
dbf8cbe
Merge branch 'master' into torch_migration
renatomello Nov 14, 2024
9762788
fix test
renatomello Nov 14, 2024
70e26ac
Merge branch 'master' into torch_migration
MatteoRobbiati Nov 18, 2024
aa22507
chore: point to qiboml main branch
MatteoRobbiati Nov 18, 2024
5208307
Merge branch 'master' into torch_migration
MatteoRobbiati Nov 20, 2024
eed754f
chore: updating lock
MatteoRobbiati Nov 20, 2024
b9e340b
fix: set tensorflow to False in test_list_available_backends since re…
MatteoRobbiati Nov 20, 2024
832d43c
feat: rm torch and tf gradients test from qibo
MatteoRobbiati Nov 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions doc/source/api-reference/qibo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2595,12 +2595,11 @@ The main calculation engine is defined in the abstract backend object
:class:`qibo.backends.abstract.Backend`. This object defines the methods
required by all Qibo models to perform simulation.

Qibo currently provides two different calculation backends, one based on
numpy and one based on Tensorflow. It is possible to define new backends by
inheriting :class:`qibo.backends.abstract.Backend` and implementing
its abstract methods.
Qibo supports several backends (see the :ref:`Backend drivers section <backend-drivers>`),
which can be used depending on the specific needs:
lightweight simulation, quantum machine learning, hardware execution, etc.

An additional backend is shipped as the separate library qibojit.
Among them, the default choice is a backend provided by the qibojit library.
This backend is supplemented by custom operators defined under which can be
used to efficiently apply gates to state vectors or density matrices.

Expand Down
33 changes: 22 additions & 11 deletions doc/source/code-examples/advancedexamples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,9 @@ refer to the :ref:`Optimizers <Optimizers>` section of the documentation.
Note that if the Stochastic Gradient Descent optimizer is used then the user
has to use a backend based on tensorflow or pytorch primitives and not the default custom
backend, as custom operators currently do not support automatic differentiation.
To switch the backend one can do ``qibo.set_backend("tensorflow")`` or ``qibo.set_backend("pytorch")``.
To switch the backend one can do ``qibo.set_backend(backend="qiboml", platform="tensorflow")``
or ``qibo.set_backend(backend="qiboml", platform="pytorch")``, after ensuring the
``qiboml`` package has been installed.
Check the :ref:`How to use automatic differentiation? <autodiff-example>`
section for more details.

Expand Down Expand Up @@ -800,7 +802,7 @@ using the ``pytorch`` framework.
import torch

from qibo import Circuit, gates, set_backend
set_backend("pytorch")
set_backend(backend="qiboml", platform="pytorch")

# Optimization parameters
nepochs = 1000
Expand Down Expand Up @@ -1501,8 +1503,10 @@ combined with the readout mitigation:
Clifford Data Regression (CDR)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For CDR instead, you don't need to define anything additional. However, keep in mind that the input
circuit is expected to be decomposed in the set of primitive gates :math:`RX(\frac{\pi}{2}), CNOT, X` and :math:`RZ(\theta)`.
For CDR instead, you don't need to define anything additional.
However, keep in mind that the input circuit is expected to be
decomposed in the set of primitive gates
:math:`RX(\frac{\pi}{2}), CNOT, X` and :math:`RZ(\theta)`.

.. testcode::

Expand All @@ -1526,14 +1530,16 @@ circuit is expected to be decomposed in the set of primitive gates :math:`RX(\fr

...

Again, the mitigated expected value improves over the noisy one and is also slightly better compared to ZNE.
Again, the mitigated expected value improves over the noisy one
and is also slightly better compared to ZNE.


Variable Noise CDR (vnCDR)
^^^^^^^^^^^^^^^^^^^^^^^^^^

Being a combination of ZNE and CDR, vnCDR requires you to define the noise levels as done in ZNE, and the same
caveat about the input circuit for CDR is valid here as well.
Being a combination of ZNE and CDR, vnCDR requires you to define
the noise levels as done in ZNE, and the same caveat about the
input circuit for CDR is valid here as well.

.. testcode::

Expand All @@ -1559,8 +1565,10 @@ caveat about the input circuit for CDR is valid here as well.

...

The result is similar to the one obtained by CDR. Usually, one would expect slightly better results for vnCDR,
however, this can substantially vary depending on the circuit and the observable considered and, therefore, it is hard to tell
The result is similar to the one obtained by CDR.
Usually, one would expect slightly better results for vnCDR.
However, this can substantially vary depending on the circuit
and the observable considered and, therefore, it is hard to tell
a priori.


Expand Down Expand Up @@ -1591,8 +1599,11 @@ The use of iCS is straightforward, analogous to CDR and vnCDR.

...

Again, the mitigated expected value improves over the noisy one and is also slightly better compared to ZNE.
This was just a basic example usage of the three methods, for all the details about them you should check the API-reference page :ref:`Error Mitigation <error-mitigation>`.
Again, the mitigated expected value improves over the noisy
one and is also slightly better compared to ZNE.
This was just a basic example usage of the three methods,
for all the details about them you should check the API-reference page
:ref:`Error Mitigation <error-mitigation>`.

.. _timeevol-example:

Expand Down
8 changes: 4 additions & 4 deletions doc/source/code-examples/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ evaluation performance, e.g.:
.. code-block:: python

import numpy as np
# switch backend to "tensorflow"
# switch backend to "tensorflow" through the Qiboml provider
import qibo
qibo.set_backend("tensorflow")
qibo.set_backend(backend="qiboml", platform="tensorflow")
from qibo import Circuit, gates

circuit = Circuit(2)
Expand All @@ -54,7 +54,7 @@ evaluation performance, e.g.:
init_state = np.ones(4) / 2.0 + i
circuit(init_state)

Note that compiling is only supported when the native ``tensorflow`` backend is
Note that compiling is only supported when the ``tensorflow`` backend is
used. This backend is much slower than ``qibojit`` which uses custom operators
to apply gates.

Expand Down Expand Up @@ -226,7 +226,7 @@ For applications that require the state vector to be collapsed during measuremen
we refer to the :ref:`How to collapse state during measurements? <collapse-examples>`

The measured shots are obtained using pseudo-random number generators of the
underlying backend (numpy or Tensorflow). If the user has installed a custom
underlying backend. If the user has installed a custom
backend (eg. qibojit) and asks for frequencies with more than 100000 shots,
a custom Metropolis algorithm will be used to obtain the corresponding samples,
for increase performance. The user can change the threshold for which this
Expand Down
88 changes: 39 additions & 49 deletions doc/source/getting-started/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ Operating systems support
In the table below we summarize the status of *pre-compiled binaries
distributed with pypi* for the packages listed above.

+------------------+------+---------+------------+
| Operating System | qibo | qibojit | tensorflow |
+==================+======+=========+============+
| Linux x86 | Yes | Yes | Yes |
+------------------+------+---------+------------+
| MacOS >= 10.15 | Yes | Yes | Yes |
+------------------+------+---------+------------+
| Windows | Yes | Yes | Yes |
+------------------+------+---------+------------+
+------------------+------+---------+-----------+---------+
| Operating System | qibo | qibojit |Tensorflow | Pytorch |
+==================+======+=========+===========+=========+
| Linux x86 | Yes | Yes | Yes | Yes |
+------------------+------+---------+-----------+---------+
| MacOS >= 10.15 | Yes | Yes | Yes | Yes |
+------------------+------+---------+-----------+---------+
| Windows | Yes | Yes | Yes | Yes |
+------------------+------+---------+-----------+---------+
Comment on lines +10 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be updated to contemplate the CPU / GPU subdivision? Both qibojit and qiboml have backends that work on both.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know, since this is already shown in the backends diagram.


.. note::
All packages are supported for Python >= 3.9.
Expand Down Expand Up @@ -148,35 +148,6 @@ Then proceed with the ``qibojit`` installation using ``pip``

_______________________

.. _installing-tensorflow:

tensorflow
^^^^^^^^^^

If the `TensorFlow <https://www.tensorflow.org>`_ package is installed Qibo
will detect and provide to the user the possibility to use ``tensorflow``
backend.

This backend is used by default if ``qibojit`` is not installed, however, if
needed, in order to switch to the ``tensorflow`` backend please do:

.. code-block:: python

import qibo
qibo.set_backend("tensorflow")

In order to install the package, we recommend the installation using:

.. code-block:: bash

pip install qibo tensorflow

.. note::
TensorFlow can be installed following its `documentation
<https://www.tensorflow.org/install>`_.

_______________________

.. _installing-numpy:

numpy
Expand All @@ -197,26 +168,45 @@ please do:
_______________________


.. _installing-pytorch:
.. _installing-qml-backends:

pytorch
^^^^^^^
Backends with automatic differentiation support
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If you need automatic differentiation support, for tracing gradients of your
quantum algorithm or for building some quantum machine learning routine,
the right backends for you are those provided by the `Qiboml <https://github.com/qiboteam/qiboml>`__
package.

If the `PyTorch <https://pytorch.org/>`_ package is installed Qibo
will detect and provide to the user the possibility to use ``pytorch``
backend.
In particular, Qiboml currently support `Pytorch <https://pytorch.org/>`_ and
`Tensorflow <https://www.tensorflow.org>`_ interfaces, integrating the qibo functionalities
into these well-known machine learning frameworks. Quantum layers can be constructed
and added to your Pytorch or Tensorflow models, and trained using any supported
optimization routine.

In order to switch to the ``pytorch`` backend please do:
In order to use these quantum machine learning backends please make sure the
preferred package is installed following `Tensorflow's <https://www.tensorflow.org/install>`_
or `Pytorch's <https://pytorch.org/get-started/locally/>`_ installation instructions.

To switch to Tensorflow or Pytorch backend please do:
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure whether it is worth to have details about the ml backends. I would probably place this in the qiboml docs and link them here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, I would remove these lines. Probably it is better to wait the moment the Qiboml docs are ready so that we can just link to the proper section.


.. code-block:: python

import qibo
qibo.set_backend("pytorch")
# in case of Tensorflow
qibo.set_backend(backend="qiboml", platform="tensorflow")
# in case of Pytorch
qibo.set_backend(backend="qiboml", platform="pytorch")

In order to install the package, we recommend the installation using:
In order to start using automatic differentiation tools with Qibo,
we recommend the installation using:

.. code-block:: bash

pip install qibo torch
pip install qibo qiboml tensorflow

_______________________
or

.. code-block:: bash

pip install qibo qiboml torch
Loading
Loading