NVIDIA / NVIDIA/cuda-quantum

[Python] Support named argument syntax for passing kernel parameters to `sample` and `observe` APIs

Open
#196 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Required prerequisites
  • 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

This runs fine:

import cudaq
from cudaq import spin

n_qubits = 2

kernel, weights = cudaq.make_kernel(list)
qubits = kernel.qalloc(n_qubits)

kernel.rx(weights[0], qubits[0])
kernel.rx(weights[1], qubits[1])

h = spin.z(0)

weights = [0,1]

exp_val = cudaq.observe(kernel, h, weights).expectation_z()


But this does not:

import cudaq
from cudaq import spin


n_qubits = 2

kernel, weights = cudaq.make_kernel(list)
qubits = kernel.qalloc(n_qubits)

kernel.rx(weights[0], qubits[0])
kernel.rx(weights[1], qubits[1])

h = spin.z(0)

params = [0,1]

exp_val = cudaq.observe(kernel, h, weights = params).expectation_z()

The error it throws is also incorrect:

TypeError: observe(): incompatible function arguments. The following argument types are supported:
    1. (kernel: _pycudaq.Kernel, spin_operator: cudaq::spin_op, *args, shots_count: int = -1) -> cudaq::observe_result

Invoked with: <_pycudaq.Kernel object at 0x7f4719066030>, <_pycudaq.SpinOperator object at 0x7f4718a1dd30>; kwargs: weights=[0, 1]

Can we enable the assignment of weights in the function call?

Thanks

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

NA

Suggestions

No response

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 by reproducing the supplied positional and keyword-argument examples for the Python observe API, then check the corresponding sample API behavior. Done means named kernel-parameter arguments work for both APIs and invalid calls report an accurate supported signature.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.