Skip to content

Commit bbf0ccf

Browse files
Merge pull request #1510 from qiboteam/torch_migration
Migrating Pytorch backend to Qiboml
2 parents 7898f00 + 832d43c commit bbf0ccf

21 files changed

+517
-1388
lines changed

doc/source/api-reference/qibo.rst

+4-5
Original file line numberDiff line numberDiff line change
@@ -2595,12 +2595,11 @@ The main calculation engine is defined in the abstract backend object
25952595
:class:`qibo.backends.abstract.Backend`. This object defines the methods
25962596
required by all Qibo models to perform simulation.
25972597

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

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

doc/source/code-examples/advancedexamples.rst

+22-11
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,9 @@ refer to the :ref:`Optimizers <Optimizers>` section of the documentation.
554554
Note that if the Stochastic Gradient Descent optimizer is used then the user
555555
has to use a backend based on tensorflow or pytorch primitives and not the default custom
556556
backend, as custom operators currently do not support automatic differentiation.
557-
To switch the backend one can do ``qibo.set_backend("tensorflow")`` or ``qibo.set_backend("pytorch")``.
557+
To switch the backend one can do ``qibo.set_backend(backend="qiboml", platform="tensorflow")``
558+
or ``qibo.set_backend(backend="qiboml", platform="pytorch")``, after ensuring the
559+
``qiboml`` package has been installed.
558560
Check the :ref:`How to use automatic differentiation? <autodiff-example>`
559561
section for more details.
560562

@@ -800,7 +802,7 @@ using the ``pytorch`` framework.
800802
import torch
801803
802804
from qibo import Circuit, gates, set_backend
803-
set_backend("pytorch")
805+
set_backend(backend="qiboml", platform="pytorch")
804806
805807
# Optimization parameters
806808
nepochs = 1000
@@ -1501,8 +1503,10 @@ combined with the readout mitigation:
15011503
Clifford Data Regression (CDR)
15021504
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15031505

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

15071511
.. testcode::
15081512

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

15271531
...
15281532

1529-
Again, the mitigated expected value improves over the noisy one and is also slightly better compared to ZNE.
1533+
Again, the mitigated expected value improves over the noisy one
1534+
and is also slightly better compared to ZNE.
15301535

15311536

15321537
Variable Noise CDR (vnCDR)
15331538
^^^^^^^^^^^^^^^^^^^^^^^^^^
15341539

1535-
Being a combination of ZNE and CDR, vnCDR requires you to define the noise levels as done in ZNE, and the same
1536-
caveat about the input circuit for CDR is valid here as well.
1540+
Being a combination of ZNE and CDR, vnCDR requires you to define
1541+
the noise levels as done in ZNE, and the same caveat about the
1542+
input circuit for CDR is valid here as well.
15371543

15381544
.. testcode::
15391545

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

15601566
...
15611567

1562-
The result is similar to the one obtained by CDR. Usually, one would expect slightly better results for vnCDR,
1563-
however, this can substantially vary depending on the circuit and the observable considered and, therefore, it is hard to tell
1568+
The result is similar to the one obtained by CDR.
1569+
Usually, one would expect slightly better results for vnCDR.
1570+
However, this can substantially vary depending on the circuit
1571+
and the observable considered and, therefore, it is hard to tell
15641572
a priori.
15651573

15661574

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

15921600
...
15931601

1594-
Again, the mitigated expected value improves over the noisy one and is also slightly better compared to ZNE.
1595-
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>`.
1602+
Again, the mitigated expected value improves over the noisy
1603+
one and is also slightly better compared to ZNE.
1604+
This was just a basic example usage of the three methods,
1605+
for all the details about them you should check the API-reference page
1606+
:ref:`Error Mitigation <error-mitigation>`.
15961607

15971608
.. _timeevol-example:
15981609

doc/source/code-examples/examples.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ evaluation performance, e.g.:
3939
.. code-block:: python
4040
4141
import numpy as np
42-
# switch backend to "tensorflow"
42+
# switch backend to "tensorflow" through the Qiboml provider
4343
import qibo
44-
qibo.set_backend("tensorflow")
44+
qibo.set_backend(backend="qiboml", platform="tensorflow")
4545
from qibo import Circuit, gates
4646
4747
circuit = Circuit(2)
@@ -54,7 +54,7 @@ evaluation performance, e.g.:
5454
init_state = np.ones(4) / 2.0 + i
5555
circuit(init_state)
5656
57-
Note that compiling is only supported when the native ``tensorflow`` backend is
57+
Note that compiling is only supported when the ``tensorflow`` backend is
5858
used. This backend is much slower than ``qibojit`` which uses custom operators
5959
to apply gates.
6060

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

228228
The measured shots are obtained using pseudo-random number generators of the
229-
underlying backend (numpy or Tensorflow). If the user has installed a custom
229+
underlying backend. If the user has installed a custom
230230
backend (eg. qibojit) and asks for frequencies with more than 100000 shots,
231231
a custom Metropolis algorithm will be used to obtain the corresponding samples,
232232
for increase performance. The user can change the threshold for which this

doc/source/getting-started/installation.rst

+39-49
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ Operating systems support
77
In the table below we summarize the status of *pre-compiled binaries
88
distributed with pypi* for the packages listed above.
99

10-
+------------------+------+---------+------------+
11-
| Operating System | qibo | qibojit | tensorflow |
12-
+==================+======+=========+============+
13-
| Linux x86 | Yes | Yes | Yes |
14-
+------------------+------+---------+------------+
15-
| MacOS >= 10.15 | Yes | Yes | Yes |
16-
+------------------+------+---------+------------+
17-
| Windows | Yes | Yes | Yes |
18-
+------------------+------+---------+------------+
10+
+------------------+------+---------+-----------+---------+
11+
| Operating System | qibo | qibojit |Tensorflow | Pytorch |
12+
+==================+======+=========+===========+=========+
13+
| Linux x86 | Yes | Yes | Yes | Yes |
14+
+------------------+------+---------+-----------+---------+
15+
| MacOS >= 10.15 | Yes | Yes | Yes | Yes |
16+
+------------------+------+---------+-----------+---------+
17+
| Windows | Yes | Yes | Yes | Yes |
18+
+------------------+------+---------+-----------+---------+
1919

2020
.. note::
2121
All packages are supported for Python >= 3.9.
@@ -148,35 +148,6 @@ Then proceed with the ``qibojit`` installation using ``pip``
148148
149149
_______________________
150150

151-
.. _installing-tensorflow:
152-
153-
tensorflow
154-
^^^^^^^^^^
155-
156-
If the `TensorFlow <https://www.tensorflow.org>`_ package is installed Qibo
157-
will detect and provide to the user the possibility to use ``tensorflow``
158-
backend.
159-
160-
This backend is used by default if ``qibojit`` is not installed, however, if
161-
needed, in order to switch to the ``tensorflow`` backend please do:
162-
163-
.. code-block:: python
164-
165-
import qibo
166-
qibo.set_backend("tensorflow")
167-
168-
In order to install the package, we recommend the installation using:
169-
170-
.. code-block:: bash
171-
172-
pip install qibo tensorflow
173-
174-
.. note::
175-
TensorFlow can be installed following its `documentation
176-
<https://www.tensorflow.org/install>`_.
177-
178-
_______________________
179-
180151
.. _installing-numpy:
181152

182153
numpy
@@ -197,26 +168,45 @@ please do:
197168
_______________________
198169

199170

200-
.. _installing-pytorch:
171+
.. _installing-qml-backends:
201172

202-
pytorch
203-
^^^^^^^
173+
Backends with automatic differentiation support
174+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
175+
176+
If you need automatic differentiation support, for tracing gradients of your
177+
quantum algorithm or for building some quantum machine learning routine,
178+
the right backends for you are those provided by the `Qiboml <https://github.com/qiboteam/qiboml>`__
179+
package.
204180

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

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

211193
.. code-block:: python
212194
213195
import qibo
214-
qibo.set_backend("pytorch")
196+
# in case of Tensorflow
197+
qibo.set_backend(backend="qiboml", platform="tensorflow")
198+
# in case of Pytorch
199+
qibo.set_backend(backend="qiboml", platform="pytorch")
215200
216-
In order to install the package, we recommend the installation using:
201+
In order to start using automatic differentiation tools with Qibo,
202+
we recommend the installation using:
217203

218204
.. code-block:: bash
219205
220-
pip install qibo torch
206+
pip install qibo qiboml tensorflow
221207
222-
_______________________
208+
or
209+
210+
.. code-block:: bash
211+
212+
pip install qibo qiboml torch

0 commit comments

Comments
 (0)