Skip to content

[python] Control sets cannot mix a qubit with a qview, and cudaq.control fails on kernels that call other kernels #4848

Description

@wsttiger

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.

Describe the feature

Two related gaps make "one external control qubit + an ancilla register"
control sets awkward in Python kernels:

  1. Gate level: any mix of a bare qubit and a qview in one control set is
    rejected (invalid argument type for control operand). All-qubit and
    single-view control sets work.
  2. Kernel level: cudaq.control(kernel, control, args...) works for leaf
    kernels (including list arguments) but fails with "Could not
    successfully apply kernel specialization" when the controlled kernel
    itself calls another kernel — so composite operations cannot be
    controlled wholesale.
import cudaq
cudaq.set_target("qpp-cpu")

# (1) mixed control set
@cudaq.kernel
def mixed():
    c = cudaq.qubit()
    q = cudaq.qvector(3)
    x.ctrl(c, q.front(2), q[2])   # error: invalid argument type for control operand

# (2) cudaq.control of a composite kernel
@cudaq.kernel
def leaf(q: cudaq.qview):
    for i in range(q.size()):
        x(q[i])

@cudaq.kernel
def composite(q: cudaq.qview):
    leaf(q)

@cudaq.kernel
def caller():
    c = cudaq.qubit()
    q = cudaq.qvector(2)
    x(c)
    cudaq.control(leaf, c, q)       # OK
    cudaq.control(composite, c, q)  # RuntimeError: Could not successfully apply kernel specialization.

Workaround: co-allocate the control qubit and the ancillas in one register
and slice views (register.front(...)/register.back(...)) — but that
leaks into public APIs: users must co-allocate their control qubit with a
library's ancilla register instead of passing any qubit they own (e.g. a
QPE counting qubit). Either fixing (1) or (2) would remove the constraint;
(2) alone would allow cudaq.control(subroutine, ctrl, ...) over composite
operations.

Environment

  • CUDA-Q built from source at commit 0be565550f4c23affdcbed9e4eaec38d2d0915e6
  • Python 3.11.x, target qpp-cpu (library mode)
  • AlmaLinux 8.10, x86_64

All reproducers below were executed and verified against this build.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions