Skip to content

Use rust-native error for CommutativeOptimization#16197

Open
raynelfss wants to merge 4 commits into
Qiskit:mainfrom
raynelfss:use-commutation-opt-error
Open

Use rust-native error for CommutativeOptimization#16197
raynelfss wants to merge 4 commits into
Qiskit:mainfrom
raynelfss:use-commutation-opt-error

Conversation

@raynelfss
Copy link
Copy Markdown
Contributor

The following commits modify CommutativeOptimization to use rust-native errors throughout its pipeline.
We implement the CommutativeOptimizationError enumeration which derives from DAGCircuitError, RemoveIdentityEquivError, and CommutationError as well as adding two new variants for python specific errors dealing with parameters and pauli evolution gates.

Pre-requisites

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:

raynelfss added 4 commits May 15, 2026 17:03
Use rust-native errors in `DAGCircuit`

This should be step 1 for splitting `PyDAGCircuit` from `DAGCircuit`.
The following commits assure that any rust accessible method for `DAGCircuit` that:
1. Does not take a `py` token;
2. Is a public method.
does not need to depend on `PyErr` to process any errors.
It is ensured that these errors:
- Maintain the level of detail that the previous ones did.
- Correctly map to the same Python exceptions as before.

The enumeration for these errors is called `DAGCircuitInnerError` (subject to change) and is implemented with `thiserror`. I chose this over `anyhow` because it is better for specifying where the error comes from based on its variant rather than the message it contains.

Lint: Use names instead of underscores

Add: `anyhow!` usage for `DAGCircuitError
- Use `anyhow!` for any error types that don't require tracking of any variables coming from the `DAG` or that are part of the function arguments.
- Consolidate the amount of `DAG` error variants down from 40+ to 21. Can be consolidated further.

Fix: Existing error formatting
- Use `this_error` to fix the formatting of current errors causing conflicts with their messages.
- Add `is_var` argument to differentiate Variables and Stretches in `VariableNotInlined`.

Fix: consolidate `RegistryAbsentObject` error.

Add: errors for `size` and `depth`.
The following commits modify `RemoveIdentityEquiv` to use rust-native errors throughout its pipeline.
In this PR we introduce the error enumeration `RemoveIdentityEquivEerror` which only has two variants, one for `DAGCircuitError` and the other is for a specific case involving `PauliEvolution` gates that only exist in Python currently.
These changes mostly depend on Qiskit#16134.
The following commits modify `CommutationAnalysis` to use rust-native errors throughout its pipeline.
This is done mostly by implementing `DAGCircuitInnerError` based on Qiskit#16134 as a possible error in `CommutationError`.
The following commits modify `CommutativeOptimization` to use rust-native errors throughout its pipeline.
We implement the `CommutativeOptimizationError` enumeration which derives from `DAGCircuitError`, `RemoveIdentityEquivError`, and `CommutationError` as well as adding two new variants for python specific errors dealing with parameters and pauli evolution gates.
@raynelfss raynelfss added this to the 2.5.0 milestone May 16, 2026
@raynelfss raynelfss requested a review from a team as a code owner May 16, 2026 15:43
@raynelfss raynelfss added the on hold Can not fix yet label May 16, 2026
@raynelfss raynelfss added the Changelog: None Do not include in the GitHub Release changelog. label May 16, 2026
@raynelfss raynelfss requested a review from jakelishman May 16, 2026 15:43
@raynelfss raynelfss added Rust This PR or issue is related to Rust code in the repository mod: transpiler Issues and PRs related to Transpiler labels May 16, 2026
@qiskit-bot
Copy link
Copy Markdown
Collaborator

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

  • @Qiskit/terra-core

@coveralls
Copy link
Copy Markdown

Coverage Report for CI Build 25966046078

Coverage decreased (-0.03%) to 87.678%

Details

  • Coverage decreased (-0.03%) from the base build.
  • Patch coverage: 106 uncovered changes across 5 files (275 of 381 lines covered, 72.18%).
  • 50 coverage regressions across 7 files.

Uncovered Changes

File Changed Covered %
crates/circuit/src/dag_circuit.rs 325 239 73.54%
crates/transpiler/src/passes/commutative_optimization.rs 34 23 67.65%
crates/transpiler/src/passes/remove_identity_equiv.rs 12 5 41.67%
crates/transpiler/src/commutation_checker.rs 1 0 0.0%
crates/transpiler/src/passes/unroll_3q_or_more.rs 1 0 0.0%

Coverage Regressions

50 previously-covered lines in 7 files lost coverage.

File Lines Losing Coverage Coverage
crates/qasm2/src/parse.rs 24 96.21%
crates/circuit/src/parameter/symbol_expr.rs 8 73.93%
crates/qasm2/src/lex.rs 8 91.77%
crates/circuit/src/dag_circuit.rs 7 84.91%
crates/circuit/src/parameter/parameter_expression.rs 1 91.04%
crates/transpiler/src/commutation_checker.rs 1 88.66%
crates/transpiler/src/passes/unitary_synthesis/decomposers.rs 1 94.71%

Coverage Stats

Coverage Status
Relevant Lines: 123108
Covered Lines: 107939
Line Coverage: 87.68%
Coverage Strength: 953282.49 hits per line

💛 - 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 the GitHub Release changelog. mod: transpiler Issues and PRs related to Transpiler 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.

3 participants