Skip to content

Commit 099d7a0

Browse files
authored
Merge pull request #1479 from qiboteam/tf_migration
Migrating TensorFlowBackend to Qiboml
2 parents 018075b + 65a20a2 commit 099d7a0

29 files changed

+1343
-1547
lines changed

doc/source/code-examples/advancedexamples.rst

+16-19
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,8 @@ or programmatically, during runtime, as follows:
7878
# retrieve the current number of threads
7979
current_threads = qibo.get_threads()
8080

81-
On the other hand, when using the ``tensorflow`` backend Qibo inherits
82-
Tensorflow's defaults for CPU thread configuration.
83-
Tensorflow allows restricting the number of threads as follows:
84-
85-
.. code-block:: python
86-
87-
import tensorflow as tf
88-
tf.config.threading.set_inter_op_parallelism_threads(1)
89-
tf.config.threading.set_intra_op_parallelism_threads(1)
90-
import qibo
91-
92-
Note that this should be run during Tensorflow initialization in the beginning
93-
of the script and before creating the qibo backend.
81+
For similar wariness when using a machine learning backend (such as TensorFlow or Pytorch)
82+
please refer to the Qiboml documentation.
9483

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

699+
Qibo doesn't provide Tensorflow and Pytorch as native backends; Qiboml has to be
700+
installed and used as provider of these quantum machine learning backends.
701+
710702
.. code-block:: python
711703
712704
import qibo
713-
qibo.set_backend("tensorflow")
714-
import tensorflow as tf
705+
qibo.set_backend(backend="qiboml", platform="tensorflow")
715706
from qibo import gates, models
716707
708+
backend = qibo.get_backend()
709+
tf = backend.tf
710+
717711
# Optimization parameters
718712
nepochs = 1000
719713
optimizer = tf.keras.optimizers.Adam()
@@ -737,8 +731,9 @@ to ensure the rotational gates are representing unitary operators.
737731
optimizer.apply_gradients(zip([grads], [params]))
738732
739733
740-
Note that the ``"tensorflow"`` backend has to be used here because other custom
741-
backends do not support automatic differentiation.
734+
Note that the ``"tensorflow"`` backend has to be used here since it provides
735+
automatic differentiation tools. To be constructed, the Qiboml package has to be
736+
installed and used.
742737

743738
The optimization procedure may also be compiled, however in this case it is not
744739
possible to use :meth:`qibo.circuit.Circuit.set_parameters` as the
@@ -748,10 +743,12 @@ For example:
748743
.. code-block:: python
749744
750745
import qibo
751-
qibo.set_backend("tensorflow")
752-
import tensorflow as tf
746+
qibo.set_backend(backend="qiboml", platform="tensorflow")
753747
from qibo import gates, models
754748
749+
backend = qibo.get_backend()
750+
tf = backend.tf
751+
755752
nepochs = 1000
756753
optimizer = tf.keras.optimizers.Adam()
757754
target_state = tf.ones(4, dtype=tf.complex128) / 2.0

examples/anomaly_detection/test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import matplotlib.pyplot as plt
55
import numpy as np
6-
import tensorflow as tf
76

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

25-
qibo.set_backend("tensorflow")
24+
qibo.set_backend(backend="qiboml", platform="tensorflow")
25+
tf = qibo.get_backend().tf
2626

2727
# Circuit ansatz
2828
def make_encoder(n_qubits, n_layers, params, q_compression):

examples/anomaly_detection/train.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from pathlib import Path
44

55
import numpy as np
6-
import tensorflow as tf
76

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

26-
qibo.set_backend("tensorflow")
25+
qibo.set_backend(backend="qiboml", platform="tensorflow")
26+
tf = qibo.get_backend().tf
2727

2828
# Circuit ansatz
2929
def make_encoder(n_qubits, n_layers, params, q_compression):

examples/benchmarks/main.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ def limit_gpu_memory(memory_limit=None):
4747
Args:
4848
memory_limit: Memory limit in MBs.
4949
"""
50-
import tensorflow as tf
50+
import qibo
51+
52+
qibo.set_backend(backend="qiboml", platform="tensorflow")
53+
tf = qibo.get_backend().tf
5154

5255
if memory_limit is None:
5356
print("\nNo GPU memory limiter used.\n")

examples/qclustering/minimization.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import math
22

33
import numpy as np
4-
import tensorflow as tf
54
from grover import grover_qc
65
from oracle import create_oracle_circ
76

7+
import qibo
88
from qibo import gates
99
from qibo.models import Circuit
1010

@@ -23,6 +23,8 @@ def duerr_hoyer_algo(distances):
2323
int
2424
New cluster assigned for that point.
2525
"""
26+
qibo.set_backend(backend="qiboml", platform="tensorflow")
27+
tf = qibo.get_backend().tf
2628

2729
k = len(distances)
2830
n = int(math.floor(math.log2(k)) + 1)

examples/reuploading_classifier/qlassifier.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from matplotlib.cm import get_cmap
66
from matplotlib.colors import Normalize
77

8+
import qibo
89
from qibo import Circuit, gates
910

1011

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

119120
elif method == "sgd":
120-
import tensorflow as tf
121+
qibo.set_backend(backend="qiboml", platform="tensorflow")
122+
tf = qibo.get_backend().tf
121123

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

0 commit comments

Comments
 (0)