Skip to content

Conversation

@jakelishman
Copy link
Member

Summary

Previously, the OperationFromPython extraction helper automatically chose CircuitData for the control-flow blocks. Since this extraction automatically required a clone anyway, the assumed type came at a cost.

This allows DAGCircuit to directly extract blocks to the correct type without the intermediate step. As a side effect, we also allow extraction to fail with a type error if blocks are not expected, and add a way to leave blocks unextracted.

Details and comments

Built on top of #15432. It's good for a ~10% improvement in the benchmark in that PR, which probably isn't worth worrying about too much.

jakelishman and others added 3 commits December 10, 2025 17:01
This removes one of the largest requirements for Python interaction in
the core data model.  However, it currently comes at a major cost of
cloning both on extraction from Python space and on producing view
objects back _to_ Python space.  We are choosing to make this trade-off
as a temporary measure, to prioritise separation of the core data model
from Python, given that transpiler performance in the presence of
control flow is already poor.

While `CircuitData` and `DAGCircuit` are both directly `pyclass`, we
either have to encode the Python interaction deep into the data
structures (deeply undesirable) to handle the blocks, or we have this
current problematic cloning.  The way around this will be to separate
off `PyCircuitData(Ptr<CircuitData>)` and similar for `DAGCircuit`,
where `Ptr<T>` is some smart-pointer type that may represent either
ownership of an underlying object or a (maybe fallible) reference to an
owned object.  The exact mechanism of this (perhaps something
`Arc`/`Weak`-based) is not yet decided, but will likely motivate a
further change of the `blocks` structure.

This commit is now a completely remade version of a previous PR
(Qiskitgh-15123), but much of the set up work, investigation and planning of
what became this was originally done by Kevin Hartman.

Co-authored-by: Kevin Hartman <[email protected]>
Previously, the `OperationFromPython` extraction helper automatically
chose `CircuitData` for the control-flow blocks.  Since this extraction
automatically required a clone anyway, the assumed type came at a cost.

This allows `DAGCircuit` to directly extract blocks to the correct type
without the intermediate step.  As a side effect, we also allow
extraction to fail with a type error if blocks are not expected, and add
a way to leave blocks unextracted.
@jakelishman jakelishman requested a review from a team as a code owner December 11, 2025 00:28
@jakelishman jakelishman added performance Changelog: None Do not include in changelog Rust This PR or issue is related to Rust code in the repository labels Dec 11, 2025
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core
  • @levbishop

@coveralls
Copy link

Pull Request Test Coverage Report for Build 20117771897

Details

  • 228 of 257 (88.72%) changed or added relevant lines in 24 files are covered.
  • 20 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.006%) to 88.305%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/dag_node.rs 6 7 85.71%
crates/circuit/src/circuit_data.rs 50 57 87.72%
crates/circuit/src/circuit_instruction.rs 26 47 55.32%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 3 92.8%
crates/circuit/src/blocks.rs 8 68.62%
crates/circuit/src/operations.rs 9 86.29%
Totals Coverage Status
Change from base Build 20108540513: -0.006%
Covered Lines: 96249
Relevant Lines: 108996

💛 - Coveralls

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 performance 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.

3 participants