Skip to content

Split PyDAGCircuit and DAGCircuit#16471

Open
raynelfss wants to merge 13 commits into
Qiskit:mainfrom
raynelfss:split-py-dag
Open

Split PyDAGCircuit and DAGCircuit#16471
raynelfss wants to merge 13 commits into
Qiskit:mainfrom
raynelfss:split-py-dag

Conversation

@raynelfss

@raynelfss raynelfss commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Partially fixes #15575

The following commits splits the representations of DAGCircuit into:

  • DAGCircuit representing the rust-native implementation of the DAG excluding its metadata.
  • PyDAGCircuit the version that gets exposed to Python.

PyDAGCircuit does not implement Deref<DAGCircuit> due to future plans requiring us to implement a reference counted Circuit for the Python version of the DAG.

The PR also applies changes to any and all transpiler passes working in Rust, as well as the C-API functions that operate at the Python level.

The changes on this PR are extensive and intrusive. So, as a reviewer, I'd suggest focusing on the changes in the qiskit/crates/circuit/src/dag_circuit.rs mainly as this is where the bulk of these changes exist. Review the rest of the items after we have reached a consensus as the changes in those files are much smaller.

Behavior changes

Control flow DAGCircuit blocks will no longer retain their metadata (which includes the DAGCircuit.name attribute) because these are Python only attributes and blocks are stored as their rust-native structs. Usage of metadata within ControlFlow blocks was never supported and users should upgrade their workflows if they were using metadata in this context.

AI/LLM disclosure

  • I didn't use LLM tooling, or only used it privately.
  • I used the following tool to help write this PR description:
  • I used the following tool to generate or modify code:

The following commit splits the representations of `DAGCircuit` into:
- `DAGCircuit` representing the rust-native implementation of the `DAG` excluding its metadata.
- `PyDAGCircuit` the version that gets exposed to Python.

`PyDAGCircuit` does not implement `Deref<DAGCircuit>` due to future plans requiring us to implement a reference counted `Circuit` for the Python version of the DAG.
The following commits make sure we preserve the metadata of the `DAGCircuit` in passes where the instance is copied. This includes:
- `CommutativeOptimization`
- `ConvertToPauliRotations`
- `DisjointLayout`
- `ElidePermutations`
- `LitinskiTransformation`
- `Split2QUnitaries`
- `TwoQubitPeepholeOptimization`
- `BasisTranslator`
- `SabreLayout`
- `SabreRouting`
- `UnitarySynthesis`
This commit moves the `name` attribute to exist only within `PyDAGCircuit` since it is Python exclusive metadata.
This commit also adds constructors with metadata from other `PyDAGCircuit` instances and a `DAGCircuit` from rust:
- `from_dagcircuit` which also accepts optional `metadata` and `name`.
- `from_dagcircuit_with_cloned_metadata` which accepts a reference to the `PyDAGCircuit` and copies over the attributes for the user.
The transpiler passes that don't perform transformations in place have been updated to use this model.
@raynelfss raynelfss changed the title [WIP] Split PyDAGCircuit and DAGCircuit Split PyDAGCircuit and DAGCircuit Jun 24, 2026
@raynelfss raynelfss added Changelog: Changed Add a "Changed" entry in the GitHub Release changelog. Rust This PR or issue is related to Rust code in the repository mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Jun 24, 2026
@raynelfss raynelfss added this to the 2.6.0 milestone Jun 24, 2026
@raynelfss raynelfss marked this pull request as ready for review June 24, 2026 18:13
@raynelfss raynelfss requested a review from a team as a code owner June 24, 2026 18:13
@raynelfss raynelfss requested a review from alexanderivrii June 24, 2026 18:13
@qiskit-bot

Copy link
Copy Markdown
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core
  • @levbishop

Some auxiliary methods would need to immutably or mutably obtain the dag and therefore should only remain as auxiliary methods to the `DAGCircuit` instead of the `PyDAGCircuit` instance.
The methods will now be called `try_read` and `try_write` and are fallible. This commit refactors the methods and updates their usage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: Changed Add a "Changed" entry in the GitHub Release changelog. mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library Rust This PR or issue is related to Rust code in the repository

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Separate PyDAGCircuit and PyCircuitData

2 participants