Skip to content

Conversation

@mtreinish
Copy link
Member

Summary

This commit splits out the Python only component of the DAGCircuit struct in Rust. The DAGCircuit struct has two fields that explicitly require Python, metadata and duration, and there are two more fields that are only ever used in Python, unit and name despite both being strings. To make the Python component distinct and separable this makes a separate Python interface struct PyDAGCircuit which wraps the inner pure rust DAGCircuit that contains the extra data for Python. This struct also defines all the Python methods so the inner rust DAGCircuit doesn't define any interface to Python anymore and it's all isolated to the

This commit will prepare us towards implementing #14240 to make pyo3 and needing python symbols a compile time option. While it is possible to conditionally compile all these aspects everywhere there are a few advantages an explict stuct provides. The first is making all the places we expect a Python interface very explicit. This makes it easy to reason about special handling only needed for python and isolate it to separate blocks. It also reduces the code complexity for when we do insert conditional compilation features, because we only need to add the cfg attributes to a few outer places instead of handling it internally in every spot something from Python is used. Looking to the future the long term goal this split enables is moving the Python aspects of the DAGCircuit and the transpiler to the pyext crate. Having all the Python interface components be separate structs and functions means when we finish the data model migration all these interface points can just be moved to the pyext crate since they'll be self contained. We can't do that yet as control flow is still dependent on the Python interface.

Details and comments

TODO:

  • Fix test failures/panics with control flow (circuit to dag issues)

This commit splits out the Python only component of the DAGCircuit
struct in Rust. The DAGCircuit struct has two fields that explicitly
require Python, `metadata` and `duration`, and there are two more fields
that are only ever used in Python, `unit` and `name` despite both being
strings. To make the Python component distinct and separable this makes
a separate Python interface struct PyDAGCircuit which wraps the inner
pure rust DAGCircuit that contains the extra data for Python. This
struct also defines all the Python methods so the inner rust DAGCircuit
doesn't define any interface to Python anymore and it's all isolated to
the

This commit will prepare us towards implementing Qiskit#14240 to make pyo3
and needing python symbols a compile time option. While it is possible
to conditionally compile all these aspects everywhere there are a few
advantages an explict stuct provides. The first is making all the places
we expect a Python interface very explicit. This makes it easy to reason
about special handling only needed for python and isolate it to separate
blocks. It also reduces the code complexity for when we do insert
conditional compilation features, because we only need to add the cfg
attributes to a few outer places instead of handling it internally in
every spot something from Python is used. Looking to the future the long
term goal this split enables is moving the Python aspects of the
DAGCircuit and the transpiler to the pyext crate. Having all the Python
interface components be separate structs and functions means when we
finish the data model migration all these interface points can just be
moved to the pyext crate since they'll be self contained. We can't do
that yet as control flow is still dependent on the Python interface.
@mtreinish mtreinish added this to the 2.3.0 milestone Nov 5, 2025
@mtreinish mtreinish added on hold Can not fix yet Changelog: None Do not include in changelog labels Nov 5, 2025
@mtreinish mtreinish added the Rust This PR or issue is related to Rust code in the repository label Nov 5, 2025
@github-project-automation github-project-automation bot moved this to Ready in Qiskit 2.3 Nov 5, 2025
Shivam-Bhardwaj added a commit to Shivam-Bhardwaj/qiskit that referenced this pull request Nov 16, 2025
Replace .unwrap() calls in pack_into and unpack_into methods with proper
error handling to prevent panics when converting circuits with control flow
operations to DAGs.

This fixes the TODO mentioned in PR Qiskit#15301 about test failures/panics with
control flow (circuit to dag issues).

Changes:
- Replace .unwrap() on qubits.find() with ok_or_else() returning DAGCircuitError
- Replace .unwrap() on clbits.find() with ok_or_else() returning DAGCircuitError
- Replace .unwrap() on vars.find() with ok_or_else() returning DAGCircuitError
- Replace .unwrap() on qubits.get() with ok_or_else() returning DAGCircuitError
- Replace .unwrap() on clbits.get() with ok_or_else() returning DAGCircuitError
- Replace .unwrap() on vars.get() with ok_or_else() returning DAGCircuitError

All control flow tests now pass successfully.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: None Do not include in changelog on hold Can not fix yet Rust This PR or issue is related to Rust code in the repository

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

1 participant