Hamiltonian list does not work with observe_async
Nobody has claimed this yet.
- 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
The code snippet below calculates expectation values using observe and observe_async and executes fine
import cudaq
from cudaq import spin
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(2)
kernel.x(qubits[0])
kernel.y(qubits[1])
hamiltonian = spin.z(0)
result1 = cudaq.observe(kernel, hamiltonian).expectation_z()
result2 = cudaq.observe_async(kernel, hamiltonian, qpu_id = 0).get().expectation_z()
result1 == result2
If I extend the hamiltonian to a list of 2 elements, I can successfully calculate the expectation value as such:
import cudaq
from cudaq import spin
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(2)
kernel.x(qubits[0])
kernel.y(qubits[1])
hamiltonian = [spin.z(0), spin.z(1)]
result = cudaq.observe(kernel, hamiltonian)
exp_val = [result[i].expectation_z() for i in range(len(result))]
However, with obersve_async the following error is produced:
import cudaq
from cudaq import spin
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(2)
kernel.x(qubits[0])
kernel.y(qubits[1])
hamiltonian = [spin.z(0), spin.z(1)]
result = cudaq.observe_async(kernel, hamiltonian, qpu_id = 0)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[2], line 15
11 kernel.y(qubits[1])
13 hamiltonian = [spin.z(0), spin.z(1)]
---> 15 result = cudaq.observe_async(kernel, hamiltonian, qpu_id = 0)
TypeError: observe_async(): incompatible function arguments. The following argument types are supported:
1. (kernel: cudaq._pycudaq.Kernel, spin_operator: cudaq::spin_op, *args, qpu_id: int = 0, shots_count: int = -1, noise_model: Optional[cudaq::noise_model] = None) -> cudaq::async_result<cudaq::observe_result>
Invoked with: <cudaq._pycudaq.Kernel object at 0x7f0c50ff2770>, [<cudaq._pycudaq.SpinOperator object at 0x7f0c50fd1630>, <cudaq._pycudaq.SpinOperator object at 0x7f0c4c674e30>]; kwargs: qpu_id=0
observe_async supports type(spin.z(0)) which is cudaq._pycudaq.SpinOperator but the type of hamiltonian = [spin.z(0), spin.z(1)] is a list.
This is also reflected in the documentation where observe can accept a list of a spinop but observe_async only accepts spinop
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue identifies the Python observe_async API and its documentation as entry points; start by comparing its accepted spin_operator types with observe's list handling. Add regression coverage for a list of spin operators and verify that the asynchronous call returns an expectation value for each element, then ensure the documented behavior matches the supported interface.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100