Skip to content

Kernels with mx measurements not reporting correct results during sampling #4333

@anpaz

Description

@anpaz

Required prerequisites

  • Consult the security policy. If reporting a security vulnerability, do not report the bug using this form. Use the process described in the policy to report the issue.
  • Make sure you've read the documentation. Your issue may be addressed there.
  • Search the issue tracker to verify that this hasn't already been reported. +1 or comment there if it has.
  • If possible, make a PR with a failing test to give us a starting point to work on!

Describe the bug

When a kernel contains an mx measurement after an earlier measurement such as mz, cudaq.sample(...) only reports the measurement outcomes collected after the mx instruction instead of returning the full bitstring for all measurements in the kernel.

For example, in a 2-qubit kernel that measures qubit 0 with mz and qubit 1 with mx, the sampled result should contain 2-bit outcomes, but instead only a 1-bit outcome is returned.

Steps to reproduce the bug

Python example:

import cudaq

@cudaq.kernel
def kernel_mx():
    qubits = cudaq.qvector(2)
    x(qubits[0])
    h(qubits[1])
    mz(qubits[0])
    mx(qubits[1])

result = cudaq.sample(kernel_mx, shots_count=10)
print(result)

Observed output:

{ 0:10 }

Expected output shape:

{ 00:10 }

or, more generally, a 2-bit result that includes both the mz outcome for qubit 0 and the mx outcome for qubit 1.

C++ example:

#include <cudaq.h>

__qpu__ void kernel() {
  cudaq::qvector qubits(2);
  x(qubits[0]);
  h(qubits[1]);
  mz(qubits[0]);
  mx(qubits[1]);
}

int main() {
  auto result = cudaq::sample(kernel);
  result.dump();
}

This appears to reproduce in C++ as well.

Expected behavior

cudaq.sample(...) should return bitstrings that include all measurement outcomes performed in the kernel, regardless of whether those measurements are in the Z basis, X basis, or mixed bases.

For the reproducer above, the result should include both measured bits, not just the measurements that occur after the mx instruction.

Is this a regression? If it is, put the last known working version (or commit) here.

Is this a regression? If it is, put the last known working version (or commit) here.

Not a regression

Environment

  • CUDA-Q version: 0.14.0
  • Python version: 3.12.3
  • C++ compiler: g++ 13.3.0
  • Operating system: ubuntu (wsl)

Suggestions

This looks like sampled measurement aggregation may be dropping previously recorded measurement results once an mx instruction is encountered, or only collecting results from a later measurement register/path. It would be worth checking how mixed-basis measurement results are appended into the final sampled bitstring.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions