Skip to content

Commit

Permalink
Merge pull request #1308 from qiboteam/tf_complex
Browse files Browse the repository at this point in the history
Casting parameters as `float64` silencing `tf` warnings
  • Loading branch information
MatteoRobbiati authored May 13, 2024
2 parents c132711 + e119b8c commit f4ddfdd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
7 changes: 4 additions & 3 deletions doc/source/code-examples/advancedexamples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,9 @@ the following script optimizes the parameters of two rotations so that the circu
output matches a target state using the fidelity as the corresponding loss
function.

Note that, as in the following example, the rotation angles have to assume real values
to ensure the rotational gates are representing unitary operators.

.. code-block:: python
import qibo
Expand All @@ -716,7 +719,7 @@ function.
# Define circuit ansatz
params = tf.Variable(
tf.random.uniform((2,), dtype=tf.float64).astype(tf.complex128)
tf.random.uniform((2,), dtype=tf.float64)
)
c = models.Circuit(2)
c.add(gates.RX(0, params[0]))
Expand All @@ -729,7 +732,6 @@ function.
fidelity = tf.math.abs(tf.reduce_sum(tf.math.conj(target_state) * final_state))
loss = 1 - fidelity
grads = tape.gradient(loss, params)
grads = tf.math.real(grads)
optimizer.apply_gradients(zip([grads], [params]))
Expand Down Expand Up @@ -763,7 +765,6 @@ For example:
fidelity = tf.math.abs(tf.reduce_sum(tf.math.conj(target_state) * final_state))
loss = 1 - fidelity
grads = tape.gradient(loss, params)
grads = tf.math.real(grads)
optimizer.apply_gradients(zip([grads], [params]))
for _ in range(nepochs):
Expand Down
4 changes: 4 additions & 0 deletions src/qibo/backends/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ def __init__(self):
super().__init__()
self.name = "tensorflow"
os.environ["TF_CPP_MIN_LOG_LEVEL"] = str(TF_LOG_LEVEL)

import tensorflow as tf # pylint: disable=import-error
import tensorflow.experimental.numpy as tnp # pylint: disable=import-error

if TF_LOG_LEVEL >= 2:
tf.get_logger().setLevel("ERROR")

tnp.experimental_enable_numpy_behavior()
self.tf = tf
self.np = tnp
Expand Down
4 changes: 2 additions & 2 deletions tests/test_models_circuit_backpropagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_variable_backpropagation():
backend = construct_tensorflow_backend()
import tensorflow as tf

theta = tf.Variable(0.1234, dtype=tf.complex128)
theta = tf.Variable(0.1234, dtype=tf.float64)
# TODO: Fix parametrized gates so that `Circuit` can be defined outside
# of the gradient tape
with tf.GradientTape() as tape:
Expand All @@ -45,7 +45,7 @@ def test_two_variables_backpropagation():
backend = construct_tensorflow_backend()
import tensorflow as tf

theta = tf.Variable([0.1234, 0.4321], dtype=tf.complex128)
theta = tf.Variable([0.1234, 0.4321], dtype=tf.float64)
# TODO: Fix parametrized gates so that `Circuit` can be defined outside
# of the gradient tape
with tf.GradientTape() as tape:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_models_circuit_parametrized.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ def test_variable_theta():

import tensorflow as tf # pylint: disable=import-error

theta1 = tf.Variable(0.1234, dtype=tf.complex128)
theta2 = tf.Variable(0.4321, dtype=tf.complex128)
theta1 = tf.Variable(0.1234, dtype=tf.float64)
theta2 = tf.Variable(0.4321, dtype=tf.float64)
cvar = Circuit(2)
cvar.add(gates.RX(0, theta1))
cvar.add(gates.RY(1, theta2))
Expand Down

0 comments on commit f4ddfdd

Please sign in to comment.