Skip to content

Conversation

mtreinish
Copy link
Member

Summary

This commit removes the py token argument for the dag constructor which wasn't strictly needed. The only pieces of the dagcircuit struct which need python is the metadata atribute (which is an Option<PyDict>) and the var tracking sets. This commit rearranges the constructor so it doesn't actively need python. This is achieved by moving the var tracking sets inside a once lock which is only initialized in a context where vars are being used. The metadata is changed to None by default.

The other use case was the control flow module
which wasn't strictly required and is removed in this PR and using the import once cell mechanism that we use for this everywhere else.

Details and comments

@mtreinish mtreinish added Changelog: None Do not include in changelog Rust This PR or issue is related to Rust code in the repository C API Related to the C API labels Mar 21, 2025
@mtreinish mtreinish added this to the 2.1.0 milestone Mar 21, 2025
@mtreinish mtreinish requested a review from a team as a code owner March 21, 2025 21:08
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

This commit removes the py token argument for the dag constructor which
wasn't strictly needed. The only pieces of the dagcircuit struct which
need python is the metadata atribute (which is an `Option<PyDict>`) and
the var tracking sets. This commit rearranges the constructor so it
doesn't actively need python. This is achieved by moving the var
tracking sets inside a once lock which is only initialized in a context
where vars are being used. The metadata is changed to None by default.

The other use case was the control flow module
which wasn't strictly required and is removed in this PR and using the
import once cell mechanism that we use for this everywhere else.
@coveralls
Copy link

coveralls commented Mar 21, 2025

Pull Request Test Coverage Report for Build 14039322959

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 131 of 133 (98.5%) changed or added relevant lines in 3 files are covered.
  • 146 unchanged lines in 8 files lost coverage.
  • Overall coverage decreased (-0.008%) to 88.06%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/dag_circuit.rs 122 124 98.39%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 1 94.79%
crates/circuit/src/dag_circuit.rs 1 86.34%
crates/qasm2/src/lex.rs 2 92.73%
qiskit/visualization/counts_visualization.py 3 76.92%
qiskit/visualization/library.py 4 0.0%
qiskit/visualization/timeline/plotters/matplotlib.py 4 0.0%
crates/qasm2/src/parse.rs 6 97.15%
qiskit/visualization/state_visualization.py 125 57.37%
Totals Coverage Status
Change from base Build 13998385695: -0.008%
Covered Lines: 72626
Relevant Lines: 82473

💛 - Coveralls

Copy link
Contributor

@raynelfss raynelfss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for these additions, I think this is self-explanatory and should be about ready to merge. I just have a couple of comments :)

Edit: Also, it seems you need to fix the rust tests in DAGCircuit.

Comment on lines +4770 to +4797
pub fn new() -> PyResult<Self> {
Ok(DAGCircuit {
name: None,
metadata: None,
dag: StableDiGraph::default(),
qregs: RegisterData::new(),
cregs: RegisterData::new(),
qargs_interner: Interner::new(),
cargs_interner: Interner::new(),
qubits: ObjectRegistry::new(),
clbits: ObjectRegistry::new(),
vars: ObjectRegistry::new(),
global_phase: Param::Float(0.),
duration: None,
unit: "dt".to_string(),
qubit_locations: BitLocator::new(),
clbit_locations: BitLocator::new(),
qubit_io_map: Vec::new(),
clbit_io_map: Vec::new(),
var_io_map: Vec::new(),
op_names: IndexMap::default(),
vars_info: HashMap::new(),
vars_by_type: VarsByType::new(),
captured_stretches: IndexMap::default(),
declared_stretches: IndexMap::default(),
})
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to have DAGCircuit implement (or derive) the Default trait. Although some of the types here would need to implement it as well so might be a little more effort than needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more than just what a Default derive would provide, things like unit are not actually the default. I could do this as a custom default impl for DAGCircuit, but I'm not sure there is much benefit. Either way if we do need Default later, it's simple enough to add.

Copy link
Contributor

@raynelfss raynelfss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM! I do have one question here about the metadata which I feel shouldn't affect things too drastically but still wanted to ask it. 🚀 Happy merging 🚀

pub fn new() -> PyResult<Self> {
Ok(DAGCircuit {
name: None,
metadata: None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any effect from having the metadata be None instead of an empty dict during initialization? I'm assuming the effect should be minimal. But this would mean that any DAGCircuit coming fom rust directly would have no metadata.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There shouldn't be because we allowed None as a type for the metadata. If we are returning a rust generated DAG to python it is already able to handle this: https://github.com/Qiskit/qiskit/blob/main/qiskit/converters/dag_to_circuit.py#L75

The only reason I separated it out the python new though was because the default for Python's DAGCircuit() was intended to be an empty dict for metadata and I didn't want to change that expectation.

@mtreinish mtreinish added this pull request to the merge queue Mar 24, 2025
Merged via the queue into Qiskit:main with commit 0bb43cc Mar 24, 2025
20 checks passed
@mtreinish mtreinish deleted the no-py-dag-new branch March 25, 2025 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C API Related to the C API Changelog: None Do not include in changelog 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.

5 participants