Skip to content

test: cover the control on a controlled swap through openqasm2 - #5263

Open
cleitonaugusto wants to merge 1 commit into
NVIDIA:mainfrom
cleitonaugusto:test-controlled-swap-keeps-control
Open

test: cover the control on a controlled swap through openqasm2#5263
cleitonaugusto wants to merge 1 commit into
NVIDIA:mainfrom
cleitonaugusto:test-controlled-swap-keeps-control

Conversation

@cleitonaugusto

Copy link
Copy Markdown

Regression test for #5192, which I reported: a controlled swap lowers to three CNOTs between the two targets, so the control is dropped and the exported OpenQASM 2 swaps unconditionally while the simulator does not.

This only adds the test. The lowering itself is still open, and there is a discussion on the issue about which form to use.

Two things about how it is written:

The control is |0> on purpose. With the control set, a controlled swap and a plain swap agree, so a test with the control on passes either way. That is why test/Transforms/DecompositionPatterns/SwapToCX.qke does not catch this: it only covers the uncontrolled swap.

It asserts that the control qubit appears in some gate, rather than pinning a particular decomposition. Whether the fix emits a Fredkin, bails out for controlled swaps, or does something else, the control has to survive, and I did not want the test to constrain that choice.

Verified against 0.15.1 from pip install cudaq: it fails there with the control absent, and the assertion prints the emitted body so the failure explains itself. Two unrelated tests in this file (test_translate_builder_qir and test_translate_builder_with_params_qir) already fail for me on the released wheel, in kernel_builder.py, so I could not run the whole file green. The rest passes.

@copy-pr-bot

copy-pr-bot Bot commented Aug 25, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the python-lang Anything related to the Python CUDA Quantum language implementation label Aug 25, 2026
Regression test for NVIDIA#5192. A controlled swap currently lowers to three
CNOTs between the targets, dropping the control, so the exported circuit
swaps unconditionally while the simulator does not.

The control is |0> on purpose. With the control set, a controlled swap
and a plain swap agree, which is why SwapToCX.qke does not catch this:
it only covers the uncontrolled case.

Asserts that the control qubit appears in some gate rather than pinning a
particular decomposition, so it does not constrain how the lowering is
fixed. Fails on 0.15.1, passes once the control survives.

Signed-off-by: Cleiton Augusto <augusto.cleiton@gmail.com>
@cleitonaugusto

Copy link
Copy Markdown
Author

For whoever picks this up: this is the regression test for the bug that #5276 fixes, so the two go together. This one fails on 0.15.1 and passes once the control survives the lowering.

They cover different layers on purpose. #5276 has a unit test on the decomposition pattern itself, which pins the wiring. This one goes through cudaq.translate to OpenQASM 2, so it also covers the path from the Python API down to the emitted text.

No rush from my side, just flagging the connection so it is not reviewed as an unrelated change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python-lang Anything related to the Python CUDA Quantum language implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant