Remote jobs#
cosmiq.remote runs your emulations on a CPU or GPU box in the cloud, and returns what
Emulator.run would have returned on your laptop, gradients included. Your training loop stays
on your laptop; only the emulation, the expensive part, runs on the box. A job is data (the
schedule, the observables and the emulator’s options), never your code.
import cosmiq as cq
from cosmiq.emulator import density
with cq.remote.session("emu:gpu", max_minutes=30) as box: # starts or reuses your box
result = box.run(program(), observables=[density()])
loss = -result.expectations["density"][0].mean()
loss.backward() # the gradient comes back from the box
Remote jobs aren’t open yet. cq.remote.local() runs the same code on your machine,
through the same function a box answers with, and needs no account:
with cq.remote.local() as box: # instead of cq.remote.session(...)
result = box.run(program(), observables=[density()])
loss = -result.expectations["density"][0].mean()
loss.backward()
So code written against local() runs on a box by swapping in session(...).
Tutorial 4 runs on local(). The
virtual QPU, a shared, hardware-like target, is below.
The API key#
Make a key on your account page (“API keys”); it’s shown once. cosmiq reads it from, in order:
session(..., api_key="cq_...");the
COSMIQ_API_KEYenvironment variable;~/.config/cosmiq/credentials: the key on a line of its own (orapi_key = cq_...). The file must be readable by you alone (chmod 600); cosmiq refuses it otherwise.
The key is masked wherever cosmiq shows it, never logged, and never put in an error. cosmiq talks to the site over HTTPS only, with certificate checks always on.
A python.org Python on macOS ships without CA certificates until you run “Install
Certificates.command” in its folder under /Applications. cosmiq’s error says so;
SSL_CERT_FILE pointing at a CA bundle works too.
Sessions#
box = cq.remote.session(
"emu:cpu", # or "emu:gpu"
max_minutes=60, # the longest the box may run; your plan's limit caps it
idle_minutes=10, # stop it after this long without a job; your plan's idle stop caps it
tier=None, # a GPU tier; default: your plan's lowest
keep=False, # True: leave the box running when the with block ends
)
session() starts a box, or reuses your live one of that kind, and waits until it’s ready.
logging.basicConfig(level=logging.INFO) shows its boot steps. Leaving the with block stops
the box unless keep=True; box.close() does it by hand, and box.status() shows its state.
A session is billed in minutes of the box, like Code Studio’s; jobs cost nothing more. If a box fails while running a job, the job runs again and its minutes count again, but it’s still accepted only once.
Running jobs#
box.run(schedule, *, observables=None, shots=None, seed=None, emulator=None, noise=None, ...)
takes Emulator.run’s arguments and returns an EmulationResult:
schedule: a program’s output (its gradient comes back), or aScheduleData;noise:None, or"device"for the device’s calibrated noise;emulator: the options to run with. By default it’sbox.emulator, which you can set:
box.emulator = cq.Emulator(precision="double", gradient="adjoint")
result = box.run(program(), observables=[density()]) # runs with those options on the box
The box, not your laptop, decides the torch device. precision="auto" means double precision
on emu:cpu and single on emu:gpu.
box.submit(...) takes the same arguments and returns a RemoteJob at once, for collecting
later:
job = box.submit(program(), observables=[density()], shots=100, seed=7)
job.status() # "queued", "running", ...
result = job.result() # waits; the result has no gradient
A few arguments don’t travel, and are refused with what to do instead:
Not carried |
Instead |
|---|---|
|
run locally, |
|
|
a |
|
|
|
|
|
an |
|
|
run it locally: a job carries no space |
Gradients#
Gradients come back automatically. run() returns expectations attached to your graph, so
loss.backward() gives the same gradient as a local run; any torch code in your program’s
drive() works, since that part of the chain rule stays on your laptop. When there are only a
few outputs, the gradient comes back with the values in the same answer; for many outputs (all
the probabilities, say) your backward pass sends a second, small job. If the gradient wouldn’t
fit in a job, the run is refused before anything is sent, so no box time is spent on it.
gradient="none" asks for no gradient. None comes back for shots, under torch.no_grad(), or
for a ScheduleData.
SPSA and finite differences: one job#
cq.gradients.estimate(program, loss_fn, method="spsa", emulator=box, ...) works as with a
local Emulator, with box.emulator’s options. Every evaluation point goes to the box in one
job, so an estimate costs one round trip, and it equals the local estimate bitwise on the same
machine and precision. Since remote runs keep no state, loss_fn reads the emulator’s
observable or the shots.
PennyLane circuits#
qml.device("cosmiq.remote", wires=4, session=box) runs cosmiq.emulator’s circuits on the
session’s box, with one request per training step:
PennyLane.
The virtual QPU#
vqpu:cosmiq-ref-1 is the virtual QPU: a shared queue that behaves like the hardware would. It
returns shots only, runs the device’s full calibrated noise
(noise="hardware"),
rounds your waveform onto the device’s grid, and draws a seed you never see.
vqpu:cosmiq-ref-1-ideal runs the same device without noise, and vqpu:cosmiq-ref-1-future
one whose errors are all twice as small. Like remote jobs, the virtual QPU isn’t open yet, and
its digital path (Qiskit circuits) is off at launch.
with cq.remote.session("vqpu:cosmiq-ref-1") as qpu: # a queue: it starts no box
shots = qpu.run(program(), shots=500).shots # one Shots per row
grad = cq.gradients.estimate(program, loss, method="spsa", emulator=qpu, shots=500)
qpu.status() # online or not, the queue's depth, the calibration, the shot rate
The program must target the device (
program.to_target("cosmiq-ref-1")).A run takes
shotsandclockonly.clock="fast"(the default) returns as soon as the shots are computed and reports how long the hardware would have taken;"realistic"holds the result until that time has passed. The physics is the device’s, so observables, a gradient, a noise choice, a seed or emulator options are refused before anything is sent.cq.gradients.estimate(..., emulator=qpu, shots=n)runs SPSA or finite differences from shots, as hardware would have to.A vQPU job has size limits (Named limits); a larger estimate is split into several jobs for you.
The same program runs on emu:cpu with noise="hardware" too, with the same physics.
Caps#
A vQPU session can cap what it sends, in virtual QPU seconds, shots and jobs:
with cq.remote.session("vqpu:cosmiq-ref-1", max_vqpu_seconds=300, max_shots=100_000) as qpu:
qpu.run(program(), shots=500)
qpu.budget() # what's capped and what's spent
Before each job is sent, its projected time and shots are checked against what’s left; a job
that would pass a cap raises RemoteError("vqpu_budget") and nothing is sent. The caps hold
within one process, not across processes (the evidence).
Receipts#
Each virtual QPU result comes with a receipt signed by the site:
job = qpu.submit(program(), shots=500)
receipt = job.receipt() # waits for the job, then checks its receipt with the site's keys
The receipt names the job, the target, the device and calibration it ran on, the times, and
digests of the job and the result. cq.remote.verify(view, keys, job=sent) runs the same check
on a job view you kept. Checking signatures needs pip install "cosmiq[verify]".
What a receipt proves: that the site accepted this result for this job from one of its pool boxes. It doesn’t prove the computation: the pool’s boxes run on third-party hosts.
Errors#
A program the box’s device check refuses raises ConstraintError, with the violations, as a
local check would. Everything else is a cq.remote.RemoteError with a .code; these are the
ones you’re likely to meet:
Code |
What happened |
|---|---|
|
the job can’t run as sent (past a size limit, say); the message names the rule |
|
the box’s cosmiq doesn’t know an option: upgrade cosmiq |
|
the result would be too large: ask for fewer outputs, or |
|
the job ran out of time, or raised while running (its first line) |
|
the box went away during the job |
|
the site refused the request |
|
the API key is missing or wrong |
|
no answer from the site, or its certificate failed |
|
the result was collected more than a day ago, and is gone |
|
the result didn’t belong to the job sent; it’s never used |
|
a virtual QPU cap, or a receipt that failed its check |
Transient errors are retried safely: a retried submit never creates a duplicate job. Every
request is logged at DEBUG on cosmiq.remote.http, without the key or any body. The full list
of codes, and the retry rules, are in the protocol reference.
Limits#
Jobs, results, rows and shots have size limits, and a job’s run time is capped by its session (Named limits). The formats, the box’s worker and the receipt’s exact encoding are in the protocol reference.