Fix sampling with mixed-basis measurements (mx/my)#4336
Open
anpaz wants to merge 5 commits intoNVIDIA:mainfrom
Open
Fix sampling with mixed-basis measurements (mx/my)#4336anpaz wants to merge 5 commits intoNVIDIA:mainfrom
anpaz wants to merge 5 commits intoNVIDIA:mainfrom
Conversation
Signed-off-by: Andres Paz <andresp@nvidia.com>
mitchdz
reviewed
Apr 16, 2026
|
|
||
| # Generated files | ||
| python/tests/mlir/generated | ||
| Testing |
Collaborator
There was a problem hiding this comment.
Is this file being generated from somewhere new? If it's just local for you, add it to your .git/info/exclude
Collaborator
Author
There was a problem hiding this comment.
Not sure. This folder appeared with test results. I ran the tests from the cli using ctest and pytest. I also triggered the tests from VSCode, so I'm not sure what exactly is creating the folder.
…during measurement basis changes Signed-off-by: Andres Paz <andresp@nvidia.com>
Signed-off-by: Andres Paz <andresp@nvidia.com>
|
CUDA Quantum Docs Bot: A preview of the documentation can be found here. |
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.
Fixes #4333
Root Cause
In sampling mode,
mx(q)is implemented ash(q); mz(q)— a basis-change gate followed by a Z-basis measurement. The problem is that every gate application callsflushAnySamplingTasks(), which samples all previously recorded qubits and clears the measurement buffer (sampleQubits). So thehgate insidemxprematurely flushes measurements recorded before it, and only the qubits measured after the flush end up in the final result.For example, in a kernel that does
mz(q[0]); mx(q[1]):mz(q[0])records qubit 0 insampleQubitsmx(q[1])callsh(q[1]), which triggersflushAnySamplingTasks()— this samples qubit 0 and clearssampleQubitsmz(q[1])(insidemx) records qubit 1 insampleQubitssampleQubits, producing 1-bit results instead of 2-bitFix
Skip the non-forced flush in
flushAnySamplingTasks()when in sampling mode without conditional measurements. Per the spec, in non-explicit sampling mode, qubit values correspond to the state at the end of kernel execution — the final forced flush infinalizeExecutionContexthandles the actual sampling. Mid-circuit flushes are only needed for conditional measurements (if mz(q)), which is already tracked byhasConditionalsOnMeasureResults.Testing
Added regression tests (C++ and Python) that verify mixed-basis measurement kernels return correctly-sized bitstrings with proper qubit ordering.