Passing a `qvector` to a multi-qubit custom operation fails

Open
#4,961 4 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@yogithayamala07-hue is already working on this.

Since Sep 27, 2026.

  • #4990 by @arcusbuilds — closed without merging

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
cpp, python
Domain
compilers

Research direction

Start with the C++ CUDAQ_REGISTER_OPERATION implementation and compare its qvector argument handling with the support added in PR #4173. Reproduce the failure using repro.cpp and verify that MyGate(q) compiles and behaves like the explicit-qubit form, while the Python example remains working.

Written by the indexing model from the issue text.

Description

c++ language documentation stale-notified
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
Describe the bug

PR #4173 ("Support qvector arguments in custom operations", fixes #2452) added support for calling a custom operation with a whole qvector:

  • custom_op(qvec)
  • custom_op(*qvec)

However, C++ front-end (CUDAQ_REGISTER_OPERATION) was not updated.

Steps to reproduce the bug

C++ (fails)

// repro.cpp
#include <cudaq.h>

// 2-qubit custom operation (SWAP), row-major, {re,im}
CUDAQ_REGISTER_OPERATION(MyGate, 2, 0, {
  {1.0,0.0},{0.0,0.0},{0.0,0.0},{0.0,0.0},
  {0.0,0.0},{0.0,0.0},{1.0,0.0},{0.0,0.0},
  {0.0,0.0},{1.0,0.0},{0.0,0.0},{0.0,0.0},
  {0.0,0.0},{0.0,0.0},{0.0,0.0},{1.0,0.0}
});

struct kernel {
  void operator()() __qpu__ {
    cudaq::qvector q(2);
    x(q[0]);
    MyGate(q);            // <-- whole qvector: FAILS to compile
    // MyGate(q[0], q[1]); // <-- explicit qubits: works fine
  }
};

int main() { auto c = cudaq::sample(kernel{}); c.dump(); return 0; }
$ nvq++ repro.cpp --target nvidia -o repro
repro.cpp:15:5: error: 'quake.custom_unitary_constant' op Invalid matrix size, required 2^N * 2^N for N-qubit operation
    MyGate(q);
    ^

Using explicit qubit refs MyGate(q[0], q[1]) compiles and runs correctly
({ 01:1000 }, SWAP on |10> -> |01>).

Python (same operation, whole qvector works):

import cudaq, numpy as np
cudaq.register_operation("mygate", np.array(
    [1,0,0,0, 0,0,1,0, 0,1,0,0, 0,0,0,1], dtype=complex))

@cudaq.kernel
def kernel():
    q = cudaq.qvector(2)
    x(q[0])
    mygate(q)          # whole qvector: works in Python (PR #4173)

print(cudaq.sample(kernel))   # { 01:1000 }
Expected behavior

Work with no error

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

Not a regression

Environment
  • CUDA-Q version: 0.15.0
  • Python version:
  • C++ compiler: nvq++
  • Operating system:
Suggestions

No response

Dominant language
C++
Stars
1.2k
Forks
508
Avg merge
1d 19h
Merged PRs (30d)
100

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

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.

More from NVIDIA/cuda-quantum

All issues in NVIDIA/cuda-quantum

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.