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 fix #394

Merged
merged 12 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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: 1 addition & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

### Bug fixes

* Add the calculation method of `takagi` when the matrix is diagonal.
* Add the calculation method of `takagi` when the matrix is diagonal. [(#394)](https://github.com/XanaduAI/thewalrus/pull/394)

### Documentation

Expand Down
6 changes: 4 additions & 2 deletions thewalrus/decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,13 @@ def takagi(A, svd_order=True):
return vals, U * np.exp(1j * phi / 2)

# If the matrix is diagonal, Takagi decomposition is easy
if np.allclose(A, np.diag(np.diag(A))):
if np.allclose(A, np.diag(np.diag(A)), rtol=1e-16):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd suggest you move this rtol as an optional parameter in the signature of the function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have moved rtol as an optional parameter of takagi function.

d = np.diag(A)
U = np.diag(np.exp(1j * 0.5 * np.angle(d)))
l = np.abs(d)
if svd_order is False:
l = np.sort(l)
U = U[np.argsort(l)]
if svd_order:
return l[::-1], U[:, ::-1]
return l, U

Expand Down
4 changes: 3 additions & 1 deletion thewalrus/tests/test_decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ def test_takagi_error():
def test_takagi_diagonal_matrix():
"""Test the takagi decomposition works well for a specific matrix that was not deecomposed accuratelyin a previous version.
RyosukeNORO marked this conversation as resolved.
Show resolved Hide resolved
See more info in PR #393 (https://github.com/XanaduAI/thewalrus/pull/393)"""
A = np.load('test_matrix_for_takagi.npy')
A = np.array([[-8.4509484628125742e-01+1.0349426984742664e-16j, 6.3637197288239186e-17-7.4398922703555097e-33j, 2.6734481396039929e-32+1.7155650257063576e-35j],
[ 6.3637197288239186e-17-7.4398922703555097e-33j, -2.0594021562561332e-01+2.2863956908382538e-17j, -5.8325863096557049e-17+1.6949718400585382e-18j],
[ 2.6734481396039929e-32+1.7155650257063576e-35j, -5.8325863096557049e-17+1.6949718400585382e-18j, 4.4171453199503476e-02+1.0022350742842835e-02j]])
d, U = takagi(A)
assert np.allclose(A, U @ np.diag(d) @ U.T)
assert np.allclose(U @ np.conjugate(U).T, np.eye(len(U)))
Expand Down
Binary file removed thewalrus/tests/test_matrix_for_takagi.npy
Binary file not shown.
Loading