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

added mm_einsum #515

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

added mm_einsum #515

wants to merge 3 commits into from

Conversation

ziofil
Copy link
Collaborator

@ziofil ziofil commented Oct 28, 2024

User description

Context:
mm_einsum for contracting a TN made of CV and Fock components

Description of the Change:
For now just the mm_einsum.py file


PR Type

enhancement, documentation


Description

  • Introduced a new module mm_einsum.py for contracting tensor networks composed of continuous-variable (CV) and Fock components.
  • Implemented functions to calculate contraction costs and determine optimal contraction paths.
  • Enhanced performance using Numba's njit for just-in-time compilation.
  • Provided comprehensive docstrings to explain the functionality and usage of each function.

PRDescriptionHeader.CHANGES_WALKTHROUGH

Relevant files
Enhancement
mm_einsum.py
Implement `mm_einsum` for tensor network contraction         

mrmustard/physics/mm_einsum.py

  • Implemented mm_einsum for contracting tensor networks with CV and Fock
    components.
  • Added functions for calculating contraction costs and optimal paths.
  • Included detailed docstrings for all functions.
  • Utilized Numba for performance optimization.
  • +211/-0 

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

    @ziofil ziofil added WIP work in progress no changelog Pull request does not require a CHANGELOG entry labels Oct 28, 2024
    @codiumai-pr-agent-pro codiumai-pr-agent-pro bot added documentation Improvements or additions to documentation enhancement New feature or request Review effort [1-5]: 4 labels Oct 28, 2024
    Copy link
    Contributor

    PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🏅 Score: 92
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Performance Optimization
    The optimal function uses an exhaustive search algorithm which may become inefficient for large tensor networks. Consider implementing a heuristic approach or using dynamic programming for better scalability.

    Error Handling
    The code lacks explicit error handling for potential edge cases, such as empty input lists or invalid Fock dimensions. Consider adding appropriate error checks and exception handling.

    mrmustard/physics/mm_einsum.py Show resolved Hide resolved
    return frozenset(indices), 0


    def optimal(
    Copy link
    Contributor

    Choose a reason for hiding this comment

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

    In the optimal function, consider adding a timeout mechanism or a maximum iteration limit to prevent potential infinite loops or excessive runtime for very large input sets. This would improve the robustness of the function. [important]

    Copy link
    Contributor

    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
    Performance
    Use built-in Python functions for simple mathematical operations to reduce dependencies and improve performance

    Consider using math.prod() instead of np.prod() for calculating the product of
    integers. It's more efficient for small sequences and doesn't require importing
    NumPy.

    mrmustard/physics/mm_einsum.py [86]

    -fock_flops = np.prod(fock_contracted_shape) * np.prod(fock_remaining_shape)
    +from math import prod
    +fock_flops = prod(fock_contracted_shape) * prod(fock_remaining_shape)
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion to use math.prod() instead of np.prod() is valid as it reduces dependency on NumPy for simple integer operations, which can improve performance and readability for small sequences.

    7
    Best practice
    Use descriptive variable names to enhance code readability and self-documentation

    Consider using a more descriptive variable name instead of m in the _CV_flops
    function to improve code readability.

    mrmustard/physics/mm_einsum.py [21-28]

     @njit
    -def _CV_flops(nA: int, nB: int, m: int) -> int:
    +def _CV_flops(nA: int, nB: int, num_contracted_cv: int) -> int:
         """Calculate the cost of contracting two tensors with CV indices.
         Args:
             nA: Number of CV indices in the first tensor
             nB: Number of CV indices in the second tensor
    -        m: Number of CV indices involved in the contraction
    +        num_contracted_cv: Number of CV indices involved in the contraction
         """
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: Renaming the variable m to num_contracted_cv improves code readability by making the purpose of the variable clearer, which is a good practice for maintainability.

    6
    Use named constants for magic values to improve code clarity and maintainability

    Consider using a constant for the infinity value in the optimal function instead of
    float("inf") to improve readability and maintainability.

    mrmustard/physics/mm_einsum.py [162]

    -best_flops: int = float("inf")
    +import math
     
    +INFINITY = math.inf
    +best_flops: int = INFINITY
    +
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: Using a named constant for infinity improves code readability and maintainability by avoiding magic numbers, although the impact is relatively minor.

    5
    Enhancement
    Use defaultdict to simplify dictionary access and avoid explicit key existence checks

    Consider using collections.defaultdict for fock_size_dict to simplify the logic in
    the attempt_decomposition function and avoid potential KeyError exceptions.

    mrmustard/physics/mm_einsum.py [100-114]

    +from collections import defaultdict
    +
     def attempt_decomposition(
    -    indices: set[int], fock_size_dict: dict[int, int]
    +    indices: set[int], fock_size_dict: defaultdict[int, int]
     ) -> tuple[set[int], int]:
         """Attempt to reduce the number of indices by combining Fock indices when possible.
         Only possible if there is only one CV index and multiple Fock indices.
     
         Args:
             indices: Set of indices to potentially decompose
    -        fock_size_dict: Dictionary mapping indices to their sizes
    +        fock_size_dict: DefaultDict mapping indices to their sizes, with a default value of 0
     
         Returns:
             Tuple of (decomposed indices, cost of decomposition)
         """
    -    fock_indices_shape = [fock_size_dict[idx] for idx in indices if idx in fock_size_dict]
    -    cv_indices = [idx for idx in indices if idx not in fock_size_dict]
    +    fock_indices_shape = [fock_size_dict[idx] for idx in indices if fock_size_dict[idx] != 0]
    +    cv_indices = [idx for idx in indices if fock_size_dict[idx] == 0]
    • Apply this suggestion
    Suggestion importance[1-10]: 4

    Why: The suggestion to use defaultdict can simplify the code by removing the need for explicit key checks, but it introduces a new dependency and changes the logic slightly, which may not be necessary if the current implementation is already clear and correct.

    4

    💡 Need additional feedback ? start a PR chat

    Copy link

    codecov bot commented Oct 28, 2024

    Codecov Report

    Attention: Patch coverage is 0% with 59 lines in your changes missing coverage. Please review.

    Project coverage is 88.87%. Comparing base (e5c02ce) to head (01b56e7).

    Files with missing lines Patch % Lines
    mrmustard/physics/mm_einsum.py 0.00% 59 Missing ⚠️
    Additional details and impacted files
    @@             Coverage Diff             @@
    ##           develop     #515      +/-   ##
    ===========================================
    - Coverage    89.77%   88.87%   -0.90%     
    ===========================================
      Files           92       93       +1     
      Lines         6054     6113      +59     
    ===========================================
    - Hits          5435     5433       -2     
    - Misses         619      680      +61     
    Files with missing lines Coverage Δ
    mrmustard/physics/mm_einsum.py 0.00% <0.00%> (ø)

    ... and 1 file with indirect coverage changes


    Continue to review full report in Codecov by Sentry.

    Legend - Click here to learn more
    Δ = absolute <relative> (impact), ø = not affected, ? = missing data
    Powered by Codecov. Last update e5c02ce...01b56e7. Read the comment docs.

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

    Successfully merging this pull request may close these issues.

    1 participant