Skip to content

Fix sampling with mixed-basis measurements (mx/my)#4336

Open
anpaz wants to merge 5 commits intoNVIDIA:mainfrom
anpaz:mx-bug
Open

Fix sampling with mixed-basis measurements (mx/my)#4336
anpaz wants to merge 5 commits intoNVIDIA:mainfrom
anpaz:mx-bug

Conversation

@anpaz
Copy link
Copy Markdown
Collaborator

@anpaz anpaz commented Apr 16, 2026

Fixes #4333

Root Cause

In sampling mode, mx(q) is implemented as h(q); mz(q) — a basis-change gate followed by a Z-basis measurement. The problem is that every gate application calls flushAnySamplingTasks(), which samples all previously recorded qubits and clears the measurement buffer (sampleQubits). So the h gate inside mx prematurely 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]):

  1. mz(q[0]) records qubit 0 in sampleQubits
  2. mx(q[1]) calls h(q[1]), which triggers flushAnySamplingTasks() — this samples qubit 0 and clears sampleQubits
  3. mz(q[1]) (inside mx) records qubit 1 in sampleQubits
  4. At finalization, only qubit 1 is in sampleQubits, producing 1-bit results instead of 2-bit

Fix

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 in finalizeExecutionContext handles the actual sampling. Mid-circuit flushes are only needed for conditional measurements (if mz(q)), which is already tracked by hasConditionalsOnMeasureResults.

Testing

Added regression tests (C++ and Python) that verify mixed-basis measurement kernels return correctly-sized bitstrings with proper qubit ordering.

Signed-off-by: Andres Paz <andresp@nvidia.com>
Comment thread .gitignore

# Generated files
python/tests/mlir/generated
Testing
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this file being generated from somewhere new? If it's just local for you, add it to your .git/info/exclude

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@anpaz anpaz marked this pull request as draft April 16, 2026 14:56
anpaz and others added 2 commits April 16, 2026 19:57
…during measurement basis changes

Signed-off-by: Andres Paz <andresp@nvidia.com>
@anpaz anpaz marked this pull request as ready for review April 16, 2026 20:01
anpaz added 2 commits April 16, 2026 20:26
Signed-off-by: Andres Paz <andresp@nvidia.com>
Signed-off-by: Andres Paz <andresp@nvidia.com>
github-actions bot pushed a commit that referenced this pull request Apr 16, 2026
@github-actions
Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

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.

Kernels with mx measurements not reporting correct results during sampling

2 participants