-
Notifications
You must be signed in to change notification settings - Fork 56
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
takagi fix #394
Changes from 1 commit
c41b49f
3ff6505
6e9b2b5
137820e
1ae82f1
05ed19d
7f1a795
cf82481
74c05b8
c81cfbb
6fb38fe
b5fbd06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,6 +202,15 @@ def takagi(A, svd_order=True): | |
vals, U = takagi(Amr, svd_order=svd_order) | ||
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))): | ||
d = np.diag(A) | ||
U = np.diag(np.exp(1j * 0.5 * np.angle(d))) | ||
l = np.abs(d) | ||
if svd_order is False: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to correct this. First you need to sort |
||
return l[::-1], U[:, ::-1] | ||
return l, U | ||
|
||
u, d, v = np.linalg.svd(A) | ||
U = u @ sqrtm((v @ np.conjugate(u)).T) | ||
if svd_order is False: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,6 +323,14 @@ def test_takagi_error(): | |
with pytest.raises(ValueError, match="The input matrix is not square"): | ||
takagi(A) | ||
|
||
def test_takagi_sepcific_matrix(): | ||
RyosukeNORO marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should write the matrix explicitly here instead of importing it from a |
||
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))) | ||
assert np.all(d >= 0) | ||
|
||
def test_real_degenerate(): | ||
"""Verify that the Takagi decomposition returns a matrix that is unitary and results in a | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need an
rtol
inside thenp.allclose