Bug fix in HoareOptimizer
(backport #13083)
#13087
Merged
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.
Summary
Fixes #13079.
Detailed Comments
The problem was due to first simplifying a controlled-gate by removing its controls (in the example: the control qubit of a CX-gate is at
|1>
, allowing to replace the CX-gate by an X-gate), then incorrectly keeping track of the simplified gate (in order to store the new X-gate for later purposes, the code mutated the original CX-gate in some weird way).The code was already using
substitute_node_with_dag
to replace the controlled-gate by the base-gate (I don't really like this, but I don't think we can usesubstitute_node
as the simplified node has fewer qubits than the original node). To fix the problem, we can instead read off the simplified gate fromsubstitute_node_with_dag
's output.While this fixes the immediate problem, the code in
HoareOptimizer
is very messy and quite possibly contains many other sources of bugs (for instance, I did not like the idea of iterating over the DAG's topological nodes, while removing and simplifying some of the nodes in-place) and not optimized for performance. A complete rewrite while porting to Rust would be ideal :).This is an automatic backport of pull request #13083 done by [Mergify](https://mergify.com).