Skip to content
Closed
Changes from all commits
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
15 changes: 15 additions & 0 deletions tensorcircuit/backends/abstract_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@
e = self.sqrt(e)
return v @ self.diagflat(e) @ self.adjoint(v)

def sqrtmhpos(self: Any, a: Tensor) -> Tensor:
"""
Return the sqrtm of a known-to-be PSD Hermitian matrix ``a``.
:param a: tensor in matrix form
:type a: Tensor
:return: sqrtm of ``a`` after setting the negative eigenvalues (if they exist) to zero
:rtype: Tensor
"""
# maybe friendly for AD and also cosidering that several backend has no support for native sqrtm
e, v = self.eigh(a)
e = self.relu(e)
e = self.sqrt(e)
return v @ self.diagflat(e) @ self.adjoint(v)

Check warning on line 76 in tensorcircuit/backends/abstract_backend.py

View check run for this annotation

Codecov / codecov/patch

tensorcircuit/backends/abstract_backend.py#L73-L76

Added lines #L73 - L76 were not covered by tests
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.


def eigvalsh(self: Any, a: Tensor) -> Tensor:
"""
Get the eigenvalues of matrix ``a``.
Expand Down