Skip to content

Commit

Permalink
Merge pull request #40 from qiboteam/tf-back
Browse files Browse the repository at this point in the history
Update TF backend with the one in Qibo
  • Loading branch information
MatteoRobbiati authored Oct 11, 2024
2 parents 7aa0942 + e1ab50b commit 7aa0eb9
Show file tree
Hide file tree
Showing 6 changed files with 1,043 additions and 698 deletions.
5 changes: 5 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ comment:

github_checks:
annotations: false

ignore:
- "src/qiboml/backends/tensorflow.py"
- "src/qiboml/backends/pytorch.py"
- "src/qiboml/backends/jax.py"
1,698 changes: 1,008 additions & 690 deletions poetry.lock

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ python = ">=3.9,<3.13"
numpy = "^1.26.4"
numba = "^0.59.0"
tensorflow = { version = "^2.16.1", markers = "sys_platform == 'linux' or sys_platform == 'darwin'" }
# TODO: the marker is a temporary solution due to the lack of the tensorflow-io 0.32.0's wheels for Windows, this package is one of
# the tensorflow requirements
torch = "^2.2.0"
jax = "^0.4.25"
jaxlib = "^0.4.25"
qibo = "^0.2.8"
# TODO: this is temporary and has to be updated with Qibo
qibo = { git = "https://github.com/qiboteam/qibo.git" }

[tool.poetry.group.dev]
optional = true
Expand Down
2 changes: 1 addition & 1 deletion src/qiboml/backends/jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def cast(self, x, dtype=None, copy=False):
dtype = self.dtype
if isinstance(x, self.tensor_types):
return x.astype(dtype)
elif self.issparse(x):
elif self.is_sparse(x):
return x.astype(dtype)
return self.np.array(x, dtype=dtype, copy=copy)

Expand Down
6 changes: 3 additions & 3 deletions src/qiboml/backends/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ def cast(

return x

def issparse(self, x):
def is_sparse(self, x):
if isinstance(x, self.np.Tensor):
return x.is_sparse

return super().issparse(x)
return super().is_sparse(x)

def to_numpy(self, x):
if isinstance(x, list):
Expand Down Expand Up @@ -151,7 +151,7 @@ def calculate_eigenvectors(self, matrix, k=6):
return self.np.linalg.eigh(matrix) # pylint: disable=not-callable

def calculate_matrix_exp(self, a, matrix, eigenvectors=None, eigenvalues=None):
if eigenvectors is None or self.issparse(matrix):
if eigenvectors is None or self.is_sparse(matrix):
return self.np.linalg.matrix_exp( # pylint: disable=not-callable
-1j * a * matrix
)
Expand Down
25 changes: 24 additions & 1 deletion src/qiboml/backends/tensorflow.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import collections
import os
from typing import Union

import numpy as np
from qibo import __version__
from qibo.backends.npmatrices import NumpyMatrices
from qibo.backends.numpy import NumpyBackend
from qibo.backends.numpy import NumpyBackend, _calculate_negative_power_singular_matrix
from qibo.config import TF_LOG_LEVEL, log, raise_error


Expand Down Expand Up @@ -194,6 +195,28 @@ def calculate_matrix_exp(self, a, matrix, eigenvectors=None, eigenvalues=None):
return self.tf.linalg.expm(-1j * a * matrix)
return super().calculate_matrix_exp(a, matrix, eigenvectors, eigenvalues)

def calculate_matrix_power(
self,
matrix,
power: Union[float, int],
precision_singularity: float = 1e-14,
):
if not isinstance(power, (float, int)):
raise_error(
TypeError,
f"``power`` must be either float or int, but it is type {type(power)}.",
)

if power < 0.0:
# negative powers of singular matrices via SVD
determinant = self.tf.linalg.det(matrix)
if abs(determinant) < precision_singularity:
return _calculate_negative_power_singular_matrix(
matrix, power, precision_singularity, self.tf, self
)

return super().calculate_matrix_power(matrix, power, precision_singularity)

def calculate_singular_value_decomposition(self, matrix):
# needed to unify order of return
S, U, V = self.tf.linalg.svd(matrix)
Expand Down

0 comments on commit 7aa0eb9

Please sign in to comment.