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

Takagi tol #393

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

### Bug fixes

* Added tolerance parameter to `deecompositions.takagi` to prevent from incorrect Autonne-Takagi decomposition, which happened with some matrices. [(#393)](https://github.com/XanaduAI/thewalrus/pull/393)

### Documentation

### Contributors
Expand Down
7 changes: 5 additions & 2 deletions thewalrus/decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def blochmessiah(S):
return O, D, Q


def takagi(A, svd_order=True):
def takagi(A, svd_order=True, tol=1e-16):
r"""Autonne-Takagi decomposition of a complex symmetric (not Hermitian!) matrix.
Note that the input matrix is internally symmetrized by taking its upper triangular part.
If the input matrix is indeed symmetric this leaves it unchanged.
Expand All @@ -162,6 +162,7 @@ def takagi(A, svd_order=True):
Args:
A (array): square, symmetric matrix
svd_order (boolean): whether to return result by ordering the singular values of ``A`` in descending (``True``) or ascending (``False``) order.
tol (float): tolerance parameter to kill small values in the matrix A. This was installed because takagi decomposition did not work at some matrix A.

Returns:
tuple[array, array]: (r, U), where r are the singular values,
Expand All @@ -174,7 +175,9 @@ def takagi(A, svd_order=True):
# Here we build a Symmetric matrix from the top right triangular part
A = np.triu(A) + np.triu(A, k=1).T

A = np.real_if_close(A)
# Kill small values in the matrix A. This was installed because takagi decomposition did not work at some matrix A with small values.
rtol = tol * np.amax(np.abs(A))
A = np.where(np.abs(A.real) <= rtol, 0, A.real) + np.where(np.abs(A.imag) <= rtol, 0, A.imag)

if np.allclose(A, 0):
return np.zeros(n), np.eye(n)
Expand Down
2 changes: 1 addition & 1 deletion thewalrus/internal_modes/prepare_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def state_prep(eps, W, thresh=1e-4, hbar=2):

def LO_overlaps(chis, LO_shape):
r"""
Computes the overlap integral between the orthonormal moes and the local oscillator shape
Computes the overlap integral between the orthonormal modes and the local oscillator shape

Args:
chis (list[array]): list of the temporal functions for the new orthonormal basis
Expand Down
Loading