-
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 methods to more easily add operations to a DAGCircuit
from Rust
#13134
Comments
On the From #13094 (comment):
I think we're ready to implement something like this. It didn't exist originally only because we didn't need it when porting |
I'm fine with the method as described by Kevin, but I also think there ought to be ways to push operations back while passing an already-known Personally I have no problem with exposing a method to allow manual insertions into the interners as well - I don't think it should be the standard way to do that, but you can't break data coherence by doing it, since interners are append-only, and adding the key doesn't invalidate existing keys. That said, I don't immediately have uses for that in mind - I have a rough imagined optimisation to the Sabre post-routing rebuilder that pre-allocates the interner, but no proof it'd be a good idea - so I'm not pushing for adding them. |
Oh, fwiw, we probably want to think about the transfer of ownership of various objects as well - I don't know what patterns are going to be most useful, but we possibly want to make it so we don't need unnecessary heap allocations when the caller already has owned variants, etc. |
I'd think it probably makes sense to accept the bits as slices (e.g. So perhaps: fn apply_operation_back(
op: PackedOperation,
qargs: &[Qubit],
cargs: &[Clbit],
params: Option<SmallVec<[Param; 3]>>,
extra_attrs: Option<ExtraInstructionAttributes>,
) -> NodeIndex { ... } This way the user can move the by-value stuff in if they don't need it, or clone it explicitly and pass the clones, which |
Right, most of the time the keys will already exist for sure. If the caller is typically dealing with varying-sized gates, though, they might be collecting them into a |
About Were we to accept |
IMO that is another hint that merging #13127 would be helpful. There aren't really any cases where we want the inner value type of this struct to exist outside of a |
Sounds good, #13127 will be considered a blocker for this. I will also review said PR. |
What should we add?
The problem:
We crurrently have to methods that allow us to add an operation to the
DAGCircuit
from rust.DAGCircuit::push_back
andDAGCircuit::push_front
which accepts aPackedInstruction
instance as an argument and must have valid interned qargs and cargs.DAGCircuit::py_apply_operation_back
andDAGCircuit::py_apply_operation_front
which performs the addition of instructions in a similar way to what was done before, however this is only with python types and is not exposed to the rest of the rust api.In each case we're either required to re-intern the qargs/cargs, or have an already valid instance of
PackedInstruction
that could work in this circuit, which is what I ultimately did for #13032. But now that we are building transpiler passes in Rust alone, having to own mutable access to theDAGCircuit
and inserting qargs directly seems very unsafe.The solution
Adding rust-native
apply_operation_back
andapply_operation_front
toDAGCircuit
that is exposed to the rest of the crates.Having said methods would allow us to:
PackedOperation
of any type and insert qargs and cargs by index (Qubit
orClbit
as defined by thecircuit
crate), since that's how it is registered in theDAGCircuit
.DAGCircuit
, such as the interners orBitData
.DAGCircuit
instances from transpiler passes that need it, such as theBasisTranslator
.This shouldn't be too hard to implement and would be of great help to all of the developers currently working on transpiler passes.
Discussion
Feel free to start an educated discussion in the comments, I'd love to hear what the rest of the team thinks about this.
Tasks
Option<Box<_>>
intoExtraInstructionAttributes
. #13127apply_operation
methods #13143The text was updated successfully, but these errors were encountered: