From Kernel to Controller#
A compiled pulse program doesn’t depend on any one control system. A controller backend turns it into what a particular controller plays: its sequencer instructions and waveform memory.
This tutorial is for people who build or program control systems. If you have not described a control system yet, From a Kernel to Your Control Electronics comes first: it goes from a controller’s data sheet to a per-channel instruction list. The backends on both pages are teaching backends; What Runs Today says what ships and what does not.
What you’ll learn:
what the exported program holds;
how to check a hardware limit on it in plain Python;
how to write, register and validate a small backend;
how one program splits across two controllers.
All the code on this page is also one Python script,
kernel_to_controller.py.
The exported program#
The export gives a scheduled program as JSON-ready data: every drive, readout, wait and sync, with its line and its time in clock cycles.
Start with one qudit at 5 GHz and a 1 GHz clock, so one cycle is one nanosecond, and a kernel that drives it and reads it out:
import cudaq_pulse as pulse
from cudaq_pulse.quantum_systems import PhysicalQudit
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,
)
@pulse.kernel
def drive_and_measure(qudit):
drive_line = get_drive_line(qudit)
readout_line = get_readout_line(qudit)
pulse_shape = gaussian(
duration=40 * pulse.ns,
amplitude=0.5,
sigma=10 * pulse.ns,
)
drive(drive_line, pulse_shape)
sync(drive_line, readout_line)
readout(readout_line, square(200 * pulse.ns, 0.05))
Export one call and list its events:
scheduled = drive_and_measure.lower(system.qudits[0])
program = scheduled.export_to_controller()
print("format:", program["format"])
print(f"{'kind':8} {'line':>4} {'start':>6} {'length':>6}")
for event in program["events"]:
line = str(event["line"])
start = event["start_cycles"]
length = event["duration_cycles"]
print(f"{event['kind']:8} {line:>4} {start:>6} {length:>6}")
Output
format: cudaq-pulse.controller/1
kind line start length
drive 0 0 40
sync None 40 0
readout 1 40 200
The same three events on their lines, one cycle per nanosecond.#
Timeline of the exported drive_and_measure on qudit 0's drive and readout lines, in cycles of the 1 GHz clock: a 40-cycle drive from cycle 0, a sync at cycle 40 that ties both lines, and a 200-cycle readout from there.
What to notice:
the 40 ns drive pulse is 40 cycles;
the readout starts where
syncput it;a sync belongs to no single line.
The format’s name carries a version. Within /1, keys and event kinds
may be added but never change meaning, so a backend written today keeps
reading programs from later releases.
Check a limit in Python#
The schedule lists every operation with its line, start and end. So a check the compiler doesn’t know about is a short Python function.
Suppose a controller can play at most two drive lines at once. Each drive adds a step up at its start and a step down at its end, and the running count is how many play at once:
def concurrent_drives(scheduled, limit):
edges = []
for event in scheduled.schedule:
if event.kind == "drive" and event.duration_cycles:
edges.append((event.start, 1))
edges.append((event.end, -1))
active = 0
violations = []
# An end sorts before a start at the same time.
for time, step in sorted(edges):
active += step
if step > 0 and active > limit:
violations.append((time, active))
return violations
Try it on four π pulses:
@pulse.kernel
def flip_four(qudits):
for qudit in qudits:
drive_line = get_drive_line(qudit)
flip = drive_line.get_calibration(pulse.gates.X)
drive(drive_line, flip)
surface = pulse.quantum_system("transmon_surface17")
four = surface.qudits[0:4]
violations = concurrent_drives(flip_four.lower(four), limit=2)
print("(cycle, drives at once):", violations)
Output
(cycle, drives at once): [(0, 3), (0, 4)]
All four π pulses start at cycle 0, two more than the limit.
The running count of drives at once, against the limit.#
What to notice: the count jumps straight to four, and the check reports each step above two.
Let the compiler keep the limit#
The compiler can schedule around such a limit itself when the control
system states it. Use the resource-constrained policy, "rcp":
limited = pulse.ControlSystem(max_concurrent_drives=2)
compiled = pulse.compile(
flip_four,
schedule="rcp",
control_system=limited,
)
program = compiled.lower(four)
starts = sorted(event.start for event in program.schedule)
print("violations:", concurrent_drives(program, limit=2))
print("pulse starts:", starts)
Output
violations: []
pulse starts: [0, 0, 80, 80]
flip_four as late as possible, and with schedule="rcp" under a
limit of two drives.#
Two timelines of flip_four on the drive lines of 4 qudits. As late as possible, all 4 pi pulses play at once, from 0 ns. With the resource-constrained policy and at most 2 drives at once, 2 play from 0 ns and the others from 40 ns.
What to notice: two pulses play first and two follow, so the limit holds.
A small backend#
A backend is an object with a name and three methods:
Method |
What it does |
|---|---|
|
says what its controller can play |
|
turns an exported program into the controller’s own form |
|
plays it |
This one emits a line of text per drive and readout. It is examples/controller_backend/text_backend.py, shown a piece at a time.
It needs the controller interface and the export format’s name:
from collections.abc import Mapping
from typing import Any
from cudaq_pulse import Controller, backends
from cudaq_pulse.backends import IncompatibleBackendError
from cudaq_pulse.controller import FORMAT
Shots = list[list[str]]
Each drive and readout becomes one line of text:
def describe(event: Mapping[str, Any]) -> str:
"""One line of text for a drive or readout."""
start = event["start_cycles"]
kind = event["kind"]
line = event["line"]
shape = event["waveform"]["shape"]
frequency = f"{event['frequency_hz']:.6g} Hz"
return f"{start} {kind} line {line} {shape} at {frequency}"
The class states the controller’s limits:
class TextBackend:
name = "text"
formats = (FORMAT,)
def control_system(self) -> Controller:
return Controller(
clock_ghz=1.0,
instruction_cycles=2,
min_idle_cycles=2,
)
Those limits are a 1 GHz clock, one instruction every 2 cycles, and idle gaps of at least 2 cycles. Compiling against them checks a program before it reaches the backend.
emit checks the format, skips what takes no instruction, and refuses
what it can’t play:
def emit(self, program: Mapping[str, Any]) -> list[str]:
if program["format"] != FORMAT:
message = f"cannot read {program['format']}"
raise IncompatibleBackendError(message)
played = []
for event in program["events"]:
if event["kind"] in ("wait", "sync"):
continue
playable = event["kind"] in ("drive", "readout")
if not playable or event["conditions"]:
message = f"cannot play event {event['index']}"
raise IncompatibleBackendError(message)
played.append(describe(event))
return played
What to notice: a pulse inside a branch is refused with
IncompatibleBackendError, instead of being played as something else.
run plays the emitted text once per shot:
def run(self, artifact: Any, *, shots_count: int = 1) -> Shots:
shots = []
for _ in range(shots_count):
shots.append(list(artifact))
return shots
Register it and run a program#
An installed backend is found by its entry point. register adds one to
the running process instead:
backends.register("text", TextBackend)
backend = backends.get("text")
compiled = pulse.compile(
drive_and_measure,
quantum_system=system,
control_system=backend.control_system(),
)
program = compiled.lower(system.qudits[0]).export_to_controller()
artifact = backend.emit(program)
for line in backend.run(artifact)[0]:
print(line)
Output
0 drive line 0 gaussian at 5e+09 Hz
40 readout line 1 square at 5e+09 Hz
Validate it#
backends.validate runs a corpus of test programs through a backend, one
for each kind of event. Each must be either emitted or refused cleanly:
report = backends.validate(
"text",
controller=Controller(clock_ghz=1.0),
)
print("ok:", report.ok)
print(str(report).splitlines()[-1])
backends.unregister("text")
Output
ok: True
backend 'text': 12 passed, 8 refused
What the text backend does with the corpus:
it plays drives, readouts, waits, syncs and loops;
it refuses classical calls, frame updates, branches and values known only at run time.
Each program of the corpus, and what the text backend did with it.#
The 20 programs of the validation corpus and what the text backend does with each: play, passed; readout, passed; wait, passed; sync, passed; loop, passed; call, refused; answer, refused; frame_update, refused; branch, refused; answer_timeout, refused; timeout_function, refused; runtime, refused; runtime_frequency, refused; shared_line, passed; shared_drive_line_two_tones, passed; shared_drive_line_one_tone, passed; several_controllers[0], passed; several_controllers[1], passed; unsupported_event, passed; newer_format, passed. 12 pass and 8 are refused cleanly; none fail.
What to notice: no program fails. Every program either passes or is
refused, and a program the backend can’t read passes by being turned away.
(A corpus program that the controller’s own limits do not allow is
skipped and marked SKIP; this controller states no limit that skips
one.)
Refusing is a pass. What fails validation is a backend that emits a program it can’t play faithfully, or reads a format it doesn’t know.
One program, two controllers#
On a control system of several controllers, each controller gets the
events of its own lines, in its own cycles, from
export_to_controller(controller=i).
In examples/23_define_a_qpu.py, one controller reads D1 out, and another plays the correction on D3 that a function on the feedback link decides:
The two exports of one program. The dashed pulse plays only when the answer says to flip.#
Timeline of one program on two controllers. Controller 0 reads D1 out; the outcome goes over the feedback link, 40 ns on the link, at most 400 ns in the correction function and 20 ns of feedback, to controller 1, which plays a pulse on D3 only if the qubit read 1, at 561 ns. A sync ties the two controllers' lines at the end of the readout.
What to notice: the sync at the end of the readout ties a line of each controller together.
Where to go next#
examples/controller_backend/toy_controller/__init__.py is a toy backend as an installable distribution, with its entry point in examples/controller_backend/pyproject.toml.
examples/custom_passes/python_pass.py is the concurrent-drive check above, as a script.
examples/transmon/transmon_surface17_multiplexed_readout.py reads four qudits through one feedline and shows each readout’s tone in the export.
Lowering to Your Controller’s Instruction Set (experimental) lowers the export to a controller’s own instructions, and Getting More out of Your Controller’s Instruction Set shows what the compiler does about feedback latency, clocks across controllers, instruction and waveform memory, and decoder calls.
For the details:
The Program a Backend Reads documents every key of the export format, and Describe Your Control System clocks, shared lines and waveform memory;
Write Your Own Passes shows how to load your own MLIR passes into
cudaq-pulse-opt.