Skip to content

Conversation

@abhinavd
Copy link

@abhinavd abhinavd commented Jan 16, 2025

This method zeros out any negative eigenvalues before taking the matrix square root. This ensures that the matrix square root of a known-to-be PSD matrix continues to have real eigenvalues. This is useful sometimes in cases where one wants something like $\sqrt{A^\dagger A}$ but $A^\dagger A$ is imperfectly computed and has numerically small negative eigenvalues.

e, v = self.eigh(a)
e = self.relu(e)
e = self.sqrt(e)
return v @ self.diagflat(e) @ self.adjoint(v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about directly including relu line in sqrtmh function implementation as non-PSD matrix has no well defined sqrtm anyway?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should also work, although hopefully no one relies on the current behaviour of sqrtmh. That's why I thought it might be safest to have a different function itself. Or maybe add an optional keyword in sqrtmh to enforce PSD-ness.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abhinavd , ah, yes, sqrt can return complex values if the input is of negative value and in complex dtype. Then I think the better way is to add an optional keyword in sqrtmh, default as psd=False.

@codecov
Copy link

codecov bot commented Jan 18, 2025

Codecov Report

Attention: Patch coverage is 20.00000% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
tensorcircuit/backends/abstract_backend.py 20.00% 4 Missing ⚠️
Files with missing lines Coverage Δ
tensorcircuit/backends/abstract_backend.py 69.10% <20.00%> (-0.83%) ⬇️

@refraction-ray
Copy link
Member

Thanks for the contribution, fixed now by e105763

@abhinavd abhinavd deleted the add_sqrtmhpos branch January 21, 2025 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants