Kernel API#

This page documents the @pulse.kernel decorator and the Kernel object it returns. See Write a Pulse Kernel for how to write a pulse kernel.

cudaq_pulse.kernel(fn: Callable[P, Any], /) → Kernel[P][source]#
cudaq_pulse.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

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.

Parameters:
  • fn (Callable[P, Any] | None)

  • cache_size (int | None)

  • schedule (str | None)

  • unroll_threshold (int | None)

  • max_duration_cycles (int | None)

  • loop_exit (str | None)

Return type:

Kernel[P] | KernelDecorator

class cudaq_pulse.Kernel(
fn,
*,
cache_size=None,
schedule=None,
unroll_threshold=None,
max_duration_cycles=None,
)[source]#

Bases: Generic[P]

A pulse kernel. k(*args) compiles on first use and runs it.

k.lower(*args) returns the compiled module without running it. cudaq_pulse.compile(k) compiles ahead of the call. P is the decorated function’s signature, so calls are checked against it.

A kernel pickles as a function does. One that its module and qualified name find, outside __main__, pickles as that name. Any other (in __main__, at the prompt, inside a function) pickles as its function and options, the function by reference with pickle and by value with cloudpickle (Ray, Dask): pickle cannot send one defined inside a function. A process keeps the kernels it unpickles this way, so sending one again does not compile it again.

Parameters:
  • fn (Callable[P, Any])

  • cache_size (int | None)

  • schedule (str | None)

  • unroll_threshold (int | None)

  • max_duration_cycles (int | None)

property runtime_parameters: tuple[str, ...]#

Parameters annotated float, list[float], Qudit or Register, and *qudits: Qudit: function arguments of the module. An unannotated qudit or register parameter is static. In the order of the signature.

property static_parameters: tuple[str, ...]#

Parameters the kernel is specialized on.

Includes float parameters whose value the program needs while it compiles: one that sets a waveform duration, goes through int() or decides Python control flow. A wait length is not one of them. In the order of the signature.

lower(*args, **kwargs)[source]#

Compile for these arguments without running; see ScheduledKernel.

Parameters:
  • args (P.args)

  • kwargs (P.kwargs)

Return type:

ScheduledKernel

cache_info()[source]#

Specializations held, compiled and evicted by k(...) calls.

Return type:

CacheInfo

cache_clear()[source]#

Clear the specializations and traced programs cached by this kernel.

Return type:

None