Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broadcasted gaussian integral #494

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open

Conversation

ziofil
Copy link
Collaborator

@ziofil ziofil commented Sep 26, 2024

User description

Context:
When circuit components have a large batch dimension, it's not efficient to loop over it when computing gaussian integrals.

Description of the Change:
Gaussian integrals now can be computed using broadcasting rules

Benefits:
Faster (about 10x)

Possible Drawbacks:
Implemented the version where two Abc triples are passed, not the one where a single Abc triple is being contracted

Related GitHub Issues:
None


PR Type

Enhancement, Tests


Description

  • Introduced a new complex_gaussian_integral_2 function in gaussian_integrals.py that supports batched inputs and uses broadcasting rules for efficient computation.
  • Enhanced error handling to check for batch size mismatches in the new function.
  • Added comprehensive tests in test_gaussian_integrals.py to validate the functionality of the new batched Gaussian integral computation, including tests for both batched and non-batched inputs and polynomial c parameter handling.

Changes walkthrough 📝

Relevant files
Enhancement
gaussian_integrals.py
Add batched Gaussian integral computation with broadcasting

mrmustard/physics/gaussian_integrals.py

  • Added a new complex_gaussian_integral_2 function with support for
    batched inputs.
  • Enhanced the function to compute Gaussian integrals using broadcasting
    rules.
  • Improved error handling for batch size mismatches.
  • +118/-80
    Tests
    test_gaussian_integrals.py
    Add tests for batched Gaussian integral computation           

    tests/test_physics/test_gaussian_integrals.py

  • Added tests for the new complex_gaussian_integral_2 function.
  • Included tests for both batched and non-batched inputs.
  • Added tests for polynomial c parameter handling.
  • +51/-3   

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

    @ziofil ziofil requested a review from apchytr September 26, 2024 17:24
    @ziofil ziofil added the no changelog Pull request does not require a CHANGELOG entry label Sep 26, 2024
    Copy link

    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: 3 🔵🔵🔵⚪⚪
    🏅 Score: 85
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Performance Concern
    The function creates a large zero matrix and then fills it using a loop, which could be inefficient for large batch sizes.

    Potential Bug
    The function assumes that the input tensors have at least 3 dimensions when batched, which may not always be true.

    @XanaduAI XanaduAI deleted a comment from codiumai-pr-agent-pro bot Sep 26, 2024
    idx1: Sequence[int],
    idx2: Sequence[int],
    measure: float = -1,
    batched: bool = False,
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    we could avoid needing a batched argument if we enforced Abc1, Abc2 to be batched by default

    Copy link
    Collaborator Author

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    True, but then one needs to go through hoops in order to use this function. Perhaps I can simply detect if the triple isn't batched and batch it if needed, and finally return it as it was given.

    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Hmm what hoops exactly? If this is a method we only use internally then things should be batched already (assuming we're consistent with that and if we're not we should fix it). If this is something we expose to users then yeah I'm okay with a batched argument

    X = math.block([[Z, I], [I, Z]])
    M = math.gather(math.gather(A, idx, axis=-1), idx, axis=-2) + X * measure
    bM = math.gather(b, idx, axis=-1)
    cpart1 = math.sqrt(math.cast((-1) ** m / math.det(M), "complex128"))
    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    I'm remembering having to introduce some logic for handling the case when math.det(M) is zero (see complex_gaussian_integral). E.g. this was an issue when calling (I believe) .normalize on QuadratureEigenstate. Should we have that here?

    Copy link
    Collaborator Author

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    I'm not sure. What were you doing in those situations?

    Copy link
    Collaborator

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    E.g. see line 136 in complex_gaussian_integral. I check if det is 0 and if it is handle it by using np.inf

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    enhancement New feature or request no changelog Pull request does not require a CHANGELOG entry Review effort [1-5]: 3 tests
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants