Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in quantum_info.linalg_operations.matrix_power for negative powers of singular matrices #192

Merged
merged 6 commits into from
Oct 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
singular matrices
  • Loading branch information
renatomello committed Oct 4, 2024
commit b2658ea2e3ba535427684c06c3a2ad7e8a5273b0
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
Loading