[c++] Mid-circuit measurements inside c-like functions generate incorrect results
Open
Nobody has claimed this yet.
needs triage
stale-notified
- Dominant language
- C++
- Stars
- 1.1k
- Forks
- 456
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 165
Description
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
Mid-circuit measurements inside c-like functions generate incorrect results due to cudaq::getKernelName returning incorrect name ("void ()")
Related: #3160
Steps to reproduce the bug
/*******************************************************************************
* Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. *
* All rights reserved. *
* *
* This source code and the accompanying materials are made available under *
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/
// clang-format off
// Simulators
// RUN: nvq++ %cpp_std --enable-mlir %s -o %t && %t | FileCheck %s
// RUN: nvq++ %cpp_std --library-mode %s -o %t && %t | FileCheck %s
// Quantum emulators
// RUN: nvq++ %cpp_std --target quantinuum --emulate %s -o %t && %t | FileCheck %s
// RUN: nvq++ %cpp_std --target ionq --emulate %s -o %t && %t | FileCheck %s
// RUN: nvq++ %cpp_std --target anyon --emulate %s -o %t && %t | FileCheck %s
// 2 different IQM machines for 2 different topologies
// RUN: nvq++ %cpp_std --target iqm --iqm-machine Adonis --emulate %s -o %t && %t | FileCheck %s
// RUN: nvq++ %cpp_std --target iqm --iqm-machine Apollo --emulate %s -o %t && %t | FileCheck %s
// RUN: nvq++ %cpp_std --target oqc --emulate %s -o %t && %t | FileCheck %s
// RUN: if %braket_avail; then nvq++ %cpp_std --target braket --emulate %s -o %t && %t | FileCheck %s; fi
// clang-format on
#include <cudaq.h>
#include <iostream>
#include <string>
#include <vector>
__qpu__ void callee(cudaq::qview<> r) {
for (auto i = 0; i < 4; i++) {
if (i % 2 == 0)
x(r[i]);
auto m = mz(r[i]);
cudaq::reset(r[i]);
if (m)
x(r[i]);
else
h(r[i]);
}
}
__qpu__ void c_caller() {
cudaq::qvector q(4);
callee(q);
}
__qpu__ void c_inlined() {
cudaq::qvector r(4);
for (auto i = 0; i < 4; i++) {
if (i % 2 == 0)
x(r[i]);
auto m = mz(r[i]);
cudaq::reset(r[i]);
if (m)
x(r[i]);
else
h(r[i]);
}
}
int main() {
{
auto counts = cudaq::sample(1000, c_caller);
counts.dump();
printf("%zu\n", counts.count("1010", "__global__"));
printf("%zu\n", counts.count("1011", "__global__"));
printf("%zu\n", counts.count("1110", "__global__"));
printf("%zu\n", counts.count("1111", "__global__"));
}
{
auto counts = cudaq::sample(1000, c_inlined);
counts.dump();
printf("%zu\n", counts.count("1010", "__global__"));
printf("%zu\n", counts.count("1011", "__global__"));
printf("%zu\n", counts.count("1110", "__global__"));
printf("%zu\n", counts.count("1111", "__global__"));
}
return 0;
}
Output:
0
0
0
1000
Expected behavior
Output:
255
255
255
255
Is this a regression? If it is, put the last known working version (or commit) here.
Not a regression
Environment
- CUDA-Q version:
- Python version:
- C++ compiler:
- Operating system:
Suggestions
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the supplied C++ reproduction, especially the c_caller and c_inlined entry points, and run its listed nvq++ and FileCheck commands. Trace cudaq::getKernelName where it returns "void ()"; done means the reproduced counts match the expected 255 values for all four bitstrings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 40/100