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

Track qubit live-ness during simulation #2020

Merged
merged 5 commits into from
Nov 15, 2024
Merged

Conversation

swernli
Copy link
Collaborator

@swernli swernli commented Nov 13, 2024

This change adds some basic qubit live-ness tracking to the simulator, achieved by changing the Value::Qubit variant to a weak pointer to the qubit and having the execution environment own and track the Rc that owns the qubit. This provides a few benefits:

  • Fixes a panic when a qubit is passed to the simulator after it has been released.
  • Adds graceful error handling to treat use-after-release as a runtime error.
  • Adds a specific error for "double release" of a qubit if it is being manually managed.
  • Updates the display of the qubit type to show the qubit ID when it is live and Qubit<released> when it is not, enabling easier debugging of qubit lifetimes.

Of note, local perf testing showing no impact on performance (thanks, Rust zero-cost abstractions!), though it stands to reason this change could theoretically have some impact on programs that use many thousands of qubits.

This change adds some basic qubit live-ness tracking to the simulator, achieved by changing the `Value::Qubit` variant to a weak pointer to the qubit and having the execution environment own and track the `Rc` that owns the qubit. This provides a few benefits:

- Fixes a panic when a qubit is passed to the simulator after it has been released.
- Adds graceful error handling to treat use-after-release as a runtime error.
- Adds a specific error for "double release" of a qubit if it is being manually managed.
- Updates the display of the qubit type to show the qubit ID when it is live and `Qubit<released>` when it is not, enabling easier debugging of qubit lifetimes.

Of note, local perf testing showing no impact on performance (thanks, Rust zero-cost abstractions!), though it stands to reason this change could theoretically have some impact on programs that use many thousands of qubits.
Copy link
Member

@minestarks minestarks left a comment

Choose a reason for hiding this comment

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

live-ness

I feel like the word you're looking for is "lifetime"?

compiler/qsc_eval/src/val.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@sezna sezna left a comment

Choose a reason for hiding this comment

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

Big fan of QubitRef

@swernli swernli added this pull request to the merge queue Nov 15, 2024
Merged via the queue into main with commit 322acf6 Nov 15, 2024
18 checks passed
@swernli swernli deleted the swernli/qubit-reference-counting branch November 15, 2024 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants