Skip to content

Add methods to more easily add operations to a DAGCircuit from Rust #13134

@raynelfss

Description

@raynelfss

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 and DAGCircuit::push_front which accepts a PackedInstruction instance as an argument and must have valid interned qargs and cargs.
  • DAGCircuit::py_apply_operation_back and DAGCircuit::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 the DAGCircuit and inserting qargs directly seems very unsafe.

The solution

Adding rust-native apply_operation_back and apply_operation_front to DAGCircuit that is exposed to the rest of the crates.

Having said methods would allow us to:

  • Insert a PackedOperation of any type and insert qargs and cargs by index (Qubit or Clbit as defined by the circuit crate), since that's how it is registered in the DAGCircuit.
  • Avoid having mutable access to private methods in the DAGCircuit, such as the interners or BitData.
  • Overall make it easier to modify and create DAGCircuit instances from transpiler passes that need it, such as the BasisTranslator.

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
- [ ] https://github.com/Qiskit/qiskit/pull/13127
- [ ] https://github.com/Qiskit/qiskit/pull/13143

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions