-
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
Conversation
… matrix to test_decompositions.py.
@timmysilv, what do you suggest we do about codefactor here? We have a function with a lot of edge cases where the computation simplifies, so we'd like to leverage that, but now it is complaining about too many return statements. |
takagi
, and test…
thewalrus/decompositions.py
Outdated
@@ -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))): |
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 the np.allclose
thewalrus/decompositions.py
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
You need to correct this. First you need to sort l
and rearrange U
accordingly. Then you can decide if you flip the order. As is, this will not return the takagi singular values sorted correctly.
def test_takagi_sepcific_matrix(): | ||
"""Test the takagi decomposition works well for a specific matrix that was not deecomposed accuratelyin a previous version. | ||
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 comment
The 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 npy
file. It is, after all, just a 3x3 matrix so it should not occupy a lof of space. If you want to minimize the space, you could write separately the real and imaginary part and the construct the matrix by summing them.
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.
Nice. Needs a few tweaks before approval.
@elib20 : for the complaint about too many returns you add #pylint ignore to tell it to shut up ;) |
Co-authored-by: Nicolas Quesada <[email protected]>
and added the diagonal matrix explicitly to test_decompositions.py.
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.
Nice. Basically there modulo small typos.
Co-authored-by: Nicolas Quesada <[email protected]>
from thewalrus.decompositions import ( | ||
williamson, | ||
blochmessiah, | ||
takagi, |
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.
did you do black -l 100 file.py
it seems to me like this changes should not happen.
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.
I did black file.py
. Should I do black -l 100 file.py
instead?
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.
Yup, that is the standard use in here.
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.
I've added an in-line suggestion for how to fix this. I also think mathematical functions with many branches are reasonable to have (provided they remain legible which this is imo), and I'm pretty sure you can avoid pylint warnings from Codefactor like any other time (adding pylint: disable=too-many-return-statements
to the function definition on line 155)
EDIT: I see the pylint disable comment above so I guess it should work 👍
if svd_order: | ||
return l[::-1], U[:, ::-1] | ||
return l, U |
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 can do the same thing on lines 218-220 as well if it helps, but I think this should suffice.
if svd_order: | |
return l[::-1], U[:, ::-1] | |
return l, U | |
return (l[::-1], U[:, ::-1]) if svd_order else (l, U) |
fixed the ordering of the matrix in takagi with diagonal matrix
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #394 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 28 28
Lines 1996 1912 -84
=========================================
- Hits 1996 1912 -84
... and 25 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
Almost there @RyosukeNORO In the test that you added you need to add an extra line to trigger the lines where the order of the takagi values and unitaries are flipped. With that you should get full coverage. |
added test to trigger both svd_order=True and False in takagi with diagonal matrix
thewalrus/decompositions.py
Outdated
@@ -202,6 +203,19 @@ 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)), rtol=1e-16): |
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.
I'd suggest you move this rtol
as an optional parameter in the signature of the function.
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.
I have moved rtol as an optional parameter of takagi function.
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.
Great job @RyosukeNORO !
Context:
There were issues with Takagi decomposition, specifically sqrtm, with some matrix. For the previous discussion, please see (#393)
Description of the Change:
added the calculation method when the matrix to be takagi decomposed is diagonal.
added the test code of the matrix for which the decomposition did not work well to test_decompositions.py.
Benefits:
Takagi decomposition works well.
Possible Drawbacks:
Related GitHub Issues:
(#393)