The Program a Backend Reads#

This page covers the program that ScheduledKernel.export_to_controller() gives a controller backend: every key, event and run-time expression of its two formats. It is for people who write the software that turns that program into a controller’s own instructions. This page describes the program as plain data, which is how a backend reads it today; it will change when the scheduled program can be read through a typed walk.

On this page a tone’s state is its frequency and phase, and a tone change is a phase or frequency change of a tone: shift_phase, set_phase, shift_frequency or set_frequency.

Exporting a program#

ScheduledKernel.export_to_controller() returns a compiled kernel as JSON-serializable data. Its format is "cudaq-pulse.controller/1" (cudaq_pulse.controller.FORMAT): the schema’s name and, after the slash, its major version. Within one version keys and event kinds may be added, but none is removed or changes meaning, so a backend ignores keys it does not know and refuses event kinds it does not know; a change that would break a backend gets a new version.

At the top level, quantum_system and clock_ghz name the quantum system and its clock, schedule the schedule policy, qudits the quantum system qudits the program uses, lines each line’s index, role ("drive" or "readout") and qudit (and, with line delays, its fine_delay_ns; see below; on a shared line, the line’s qudits and max_concurrent_tones; see Shared lines), and limits the controller limits it was checked against (null without). Each line also has native_frequency_hz, the frequency of its own tone (line.tone): on a drive line the frequency the qudit’s 0-1 drive tone plays at, and on a readout line its readout’s frequency_hz, or that same drive frequency when the readout states none. Every shot starts that tone at this frequency, and its phase_rad advances from it when the tone is retuned, so a backend that plays a line’s other frequencies (another transition’s tone, a shifted frequency) as offsets from one oscillator needs nothing from the quantum system.

The frequency a drive tone plays at is the qudit’s dressed_frequency_hz: its 0-1 frequency with the system’s couplings included. On a system without couplings that is the qudit’s frequency_hz. On a system with couplings it is not: the couplings shift each qudit’s 0-1 transition, the tones play at the shifted frequency, and frequency_hz stays the bare value the Hamiltonian uses. Two qudits at 5.0 GHz and 5.15 GHz coupled with 3 MHz:

# qudit 0: frequency_hz 5000000000.0, dressed_frequency_hz 4999940023.980819
# qudit 1: frequency_hz 5150000000.0, dressed_frequency_hz 5150059976.019181
"lines": [
  {"line": 0, "role": "drive",   "qudit": 0, "native_frequency_hz": 4999940023.980819},
  {"line": 1, "role": "readout", "qudit": 0, "native_frequency_hz": 4999940023.980819},
  {"line": 2, "role": "drive",   "qudit": 1, "native_frequency_hz": 5150059976.019181}
]

A drive on line 0 has frequency_hz 4999940023.980819, and the readout line, whose readout states no frequency, starts there too. A system made with frames="bare" plays its drive tones at frequency_hz instead: the same export then gives 5000000000.0 and 5150000000.0, and dressed_frequency_hz gives those values as well, since it is the frequency the tone plays at. A backend should take a line’s frequency from native_frequency_hz and never from the qudit’s frequency_hz.

On a system that does not state its frequencies they are offsets: quantum_system.frequency_frame is "shared_reference" when every frequency_hz and native_frequency_hz is an offset from one reference the qudits share, and "own_carrier" when each line’s native frequency is 0 and its events’ frequencies are offsets from its own qudit’s carrier (see Describe a Quantum System); the export’s keys are the same. A kernel with qudit parameters gives it as the slot’s symbols instead (s0.f01, s0.f_ro; see below) and its lines leave it out. events lists every drive, readout, wait, sync, classical call and frame_update event (a tone change that no later pulse takes) in program order, once per loop iteration and both arms of a branch. segments, calls, links and manifest are those of ScheduledKernel.export_to_emulator() (see Call Classical Code During a Shot).

A kernel with list or qudit parameters compiles once for every call, and its export says what each call binds: program_id is the same for every call of that compile, and symbols lists the call’s values, each with its symbol, kind and value: a float parameter or element ("float"), a runtime qudit’s slot ("slot", s0, the carrier bound), and the native frequencies of the slot’s lines ("frequency"): s0.f01, the frequency its 0-1 drive tone plays at (its dressed_frequency_hz, as above), for a drive line, and s0.f_ro, the frequency_hz of its readout (else that drive frequency), for a readout line. Each event names in its symbols the slot of its line (qudit) and, by field (frequency_hz, phase_rad, waveform), the symbols its value depends on; a tone on a slot’s line depends on that line’s native frequency. Each event has:

index, kind

Its position in events and one of "drive", "readout", "wait", "sync", "submit", "answer" and "frame_update".

line, qudit

The line it plays on and that line’s qudit; null for a sync. For a classical call, line is its link and qudit is -1.

tone_slot

Only on a drive, readout, wait or frame_update event on a shared line (see Shared lines): the line’s tone it plays, as in /2.

start_cycles, duration_cycles, end_cycles

The final schedule as integers, in clock cycles. A backend places every event at its start and does not schedule again.

iteration

The iteration of each loop around it, outermost first.

segment

The segment it starts in. A wait whose length is an answer, and a blocking wait for an answer, end one segment of the lines that wait for it and start the next; an event shifts by the run-time lengths of its segment’s barrier and of the barriers of the segments it follows. Each segment follows the one before it, or the segment its after names.

conditions

The measurement-conditioned branches the event lies in, outermost first: the branch, the arm (true for the if arm), and the readouts and answers (event indices) the condition is computed from. test is "is_set" when the condition is whether one readout is set, "answer" when it is one answer value, else "expression", and then expression is the condition, a bool expression (see Run-time expressions) over those readouts and answers. A condition with test "answer" also has result, which result of that answer it is, numbered as in an expression’s {"answer": i, "result": k}: for a call with a timeout, result 0 is the flag that the answer came in time and result k + 1 the function’s value k. The event plays when every condition is its arm. Events in the two arms of one branch may overlap in time; only one arm plays.

Drives and readouts also have:

waveform, amplitude

The waveform’s expression, {"shape": "gaussian", "params": {"duration": ..., "amplitude": ..., "sigma": ...}}, with scale, neg, add, sub and mul nodes over of, and samples for custom_samples (which custom() envelopes become when the kernel is traced), at the line’s own rate (its controller’s sample_rate_ghz); and its overall complex amplitude, {"re": ..., "im": ...}. Times in parameters are in cycles.

samples

With export_to_controller(sample_rate_ghz=...), a whole multiple of the clock, the envelope sampled at that rate, {"re": [...], "im": [...]}. cudaq_pulse.controller.sample_waveform computes the same samples for one waveform. Sample k of a shape (gaussian, drag, …) is its envelope at the start of that sample, k samples from the pulse’s start, as the simulator plays it; a custom() envelope was sampled at the middle of each sample when the kernel was traced, and custom_samples are played as given. An instrument that evaluates a shape at the middle of each sample plays these samples with the shape delayed by half a sample.

frequency_hz, phase_rad

The tone’s frequency and its phase as the event starts, with every tone change and virtual Z before it applied and the phase a detuned tone gains over time, or null where the two arms of a branch leave them different or a tone change is known only at run time.

tone, frame_updates, phase_offset_rad

Only on a tone whose state depends on run time: one with a tone change computed from a readout or an answer, or made in a branch. The tone’s id (such tones are numbered in the order they are first played), and the tone changes on that tone since its previous drive or readout, in program order: each with id, op ("shift_phase", "set_phase", "shift_frequency" or "set_frequency"), value (a number, or an expression), start_cycles, where it is made on the tone’s timeline (see below), from which the tone gains phase at its new frequency even where the value is known only later, and conditions, the branch arms it is on the path of. One change pending before a branch whose arm plays on the tone is listed again, with the other arm’s condition, at the next drive after the branch; the same id names it both times. A controller tracks such a tone’s state itself, applying these instead of reading frequency_hz and phase_rad, each id once. A drive whose own phase offset does not change the tone state has phase_offset_rad.

A program with such tones has runtime_tones (after events and frame_state): by each tone’s id, as a string, its line (a line’s own tone’s line, else the line it first plays on) and the tone state every shot starts it in, reset_frequency_hz and reset_phase (in radians), to which a controller tracking the tone applies its frame_updates. On the line of a runtime qudit, the entry’s symbols name the symbol its reset frequency is, s0.f01 on a drive line and s0.f_ro on a readout line. A program without such tones has no runtime_tones.

Every shot starts from the reset tone state: each tone at its own frequency, with phase 0. A tone change (shift_phase, set_phase, shift_frequency, set_frequency) is a point on the timeline: in the scheduled IR it has start_cycles and a duration_cycles of 0. It is where the program makes it on its tone’s line: after the waits, pulses and syncs before it there and after the readouts it depends on (in the arm of a branch on a measurement, or with a value computed from one); or, on a tone that next plays on another line earlier, where its previous drive or readout ends. A frequency shifted before a wait and back after it so runs shifted through the wait, and the drives after it have the phase gained. The next drive or readout on its tone takes it in its frequency_hz and phase_rad; in a loop, the next iteration’s do. In a loop whose iterations do not play its tone, a change is at the start of each iteration, or, where it depends on readouts, once they complete. A change written after a wait whose length is an answer, or after an answer waited for as long as it takes, is no earlier than the segment that point opens starts, however early its tone and its line are free: the controller reaches it only then. So the time from the change to the drive or readout that takes it does not depend on how long the wait or the answer takes, nor does the phase the tone gains in it; and the change has the same place whether the loop around it is kept or written out. A change takes no time: every drive, readout, wait and loop, and the program, starts and lasts as it does without it.

The compiler keeps every change the program makes, and where no later drive or readout takes a change on some path, the export gives that execution as a "frame_update" event of its own, decided from the program alone:

  • A change nothing takes is a frame_update event where it is made, in every iteration of a loop around it; a loop kept rolled stays rolled, the frame_update event in its body.

  • A change a loop carries out of its last iteration, which nothing after the loop takes, is a frame_update event after the loop, at its place in the last iteration.

  • A change made before a branch that only one arm takes, in the segment the branch is in, is a frame_update event at the start of the other arm (or of the missing else), with that arm’s conditions; the arm that takes it plays with it as before.

  • When a segment starts between such a change and the branch (an answer waited for as long as it takes, or a wait whose length is an answer, lies between them), the change is a frame_update event where it is made, in its own segment and with no conditions: it happens whichever arm runs, and the arm’s condition is known only in the later segment. A change of a tone’s phase before the answer and a drive in one arm after it are listed as:

    {"index": 0, "kind": "frame_update", "start_cycles": 0, "segment": 0,
     "conditions": [], "phase_rad": 0.5, ...}
    {"index": 5, "kind": "drive", "start_cycles": 824, "segment": 1,
     "conditions": [{"branch": 0, "arm": true, "answers": [3], ...}],
     "phase_rad": 0.5, ...}
    

    A change a loop’s last iteration leaves to such an arm is one frame_update event after the loop, at its place and in its segment in the last iteration, the loop kept rolled or written out.

  • A change a loop carries through a branch, or out of a loop whose iterations do not play its tone, is a frame_update event at every execution; a drive or readout that takes it on another path has it in its tone state as well.

A frame_update event has op and value, as in frame_updates, its start_cycles (its place) and a duration_cycles of 0, and the tone’s frequency_hz and phase_rad with the change applied; on a tone whose state depends on run time, tone and the frame_updates it applies, as a drive has. A controller tracking that tone state applies the event’s frame_updates, each id once, as it does a drive’s; the last of them is the event’s own change, which its op and value restate, so it is not applied again. Its line is its tone’s, else the line the tone last played on, else null. A controller whose tone states persist from one shot to the next declares Controller(persistent_frames=True); such changes then have no event, and the program’s frame_state (after events) gives the state each tone ends the shot in, by line: its line, and its frequency_hz and phase_rad at the end of the shot, or, for a tone whose state depends on run time, its tone and the frame_updates not yet taken.

A waveform parameter computed from a readout or an answer is null in params and has an expression (see Run-time expressions) in the node’s runtime, by parameter name. A variable wait’s duration_cycles is its least length, and it also has length, the expression of its length in cycles, and min_cycles and max_cycles, its bounds. A program that computes nothing from its readouts and answers exports none of these keys.

A sync has lines, the lines it joins, and lasts 0 cycles: no event on those lines before it ends after its start, and none after it starts before. Its start is never before the start of its segment: a sync after an answer its lines waited for is listed where that answer’s segment starts.

A classical call is a submit, while the request holds its link, and an answer, when the answer is taken. Both have link and function; a submit has measurements, the readout events whose outcomes it sends, and operands, its arguments in the order of the function’s argument descriptors (one per scalar or element, and one {"readout": i} per measurement), each an expression; an answer has wait, "bounded" (by the latency budget), "blocking" or "timeout", with timeout_ns. A call taken with result(timeout=...) waits with that timeout, and one of a function declared with latency_mode="timeout" (and taken without one) with the function’s timeout_ns.

The events of one operation, and of operations with the same waveform, share their waveform and amplitude objects; copy one before changing it.

Run-time expressions#

What a program computes from its readouts and answers (a run-time waveform parameter, a wait’s length, a frame_update event’s value, a submit’s operands, a branch’s expression) is an expression in a closed vocabulary of terms, whatever operations the compiler computes it with: y = x if x > 0.1 (else 0.1) and y = x if 0.1 < x are both max. An expression is one of:

{"const": x}

A number, or true or false.

{"readout": i}

The level the readout event i observed, an integer; 0 is the ground state, and the outcome is set for any other level.

{"answer": i, "result": k}

Result k of the answer event i: a float for a float result, an integer for an int one, a bool for a bit. A call taken with result(timeout=...), or of a function declared with latency_mode="timeout", has the bool that its answer came in time as result 0, before the function’s values.

{"op": term, "type": t, "of": [...]}

A term over the expressions in of, computed in the type t of its operands, "int", "float" or "bool". A const in a float term is that float, so {"const": 3} there is 3.0.

{"term": n}

Term n of the expression’s terms, where that term is used.

An expression that is a term may have "terms": [...] beside its op, type and of: terms written once there and used by their place in the list, the first being term 0. A term is in terms when the expression uses it more than once, or when the expression would otherwise nest more than 65 terms deep; every other term is written where it is used. A term of terms uses only terms before it, so computing the list in order computes each term once. For t = x * 0.001 (x result 0 of answer event 1), t = t + t and a phase shift of t + t:

{"terms": [
   {"op": "mul", "type": "float", "of": [{"answer": 1, "result": 0}, {"const": 0.001}]},
   {"op": "add", "type": "float", "of": [{"term": 0}, {"term": 0}]}],
 "op": "add", "type": "float", "of": [{"term": 1}, {"term": 1}]}

terms is only at the root of an expression and belongs to all of it; code that makes an expression an operand of a new term moves the list to the new root. The compiler writes no expression that nests more than 65 terms deep, however many terms the program computes it with, which is the deepest it wrote one before there were terms; the length of a wait on a controller with a clock of its own is up to three terms more (its bounds, and the conversion to the controller’s cycles), as before. An expression has no terms unless it uses a term more than once or is deeper than that: an export that held no such expression is written as before.

cudaq_pulse.controller.ExpressionTerm names every term. A value computed any other way is an UnsupportedError when the program is exported, never a term a backend has to guess at; cudaq_pulse.controller.evaluate_expression(expression, readouts, answers) computes an expression as the controller must, which is the reference for a backend’s own evaluation. Integers are 64-bit two’s complement and floats IEEE 754 binary64, rounded to nearest, ties to even.

add, sub, mul

a + b, a - b and a * b. Integers wrap modulo 2 to the power 64; floats overflow to ±inf.

div

a / b. An integer quotient is truncated toward zero; a float divided by zero is ±inf, and 0 / 0 is NaN.

floor_div, rem

Integers only: the quotient rounded toward −inf, and the remainder a - b * div(a, b), which has the sign of a. An integer div, floor_div or rem by 0, or of −2 to the power 63 by −1, has no value, and the controller fails the run.

neg

-a. The integer −2 to the power 63 wraps to itself; a float changes sign, 0 and NaN too.

max, min

max(a, b) is a when a > b, else b, and min(a, b) is a when a < b, else b. For integers that is the usual maximum and minimum, which the export writes with the operands of the comparison it comes from. For floats the order is part of the value, as the kernel’s comparison was: max(NaN, 1) is 1 and max(1, NaN) NaN, max(-0.0, 0.0) is 0.0 and max(0.0, -0.0) −0.0.

eq, ne, lt, le, gt, ge

A bool: a == b, a != b, a < b, a <= b, a > b, a >= b. Integers compare signed. Floats compare ordered, so every comparison with a NaN is false but ne, which is true. Bools have only eq and ne.

select

of is [c, a, b]: a when the bool c is true, else b; type is the type of a and b.

to_float

The integer a as the nearest float (type "int").

to_int

The float a truncated toward zero (type "float"); NaN, ±inf and floats outside the 64-bit integers have no value, and the controller fails the run.

controller_cycles

A length of a program cycles in the controller’s cycles, a * num / den, with num, den and timestep beside of (see Clocks); the controller fails the run when a is not a whole number of timesteps.

A frontend computes an integer answer used as a time in floats, so wait(d, n + 4) is to_int(add(add(to_float(n), 4), 0.5)). Whether a readout is set is ne(readout, 0), and a bool used as an integer a select of 1 (or −1) and 0. A comparison has its constant second: 3 < n is gt(n, 3). Bools combine without terms of their own: a and b is select(a, b, false), a or b is select(a, true, b), a != b (exclusive or) is ne(a, b), and not a is ne(a, true), except that not of an integer comparison is the opposite comparison (not (n <= 3) is gt(n, 3)) and of eq or ne the other; a float not (x <= 0.1) stays as it is, since it holds of NaN and x > 0.1 does not. There is no JSON schema; this section and evaluate_expression define the expressions.

The structured format#

export_to_controller(version=2) gives format "cudaq-pulse.controller/2", which grows with a program’s operations rather than with its executions, as a sequencer runs it. It is written for a controller on the program clock, in timesteps of any length; for one on its own clock, or with line delays, export /1. It has the keys of /1, except that:

events

Each event’s kind is one of cudaq_pulse.Event, named for what a controller does whatever the modality: "play" (a /1 drive), "readout" (a /1 readout), "wait", "sync", "loop", "call" (a /1 submit), "answer" and "frame_update". An event in a branch keeps its kind and lists its conditions, and phase and frequency changes are in the tones of the events that play them; a change no event plays is a "frame_update", with its tone state in tones. A backend refuses a kind it does not know.

A tree. A loop whose iterations repeat (each event moves by one period, and every tone keeps its frequency) is one loop event: trips iterations from first_iteration (omitted when 0), one period_cycles apart, its start_cycles and duration_cycles spanning them, and its body, the events of its first iteration. An event in the body of loops starts start_cycles plus, for each loop around it, the iterations since its first times its period. A first iteration that differs from the rest is exported before its loop; a loop that does not repeat is exported once per iteration, as in /1.

waveforms

Each distinct waveform expression once; a play or readout gives the index of its own in waveform. With sample_rate_ghz, samples holds each one’s samples, in the same order. Each also has id, 16 hex digits that depend on its expression alone, so they stay the same from one compile, call, line or controller to the next, and shape_id, equal for waveforms that differ only in overall amplitude and phase (shape_key), the ones a controller that scales what it plays stores once.

parameters

For a module with float parameters, each one’s value in this call and what it reaches: "amplitude" (an overall factor of a waveform), "phase", "frequency", "shape" (another waveform parameter), "timing" (a length) or "other". A parameter that reaches only amplitudes and phases leaves every shape_id the same for every value. Omitted without parameters.

controller

In place of limits: the controller, its fields under the names limits gives them. What it computes is in processors, always listed (empty for a controller without processors), each one as Processor.as_dict() gives it: links, clock_ghz, cycles_per_op (the cost of each class of operation it computes), unsupported (the classes it cannot compute, by name), max_concurrent_calls, max_ops_per_function, max_ops_total, max_state_bytes_per_function, max_state_bytes_total, max_calls_per_shot, max_bytes_per_shot, fixed_cost_only and dispatch_keys.

quantum_system

In place of quantum_system: the name of the quantum system the program is for.

sample_rate_ghz

Beside clock_ghz, always a number: the rate at which the controller’s converters play waveform samples, so that the program states its own timing. A waveform of n samples lasts n * clock_ghz / sample_rate_ghz cycles. The ratio may be fractional (2.4 GHz on a 1 GHz clock), but every waveform lasts a whole number of samples, and the export raises ValueError for one that does not rather than round. The rate is the controller’s, else the export’s sample_rate_ghz, which must then be the same, else clock_ghz (one sample per cycle). Unlike in /1, it need not be a whole multiple of the clock, and samples are present only when the export is given sample_rate_ghz.

links

Each link with send_ns, bandwidth_bytes_per_ns, answer_delay_ns and slots for Link.fixed_ns, bytes_per_ns, feedback_ns and slots.

lines

Each line’s qudits, a list, in the order the program first uses the lines. A shared line (see Shared lines) is listed once, with every qudit it reaches and its max_concurrent_tones, which a dedicated line does not have. In place of native_frequency_hz, native_frequencies_hz: the native frequency of each of its qudits, in the same order.

Events on a shared line

A play, readout, wait or frame_update event on a shared line has, after its line, its tone_slot (0 to the line’s max_concurrent_tones minus 1: the tone it plays, which a controller maps to one of the line’s oscillators) and qudits, the one qudit it is for. Tones that play at the same time have different slots. Slots are given from the program alone, so every export of it gives the same: a line playing one tone at once gives every event slot 0; else each qudit keeps one slot for the whole program when the line has enough for the qudits whose events overlap (lowest free slot first, in the order of their first events); else each tone takes the lowest free slot in start order, which a rolled loop cannot hold (UnsupportedError: unroll it, or give the line more tones). A sync lists each of its lines once; runtime_tones and frame_state entries name the line and, on a shared line, the qudits (and tone_slot) of their tone. On dedicated lines none of these keys are present, and a program that plays no shared line exports as it does on a system without them.

Broadcast lines

A broadcast drive line (see Broadcast lines) is listed once, with "broadcast": true, every qudit it reaches, and one native_frequency_hz, its tone’s, in place of native_frequencies_hz. A play or frame_update event on it has, after its line, the line’s qudits, all of which it reaches, and no tone_slot; so do its runtime_tones and frame_state entries.

Played events and frame_update events

tones, a list of the tones the event plays (a frame_update event’s tone state), each with its frequency_hz, its phase_rad in the loops’ first iterations, and phase_step_rad, the phase added per iteration of each loop around it, outermost first.

Segments in a rolled loop

An event’s segment is that of the loops’ first iterations; in a loop whose body ends segments (a blocking wait for an answer, or a wait whose length is an answer), segment_step is the segments added per iteration of each loop around it, outermost first, omitted where every step is 0.

Values known at run time

As in /1, and only where a value is computed from a readout or an answer: tone, frame_updates and phase_offset_rad on a play or readout, length, min_cycles and max_cycles on a wait, and operands on a call; an expression’s readout and answer are places, like other references, and resolve to the latest execution of that place before the event, so a rolled body refers to its own events, and a tone change’s start_cycles is given as an event’s is. A waveform parameter known only at run time is its expression in the entry of the program’s waveforms the event plays, where /1 has null and the node’s runtime. The program’s runtime_tones and frame_state are as in /1. A loop whose body changes or plays a tone whose state is tracked at run time is exported once per iteration, so that each tone change keeps its own id; a loop whose run-time values repeat with its iterations stays one loop.

Symbols

A kernel with list or qudit parameters has program_id and symbols as in /1, and each event the symbols its fields depend on; an event in a loop’s body names those of any of its iterations.

Left out

What follows from the rest or is the default: an event’s index (its place in a pre-order walk of the tree, loops included, which is what references give), its qudit (its line’s), its end_cycles, an empty iteration (null for a loop kept rolled), a segment of 0, empty conditions, readouts and answers, the line of a sync or a classical call, a timeout_ns of null, and the amplitude, which follows from the waveform. A reference is to the latest execution of the event it names.

programs, bindings

Reserved for programs that several controllers share and the tables that bind them to each; empty for now.

cudaq_pulse.controller.unroll(program) turns a /2 program into the /1 program of the same kernel, for a backend that needs every execution; phases agree up to rounding and branches are numbered in the order they first appear. A /2 program lists a shared line once, so on one, unroll numbers the lines in the order of the /2 lines, each shared line’s qudits in its order (those the program plays at), where the /1 export may number them in the order the kernel takes them, and a sync names every /1 line of a shared line it joins.

Where to go next#