Fix for a bug in the compilation of controlled gates #2251
+36
−5
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 a bug in the compilation of controlled gates resulting in X gates added in incorrect positions.
Fixes #2252
Details and comments
When adding a gate with control qubits, a control state string determines whether the control qubit should be 1 or 0. In the case of
0
, compilation adds an X gate before and after the application of a standard, controlled-by-1 gate. The code for adding the X gates isThe code goes over the qubit list in reverse order, which does not correspond to the way the control string is interpreted in
Qiskit
, and the loop goes over all the qubits in the gate, including the target qubit, resulting in X gates always added before and after this qubit (creating a non-equivalent behavior if, e.g. the controlled gate is Z).This PR addresses this issue by going over the qubits in the standard order and not handling the last one in the qubits list.