Skip to content
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

Unexpected behavior with list[bool] for argument of kernel #2330

Closed
4 tasks done
ikkoham opened this issue Oct 30, 2024 · 6 comments · May be fixed by #2349
Closed
4 tasks done

Unexpected behavior with list[bool] for argument of kernel #2330

ikkoham opened this issue Oct 30, 2024 · 6 comments · May be fixed by #2349
Assignees
Labels
python-lang Anything related to the Python CUDA Quantum language implementation

Comments

@ikkoham
Copy link
Contributor

ikkoham commented Oct 30, 2024

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

Unexpected behavior when using enumerate to create kernel.
Detail is below.

Steps to reproduce the bug

import cudaq
import itertools

@cudaq.kernel
def test(num_qubits: int, mask: list[bool]):
    q = cudaq.qvector(num_qubits)
    for i, m in enumerate(mask):
        if m:
            x(q[i])
    mz(q)
    
num_qubits = 3
for mask in itertools.product((True, False), repeat=num_qubits):
    print(mask)
    print(cudaq.sample(test, num_qubits, list(mask)))

outputs

(True, True, True)
{ 000:1000 }

(True, True, False)
{ 000:1000 }

(True, False, True)
{ 000:1000 }

(True, False, False)
{ 100:1000 }

(False, True, True)
{ 000:1000 }

(False, True, False)
{ 000:1000 }

(False, False, True)
{ 000:1000 }

(False, False, False)
{ 000:1000 }

Only the case (True, False, False) is correct.

Expected behavior

Should return the correct bit string

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

Not a regression

Environment

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

Suggestions

No response

@ikkoham
Copy link
Contributor Author

ikkoham commented Oct 30, 2024

hmm, this may not be the problem of enumerate.

import cudaq
import itertools

@cudaq.kernel
def test(num_qubits: int, mask: list[bool]):
    q = cudaq.qvector(num_qubits)
    for i in range(num_qubits):
        if mask[i]:
            x(q[i])
    mz(q)
    
num_qubits = 3
for mask in itertools.product((True, False), repeat=num_qubits):
    print(list(mask))
    print(cudaq.sample(test, num_qubits, list(mask)))

outputs the same

@ikkoham
Copy link
Contributor Author

ikkoham commented Oct 30, 2024

This is the bug of list[bool].

import cudaq
import itertools

@cudaq.kernel
def test(num_qubits: int, mask: list[int]):
    q = cudaq.qvector(num_qubits)
    for i in range(num_qubits):
        if mask[i]:
            x(q[i])
    mz(q)
    
num_qubits = 4
for mask in itertools.product((1, 0), repeat=num_qubits):
    print(list(mask))
    print(cudaq.sample(test, num_qubits, list(mask)))

works as expected.

@ikkoham
Copy link
Contributor Author

ikkoham commented Oct 30, 2024

import cudaq
import itertools

@cudaq.kernel
def test(num_qubits: int, mask: list[int]):
    q = cudaq.qvector(num_qubits)
    for i, m in enumerate(mask):
        if m:
            x(q[i])
    mz(q)
    
num_qubits = 4
for mask in itertools.product((1, 0), repeat=num_qubits):
    print(list(mask))
    print(cudaq.sample(test, num_qubits, list(mask)))

is ok

@ikkoham ikkoham changed the title Unexpected behavior with enumerate Unexpected behavior with list[bool] for argument of kernel Oct 30, 2024
@bebora
Copy link
Contributor

bebora commented Oct 30, 2024

This is likely the same problem as the one reported in #2262.

@annagrin annagrin self-assigned this Nov 1, 2024
@schweitzpgi schweitzpgi added the python-lang Anything related to the Python CUDA Quantum language implementation label Nov 19, 2024
@schweitzpgi
Copy link
Collaborator

schweitzpgi commented Nov 19, 2024

#2338 fixes a number of bugs with arguments, etc.

Output if #2338 is applied:

[True, True, True]
{ 111:1000 }

[True, True, False]
{ 110:1000 }

[True, False, True]
{ 101:1000 }

[True, False, False]
{ 100:1000 }

[False, True, True]
{ 011:1000 }

[False, True, False]
{ 010:1000 }

[False, False, True]
{ 001:1000 }

[False, False, False]
{ 000:1000 }

@schweitzpgi
Copy link
Collaborator

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python-lang Anything related to the Python CUDA Quantum language implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants