XanaduAI / MrMustard

A differentiable bridge between phase space and Fock space
https://mrmustard.readthedocs.io/
Apache License 2.0
78 stars 27 forks source link

separated ket dm from base #510

Closed ziofil closed 3 weeks ago

ziofil commented 1 month ago

User description

Context: The file mrmustard/lab_dev/states/base.py was 1200+ lines long and it was hard to navigate.

Description of the Change: DM and Ket are now defined in their own files.

Benefits:

  1. Easier to navigate and understand where we are in the file (e.g. sometimes one had to scroll up or down to check whether a method with the same name was part of Ket or DM)
  2. Removed a few ugly instance checks
  3. states/base.py now is more abstract

Possible Drawbacks: Some minor code duplication because e.g. DM cannot use Ket.random in its own random method anymore (unless we allow for imports in methods).


PR Type

enhancement


Description


Changes walkthrough 📝

Relevant files
Enhancement
13 files
__init__.py
Refactor import statements for clarity and specificity     

mrmustard/lab_dev/states/__init__.py
  • Updated import statements to import specific classes instead of using
    wildcard imports.
  • Added explicit imports for Ket and DM classes.
  • +12/-10 
    base.py
    Refactor base state class to remove Ket and DM                     

    mrmustard/lab_dev/states/base.py
  • Removed Ket and DM class definitions from the file.
  • Adjusted imports and references to use new ket and dm modules.
  • Simplified the from_phase_space method return type.
  • +7/-700 
    coherent.py
    Update import for Ket class in coherent state                       

    mrmustard/lab_dev/states/coherent.py - Updated import to use `Ket` from the new `ket` module.
    +1/-1     
    displaced_squeezed.py
    Update import for Ket class in displaced squeezed state   

    mrmustard/lab_dev/states/displaced_squeezed.py - Updated import to use `Ket` from the new `ket` module.
    +1/-1     
    dm.py
    Create DM class in a separate module                                         

    mrmustard/lab_dev/states/dm.py
  • Created a new file for the DM class.
  • Implemented methods for density matrix operations and properties.
  • +421/-0 
    ket.py
    Create Ket class in a separate module                                       

    mrmustard/lab_dev/states/ket.py
  • Created a new file for the Ket class.
  • Implemented methods for ket state operations and properties.
  • +362/-0 
    number.py
    Update import for Ket class in number state                           

    mrmustard/lab_dev/states/number.py - Updated import to use `Ket` from the new `ket` module.
    +1/-1     
    quadrature_eigenstate.py
    Update import for Ket class in quadrature eigenstate         

    mrmustard/lab_dev/states/quadrature_eigenstate.py - Updated import to use `Ket` from the new `ket` module.
    +1/-1     
    sauron.py
    Update import for Ket class in Sauron state                           

    mrmustard/lab_dev/states/sauron.py - Updated import to use `Ket` from the new `ket` module.
    +1/-1     
    squeezed_vacuum.py
    Update import for Ket class in squeezed vacuum state         

    mrmustard/lab_dev/states/squeezed_vacuum.py - Updated import to use `Ket` from the new `ket` module.
    +1/-1     
    thermal.py
    Update import for DM class in thermal state                           

    mrmustard/lab_dev/states/thermal.py - Updated import to use `DM` from the new `dm` module.
    +1/-1     
    two_mode_squeezed_vacuum.py
    Update import for Ket class in two-mode squeezed vacuum state

    mrmustard/lab_dev/states/two_mode_squeezed_vacuum.py - Updated import to use `Ket` from the new `ket` module.
    +1/-1     
    vacuum.py
    Update import for Ket class in vacuum state                           

    mrmustard/lab_dev/states/vacuum.py - Updated import to use `Ket` from the new `ket` module.
    +1/-1     
    Tests
    1 files
    test_ansatz.py
    Update import for DM class in test cases                                 

    tests/test_physics/test_ansatz.py - Updated import to use `DM` from the new `dm` module.
    +1/-1     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    codiumai-pr-agent-pro[bot] commented 1 month ago

    PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🏅 Score: 85
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Code Duplication
    The `auto_shape` method is duplicated in both `Ket` and `DM` classes with very similar implementation. Consider refactoring to reduce duplication. Performance Concern
    The `random` method creates a full symplectic matrix and then extracts submatrices, which might be inefficient for large mode numbers. Consider optimizing this process.
    codiumai-pr-agent-pro[bot] commented 1 month ago

    PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Best practice
    ✅ Use a more numerically stable method for matrix inversion ___
    Suggestion Impact:The suggestion to use math.solve instead of math.inv was implemented, improving numerical stability. However, the implementation used math.transpose and math.dagger, which differs slightly from the suggestion. code diff: ```diff - A = S_2 @ math.conj(math.inv(S_1)) # use solve for inverse + A = math.transpose(math.solve(math.dagger(S_1), math.transpose(S_2))) ```
    ___ **Consider using math.solve instead of math.inv for better numerical stability when
    computing the inverse of a matrix.** [mrmustard/lab_dev/states/ket.py [210]](https://github.com/XanaduAI/MrMustard/pull/510/files#diff-065ae49c98ca8757bb66ef7763a81070e7b0eab2e0899ffebc2b4f2491d6df4eR210-R210) ```diff -A = S_2 @ math.conj(math.inv(S_1)) # use solve for inverse +A = math.solve(math.conj(S_1), S_2.T).T ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 9 Why: The suggestion to use `math.solve` instead of `math.inv` is a best practice for improving numerical stability when computing matrix inverses. This change enhances the robustness of the code.
    9
    ✅ Group related imports together to improve code organization and readability ___ **Consider grouping related imports together, such as placing Ket and DM imports next
    to each other, to improve code organization and readability.** [mrmustard/lab_dev/states/__init__.py [19-30]](https://github.com/XanaduAI/MrMustard/pull/510/files#diff-37701f830dda560cc6af996dc585644c803ee07e7a0639f11db31cb41044a766R19-R30) ```diff from .base import State from .ket import Ket from .dm import DM + from .coherent import Coherent from .displaced_squeezed import DisplacedSqueezed from .number import Number from .quadrature_eigenstate import QuadratureEigenstate from .squeezed_vacuum import SqueezedVacuum from .thermal import Thermal from .two_mode_squeezed_vacuum import TwoModeSqueezedVacuum from .vacuum import Vacuum from .sauron import Sauron ``` `[Suggestion has been applied]`
    Suggestion importance[1-10]: 5 Why: Grouping related imports can enhance readability and organization, but the impact is minor since the current order is already clear. The suggestion is valid but offers only a slight improvement.
    5
    Performance
    ✅ Use a more efficient method to check if a matrix is Hermitian ___ **Consider using a more efficient method to check if gamma_A is Hermitian, such as
    np.allclose(gamma_A, gamma_A.conj().T) instead of comparing norms.** [mrmustard/lab_dev/states/dm.py [79-82]](https://github.com/XanaduAI/MrMustard/pull/510/files#diff-17d0cf9d48cb14fcbaadec367030d4f41d5c782c2a069d5d9342569588e2ba38R79-R82) ```diff -if ( - math.real(math.norm(gamma_A - math.conj(gamma_A.T))) > settings.ATOL -): # checks if gamma_A is Hermitian +if not math.allclose(gamma_A, math.conj(gamma_A.T), atol=settings.ATOL): # checks if gamma_A is Hermitian return False ``` `[Suggestion has been applied]`
    Suggestion importance[1-10]: 8 Why: The suggestion to use `math.allclose` instead of comparing norms is valid and improves the efficiency and readability of the code. It directly checks if the matrix is Hermitian, which aligns with the intended functionality.
    8

    💡 Need additional feedback ? start a PR chat

    codecov[bot] commented 1 month ago

    Codecov Report

    Attention: Patch coverage is 97.23926% with 9 lines in your changes missing coverage. Please review.

    Project coverage is 89.41%. Comparing base (6ab5aec) to head (2805e5f). Report is 1 commits behind head on develop.

    Files with missing lines Patch % Lines
    mrmustard/lab_dev/states/dm.py 95.56% 7 Missing :warning:
    mrmustard/lab_dev/states/ket.py 98.57% 2 Missing :warning:
    Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #510 +/- ## =========================================== + Coverage 89.33% 89.41% +0.07% =========================================== Files 87 89 +2 Lines 5984 6029 +45 =========================================== + Hits 5346 5391 +45 Misses 638 638 ``` | [Files with missing lines](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI) | Coverage Δ | | |---|---|---| | [mrmustard/lab\_dev/states/\_\_init\_\_.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fstates%2F__init__.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvc3RhdGVzL19faW5pdF9fLnB5) | `100.00% <100.00%> (ø)` | | | [mrmustard/lab\_dev/states/base.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fstates%2Fbase.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvc3RhdGVzL2Jhc2UucHk=) | `96.57% <100.00%> (+0.06%)` | :arrow_up: | | [mrmustard/lab\_dev/states/coherent.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fstates%2Fcoherent.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvc3RhdGVzL2NvaGVyZW50LnB5) | `100.00% <100.00%> (ø)` | | | [mrmustard/lab\_dev/states/displaced\_squeezed.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fstates%2Fdisplaced_squeezed.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvc3RhdGVzL2Rpc3BsYWNlZF9zcXVlZXplZC5weQ==) | `100.00% <100.00%> (ø)` | | | [mrmustard/lab\_dev/states/number.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fstates%2Fnumber.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvc3RhdGVzL251bWJlci5weQ==) | `100.00% <100.00%> (ø)` | | | [mrmustard/lab\_dev/states/quadrature\_eigenstate.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fstates%2Fquadrature_eigenstate.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvc3RhdGVzL3F1YWRyYXR1cmVfZWlnZW5zdGF0ZS5weQ==) | `100.00% <100.00%> (ø)` | | | [mrmustard/lab\_dev/states/sauron.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fstates%2Fsauron.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvc3RhdGVzL3NhdXJvbi5weQ==) | `100.00% <100.00%> (ø)` | | | [mrmustard/lab\_dev/states/squeezed\_vacuum.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fstates%2Fsqueezed_vacuum.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvc3RhdGVzL3NxdWVlemVkX3ZhY3V1bS5weQ==) | `100.00% <100.00%> (ø)` | | | [mrmustard/lab\_dev/states/thermal.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fstates%2Fthermal.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvc3RhdGVzL3RoZXJtYWwucHk=) | `100.00% <100.00%> (ø)` | | | [...mustard/lab\_dev/states/two\_mode\_squeezed\_vacuum.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fstates%2Ftwo_mode_squeezed_vacuum.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvc3RhdGVzL3R3b19tb2RlX3NxdWVlemVkX3ZhY3V1bS5weQ==) | `100.00% <100.00%> (ø)` | | | ... and [3 more](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI) | | ------ [Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI). > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI) > `Δ = absolute (impact)`, `ø = not affected`, `? = missing data` > Powered by [Codecov](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?dropdown=coverage&src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI). Last update [6ab5aec...2805e5f](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/510?dropdown=coverage&src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI).