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

takagi fix #394

merged 12 commits into from
Jul 8, 2024

Conversation

RyosukeNORO
Copy link
Collaborator

@RyosukeNORO RyosukeNORO commented Jul 5, 2024

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)

@elib20
Copy link
Collaborator

elib20 commented Jul 5, 2024

@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.

@RyosukeNORO RyosukeNORO changed the title added the calculation method of diagonal matrix to takagi, and test… takagi fix Jul 5, 2024
@@ -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))):
Copy link
Collaborator

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

d = np.diag(A)
U = np.diag(np.exp(1j * 0.5 * np.angle(d)))
l = np.abs(d)
if svd_order is False:
Copy link
Collaborator

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')
Copy link
Collaborator

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.

Copy link
Collaborator

@nquesada nquesada left a 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.

@nquesada
Copy link
Collaborator

nquesada commented Jul 5, 2024

@elib20 : for the complaint about too many returns you add #pylint ignore to tell it to shut up ;)

RyosukeNORO and others added 3 commits July 5, 2024 13:43
Co-authored-by: Nicolas Quesada <991946+nquesada@users.noreply.github.com>
and added the diagonal matrix explicitly to test_decompositions.py.
Copy link
Collaborator

@nquesada nquesada left a 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.

RyosukeNORO and others added 2 commits July 5, 2024 13:55
Co-authored-by: Nicolas Quesada <991946+nquesada@users.noreply.github.com>
Comment on lines 22 to 25
from thewalrus.decompositions import (
williamson,
blochmessiah,
takagi,
Copy link
Collaborator

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.

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 did black file.py. Should I do black -l 100 file.py instead?

Copy link
Collaborator

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.

Copy link
Collaborator

@timmysilv timmysilv left a 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 👍

Comment on lines +212 to +214
if svd_order:
return l[::-1], U[:, ::-1]
return l, U
Copy link
Collaborator

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.

Suggested change
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
Copy link

codecov bot commented Jul 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (f935053) to head (b5fbd06).
Report is 2 commits behind head on master.

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     
Files with missing lines Coverage Δ
thewalrus/decompositions.py 100.00% <100.00%> (ø)

... and 25 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f935053...b5fbd06. Read the comment docs.

@nquesada
Copy link
Collaborator

nquesada commented Jul 5, 2024

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.

@@ -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):
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.

Copy link
Collaborator

@nquesada nquesada left a comment

Choose a reason for hiding this comment

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

Great job @RyosukeNORO !

@RyosukeNORO RyosukeNORO merged commit 0e51518 into master Jul 8, 2024
5 checks passed
@RyosukeNORO RyosukeNORO deleted the takagi-sqrtm-fix branch July 8, 2024 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants