Source code for cudaq_pulse.kernel.decorator

# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

from collections.abc import Callable
from typing import TYPE_CHECKING, Any, Protocol, overload

if TYPE_CHECKING:
    from ._kernel import Kernel


class KernelDecorator(Protocol):
    """``@kernel(...)`` with options: makes a kernel of the function it decorates."""

    def __call__[**P](self, fn: Callable[P, Any], /) -> Kernel[P]: ...


@overload
def kernel[**P](fn: Callable[P, Any], /) -> Kernel[P]: ...
@overload
def kernel(
    fn: None = None,
    /,
    *,
    cache_size: int | None = None,
    schedule: str | None = None,
    unroll_threshold: int | None = None,
    max_duration_cycles: int | None = None,
    loop_exit: str | None = None,
) -> KernelDecorator: ...


[docs] def kernel[**P]( fn: Callable[P, Any] | None = None, /, *, cache_size: int | None = None, schedule: str | None = None, unroll_threshold: int | None = None, max_duration_cycles: int | None = None, loop_exit: str | None = None, ) -> Kernel[P] | KernelDecorator: """Decorator that makes a Python function a pulse kernel. Calling the kernel compiles it on first use and runs it on the quantum system of its qudit handles, returning what the kernel returns. Parameters annotated ``float`` stay symbolic in the compiled module; the kernel is specialized on every other parameter, and up to *cache_size* specializations are kept (``CUDAQ_PULSE_CACHE_SIZE``, 32 by default). Supported control flow inside the kernel: - ``for i in range(N)`` (bound known at compile time) - ``if`` / ``else`` on values known at compile time, or on a measurement Example:: @cudaq_pulse.kernel def rabi(q, amp: float): d = get_drive_line(q) drive(d, gaussian(40, amp, 10.0)) qs = cudaq_pulse.quantum_system("transmon_surface17") rabi(qs.qudits[0], 0.3) A kernel called from another kernel is scheduled with it, unless it sets its own policy: with ``schedule="asap"`` or ``schedule="alap"`` it is scheduled on its own, then placed whole by the caller's policy, so its internal timing is the same wherever it is called. Compiled or called on its own, the kernel uses its policy unless ``cudaq_pulse.compile(k, schedule=...)`` overrides it. *unroll_threshold* sets which ``for ... in range(n)`` loops are unrolled while tracing (see ``cudaq_pulse.compile``): a non-negative ``int``, 8 by default, and 0 never unrolls. With *max_duration_cycles*, compiling fails if any execution of the kernel can take longer than that many clock cycles: each call, for a kernel called with its own ``schedule=`` (for example the body of a loop that must fit its cycle), or the whole program, for the kernel compiled. Waits whose length is only known when the program runs count at their highest length. *loop_exit* says what a loop that is left when the program runs (``if flag: break`` on a measurement result or an answer, in ``for _ in range(budget)``) compiles to on a control system that cannot leave a counted loop early, which is refused without it. With ``"run_out"`` the loop runs its whole budget: once it is left, the rounds that remain do nothing and take the time they would have taken, so the program always lasts as long, the names hold after the loop what they held where it was left, and a readout of a round that did not run has no outcome. ``"host"`` is not available yet. A kernel with ``loop_exit=`` keeps every loop one loop, as ``unroll_threshold=0`` does (any other *unroll_threshold* with it is refused), so that a short loop can be left too. """ from ._bytecode_normalize import check_interpreter from ._kernel import Kernel from ._schedule_blocks import validate_kernel_schedule, validate_max_duration from ._unroll import resolve_unroll_threshold check_interpreter() validate_kernel_schedule(schedule) validate_max_duration(max_duration_cycles) if unroll_threshold is not None: resolve_unroll_threshold(unroll_threshold) def wrap[**Q](f: Callable[Q, Any], /) -> Kernel[Q]: return Kernel( f, cache_size=cache_size, schedule=schedule, unroll_threshold=unroll_threshold, max_duration_cycles=max_duration_cycles, ) if loop_exit is not None: from ._loop_exit import choosing return choosing(wrap, fn, loop_exit) # type: ignore[no-any-return] return wrap if fn is None else wrap(fn)