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

Symplectic override on Ggate #495

Closed apchytr closed 1 month ago

apchytr commented 1 month ago

User description

Context: Ggate.symplectic should probably be returning the symplectic matrix it's defined with rather than recomputing.

Description of the Change: Overrride symplectic on Ggate to return the symplectic matrix stored in the param set.

Benefits: Avoid having to recompute the symplectic matrix


PR Type

enhancement, tests


Description


Changes walkthrough πŸ“

Relevant files
Enhancement
ggate.py
Override `symplectic` property to return stored matrix     

mrmustard/lab_dev/transformations/ggate.py
  • Added a property symplectic to return the stored symplectic matrix.
  • Replaced parameter_set.add_parameter with _add_parameter.
  • +7/-3     
    Tests
    test_ggate.py
    Add test for `symplectic` property in `Ggate`                       

    tests/test_lab_dev/test_transformations/test_ggate.py
  • Added an assertion to check if symplectic returns the correct matrix.
  • +1/-0     

    πŸ’‘ 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 πŸ”

    ⏱️ Estimated effort to review: 2 πŸ”΅πŸ”΅βšͺβšͺβšͺ
    πŸ… Score: 95
    πŸ§ͺ PR contains tests
    πŸ”’ No security concerns identified
    ⚑ No key issues to review
    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
    Add a tolerance parameter to the assertion for more robust testing ___ **Consider adding a tolerance parameter to the math.allclose() function call. This can
    help prevent potential issues with floating-point precision and make the test more
    robust.** [tests/test_lab_dev/test_transformations/test_ggate.py [33]](https://github.com/XanaduAI/MrMustard/pull/495/files#diff-e9c4d0c2193b280b6aa6f4c21a02bc9dc4161ce629671f0be05d20a4cb5bfd76R33-R33) ```diff -assert math.allclose(Eye.symplectic, math.eye(2)) +assert math.allclose(Eye.symplectic, math.eye(2), atol=1e-8) ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 9 Why: Introducing a tolerance parameter to the 'math.allclose()' function call increases the robustness of the test by accounting for floating-point precision issues. This is an important enhancement for ensuring reliable test results.
    9
    Add a docstring to the property for improved documentation ___ **Consider adding a docstring to the symplectic property to explain its purpose and
    return value. This will improve code documentation and make it easier for other
    developers to understand and use the property.** [mrmustard/lab_dev/transformations/ggate.py [66-68]](https://github.com/XanaduAI/MrMustard/pull/495/files#diff-bf96755105326dd7077e8fef650082b18d16970f6f49f0be585a4957ff99d023R66-R68) ```diff @property def symplectic(self): + """ + Returns the symplectic matrix associated with this Ggate. + + Returns: + RealMatrix: The symplectic matrix. + """ return self.parameter_set.symplectic.value ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 8 Why: Adding a docstring to the 'symplectic' property improves code documentation, making it easier for other developers to understand the purpose and return value of the property. This is a best practice that enhances code maintainability and usability.
    8
    Enhancement
    Improve parameter naming for better code readability ___ **Consider using a more descriptive name for the parameter. Instead of s, use
    symplectic_matrix to improve code readability and make the purpose of the parameter
    clearer.** [mrmustard/lab_dev/transformations/ggate.py [61-64]](https://github.com/XanaduAI/MrMustard/pull/495/files#diff-bf96755105326dd7077e8fef650082b18d16970f6f49f0be585a4957ff99d023R61-R64) ```diff self._representation = Bargmann.from_function( - fn=lambda s: Unitary.from_symplectic(modes, s).bargmann_triple(), + fn=lambda symplectic_matrix: Unitary.from_symplectic(modes, symplectic_matrix).bargmann_triple(), s=self.parameter_set.symplectic, ) ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: The suggestion to rename the parameter from 's' to 'symplectic_matrix' enhances code readability and clarity, making the code easier to understand and maintain. This is a valuable improvement, although it does not affect the functionality.
    7

    πŸ’‘ Need additional feedback ? start a PR chat

    codecov[bot] commented 1 month ago

    Codecov Report

    All modified and coverable lines are covered by tests :white_check_mark:

    Project coverage is 89.74%. Comparing base (068bdf7) to head (23f8548). Report is 1 commits behind head on develop.

    Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #495 +/- ## ======================================== Coverage 89.73% 89.74% ======================================== Files 104 104 Lines 7621 7623 +2 ======================================== + Hits 6839 6841 +2 Misses 782 782 ``` | [Files with missing lines](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/495?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/transformations/ggate.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/495?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Ftransformations%2Fggate.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvdHJhbnNmb3JtYXRpb25zL2dnYXRlLnB5) | `100.00% <100.00%> (ΓΈ)` | | ------ [Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/495?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/495?dropdown=coverage&src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI). Last update [068bdf7...23f8548](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/495?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).