Fixes #15748: Prevent Rust panic during nested control flow compose()#16229
Open
gitHazik wants to merge 1 commit into
Open
Fixes #15748: Prevent Rust panic during nested control flow compose()#16229gitHazik wants to merge 1 commit into
gitHazik wants to merge 1 commit into
Conversation
Replace unwrap() with error handling for bit and variable lookups.
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:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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