Skip to content

Use rust-native error for RemoveIdentityEquiv #16195

Open
raynelfss wants to merge 2 commits into
Qiskit:mainfrom
raynelfss:use-remove-identity-error
Open

Use rust-native error for RemoveIdentityEquiv #16195
raynelfss wants to merge 2 commits into
Qiskit:mainfrom
raynelfss:use-remove-identity-error

Conversation

@raynelfss
Copy link
Copy Markdown
Contributor

@raynelfss raynelfss commented May 16, 2026

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.

Use this to visualize the actual diff

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 2 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.
@raynelfss raynelfss requested a review from a team as a code owner May 16, 2026 14:22
@raynelfss raynelfss requested a review from gadial May 16, 2026 14:22
@raynelfss raynelfss added on hold Can not fix yet Changelog: None Do not include in the GitHub Release changelog. 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 25964281920

Coverage decreased (-0.01%) to 87.699%

Details

  • Coverage decreased (-0.01%) from the base build.
  • Patch coverage: 94 uncovered changes across 3 files (248 of 342 lines covered, 72.51%).
  • 29 coverage regressions across 6 files.

Uncovered Changes

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

Coverage Regressions

29 previously-covered lines in 6 files lost coverage.

File Lines Losing Coverage Coverage
crates/qasm2/src/parse.rs 12 97.15%
crates/circuit/src/dag_circuit.rs 7 84.91%
crates/qasm2/src/lex.rs 6 91.77%
crates/transpiler/src/passes/unitary_synthesis/mod.rs 2 96.88%
crates/circuit/src/parameter/parameter_expression.rs 1 91.04%
crates/transpiler/src/passes/unitary_synthesis/decomposers.rs 1 94.71%

Coverage Stats

Coverage Status
Relevant Lines: 123081
Covered Lines: 107941
Line Coverage: 87.7%
Coverage Strength: 953595.72 hits per line

💛 - Coveralls

Copy link
Copy Markdown
Contributor

@gadial gadial left a comment

Choose a reason for hiding this comment

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

Looks good to me.

My only question is whether the .map_err(RemoveIdentityEquivError::PyPauliRotationTraceAndDim)?; can be avoided by more implicit conversion, and whether it should be avoided like this. But it's a very minor concern.

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.

4 participants