Skip to content

Commit

Permalink
singular matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Oct 4, 2024
1 parent 4a81d92 commit b2658ea
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/qibojit/backends/gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,21 @@ def calculate_matrix_power(
self, matrix, power: Union[float, int], precision_singularity: float = 1e-14
):

if isinstance(power, int):
if isinstance(power, int) and power >= 0.0:
return self.cp.linalg.matrix_power(matrix, power)

if power < 0.0:
# negative powers of singular matrices via SVD
determinant = self.cp.linalg.det(matrix)
if abs(determinant) < precision_singularity:
U, S, Vh = self.cp.linalg.svd(matrix)
S_inv = self.cp.where(
self.cp.abs(S) < precision_singularity, 0.0, S**power
)
return (
self.cp.linalg.inv(Vh) @ self.cp.diag(S_inv) @ self.cp.linalg.inv(U)
)

copied = self.to_numpy(matrix)
copied = super().calculate_matrix_power(copied, power, precision_singularity)

Expand Down

0 comments on commit b2658ea

Please sign in to comment.