Skip to content

Commit

Permalink
solve errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Simone-Bordoni committed Sep 24, 2024
1 parent 061a43d commit 3ce8a2d
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 145 deletions.
14 changes: 14 additions & 0 deletions src/qibo/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ def _check_backend(backend):
return backend


def _find_backend(x):
"""Finds the backend of a given object."""
if isinstance(x, tuple) or isinstance(x, list):
return _find_backend(x[0])
if x.__class__.__name__ == "tensor":
return PyTorchBackend()
elif x.__class__.__name__ == "TensorflowTensor":
return TensorflowBackend()
elif isinstance(x, np.ndarray):
return NumpyBackend()
else:
raise TypeError("Unsupported type for backend detection")


def list_available_backends(*providers: str) -> dict:
"""Lists all the backends that are available."""
available_backends = MetaBackend().list_available()
Expand Down
34 changes: 21 additions & 13 deletions src/qibo/backends/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self):
# These functions in Torch works in a different way than numpy or have different names
self.np.transpose = self.np.permute
self.np.copy = self.np.clone
self.np.power = self.np.pow
self.np.expand_dims = self.np.unsqueeze
self.np.mod = self.np.remainder
self.np.right_shift = self.np.bitwise_right_shift
Expand Down Expand Up @@ -115,7 +116,6 @@ def cast(

if copy:
return x.clone()

return x

def matrix_parametrized(self, gate):
Expand All @@ -138,21 +138,29 @@ def matrix_parametrized(self, gate):
theta=gate.init_kwargs["theta"],
phi=gate.init_kwargs["phi"],
)
else:
if not isinstance(gate.parameters[0], self.np.Tensor):
parameters = tuple(
self.cast_parameter(param, trainable=gate.trainable)
for param in gate.parameters
)
gate.parameters = parameters
elif gate.parameters[0].requires_grad:
gate.trainable = True
else:
gate.trainable = False
_matrix = _matrix(*gate.parameters)
return _matrix
if not self.check_parameters(gate.parameters):
new_parameters = []
for parameter in gate.parameters:
if not isinstance(parameter, self.np.Tensor):
parameter = self.cast_parameter(parameter, trainable=gate.trainable)
elif parameter.requires_grad:
gate.trainable = True
new_parameters.append(parameter)
gate.parameters = tuple(new_parameters)
_matrix = _matrix(*gate.parameters)
return _matrix

def check_parameters(self, parameters):
"""Check if the parameters are torch tensors."""
for parameter in parameters:
if not isinstance(parameter, self.np.Tensor):
return False
return True

def cast_parameter(self, x, trainable):
if isinstance(x, int) and trainable:
return self.np.tensor(x, dtype=self.np.float64, requires_grad=True)
return self.np.tensor(x, requires_grad=trainable)

def is_sparse(self, x):
Expand Down
Loading

0 comments on commit 3ce8a2d

Please sign in to comment.