-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add .index() and new() method to Qubit and Clbit #13284
Conversation
One or more of the following people are relevant to this code:
|
The Qubit and Clbit rust newtypes are a u32 that are used to track the Qubits or Clbits in a circuit. The u32 value represents the index of the Qubit or Clbit in the circuit and are often used as indices in Vecs or other structures where the index must be a usize. Accordingly there are a lot of times we need to convert a Qubit or Clbit object into a usize and previously the only way to do that was use `.0 as usize` on a given qubit object. To improve the ergonomics of that pattern this commit adds two new methods for converting from and to a usize new() and index() respectively. These are modeled after what petgraph does with it's NodeIndex type which is a similar pattern (although NodeIndex is generic over the inner type, although it defaults to u32).
Pull Request Test Coverage Report for Build 11242635350Details
💛 - Coveralls |
This is nice. A comment though: with both |
The problem is that as the inner type is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's certainly a nice improvement for us to be rid of the casting peppered everywhere!
My main suggestion would be to add an assertion to {Qu|Cl}bit::new
that the index
fits, which would make things more sound than what we have today.
I'm in favor of keeping the BitType
field public, esp. with the new constructor, since users really need to know that whatever they're stuffing into a qubit / clbit must fit within a u32
. Can you add an explicit warning to the new
methods that the usize
needs to fit within a BitType
?
This commit changes the behavior of the Qubit::new() and Clbit::new() constructors so that they panic if a user provides a usize that's too large for the u32 we store internally. Previously the way it was written it would just wrap the value to u32::MAX. Co-authored-by: Kevin Hartman <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes! This is looking good from my perspective now.
Summary
The Qubit and Clbit rust newtypes are a u32 that are used to track the Qubits or Clbits in a circuit. The u32 value represents the index of the Qubit or Clbit in the circuit and are often used as indices in Vecs or other structures where the index must be a usize. Accordingly there are a lot of times we need to convert a Qubit or Clbit object into a usize and previously the only way to do that was use
.0 as usize
on a given qubit object. To improve the ergonomics of that pattern this commit adds two new methods for converting from and to a usize new() and index() respectively. These are modeled after what petgraph does with it's NodeIndex type which is a similar pattern (although NodeIndex is generic over the inner type, although it defaults to u32).Details and comments