NVIDIA/cuda-quantum

Scope of variables is inconsistent within cudaq.kernel

Opened this issue · 0 comments

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

The scope of variables in cudaq.kernel is inconsistent. Here, I'm presenting an instance of code where, inside a loop, a given variable is updated, but that change is not reflected outside the loop.

The 'res' variable takes up the False value inside the loop, but it preserves the True value outside the loop. Completely evading the change that happened inside the loop.

image

Steps to reproduce the bug

import cudaq

@cudaq.kernel
def test():
    qubits=cudaq.qvector(2)
    
    res = True
    for i in range(2):
        res = mz(qubits[i])
        if res == False:
            inner_mz = mz(qubits)

    if res == True:
        outer_mz = mz(qubits)

result=cudaq.sample(test)
print(result)

Output:

{
global : { 00:1000 }
outer_mz : { 00:1000 }
inner_mz : { 0000:1000 }
res : { 00:1000 }
}

Expected behavior

{
global : { 00:1000 }
inner_mz : { 0000:1000 }
res : { 00:1000 }
}

The if condition outside the loop should not work because the value of 'res' is changed inside the loop.

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

Not a regression

Environment

  • CUDA Quantum version:
  • Python version:
  • C++ compiler:
  • Operating system:

Suggestions

No response