Your first program#

This page builds a small program — three atoms and a laser pulse with three settings to learn — checks it against the bundled reference device, and trains it for five steps with PyTorch. The site runs this page at every build, so the output shown is the code’s own.

Times are in µs and distances in µm. Frequencies are written with MHz from cosmiq.units, as in 1.5 * MHz (Conventions has the details).

import torch
import cosmiq as cq
from cosmiq.emulator import density
from cosmiq.units import MHz, us

t = [0.0, 0.2 * us, 0.8 * us, 1.0 * us]  # on the device's 4 ns clock
program = cq.program(
    cq.Register.chain(3, spacing=6.1),  # µm
    lambda p: cq.GlobalDrive(
        amplitude=cq.pwl(t, [0.0, p["omega"], p["omega"], 0.0]),
        detuning=cq.pwl(t, [p["start"], p["start"], p["stop"], p["stop"]]),
    ),
    {"omega": 1.5 * MHz, "start": -2.0 * MHz, "stop": 2.0 * MHz},
).to_target("cosmiq-ref-1")  # checked against the device; ConstraintError if not

emulator = cq.Emulator(observable=density())  # the CPU, complex128
optimizer = torch.optim.Adam(program.parameters(), lr=0.1)
for _ in range(5):
    optimizer.zero_grad()
    loss = -emulator(program())[0].sum()  # maximise the Rydberg population
    loss.backward()  # exact gradients, through the device's rounding
    optimizer.step()
  • cq.program(register, drive_fn, params) makes the program: a PyTorch module whose trainable parameters are the dict’s values. drive_fn turns them into the laser pulse, and program() returns the resulting schedule.

  • .to_target("cosmiq-ref-1") checks the program against the reference device straight away. A value the device can’t play raises ConstraintError, naming the parameter behind it (Quickstart).

  • cq.Emulator(observable=density()) simulates the atoms and returns how excited each atom ends up; you can call .backward() on the result to get exact gradients for training (The emulator).

The same gradient from shots#

Real hardware returns only measurement counts, so there a gradient has to be estimated from them. cq.gradients.estimate does this with SPSA: it nudges every setting at once in a random direction, runs the program on both sides, and compares the counts:

# The same gradient from shots, as the hardware would have to estimate it:
shots_only = cq.Emulator(samples_only=True)
g = cq.gradients.estimate(
    program,
    lambda result: float(result.shots[0].rydberg_density.sum()),
    emulator=shots_only,
    shots=500,
    samples=10,  # SPSA directions, two runs each
    seed=1,
)
print(g.method, g.shots)  # spsa 10000
for name, value in g.value.items():
    print(name, float(value), "±", float(g.stderr[name]))
spsa 10000
params.omega 0.07500000000000032 ± 0.04662498138218296
params.start -0.03900000000000017 ± 0.05128244230620176
params.stop -0.10700000000000048 ± 0.039074003861618675

shots=500 is the number of measurements in each run. samples=10 is the number of random directions, each run twice, so 10,000 shots in all. seed=1 makes the result repeatable. Each estimate comes with its standard error, after the ±.

Next: the quickstart, or the tutorials.