NVIDIA / NVIDIA/cuda-quantum

[python] cudaq.sample API assumes broadcast on nested list arguments

Open
#2,895 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stale-notified
Dominant language
C++
Stars
1.1k
Forks
456
Avg merge
1d 22h
Merged PRs (30d)
165

Description

Should cudaq.sample be changed to accept an explicit broadcast parameter?

Details:

cudaq.sample checks if the first argument is a 2-d array and broadcasts by passing elements of the array to the kernel.

This leads to confusion for a non-broadcasting sample that expects a nested list, for example:

@cudaq.kernel
def test_list_list(a: list[list[int]]):
    qc = cudaq.qvector(3)
    j = 0
    for i in a:
        if i[0] == 1:
            x(qc[j])
        j += 1
    mz(qc)

print(cudaq.sample(test_list_list, [[0,1],[1,0],[1,0]]))

As a workaround, we can make kernel take a non-list first parameter:

@cudaq.kernel
def test_list_list(n: int, a: list[list[int]]):
    qc = cudaq.qvector(3)
    j = 0
    for i in a:
        if i[0] == 1:
            x(qc[j])
        j += 1
    mz(qc)

print(cudaq.sample(test_list_list, 2, [[0,1],[1,0],[1,0]]))

Related: https://github.com/NVIDIA/cuda-quantum/issues/1809
Related: https://github.com/NVIDIA/cuda-quantum/issues/2627

Contributor guide

Open the contributing guide

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.

Research direction

Start at the cudaq.sample entry point and reproduce the provided test_list_list example, comparing it with the workaround that adds a leading integer parameter. Trace how nested-list arguments are classified for broadcasting. Done means the intended broadcast behavior is explicit and the nested-list case is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, quantum-computing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.