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

ParameterSet in `.dual` and `.adjoint` #492

Closed apchytr closed 1 month ago

apchytr commented 1 month ago

User description

Context: Sometimes it's useful to keep the parameter set of a circuit component after calling .dual or .adjoint. E.g. in the sampler if we wish to store POVM elements as State.dual and access their parameters dual.phi.value.

Description of the Change: .dual and .adjoint return a CC with the parameters of the original CC


PR Type

enhancement, tests


Description


Changes walkthrough πŸ“

Relevant files
Enhancement
circuit_components.py
Preserve parameters in `adjoint` and `dual` methods           

mrmustard/lab_dev/circuit_components.py
  • Added parameter copying in adjoint and dual methods.
  • Ensured parameters are retained in the returned CircuitComponent.
  • +4/-2     
    parameter_set.py
    Add equality comparison for `ParameterSet`                             

    mrmustard/math/parameter_set.py
  • Implemented __eq__ method for ParameterSet.
  • Allows comparison of ParameterSet objects.
  • +9/-0     
    Tests
    test_circuit_components.py
    Test parameter retention in `adjoint` and `dual`                 

    tests/test_lab_dev/test_circuit_components.py
  • Added assertions to test parameter retention in adjoint and dual.
  • Verified parameter set equality in tests.
  • +6/-0     
    test_circuits.py
    Update circuit representation tests for parameter display

    tests/test_lab_dev/test_circuits.py
  • Updated circuit representation tests.
  • Adjusted expected output for parameter set display.
  • +6/-6     
    test_parameter_set.py
    Test equality method in `ParameterSet`                                     

    tests/test_math/test_parameter_set.py
  • Added tests for __eq__ method in ParameterSet.
  • Verified equality and inequality of parameter sets.
  • +28/-0   

    πŸ’‘ PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    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 ✨

    CategorySuggestion                                                                                                                                    Score
    Performance
    Use a more efficient method for parameter copying in the adjoint and dual methods ___ **Consider using a more efficient method to copy parameters, such as a shallow copy or
    a dedicated method for parameter copying. This can improve performance, especially
    for components with many parameters.** [mrmustard/lab_dev/circuit_components.py [169-170]](https://github.com/XanaduAI/MrMustard/pull/492/files#diff-05cc8f1f470b3eab23b8a8b1b1a628a97c87852722ea6b5408e1bb70efd4ab72R169-R170) ```diff -for param in self.parameter_set.all_parameters.values(): - ret._add_parameter(param) +ret._copy_parameters_from(self) ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 8 Why: The suggestion to use a more efficient method for parameter copying is valid and can improve performance, especially for components with many parameters. It addresses a potential performance bottleneck in the code.
    8
    Enhancement
    Use a more concise and potentially more efficient method for equality comparison ___ **Consider using all() with generator expressions for a more concise and potentially
    more efficient equality check. This approach can short-circuit the comparison as
    soon as a mismatch is found.** [mrmustard/math/parameter_set.py [224-226]](https://github.com/XanaduAI/MrMustard/pull/492/files#diff-84eeda43424564d268538955f7993c6c8ce692b04fe55dd4e644adea4a06aea0R224-R226) ```diff -return ( - self._names == other._names - and self._constants == other._constants - and self._variables == other._variables +return all( + getattr(self, attr) == getattr(other, attr) + for attr in ('_names', '_constants', '_variables') ) ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: The suggestion to use `all()` with generator expressions for equality checks is a valid enhancement. It can make the code more concise and potentially improve performance by short-circuiting on the first mismatch.
    7
    Add tests for edge cases in parameter set equality comparison ___ **Consider adding tests for edge cases, such as comparing empty parameter sets or
    parameter sets with different orders of parameters. This will ensure the equality
    comparison is robust across various scenarios.** [tests/test_math/test_parameter_set.py [129-135]](https://github.com/XanaduAI/MrMustard/pull/492/files#diff-073ffa66b1738e2977f58d9fd433691c911f48748ee3c54485c819a8debc2206R129-R135) ```diff assert ps1 == ps2 - +assert ps2 == ps1 # Test symmetry +assert ParameterSet() == ParameterSet() # Test empty sets +ps4 = ParameterSet() +ps4.add_parameter(var1) +ps4.add_parameter(const1) +assert ps3 == ps4 # Test different order of parameters assert ps1 != ps3 ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: The suggestion to add tests for edge cases is a good enhancement. It ensures the robustness of the equality comparison across various scenarios, which is important for maintaining code reliability. However, it is not addressing a critical issue.
    7
    Add more detailed assertions for parameter set equality in tests ___ **Consider adding more specific assertions to test the individual parameters within
    the parameter sets. This will provide more detailed verification of the parameter
    copying process.** [tests/test_lab_dev/test_circuit_components.py [124]](https://github.com/XanaduAI/MrMustard/pull/492/files#diff-cf594d34002781ecbd6f00d55be035f5d6821af5cfde94038830e0e3ea5f5bc3R124-R124) ```diff assert d1_adj.parameter_set == d1.parameter_set +for param_name, param in d1.parameter_set.all_parameters.items(): + assert param_name in d1_adj.parameter_set.all_parameters + assert d1_adj.parameter_set.all_parameters[param_name] == param ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 6 Why: Adding more specific assertions provides a more thorough verification of the parameter copying process, enhancing the robustness of the tests. However, it's a minor enhancement rather than a critical fix.
    6

    πŸ’‘ 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.73%. Comparing base (068bdf7) to head (0af8230).

    Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #492 +/- ## ======================================== Coverage 89.73% 89.73% ======================================== Files 104 104 Lines 7621 7621 ======================================== Hits 6839 6839 Misses 782 782 ``` ------ [Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/492?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/492?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...0af8230](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/492?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).
    apchytr commented 1 month ago

    Implemented in Sampler