Getting More out of Your Controller’s Instruction Set#
Note
Experimental, as is Lowering to Your Controller’s Instruction Set, which this page builds on.
Every controller has limits. This tutorial takes six common ones. For each, it shows what the compiler does, so that a small instruction set is enough.
What you’ll learn:
how the compiler meets feedback latency, within and across controllers
how two controllers in different boxes act as one QPU
how loops, decoder deadlines and waveform memory reach your ISA
which limits are checked when a program compiles, not on the hardware
All the code on this page is also one Python script,
isa_limits.py.
Set up#
This page uses the toy controllers and the lowering pass of Lowering to Your Controller’s Instruction Set. The full code is in examples/isa_lowering/toy_isa.py. Save it beside your own code.
import dataclasses
import textwrap
import cudaq_pulse as pulse
from cudaq_pulse.controller import ControllerLimitError
from toy_isa import lower, toy_control_system, toy_qpu
qpu = toy_qpu()
first, second = toy_control_system().controllers
q0, q1, q2 = qpu.qudits
print("model: ", first.model)
print("feedback latency:", first.feedback_latency_cycles, "cycles")
Output
model: toy.sequencer
feedback latency: 80 cycles
The QPU has three qudits on two controllers:
The first controller drives qudits 0 and 1, and reads both on one feedline.
The second controller drives and reads qudit 2.
Outcomes reach the other controller 40 cycles later.
Both count from a 250 MHz reference clock, and a decoder sits on a link.
A refused program says why in one long message. This helper prints it wrapped to the page:
def show(error):
message = str(error.violations[0])
for line in textwrap.wrap(message, width=64):
print(line)
Feedback latency#
A common constraint: a branch can use a measurement only some time after the acquisition ends. It takes longer still when another controller made the measurement.
Your ISA needn’t count those cycles. The compiler does.
A branch on another controller’s readout#
Here the first controller reads qudit 0. The second flips qudit 2 when it reads 1:
@pulse.kernel
def flip_other(measured, other):
readout_line = get_readout_line(measured)
drive_line = get_drive_line(other)
probe = square(duration=200, amplitude=0.05)
flip = gaussian(duration=40, amplitude=0.5, sigma=10.0)
outcome = readout(readout_line, probe)
sync(readout_line, drive_line)
if outcome:
drive(drive_line, flip)
The default schedule waits for it#
The readout ends at cycle 200, and its bit reaches the second controller 120 cycles later: 80 of feedback latency and 40 of peer latency. Every schedule keeps the flip that far from its readout, so the default one starts it at cycle 320. Each controller’s part then lowers on its own:
compiled = pulse.compile(flip_other, quantum_system=qpu)
split = compiled.lower(q0, q2)
part = split.export_to_controller(controller=0)
for line in str(lower(part)).splitlines():
print(line)
Output
.chan ch0.0 readout q0
.wave w0 200
set_freq ch0.0 5000000000
acquire ch0.0 w0 0.05 m0
The first controller only acquires. The second waits for the bit, then branches on it:
part = split.export_to_controller(controller=1)
for line in str(lower(part)).splitlines():
print(line)
Output
.chan ch0 drive q2
.wave w0 40
wait 80
branch !m0 end0
set_freq ch0 5600000000
play ch0 w0 0.5
end0:
The two listings in time, on the 1 GHz clock, so a nanosecond is a
cycle. Bit m0 is ready for the second controller at cycle 320, where
its wait 80 ends.#
Timeline of flip_other on two toy controllers, in nanoseconds of the 1 GHz clock. Controller 0 acquires q0 from 0 to 200 ns into bit m0. The bit is ready on controller 0 80 ns later, after its feedback latency, and on controller 1 another 40 ns later, after the peer latency. Controller 1 plays the flip of q2 from 320 ns, only when the bit is 1.
What to notice:
The flip waits 120 cycles: 80 of feedback latency, then 40 from one controller to the other.
Both parts use the program’s event indices, so the bit is
m0on both.
A controller that can’t forward outcomes#
Sending a bit to another controller is a capability, forward_outcome.
A controller without it is refused this program:
local_only = dataclasses.replace(
first,
supports=["play", "readout", "wait", "sync", "branch"],
)
system = pulse.ControlSystem(
controllers=[local_only, second],
reference_clock_ghz=0.25,
)
compiled = pulse.compile(
flip_other,
quantum_system=qpu,
control_system=system,
)
The refusal names the readout that needs it:
try:
compiled.lower(q0, q2)
except ControllerLimitError as error:
show(error)
Output
controllers[0] (toy.sequencer): the readout on line 0 (qudit 0)
at cycle 0 needs 'forward_outcome', which the controller does
not support; run that part of the program on a controller that
supports it, or write it without 'forward_outcome'
Every operation in a branch waits for the bit, a wait too: a backend may
branch where the arm starts. Idle time that doesn’t depend on the bit
belongs before the if, not inside it, where it would start only once
the bit has arrived.
Takeaway: describe the latencies once, and every branch is placed where its bit has arrived.
One QPU from two controllers#
A common constraint: controllers in different boxes have no shared notion of “now”. Nothing in the ISA lines them up.
The compiler checks that they can be lined up, before anything runs.
A pulse on each controller#
This kernel syncs a pulse on each controller:
@pulse.kernel
def joint(a, b):
first_line = get_drive_line(a)
second_line = get_drive_line(b)
x90 = gaussian(duration=40, amplitude=0.25, sigma=10.0)
flat = square(duration=40, amplitude=0.1)
drive(first_line, x90)
sync(first_line, second_line)
drive(first_line, flat)
drive(second_line, flat)
With the QPU’s clock#
With the QPU’s reference clock, the sync is in both parts, under one index, at one cycle:
together = pulse.compile(joint, quantum_system=qpu).lower(q0, q2)
for number in range(2):
part = together.export_to_controller(controller=number)
print(f"controller {number}:")
for event in part["events"]:
print(f" {event['index']} {event['kind']:<5} "
f"at {event['start_cycles']}")
Output
controller 0:
0 drive at 0
1 sync at 40
2 drive at 40
controller 1:
1 sync at 40
3 drive at 40
Takeaway: both sequencers just wait until cycle 40. The ISA needs no handshake instruction.
Rolled loops and instruction memory#
A common constraint: a sequencer holds a few hundred or a few thousand instructions. A long pulse train has to stay a loop.
A pulse train#
Here is a train of 1,000 pulses:
@pulse.kernel
def train(qudit):
drive_line = get_drive_line(qudit)
x90 = gaussian(duration=40, amplitude=0.25, sigma=10.0)
for _ in range(1000):
drive(drive_line, x90)
wait(drive_line, 8)
On a controller without loops#
The toy sequencer has no loop instruction, and room for 256 pulses. So the train is refused, with the reason:
try:
pulse.compile(train, quantum_system=qpu).lower(q0)
except ControllerLimitError as error:
show(error)
Output
controllers[0] (toy.sequencer): 1000 drives and readouts; the
controller plays at most 256. It does not support 'loop', so
rolled loops are unrolled for it: the loop of 1000 iterations
from cycle 0 plays 999 more
On a controller with loops#
Give the controller the loop capability, and the train compiles:
capabilities = list(first.supports or [])
capabilities.append("loop")
looping = dataclasses.replace(first, supports=capabilities)
compiled = pulse.compile(
train,
quantum_system=qpu,
control_system=looping,
)
rolled = compiled.lower(q0)
flat = rolled.export_to_controller()
print(len(flat["events"]), "events")
Output
2000 events
The default format, /1, lists all 2,000 executions. The structured
format keeps the train as one loop:
structured = rolled.export_to_controller(version=2)
loop = structured["events"][0]
print(loop["kind"], loop["trips"], "trips")
print(loop["period_cycles"], "cycles each")
for event in loop["body"]:
print(" ", event["kind"])
Output
loop 1000 trips
48 cycles each
play
wait
Against the controller’s instruction memory:
| pulses | |
|---|---|
| the controller holds | 256 |
| 1,000-pulse train, no loop (refused) | 1,000 |
| 1,000-pulse train, with loop | 1 |
Takeaway: a sequencer with a loop counter lowers the body once.
A decoder with a deadline#
A common constraint in QEC: syndromes go to a decoder off the sequencer. Its answer comes back after a latency the sequencer can’t predict.
The compiler places the call, and the moment to give up on it.
The decoder#
The decoder here is a parity check. Its budget is 100 ns:
def parity(bits: pulse.Bits) -> pulse.Bits:
return [bits[0] ^ bits[1]]
decode = pulse.classical(
parity,
link="decoder",
max_latency_ns=100,
)
A round with a deadline#
A QEC round reads two syndromes and asks the decoder. It waits at most 60 ns for the answer, then applies the correction, or the fallback when the answer is late:
@pulse.kernel
def qec_round(data, ancilla):
probe = square(duration=200, amplitude=0.05)
flip = gaussian(duration=40, amplitude=0.5, sigma=10.0)
syndrome_0 = readout(get_readout_line(data), probe)
syndrome_1 = readout(get_readout_line(ancilla), probe)
request = decode.submit([syndrome_0, syndrome_1])
in_time, fix = request.result(timeout=60 * pulse.ns)
if fix[0]:
drive(get_drive_line(data), flip)
if not in_time:
drive(get_drive_line(ancilla), flip)
The result starts with a flag, in_time. It is true when the answer
came by the deadline.
When it did,
fixis the decoder’s correction.When it didn’t,
fixis all zeros, and the round takes its fallback. Here the fallback flips the ancilla, so its next readout marks the round as undecoded.
The listing#
Lower the round. Both syndromes are read at once, on the two tones of the feedline:
compiled = pulse.compile(
qec_round,
quantum_system=qpu,
)
qec = compiled.lower(q0, q1)
listing = lower(qec.export_to_controller())
for line in listing.code:
print(line)
Output
set_freq ch0.0 5000000000
acquire ch0.0 w0 0.05 m0
set_freq ch0.1 5300000000
acquire ch0.1 w0 0.05 m1
wait 70
submit L0 m0 m1
wait 18
poll L0 a3
branch !a3 end0
set_freq ch1 5000000000
play ch1 w1 0.5
end0:
branch a3.ok end1
set_freq ch2 5300000000
play ch2 w1 0.5
end1:
What to notice:
The request leaves at cycle 280, when both bits are on the bus.
The
pollis at cycle 352: the submit’s 4 cycles, the 60 ns deadline, and the link’s 8 ns back.The branch on
a3is the correction. The branch ona3.okis the fallback.
The round in time, from the compiled program. The request leaves when both bits are on the bus, and the answer is due after the 60 ns deadline and the link’s way back.#
Timeline of one QEC round on the toy QPU: both syndromes are read at once on the feedline's two tones, and the request leaves for the decoder at 280 ns. The kernel waits for the answer with a 60 ns timeout, plus the link's 8 ns back, so it is due at 352 ns. Two flips that play only on the shots that need them start there: the correction and the fallback.
What to notice: the 80 ns between the readouts and the request is the feedback latency, before the bits are on the bus.
Each branch reads its own result of the answer. In the export, each condition says which one:
for event in qec.export_to_controller()["events"]:
for condition in event["conditions"]:
line = event["line"]
answer = condition["answers"][0]
result = condition["result"]
print(f"line {line}: answer {answer}, result {result}")
Output
line 2: answer 3, result 1
line 3: answer 3, result 0
Result 0 is the in-time flag. Result 1 is the decoder’s first value.
A decoder between two controllers#
Both controllers reach the decoder’s link. So the answer can steer a pulse on either one.
In this round, the first controller reads the syndromes. The data qudit it corrects is on the second controller:
@pulse.kernel
def correct_across(ancilla_0, ancilla_1, data):
probe = square(duration=200, amplitude=0.05)
flip = gaussian(duration=40, amplitude=0.5, sigma=10.0)
syndrome_0 = readout(get_readout_line(ancilla_0), probe)
syndrome_1 = readout(get_readout_line(ancilla_1), probe)
request = decode.submit([syndrome_0, syndrome_1])
in_time, fix = request.result(timeout=60 * pulse.ns)
if fix[0]:
drive(get_drive_line(data), flip)
if not in_time:
drive(get_drive_line(ancilla_1), flip)
Lower it with qudits 0 and 1 as the ancillas and qudit 2 as the data, then print each controller’s listing:
compiled = pulse.compile(
correct_across,
quantum_system=qpu,
)
across = compiled.lower(q0, q1, q2)
for number in range(2):
part = across.export_to_controller(controller=number)
print(f"; controller {number}")
for line in lower(part).code:
print(line)
Output
; controller 0
set_freq ch0.0 5000000000
acquire ch0.0 w0 0.05 m0
set_freq ch0.1 5300000000
acquire ch0.1 w0 0.05 m1
wait 70
submit L0 m0 m1
wait 18
poll L0 a3
branch a3.ok end1
set_freq ch1 5300000000
play ch1 w1 0.5
end1:
; controller 1
wait 88
poll L0 a3
branch !a3 end0
set_freq ch0 5600000000
play ch0 w0 0.5
end0:
What to notice:
Only the first controller submits. The request leaves at cycle 280, as before.
Both controllers poll at cycle 352. Each branches on the result it needs: the second on the fix, the first on the in-time flag.
The second controller never branches on
m0orm1. It uses only the decoder’s answer.
Here is the round playing, once with the answer in time and once late:
A decoder between two controllers
If the answer is in time with a3 = 1, controller 1 drives q2 and controller 0 skips its drive on q1.
- Controller 0 acquires m0 and m1 from q0 and q1.
- The acquisition ends at 200. Each bit takes 80 cycles of feedback latency.
- submit sends m0 m1 over link L0 to the decoder.
- The answer may come any time in the next 60 cycles.
- Controllers 0 and 1 poll at 352: the answer is there, a3 = 1. Then controller 1 drives q2 and controller 0 skips its drive on q1.
If the answer is late, controller 0 drives q1 and controller 1 skips its drive on q2.
- Controller 0 acquires m0 and m1 from q0 and q1.
- The acquisition ends at 200. Each bit takes 80 cycles of feedback latency.
- submit sends m0 m1 over link L0 to the decoder.
- The answer may come any time in the next 60 cycles.
- Controllers 0 and 1 poll at 352: no answer, so a3.ok = 0. Then controller 0 drives q1 and controller 1 skips its drive on q2.
The shaded stretch on the link runs from the request’s departure to its deadline. The decoder may answer at any point in it. The polls, 8 cycles after the deadline, take the answer if it came.
Takeaway: the deadline is a number the compiler schedules around. Your sequencer only polls once, where the compiler put the poll.
Waveform memory and oscillators#
A common constraint: envelopes live in a small memory next to the converters. A new shape costs memory, but a new gain costs nothing.
A sweep is one table entry#
An amplitude sweep compiles once, with a float argument:
@pulse.kernel
def rabi(qudit, amplitude: float):
shape = gaussian(
duration=40,
amplitude=amplitude,
sigma=10.0,
)
drive(get_drive_line(qudit), shape)
sweep = pulse.compile(rabi, quantum_system=qpu)
for amplitude in (0.2, 0.4):
program = sweep.lower(q0, amplitude).export_to_controller()
print(lower(program).code[-1])
Output
play ch0 w0 0.2
play ch0 w0 0.4
Every value plays the same entry, w0, at its own gain.
New shapes and new frequencies#
Thirty pulse widths are thirty entries. And a channel holds only so many oscillator frequencies:
@pulse.kernel
def widths(qudit):
drive_line = get_drive_line(qudit)
for k in range(30):
sigma = 4.0 + 0.5 * k
shape = gaussian(duration=40, amplitude=0.25, sigma=sigma)
drive(drive_line, shape)
And three frequencies on one line:
@pulse.kernel
def hops(qudit):
drive_line = get_drive_line(qudit)
x90 = gaussian(duration=40, amplitude=0.25, sigma=10.0)
for _ in range(3):
drive(drive_line, x90)
shift_frequency(drive_line.tone, 1.0e6)
Both are checked against the description when they compile:
for kernel in (widths, hops):
try:
pulse.compile(kernel, quantum_system=qpu).lower(q0)
except ControllerLimitError as error:
show(error)
Output
controllers[0] (toy.sequencer): line 0: its waveforms take 1200
samples; the controller's waveform memory holds 1024
controllers[0] (toy.sequencer): line 0: 3 tone frequencies; the
controller holds 2 per line
What to notice:
Thirty widths overflow the channel’s 1,024 samples.
Three frequencies overflow its two oscillator settings.
Both are compile errors, not a failed upload.
Here are the three programs against the channel’s memories:
| waveform samples | frequencies on the line | |
|---|---|---|
| the controller holds | 1,024 | 2 |
| Rabi sweep at 0.2 and 0.4 | 40 | 1 |
| 30 pulse widths (refused) | 1,200 | 1 |
| 3 frequencies on one line (refused) | 40 | 3 |
Phase is cheaper still. The compiler folds constant phase shifts into the next pulse’s frame, as it did with the Ramsey sequence’s virtual Z.
Takeaway: your description’s memory sizes are checked for you, before anything reaches the hardware.
Where to go next#
Lowering to Your Controller’s Instruction Set builds the toy controllers and the pass this page uses.
Real-Time Decoding with a Deadline runs a QEC round with a decoder deadline and a fallback table.
examples/23_define_a_qpu.py loads controllers from an installed description and saves a QPU as one file.
Describe Your Control System documents each limit a controller states.