Compilation Pipeline#
CUDA-Q Pulse compiles @pulse.kernel functions through a multi-stage pipeline
that goes from Python source to a scheduled program: every operation with
its line and its start in clock cycles. From there the program goes one of
two ways: to control electronics, checked against a control system and
handed to a controller backend, or to a simulator.
The stages, numbered as the sections below, with the kernel of
Stage 5: To Control Electronics followed through them. A
CompiledKernel holds the scheduled module. From the scheduled program
the way to control electronics goes on in four steps; the way to a
simulator is one box here and a section of its own.#
The pipeline as a column of stages, with the kernel measure_and_flip of this page followed through it, the steps numbered as the page's stages. A @pulse.kernel function is traced from its bytecode (1) into a packed int64 buffer, a numpy array, which one zero-copy FFI call (2) builds into the Pulse dialect in memory. The passes of passes= (3) give the optimized Pulse dialect, and the schedule of the policy (4) the scheduled program: 9 pulse operations, 440 ns, what kernel.lower(...) returns. From the scheduled program two columns go on, side by side, four steps on the left and one on the right. On the left, to control electronics (5), outlined solid: the program is checked against the control system, controller.check; read by a controller backend, scheduled.schedule with 2 timed events; turned into the controller's instructions, Backend.emit; and run on the control electronics, Backend.run. On the right, to a simulator (6), outlined dashed, one box: the simulation of the model, pulse.simulate, which the Simulation section describes.
Stage 1: Bytecode Tracing#
The @pulse.kernel decorator captures the decorated function’s CPython
bytecode. When a kernel compiles, a PackedIRBuilder traces
the bytecode and writes each operation into a flat numpy.ndarray
of int64 values. This packed buffer encodes operation types,
arguments, and waveform attributes in a compact binary format.
Stage 2: FFI to C++#
The packed buffer is sent to the C++ PulseModuleBuilder in a single
zero-copy FFI call via nanobind. The builder iterates the buffer and
constructs typed MLIR operations (pulse.drive, pulse.gaussian,
etc.) directly into an mlir::ModuleOp.
Stage 3: MLIR Passes#
The in-memory MLIR module is then run through the C++ passes named in
passes= via mlir::PassManager; pattern passes in a row run as one
rewrite. See MLIR Passes for details on each pass.
Stage 4: Scheduling#
Choose a Schedule shows each policy on one kernel.
The schedule pass of the policy (pulse-schedule-alap by default)
assigns start_cycles and duration_cycles attributes to every timed
operation, respecting data dependencies, sync constraints, and operation
durations; a module that declares classical calls is then checked with
pulse-classical-check. A pulse waits for
its line and for its tone: pulses that share a tone play in the order the
tone is threaded through them.
float parameters stay function arguments of the scheduled module. A call
with values binds them on a copy, folds it, reruns fold_phases and
fuse_pulses where the values open them, and schedules it again.
Stage 5: To Control Electronics#
A scheduled program is ready for a controller: nothing is lowered further inside the compiler, and a backend places every operation at the start the scheduler gave it.
Checked against the control system. A quantum system’s
pulse.ControlSystem states each pulse.Controller’s clock, timestep,
sample rate, pulse lengths, waveform memory, feedback latency and lines.
Compiling for it (pulse.compile(kernel, control_system=...), or a
quantum system that has one) checks the scheduled program against every
limit, and a program a controller cannot run fails with
ControllerLimitError, which names the line, the limit and the fix.
cudaq_pulse.controller.check(scheduled, controller) returns the same
violations for any controller without raising.
Read by a controller backend. scheduled.schedule lists every drive,
readout, wait and classical call with its line, start and length in clock
cycles. scheduled.export_to_controller() gives the same program as
plain data with each pulse’s waveform, frequency and phase and the
measurements each branch tests; on several controllers,
export_to_controller(controller=k) gives controller k its own part,
in its own cycles. A backend (cudaq_pulse.backends.Backend) turns that
into the controller’s instructions in emit and runs them in run.
Measurement feedback. An operation that depends on a readout (a pulse
in a branch on it, a value computed from it) starts no sooner than the
reading controller’s feedback_latency_cycles after the readout ends,
plus its peer_latency_cycles when another controller uses the outcome.
Every schedule waits that long, so the start a backend receives is one the
electronics can meet.
Segments and run-time waits. A wait whose length is known only during
the shot, and a blocking wait for a classical answer, end one segment of
the lines that wait and start the next. scheduled.segments() lists
them: where each starts in the schedule, and how much later it can start
in a run. scheduled.classical_report() lists every classical call with
when it is sent, when its answer is due and its slack.
This block does the first two steps for a box with a 4-cycle timestep and 200 cycles of feedback latency: it compiles a kernel for the control system, checks it, and reads the timed program a backend receives:
import cudaq_pulse as pulse
from cudaq_pulse import controller
from cudaq_pulse.quantum_systems import PhysicalQudit
box = pulse.Controller(clock_ghz=1.0, cycles_per_timestep=4, feedback_latency_cycles=200)
qudit = PhysicalQudit(
index=0, frequency_hz=5.0e9, t1_us=50.0, t2_star_us=30.0, anharmonicity_hz=-2.0e8
)
system = pulse.quantum_system(
name="one-qubit",
qudits={0: qudit},
clock_ghz=1.0,
control_system=pulse.ControlSystem(controllers=[box]),
)
@pulse.kernel
def measure_and_flip(qudit, duration, sigma, readout_duration):
drive_line = get_drive_line(qudit)
readout_line = get_readout_line(qudit)
outcome = readout(readout_line, square(readout_duration, 0.05))
sync(drive_line, readout_line)
if outcome:
drive(drive_line, gaussian(duration, 0.5, sigma))
scheduled = measure_and_flip.lower(
system.qudits[0], 40 * pulse.ns, 10 * pulse.ns, 200 * pulse.ns
)
print(controller.check(scheduled)) # []: within every limit of the box
for event in scheduled.schedule:
print(event.kind, event.line, event.start, event.end)
# readout 1 0 200
# drive 0 400 440
The drive starts at cycle 400: the readout’s end plus the box’s feedback latency. From a Kernel to Your Control Electronics goes on from here to the instructions of each channel.
Where to read on:
From a Kernel to Your Control Electronics takes one kernel through all of this to a per-channel instruction list.
One QPU, Several Control Systems shows what latency, several controllers and limits do to a schedule, and From Kernel to Controller the backend interface and its validation.
Lowering to Your Controller’s Instruction Set and Getting More out of Your Controller’s Instruction Set lower to a fuller instruction set.
Describe Your Control System, The Program a Backend Reads and Write and Validate a Backend are the reference.
Stage 6: To a Simulator#
The same scheduled program can be simulated instead: pulse.simulate
evolves the quantum system’s model under its pulses, on a CPU or, through
three further dialect conversions, on a GPU. Simulation
holds that lowering, level by level, and what the model covers.
examples/09_compilation_pipeline.py walks through pulse.compile() and
lower().