Skip to content

Argument assignment in execution fucntion throws an error #2796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
4 tasks done
zohimchandani opened this issue Apr 9, 2025 · 1 comment
Open
4 tasks done

Argument assignment in execution fucntion throws an error #2796

zohimchandani opened this issue Apr 9, 2025 · 1 comment

Comments

@zohimchandani
Copy link
Collaborator

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

import cudaq

@cudaq.kernel
def bit_flip(which_m: int):
    
    qubits = cudaq.qvector(1)
            
    #create psi
    u3(1,2,3, qubits[0])
    
    if which_m == 1:
        mz(qubits[0])
 
#this works since argument is defined outside of sample and passed in         
which_m = 1
counts = cudaq.sample(bit_flip, which_m, shots_count = 1)

#this throws an error when argument is defined  within sample 
counts = cudaq.sample(bit_flip, which_m=1, shots_count = 1)

Steps to reproduce the bug

NA

Expected behavior

NA

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

@bmhowe23
Copy link
Collaborator

bmhowe23 commented Apr 9, 2025

I think this is consistent with how Python handles other functions that take both positional and keyword arguments like cudaq.sample. For example, the following fails with an error message.

def custom_func(a, b, c=0):
    print(f"a={a}, b={b}, c={c}")

custom_func(1, which_m = 2, c=3)

But if you use the walrus operator (:=), you can fix this:

def custom_func(a, b, c=0):
    print(f"a={a}, b={b}, c={c}")

custom_func(1, which_m:=2, c=3)

Similarly, you can use which_m:=1 in your case.

Let me know if that works for you.

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

No branches or pull requests

2 participants