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

Fix TensorFlow v2.17 compatibility by removing optimizer caching #489

Open zeyueN opened 2 months ago

zeyueN commented 2 months ago

User description

Context:

Description of the Change:

Benefits:

Possible Drawbacks:

Related GitHub Issues:


PR Type

bug_fix


Description


Changes walkthrough ๐Ÿ“

Relevant files
Bug fix
backend_manager.py
Remove caching for Euclidean optimizer to fix compatibility

mrmustard/math/backend_manager.py
  • Removed caching of the Euclidean optimizer object.
  • Always create a new Euclidean optimizer instance.
  • +1/-2     

    ๐Ÿ’ก 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 2 months 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
    ๐Ÿงช No relevant tests
    ๐Ÿ”’ No security concerns identified
    โšก Key issues to review

    Performance Concern
    Creating a new optimizer instance on every property access might impact performance if called frequently
    codiumai-pr-agent-pro[bot] commented 2 months 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
    Initialize the optimizer in the constructor instead of the property getter ___ **Consider initializing self._euclidean_opt in the class constructor instead of in the
    property getter. This would ensure that the optimizer is created only once when the
    object is instantiated, rather than every time the property is accessed.** [mrmustard/math/backend_manager.py [1316-1320]](https://github.com/XanaduAI/MrMustard/pull/489/files#diff-272a316cdf15707ddb7384f13258135f593fc560e21dc19cf17c16cfb5d0aa1cR1316-R1320) ```diff +def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self._euclidean_opt = self.DefaultEuclideanOptimizer() + @property def euclidean_opt(self): r"""The configured Euclidean optimizer.""" - self._euclidean_opt = self.DefaultEuclideanOptimizer() return self._euclidean_opt ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 8 Why: This suggestion improves performance by ensuring the optimizer is created only once during object instantiation, rather than every time the property is accessed. This change aligns with typical object-oriented design practices.
    8
    Best practice
    โœ… Rename property to method if new instance creation is intended ___ **If creating a new optimizer instance on every access is intentional, consider
    renaming the property to a method (e.g., get_euclidean_opt()) to better reflect its
    behavior of always returning a new instance.** [mrmustard/math/backend_manager.py [1316-1320]](https://github.com/XanaduAI/MrMustard/pull/489/files#diff-272a316cdf15707ddb7384f13258135f593fc560e21dc19cf17c16cfb5d0aa1cR1316-R1320) ```diff -@property -def euclidean_opt(self): - r"""The configured Euclidean optimizer.""" - self._euclidean_opt = self.DefaultEuclideanOptimizer() - return self._euclidean_opt +def get_euclidean_opt(self): + r"""Get a new instance of the configured Euclidean optimizer.""" + return self.DefaultEuclideanOptimizer() ``` `[Suggestion has been applied]`
    Suggestion importance[1-10]: 7 Why: Renaming the property to a method clarifies the behavior of returning a new instance each time, which improves code readability and aligns with the intended functionality. However, it may not be necessary if the behavior is not intentional.
    7