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

[dyn-alloc-base] Lightning dynamically allocate qubit memory #1043

Merged
merged 88 commits into from
Feb 10, 2025

Conversation

josephleekl
Copy link
Contributor

@josephleekl josephleekl commented Jan 21, 2025

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:
This PR enables lightning devices to dynamically allocate memory for statevectors just before execution. The usual qml.device("lightning.x", wires=y) is still supported, but user can also not provide wires, i.e. qml.device("lightning.x"), and the qubits required for the statevector is provided by the circuit

Description of the Change:
For LQ/LK/LG - the state-vector is only created at the execute step, and not during device creation. For all devices, if wires is provided during device creation, this will be the number of wires allocated in the device state-vector/tensornet; otherwise, it will be determined from the circuit to be executed.

Benefits:

dev = qml.device("lightning.qubit")
@qml.qnode(dev)
def circuit():
    qml.Identity(wires=3)
    qml.Identity(wires=1)
    qml.Hadamard(wires=0)
    return qml.state()


@qml.qnode(dev)
def circuit1():
    qml.Identity(wires=1)
    qml.Hadamard(wires=0)
    return qml.state()

circuit()
> array([0.70710678+0.j, 0.70710678+0.j, 0.        +0.j, 0.        +0.j,
       0.        +0.j, 0.        +0.j, 0.        +0.j, 0.        +0.j])
 
circuit1()
> array([0.70710678+0.j, 0.        +0.j, 0.70710678+0.j, 0.        +0.j])

Possible Drawbacks:

Related GitHub Issues:

[sc-82702]

Copy link

codecov bot commented Jan 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.95%. Comparing base (c44508c) to head (5586c12).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1043      +/-   ##
==========================================
+ Coverage   98.04%   98.95%   +0.91%     
==========================================
  Files         233      266      +33     
  Lines       39556    44514    +4958     
==========================================
+ Hits        38782    44049    +5267     
+ Misses        774      465     -309     

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

@josephleekl josephleekl marked this pull request as ready for review January 23, 2025 20:50
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.

Nice job! A few comments and suggestions.

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 @josephleekl! Nice job!

Copy link
Member

@maliasadi maliasadi left a comment

Choose a reason for hiding this comment

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

Nice work @josephleekl! Just a few minor comments before merging the PR 🎉

@AmintorDusko
Copy link
Contributor

Hey @josephleekl, super useful posting the links to the changes for each comment/suggestion. Much easier to follow, thank you! 🙏

Copy link
Member

@maliasadi maliasadi left a comment

Choose a reason for hiding this comment

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

Awesome! Happy to approve 🥳

@josephleekl josephleekl merged commit 93fafb1 into master Feb 10, 2025
94 checks passed
@josephleekl josephleekl deleted the dyn-alloc branch February 10, 2025 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci:use-gpu-runner Enable usage of GPU runner for this Pull Request ci:use-multi-gpu-runner Enable usage of Multi-GPU runner for this Pull Request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants