Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.11k stars 2.34k forks source link

Store Vars by index in `DAGCircuit` #13048

Open jlapeyre opened 1 month ago

jlapeyre commented 1 month ago

Closes #13027

Summary

This follows the plan in #1307. This PR is centered on augmenting this https://github.com/Qiskit/qiskit/blob/cc2edc9f7172d75ec454fdaf5748326cd3720f90/crates/circuit/src/dag_circuit.rs#L237-L240

with

pub vars: BitData<Var>,

That is, we use a Rust data structure BitData to store and manipulate variables, although the individual variables are still PyObjects. Previously, the Python-side containers and methods for manipulating variables were used.

Status

Most of this is done, modulo debugging because it does not yet successfully compile. The part that is left is in https://github.com/Qiskit/qiskit/blob/cc2edc9f7172d75ec454fdaf5748326cd3720f90/crates/circuit/src/dag_circuit.rs#L2866 This is a bit more work than all the rest because it involves iteration over the Vars and logic on the contents. For example, a set difference implemented as a Python call must be translated to or otherwise replaced in Rust.

It would be easier to a PR like this if there were some refactoring in dag_circuit.rs. For example, fn substitute_node_with_dag is over 500 lines long. One problem is the scope of local variables is not clear from the syntactic clues.