Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for a bug in the compilation of controlled gates #2251

Merged
merged 1 commit into from
Nov 11, 2024

Conversation

gadial
Copy link
Collaborator

@gadial gadial commented Nov 5, 2024

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 is

for i in range(len(qubits)):
   if (ctrl_state >> i) & 1 == 0:
     qubits_i = [qubits[len(qubits) - 1 - i]]
     aer_circ.gate("x", qubits_i, params, [], conditional_reg, aer_cond_expr, label if label else "x")

The 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.

@gadial gadial requested review from doichanj and hhorii November 5, 2024 15:13
@gadial gadial merged commit 269c26f into Qiskit:main Nov 11, 2024
34 checks passed
@gadial gadial deleted the controlled_gate_compile_bugfix branch November 11, 2024 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect representation of state vector with state.draw('latex')
2 participants