Improve compose() errors for circuit width mismatches#15945
Improve compose() errors for circuit width mismatches#15945TSS99 wants to merge 3 commits intoQiskit:mainfrom
Conversation
|
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/circuit/quantumcircuit.py
Outdated
| if other.num_qubits > dest.num_qubits: | ||
| raise CircuitError( | ||
| "Trying to compose with another QuantumCircuit which has more 'in' edges." | ||
| f"Cannot compose a circuit with {other.num_qubits} qubit(s) " |
There was a problem hiding this comment.
The point here is that other cannot have more qubits/clbits than dest. Can we make the message reflect that? E.g.
Cannot compose onto a circuit that has less qubits ({other.num_qubits} > {dest.num_qubits})
for all of them?
| bfs_successors as core_bfs_successors, descendants as core_descendants, | ||
| }; | ||
|
|
||
| fn dag_compose_width_error(bit_kind: &str, other: usize, dest: usize) -> String { |
There was a problem hiding this comment.
We should tell cargo to inline this since it's just a call to format!
| fn dag_compose_width_error(bit_kind: &str, other: usize, dest: usize) -> String { | |
| #[inline] | |
| fn dag_compose_width_error(bit_kind: &str, other: usize, dest: usize) -> String { |
The even nicer solution would be to have Rust-based DAGCircuitErrors and have an error variant for this with the correctly formatted string. But given that the error type is imported from Python we can't do that yet.
Summary
Fixes #13935.
This updates
QuantumCircuit.compose()to raise clearer error messages when the circuit being composed is wider than the destination circuit.Specifically:
What changed
"more 'in' edges"error with user-facingCircuitErrormessagesWhy
The previous error message exposed internal graph terminology that is hard for new users to interpret. This change makes the failure mode more actionable and easier to understand.
Testing
AI disclosure
I used OpenAI Codex (GPT-5) for implementation assistance, and I reviewed and understood all changes before submitting this PR.