NVIDIA / NVIDIA/cuda-quantum

Unable to Apply Operators from a List of Operator Names in `cudaq.register_operation()`

Open
#2,595 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
  • 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

I am trying to apply several general unitary matrices to a circuit. I can successfully use cudaq.register_operation(...) by explicitly specifying the operation name. However, I have a use case where I need to store these operator names in a list and retrieve them within a loop. While no error is raised, nothing gets added to the kernel when I attempt this approach. Below is a minimal working example that demonstrates the issue.

Steps to reproduce the bug

Working Example (Using Operator Name Directly):

import cudaq
import math
from cudaq import spin
import numpy as np

N = 2

# Define the observable in spin operator form
observable = 0.5 * spin.z(0) * spin.z(1)

U = np.array([
    [0, 0, 0.92387953, -0.38268343j],
    [0, 0, -0.38268343j, 0.92387953],
    [-0.38268343j, 0.92387953, 0, 0],
    [0.92387953, -0.38268343j, 0, 0]
]) 

cudaq.register_operation("spa", U)
op = ["spa"]

##############################
###   BASE FUNCTIONALITY   ###
##############################
@cudaq.kernel
def cudaq_circuit():
    qubits = cudaq.qvector(N)
    spa(qubits[0], qubits[1])

# Observe the circuit with the specified observable
result = cudaq.observe(cudaq_circuit, observable)
print("Using directly: ", result.expectation())    ### Output :  0.3535533770918846
Expected behavior

Desired Implementation (Using Operator from List):

@cudaq.kernel
def cudaq_circuit():
    qubits = cudaq.qvector(N)
    op[0](qubits[0], qubits[1])

# Observe the circuit with the specified observable
result = cudaq.observe(cudaq_circuit, observable)
print("Trying to use from vector of operator names: ", result.expectation())    ### Output:  0.5
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

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 running the minimal example around cudaq.register_operation(), cudaq.kernel, and cudaq.observe, comparing the direct operator call with op0. Trace where the operator name is resolved during kernel construction. Done means the list-based call adds the registered operation and produces the expected observation result.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api, backend
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.