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 the custom_gradient issue with hermite_renormalized #506

Closed maliasadi closed 2 weeks ago

maliasadi commented 1 month ago

User description

Before submitting

Please complete the following checklist when submitting a PR:

When all the above are checked, delete everything above the dashed line and fill in the pull request template.


Context: This PR fixes the following MM issue with TensorFlow >= 2.15:

import tensorflow as tf
print(tf.__version__)

import numpy as np
import mrmustard.lab_dev as mrml
from mrmustard import math
from mrmustard.training import Optimizer
math.change_backend('tensorflow')

S = mrml.SqueezedVacuum([0], r=1.0, phi=0, r_trainable=True, phi_trainable=True)

# made up, means nothing
def cost():
    ket = S.fock(shape=[3])
    return - math.real(ket[2])

circuit = mrml.Circuit([S])

# we used to do this:
opt = Optimizer()
opt.minimize(cost, by_optimizing=[circuit], max_steps=20)
ValueError: custom_gradient function expected to return 4 gradients, but returned 3 instead.

This issue arises in custom operations where a gradient function is manually defined, and the expected number of gradients doesn't match the number returned by the function. To fix this issue, we updated all custom_gradient use cases in MM to return correct number of gradients. :heavy_check_mark:

Python formatting could also be improved with supporting different python version targets in black (to avoid formatting inconsistencies when using different py versions) and sorting imports using isort.


PR Type

enhancement, tests


Description

[sc-75755]


Changes walkthrough πŸ“

Relevant files
Enhancement
backend_tensorflow.py
Update gradient function to return extra None value           

mrmustard/math/backend_tensorflow.py - Modified the `grad` function to return an additional `None` value.
+1/-1     
Dependencies
pyproject.toml
Update TensorFlow version and add isort configuration       

pyproject.toml
  • Updated TensorFlow version to 2.17.0.
  • Added isort as a dependency.
  • Configured isort and black settings.
  • +11/-6   
    Configuration changes
    Makefile
    Enhance Makefile with isort and conditional formatting     

    Makefile
  • Added isort to the format command.
  • Introduced conditional checks for formatting.
  • +11/-2   
    Tests
    conftest.py
    Add hypothesis settings import                                                     

    tests/conftest.py - Added import for `hypothesis.settings`.
    +3/-1     
    Formatting
    test_circuit.py
    Reformat import statements for consistency                             

    tests/test_lab/test_circuit.py - Reformatted import statements for consistency.
    +1/-8     

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

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 πŸ”΅πŸ”΅πŸ”΅βšͺβšͺ
    πŸ… Score: 85
    πŸ§ͺ No relevant tests
    πŸ”’ No security concerns identified
    ⚑ Recommended focus areas for review

    Possible Bug
    The `grad` function in `hermite_renormalized` now returns an additional `None` value. This change might affect the behavior of the function and its callers. Code Smell
    Multiple import statements have been reordered and grouped. This change might affect the readability and organization of the code.
    codiumai-pr-agent-pro[bot] commented 1 month ago

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Best practice
    Specify exact versions for dependencies to improve reproducibility ___ **Consider specifying exact versions for dependencies to ensure reproducibility,
    especially for critical packages like TensorFlow.** [pyproject.toml [52-53]](https://github.com/XanaduAI/MrMustard/pull/506/files#diff-50c86b7ed8ac2cf95bd48334961bf0530cdc77b5a56f852c5c61b89d735fd711R52-R53) ```diff tensorflow = {version = "2.17.0" } -tensorflow-intel = { version = ">2.15.0", platform = "win32" } +tensorflow-intel = { version = "2.17.0", platform = "win32" } ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: Specifying exact versions for dependencies can enhance reproducibility by ensuring that the same versions are used across different environments. This is particularly important for critical packages like TensorFlow.
    7
    Enhancement
    Enhance user guidance by providing an example for the new check option ___ **Consider adding a help message for the new check option in the format target to
    improve user guidance.** [Makefile [21]](https://github.com/XanaduAI/MrMustard/pull/506/files#diff-76ed074a9305c04054cdebb9e9aad2d818052b07091de1f20cad0bbac34ffb52R21-R21) ```diff @echo " format [check=1] to run isort and black formatting; use with 'check=1' to check instead of modify" +@echo " Example: make format check=1" ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 5 Why: Adding an example for the new `check` option in the help message can improve user guidance and make it easier for users to understand how to use the new feature. This is a minor enhancement but can be helpful for clarity.
    5

    πŸ’‘ 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.41%. Comparing base (5fe4a5f) to head (60d106c). Report is 1 commits behind head on develop.

    Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #506 +/- ## ======================================== Coverage 89.41% 89.41% ======================================== Files 89 89 Lines 6026 6026 ======================================== Hits 5388 5388 Misses 638 638 ``` | [Files with missing lines](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/506?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/circuit\_components.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/506?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fcircuit_components.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvY2lyY3VpdF9jb21wb25lbnRzLnB5) | `99.09% <100.00%> (ΓΈ)` | | ------ [Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/506?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/506?dropdown=coverage&src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI). Last update [5fe4a5f...60d106c](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/506?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).