shuaigroup / Renormalizer

Quantum dynamics package based on tensor network states
https://shuaigroup.github.io/Renormalizer/
Apache License 2.0
52 stars 16 forks source link

Fix OpSum bug #141

Closed liwt31 closed 2 years ago

liwt31 commented 2 years ago

Overriding __rmul__ of list has an interesting side effect

import numpy as np
a = np.float64(1)

# raises an error, as expected
b = a * [1, 2]

class MyList(list): pass
# also an error
b = a * MyList([1, 2])

class MyList(list):
    def __rmul__(self, other):
        print("rmul")
        return super().__rmul__(other)
# c is a numpy array, and "rmul" is not printed
c = a * MyList([1, 2])

In the OpSum class, we expect the multiplied result to be OpSum rather than np.ndarray, so override __array_ufunc__ to prevent the behavior.

codecov[bot] commented 2 years ago

Codecov Report

Base: 85.02% // Head: 85.00% // Decreases project coverage by -0.01% :warning:

Coverage data is based on head (af1225b) compared to base (6007bf3). Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #141 +/- ## ========================================== - Coverage 85.02% 85.00% -0.02% ========================================== Files 105 105 Lines 9889 9890 +1 ========================================== - Hits 8408 8407 -1 - Misses 1481 1483 +2 ``` | [Impacted Files](https://codecov.io/gh/shuaigroup/Renormalizer/pull/141?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=shuaigroup) | Coverage Δ | | |---|---|---| | [renormalizer/model/op.py](https://codecov.io/gh/shuaigroup/Renormalizer/pull/141/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=shuaigroup#diff-cmVub3JtYWxpemVyL21vZGVsL29wLnB5) | `80.12% <100.00%> (+0.12%)` | :arrow_up: | | [renormalizer/lib/davidson/davidson.py](https://codecov.io/gh/shuaigroup/Renormalizer/pull/141/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=shuaigroup#diff-cmVub3JtYWxpemVyL2xpYi9kYXZpZHNvbi9kYXZpZHNvbi5weQ==) | `69.48% <0.00%> (-0.94%)` | :arrow_down: | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=shuaigroup). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=shuaigroup)

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.