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 TensorFlowBackend to Qiboml #1479

Merged
merged 51 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
05c0463
feat: remove tf backend
MatteoRobbiati Oct 8, 2024
2a65005
feat: construct tf backend from qiboml
MatteoRobbiati Oct 8, 2024
c6264e1
tests: adapt tests to tf parsed from qiboml
MatteoRobbiati Oct 8, 2024
603ac3f
chore: updating lock
MatteoRobbiati Oct 8, 2024
31618c1
fix: rm wrong import
MatteoRobbiati Oct 8, 2024
3801382
chore: rm tensorflow from deps
MatteoRobbiati Oct 8, 2024
6fc80d3
fix: conflicts with master
MatteoRobbiati Oct 8, 2024
0ecd2b7
chore: restoring lock
MatteoRobbiati Oct 8, 2024
10d432a
fix: rm tf backend
MatteoRobbiati Oct 8, 2024
8f17625
change backend priorities
MatteoRobbiati Oct 8, 2024
108c4ad
chore: restore tf dependency in tests
MatteoRobbiati Oct 9, 2024
e4cad13
fix: conditions involving tf
MatteoRobbiati Oct 9, 2024
217f729
fix: tests updating the tf related checks
MatteoRobbiati Oct 10, 2024
eea11bd
Merge branch 'master' into tf_migration
MatteoRobbiati Oct 11, 2024
2d4a67c
fix: tf dependency
MatteoRobbiati Oct 11, 2024
ace74a9
pointing to working branch
MatteoRobbiati Oct 14, 2024
986b84a
Merge branch 'master' into tf_migration
MatteoRobbiati Oct 14, 2024
fe84d9e
chore: update lock
MatteoRobbiati Oct 14, 2024
0a76c00
fix: test models circuit parametrized
MatteoRobbiati Oct 15, 2024
82f540f
Merge branch 'master' into tf_migration
MatteoRobbiati Oct 15, 2024
4845926
fix: replacing backend.name with backend.platform in three more qibo …
MatteoRobbiati Oct 15, 2024
3380fa8
fix: name --> platform in utils.py
MatteoRobbiati Oct 15, 2024
e828343
fix: adapt tf tests to qiboml backend
MatteoRobbiati Oct 15, 2024
bc92554
fix: conflicts after merging master
MatteoRobbiati Oct 16, 2024
8049499
use aliases
renatomello Oct 16, 2024
f768db7
pylint
renatomello Oct 16, 2024
32c9224
lint improvements
renatomello Oct 16, 2024
2fdbf85
lint improvements
renatomello Oct 16, 2024
85df75d
rollback modification
renatomello Oct 16, 2024
205e260
update lock
renatomello Oct 16, 2024
e0d6267
chore: update lock after integrating last modifications into qiboml
MatteoRobbiati Oct 16, 2024
5c5fd92
Merge branch 'tf_migration' of github.com:qiboteam/qibo into tf_migra…
MatteoRobbiati Oct 16, 2024
d4a82d5
fix GPU tests
renatomello Oct 17, 2024
8c6e162
update lock (again)
renatomello Oct 17, 2024
403621b
remove alias
renatomello Oct 17, 2024
7a1c17f
update lock
renatomello Oct 17, 2024
d27446c
revert test order
renatomello Oct 17, 2024
596fd35
Update src/qibo/models/error_mitigation.py
renatomello Oct 17, 2024
5bbeeab
merging origin
MatteoRobbiati Oct 21, 2024
5f36868
Using QIBO_NATIVE_BACKENDS as Andrea suggested
MatteoRobbiati Oct 21, 2024
124816f
chore: update lock
MatteoRobbiati Oct 21, 2024
a968723
fix: ('clifford',) as tuple
MatteoRobbiati Oct 21, 2024
a8fda30
feat: removing direct dependence from tensorflow in tests
MatteoRobbiati Oct 23, 2024
1febc17
Merge branch 'master' into tf_migration
renatomello Oct 24, 2024
45b5870
Merge branch 'master' into tf_migration
MatteoRobbiati Oct 25, 2024
521cbe2
feat: removing tf imports from examples and docs
MatteoRobbiati Oct 25, 2024
652cd4d
chore: remove tf from tests deps
MatteoRobbiati Oct 25, 2024
f9e0f5c
chore: updating lock file
MatteoRobbiati Oct 25, 2024
2dc67c4
Merge branch 'master' into tf_migration
renatomello Oct 28, 2024
8a22e69
fix: remove tf from pyproject
MatteoRobbiati Oct 28, 2024
65a20a2
chore: pointing to qiboml main github repo
MatteoRobbiati Oct 28, 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
35 changes: 16 additions & 19 deletions doc/source/code-examples/advancedexamples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,8 @@ or programmatically, during runtime, as follows:
# retrieve the current number of threads
current_threads = qibo.get_threads()

On the other hand, when using the ``tensorflow`` backend Qibo inherits
Tensorflow's defaults for CPU thread configuration.
Tensorflow allows restricting the number of threads as follows:

.. code-block:: python
import tensorflow as tf
tf.config.threading.set_inter_op_parallelism_threads(1)
tf.config.threading.set_intra_op_parallelism_threads(1)
import qibo
Note that this should be run during Tensorflow initialization in the beginning
of the script and before creating the qibo backend.
For similar wariness when using a machine learning backend (such as TensorFlow or Pytorch)
please refer to the Qiboml documentation.
Copy link
Contributor

Choose a reason for hiding this comment

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

A link here would be probably helpful, even though I don't think we have any right now in qiboml. This is more a reminder to add it, once we have it.

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 think we can add a simple documentation in Qiboml for now, just with these instructions.


Using multiple GPUs
^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -707,13 +696,18 @@ circuit output matches a target state using the fidelity as the corresponding lo
Note that, as in the following example, the rotation angles have to assume real values
to ensure the rotational gates are representing unitary operators.

Qibo doesn't provide Tensorflow and Pytorch as native backends; Qiboml has to be
installed and used as provider of these quantum machine learning backends.

.. code-block:: python
import qibo
qibo.set_backend("tensorflow")
import tensorflow as tf
qibo.set_backend(backend="qiboml", platform="tensorflow")
from qibo import gates, models
backend = qibo.get_backend()
tf = backend.tf
# Optimization parameters
nepochs = 1000
optimizer = tf.keras.optimizers.Adam()
Expand All @@ -737,8 +731,9 @@ to ensure the rotational gates are representing unitary operators.
optimizer.apply_gradients(zip([grads], [params]))
Note that the ``"tensorflow"`` backend has to be used here because other custom
backends do not support automatic differentiation.
Note that the ``"tensorflow"`` backend has to be used here since it provides
automatic differentiation tools. To be constructed, the Qiboml package has to be
installed and used.

The optimization procedure may also be compiled, however in this case it is not
possible to use :meth:`qibo.circuit.Circuit.set_parameters` as the
Expand All @@ -748,10 +743,12 @@ For example:
.. code-block:: python
import qibo
qibo.set_backend("tensorflow")
import tensorflow as tf
qibo.set_backend(backend="qiboml", platform="tensorflow")
from qibo import gates, models
backend = qibo.get_backend()
tf = backend.tf
nepochs = 1000
optimizer = tf.keras.optimizers.Adam()
target_state = tf.ones(4, dtype=tf.complex128) / 2.0
Expand Down
4 changes: 2 additions & 2 deletions examples/anomaly_detection/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf

import qibo
from qibo import Circuit, gates
Expand All @@ -22,7 +21,8 @@ def main(n_layers, train_size, filename, plot, save_loss):
save_loss (bool): save losses for standard and anomalous data (default False).
"""

qibo.set_backend("tensorflow")
qibo.set_backend(backend="qiboml", platform="tensorflow")
tf = qibo.get_backend().tf

# Circuit ansatz
def make_encoder(n_qubits, n_layers, params, q_compression):
Expand Down
4 changes: 2 additions & 2 deletions examples/anomaly_detection/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from pathlib import Path

import numpy as np
import tensorflow as tf

import qibo
from qibo import Circuit, gates
Expand All @@ -23,7 +22,8 @@ def main(n_layers, batch_size, nepochs, train_size, filename, lr_boundaries):
lr_boundaries (list): epochs when learning rate is reduced, 6 monotone growing values from 0 to nepochs (default [3,6,9,12,15,18]).
"""

qibo.set_backend("tensorflow")
qibo.set_backend(backend="qiboml", platform="tensorflow")
tf = qibo.get_backend().tf

# Circuit ansatz
def make_encoder(n_qubits, n_layers, params, q_compression):
Expand Down
5 changes: 4 additions & 1 deletion examples/benchmarks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def limit_gpu_memory(memory_limit=None):
Args:
memory_limit: Memory limit in MBs.
"""
import tensorflow as tf
import qibo

qibo.set_backend(backend="qiboml", platform="tensorflow")
tf = qibo.get_backend().tf

if memory_limit is None:
print("\nNo GPU memory limiter used.\n")
Expand Down
4 changes: 3 additions & 1 deletion examples/qclustering/minimization.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import math

import numpy as np
import tensorflow as tf
from grover import grover_qc
from oracle import create_oracle_circ

import qibo
from qibo import gates
from qibo.models import Circuit

Expand All @@ -23,6 +23,8 @@ def duerr_hoyer_algo(distances):
int
New cluster assigned for that point.
"""
qibo.set_backend(backend="qiboml", platform="tensorflow")
tf = qibo.get_backend().tf

k = len(distances)
n = int(math.floor(math.log2(k)) + 1)
Expand Down
4 changes: 3 additions & 1 deletion examples/reuploading_classifier/qlassifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from matplotlib.cm import get_cmap
from matplotlib.colors import Normalize

import qibo
from qibo import Circuit, gates


Expand Down Expand Up @@ -117,7 +118,8 @@ def minimize(self, method="BFGS", options=None, compile=True):
parameters = r[1].result.xbest

elif method == "sgd":
import tensorflow as tf
qibo.set_backend(backend="qiboml", platform="tensorflow")
tf = qibo.get_backend().tf

circuit = self.circuit(self.training_set[0])

Expand Down
Loading