Loop variable read after a zero trip for returns uninitialized memory

Open
#5,250 1 comment 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@sacpis is already working on this.

Since Aug 25, 2026.

  • #5301 by @sacpis — closed without merging

Assessment

This issue has not been assessed yet.

Description

stale-notified
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

In Python, a for loop that never runs leaves its loop variable unbound, so reading it afterwards raises NameError. In a CUDA-Q kernel the read silently returns whatever is in the variable's stack slot.

Steps to reproduce the bug
import cudaq

@cudaq.kernel
def k(n: int):
    q = cudaq.qvector(3)
    for i in range(n):
        h(q[i])
    x(q[i])

print(cudaq.sample(k, 0, shots_count=20))

Gives output as

RuntimeError: Provided index [18446744073709551615] >= array size [3]
Expected behavior

Reading a loop variable after a zero trip loop should raise, matching Python's NameError, rather than produce a value.

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

Dominant language
C++
Stars
1.2k
Forks
508
Avg merge
1d 19h
Merged PRs (30d)
100

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NVIDIA/cuda-quantum

All issues in NVIDIA/cuda-quantum

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.