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

Optimize memory peak for _preprocess_state_vector in LightningTensor #943

Merged
merged 15 commits into from
Oct 15, 2024

Conversation

LuisAlfredoNu
Copy link
Contributor

@LuisAlfredoNu LuisAlfredoNu commented Oct 11, 2024

Before submitting

Please complete the following checklist when submitting a PR:

  • All new features must include a unit test.
    If you've fixed a bug or added code that should be tested, add a test to the
    tests directory!

  • All new functions and code must be clearly commented and documented.
    If you do make documentation changes, make sure that the docs build and
    render correctly by running make docs.

  • Ensure that the test suite passes, by running make test.

  • Add a new entry to the .github/CHANGELOG.md file, summarizing the
    change, and including a link back to the PR.

  • Ensure that code is properly formatted by running make format.

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


Context:
After profiling Lightning Tensor with scalene profiler. We found a bottleneck of memory in the function _preprocess_state_vector (code) which allocates 3 arrays with dimension 2 ** wires * wires.

Description of the Change:
Optimize the cartesian product to reduce the amount of memory necessary to set the StatePrep with LTensor

Benefits:
Reduce by half the peak of memory for large systems close to 30 qubit
image

Benchmark code

    import pennylane as qml
    import numpy as np

    wires = 27

    state = np.random.rand(2**(wires-1))
    state = state / np.linalg.norm(state)

    dev = qml.device('lightning.tensor', wires=wires)

    dev_wires = dev.wires.tolist()

    @qml.qnode(dev)
    def circuit(state=state,dev_wires=dev_wires):
        qml.StatePrep(state, wires=dev_wires[1:])
        return qml.expval(qml.Z(0)), qml.state()

    return  circuit(state, dev_wires)

Possible Drawbacks:
This change reduces readability but with a good improvement.

Related GitHub Issues:
[sc-75692]

Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit .github/CHANGELOG.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@LuisAlfredoNu LuisAlfredoNu added ci:build_wheels Activate wheel building. urgent Mark a pull request as high priority ci:use-gpu-runner Enable usage of GPU runner for this Pull Request labels Oct 11, 2024
@LuisAlfredoNu LuisAlfredoNu marked this pull request as ready for review October 11, 2024 20:36
Copy link

codecov bot commented Oct 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.16%. Comparing base (4945ed0) to head (e3f65eb).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #943      +/-   ##
==========================================
- Coverage   96.24%   95.16%   -1.09%     
==========================================
  Files         215       98     -117     
  Lines       29460    11922   -17538     
==========================================
- Hits        28353    11345   -17008     
+ Misses       1107      577     -530     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@mlxd mlxd left a comment

Choose a reason for hiding this comment

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

Feel free to ignore me if these comments are being a blocker. Otherwise, just looking for some clarifications :)

pennylane_lightning/lightning_tensor/_tensornet.py Outdated Show resolved Hide resolved
pennylane_lightning/lightning_tensor/_tensornet.py Outdated Show resolved Hide resolved
pennylane_lightning/lightning_tensor/_tensornet.py Outdated Show resolved Hide resolved
pennylane_lightning/lightning_tensor/_tensornet.py Outdated Show resolved Hide resolved
Copy link
Contributor

@AmintorDusko AmintorDusko left a comment

Choose a reason for hiding this comment

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

Thank you, @LuisAlfredoNu, for the very comprehensive explanation and in-office discussion. Happy to approve!

Copy link
Member

@mlxd mlxd left a comment

Choose a reason for hiding this comment

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

💯

@LuisAlfredoNu LuisAlfredoNu merged commit e1ec3ad into master Oct 15, 2024
86 of 87 checks passed
@LuisAlfredoNu LuisAlfredoNu deleted the tensor_state_prep branch October 15, 2024 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci:build_wheels Activate wheel building. ci:use-gpu-runner Enable usage of GPU runner for this Pull Request urgent Mark a pull request as high priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants