NVIDIA / NVIDIA/cuda-quantum

Custom unitary op cannot be loaded in a kernel using a parameter

Open
#2,137 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stale-notified
Dominant language
C++
Stars
1.1k
Forks
455
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

Custom unitary op cannot be loaded in a kernel if the name is constructed from a parameter.
The following example gives an error:

RecursionError: maximum recursion depth exceeded while calling a Python object
Steps to reproduce the bug
def register_custom_operation(unitary_index: int, matrix: np.ndarray):
    cudaq.register_operation(f"U_{unitary_index}", matrix)

kernel, s, i = cudaq.make_kernel(cudaq.State, int)
qubits = kernel.qalloc(s)
kernel.__getattr__(f'U_{i}')(qubits[0], qubits[1], qubits[2])

state = np.zeros(8)
state[0] = 1
state = np.array(state, dtype=cudaq.complex())
states = [state]
for i in range(0,3):
    matrix = flip_qubit(np.identity(8), i)
    register_custom_operation(i, matrix)

    cudaq_state = cudaq.State.from_data(state)
    state = np.array(cudaq.get_state(kernel, cudaq_state, i))
    states.append(state)

for s in states:
    counts = cudaq.sample(kernel_helper, s)
    print(counts)
Expected behavior

We might consider creating a special API instead of getting the attribute to make this code work

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

Not a regression

Environment
  • CUDA Quantum 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 Python reproducer around cudaq.register_operation, cudaq.make_kernel, kernel.getattr, and cudaq.get_state to confirm the recursion error. Trace how the parameter-built operation name is resolved, then verify that the custom unitary loads and executes when the name depends on a kernel parameter.

Written by the indexing model from the issue text.

Assessment

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