Repository navigation
Fix open controls on controlled gates with multiple target qubits - #2463
Open
Eduardo Alcantara (edubraqd) wants to merge 1 commit into
Open
Eduardo Alcantara (edubraqd) wants to merge 1 commit into
Eduardo Alcantara (edubraqd) wants to merge 1 commit into
Conversation
When a controlled gate with a non-default ctrl_state is assembled, the X gates implementing the open controls were applied to the first len(qubits) - 1 qubits. For gates with more than one target qubit (cswap, mcswap) this includes a target qubit, so SwapGate().control(1, ctrl_state=0) was simulated with an extra X on the first target before and after the swap. Use the operation's num_ctrl_qubits instead of deriving the count from the number of qubits. Fixes Qiskit#2342 Co-Authored-By: Claude Opus 5 <[email protected]>
|
|
This branch has not been deployed
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.
Summary
Fixes #2342.
SwapGate().control(1, ctrl_state=0)(and any other controlled gate with more than one target qubit and a non-defaultctrl_state) was simulated incorrectly: the X gates that implement the open controls were also applied to the first target qubit.Details and comments
In
_assemble_op, the number of control qubits was derived aslen(qubits) - 1, which is only correct for single-target gates. Forcswap_o0(1 control, 2 targets) this loop covers qubits 0 and 1, so the open control bit is applied to the control and to the first target, before and after the gate. This is exactly the extra X onadescribed in the issue, and it affects every method that goes through the Python assembler (statevector, unitary, matrix_product_state, ...).The fix uses the operation's
num_ctrl_qubitsinstead, falling back to the previous behaviour if the attribute is missing.Verification:
cswapwithctrl_state=0case totest_controlled_gates(test/terra/backends/aer_simulator/test_circuit.py). It fails onmainwith the statevector[0, 0.5, 0.5, 0.5, 0, ...]instead of[0.5, 0.5, 0, 0.5, 0.5, ...], and passes with this change.|sv[0]| = 1.0forstatevector,unitaryandmatrix_product_state(was0.5for all three).test_circuit.py,test_cliffords.py,test_auto_method.py,test_multiplexer.pyandtest_standard_gates.pypass (516 tests),black --checkclean.The Python-side change was tested against the 0.17.2 binary extension; no C++ changes are involved.
Disclosure: I used Claude (Anthropic) as an assistant to investigate this issue and draft the change; I reviewed and ran everything above locally.
🤖 Generated with Claude Code