Skip to content

Fixes #15748: Prevent Rust panic during nested control flow compose()#16229

Open
gitHazik wants to merge 1 commit into
Qiskit:mainfrom
gitHazik:main
Open

Fixes #15748: Prevent Rust panic during nested control flow compose()#16229
gitHazik wants to merge 1 commit into
Qiskit:mainfrom
gitHazik:main

Conversation

@gitHazik
Copy link
Copy Markdown

Fixes #15748.
While running some fuzzing tests on compose(), I hit a hard Rust panic (called Option::unwrap() on a None value) in dag_circuit.rs. It gets triggered when composing circuits with nested control flow (like an if_test inside another if_test).

What changed:
I replaced the .unwrap() calls inside DAGCircuit::additional_wires (specifically the wires_from_expr closure) with .ok_or_else(). Now, instead of hard-crashing the Python interpreter, it safely raises a ValueError indicating a corrupted circuit state/mapping failure. I also added a regression test for this.

Note on the root cause:
I tracked the actual mapping bug down to CircuitData::native_extend in circuit_data.rs. When copying over other.blocks, it just clones them directly instead of recursively mapping the inner qubits and clbits. It also fails to update the stale bit references inside the expr::Expr conditions of ControlFlow ops, which is what ultimately triggers the None lookup in the DAG builder.

Since the classical expression engine is actively being rewritten in Rust right now, I figured it was safer to just catch the panic here and fail gracefully rather than trying to rewrite the AST traversal and causing massive merge conflicts with your ongoing work.

i used chatgpt to generate this description to explain better what i did

Replace unwrap() with error handling for bit and variable lookups.
@gitHazik gitHazik requested a review from a team as a code owner May 21, 2026 17:21
@gitHazik gitHazik requested a review from jakelishman May 21, 2026 17:21
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label May 21, 2026
@qiskit-bot
Copy link
Copy Markdown
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

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

  • @Qiskit/terra-core

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 21, 2026

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community PR PRs from contributors that are not 'members' of the Qiskit repo

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Rust panic called 'Option::unwrap()' on a 'None' value

3 participants