burn_quantum 0.4.0

Differentiable qubit and qudit simulation with Burn's native tensor and module APIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Asynchronous immutable local-simulator jobs with bounded queues and raw records.
//!
//! This service never selects hardware or silently falls back from a provider.
//! Cancellation/deadlines are cooperative between shots; a running evaluator is
//! not forcibly interrupted. The same request type can be checked by a future
//! provider adapter, but local results are explicitly labeled simulator results.
use super::{JobIdentity, shot_seed};
use crate::{Error, Result};
use serde::{Deserialize, Serialize};
use std::{
    collections::{BTreeMap, VecDeque},
    sync::{
        Arc, Condvar, Mutex,
        atomic::{AtomicBool, Ordering},
    },
    thread::JoinHandle,
    time::{Duration, Instant},
};

/// Executor-declared local capabilities and immutable calibration validity.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct JobCapabilities {
    /// Versioned executor implementation fingerprint.
    pub executor: String,
    /// Supported immutable program fingerprints.
    pub programs: Vec<String>,
    /// Compiler fingerprint accepted by this service.
    pub compiler: String,
    /// Physical model fingerprint accepted by this service.
    pub physical_model: String,
    /// Exact physical-model/calibration fingerprint accepted by this service.
    pub calibration: String,
    /// Wall-clock expiry supplied by caller as Unix seconds; checked at submit.
    pub calibration_expires_at: u64,
    /// Supported named readouts (no implicit substitutes).
    pub readouts: Vec<String>,
    /// Supported numerical precisions, a nonempty subset of 32/64.
    pub precision_bits: Vec<u8>,
    /// Maximum attempts per request, including rejected/herald-failed shots.
    pub max_attempts: u64,
    /// Maximum classical scalar bindings per request.
    pub max_parameters: usize,
}
/// Immutable request; seed and stable attempt IDs are independent of scheduling.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SimulationRequest {
    /// Semantic request schema, currently 1.
    pub version: u32,
    /// Program/calibration/executor/seed/attempt count.
    pub identity: JobIdentity,
    /// Immutable compiler version/content fingerprint.
    pub compiler: String,
    /// Immutable physical-model fingerprint.
    pub physical_model: String,
    /// Finite classical parameter/input bindings, frozen at submission.
    pub bindings: Vec<f64>,
    /// Ordered readout names, all explicitly supported by the executor.
    pub readouts: Vec<String>,
    /// Numerical precision in bits, 32 or 64 (evaluator must support the request).
    pub precision_bits: u8,
    /// Explicit requested absolute approximation bound, finite in `[0,1]`.
    pub error_budget: f64,
    /// Maximum total retained encoded raw-record bytes for this job.
    pub max_result_bytes: usize,
    /// Wall-clock budget from submission, including time queued.
    pub timeout_ms: u64,
}
/// One actual attempted experiment; keep raw outcomes even when unaccepted.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RawSimulationShot {
    /// Detector/measurement record, whose schema belongs to `identity.executor`.
    pub raw: Vec<u64>,
    /// Explicit scientific acceptance, separate from execution completion.
    pub accepted: bool,
    /// Named-request readouts in request order; finite even on failure records.
    pub values: Vec<f64>,
    /// Physical source/ancilla photons consumed by this attempt.
    pub photons: u64,
    /// Modeled physical elapsed ticks, including generation/waiting/control.
    pub latency_ticks: u64,
}
/// Stable service-local handle. Requests using the same id are immutable/idempotent.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct JobId(pub u64);
/// Execution status; failed/cancelled/expired jobs retain completed raw attempts.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum JobStatus {
    /// Waiting in the bounded FIFO.
    Queued,
    /// Evaluator is active.
    Running,
    /// Every requested attempt completed, independent of scientific acceptance.
    Completed,
    /// Cooperative user cancellation.
    Cancelled,
    /// Submission-relative deadline expired.
    TimedOut,
    /// Evaluator, panic or resource-budget failure, with a diagnostic.
    Failed(String),
}
/// Honest simulator result envelope; acceptance never changes attempted-shot IDs.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SimulationResult {
    /// Immutable originating request.
    pub request: SimulationRequest,
    /// Always `local_simulator`; no provider/hardware claim.
    pub execution_kind: String,
    /// Current lifecycle state.
    pub status: JobStatus,
    /// Complete `(attempt_id, raw record)` pairs, ordered by ID.
    pub shots: Vec<(u64, RawSimulationShot)>,
    /// Retained encoded bytes, checked before every append.
    pub encoded_bytes: usize,
    /// Elapsed host wall time at this snapshot (not physical latency).
    pub wall_seconds: f64,
}
/// Queue/concurrency/memory limits independent of requested physical costs.
#[derive(Clone, Copy, Debug)]
pub struct JobServiceLimits {
    /// Fixed number of simulator worker threads.
    pub workers: usize,
    /// Maximum simultaneously queued jobs (running jobs excluded).
    pub queued: usize,
    /// Maximum retained job identities/results, including terminal jobs.
    pub retained: usize,
    /// Maximum result bytes allowed for one request.
    pub result_bytes: usize,
}
impl Default for JobServiceLimits {
    fn default() -> Self {
        Self {
            workers: 2,
            queued: 64,
            retained: 256,
            result_bytes: 16 << 20,
        }
    }
}
struct Entry {
    request: SimulationRequest,
    status: JobStatus,
    shots: Vec<(u64, RawSimulationShot)>,
    bytes: usize,
    submitted: Instant,
    calibration_lease: Duration,
    cancel: Arc<AtomicBool>,
}
struct State {
    entries: BTreeMap<JobId, Entry>,
    queue: VecDeque<JobId>,
    shutdown: bool,
}
struct Shared {
    state: Mutex<State>,
    wake: Condvar,
}
/// Asynchronous local executor. Drop requests cancellation and joins workers;
/// evaluator callbacks must terminate cooperatively for bounded shutdown.
pub struct LocalSimulationJobs {
    shared: Arc<Shared>,
    threads: Vec<JoinHandle<()>>,
    capabilities: JobCapabilities,
    limits: JobServiceLimits,
}
impl LocalSimulationJobs {
    /// Launch a bounded pool around a real local simulator evaluator. The callback
    /// receives immutable request, attempt id, stable seed, and cancellation flag.
    pub fn new<F>(
        capabilities: JobCapabilities,
        limits: JobServiceLimits,
        evaluate: F,
    ) -> Result<Self>
    where
        F: Fn(&SimulationRequest, u64, u64, &AtomicBool) -> Result<RawSimulationShot>
            + Send
            + Sync
            + 'static,
    {
        if capabilities.programs.is_empty()
            || capabilities.programs.iter().any(|p| p.is_empty())
            || capabilities.compiler.is_empty()
            || capabilities.physical_model.is_empty()
            || capabilities.executor.is_empty()
            || capabilities.calibration.is_empty()
            || capabilities.max_attempts == 0
            || capabilities.readouts.is_empty()
            || capabilities.precision_bits.is_empty()
            || capabilities
                .precision_bits
                .iter()
                .any(|p| ![32, 64].contains(p))
            || limits.workers == 0
            || limits.workers > 256
            || limits.queued == 0
            || limits.retained == 0
            || limits.result_bytes == 0
        {
            return Err(Error::InvalidValue(
                "invalid asynchronous job service limits".into(),
            ));
        }
        let shared = Arc::new(Shared {
            state: Mutex::new(State {
                entries: BTreeMap::new(),
                queue: VecDeque::new(),
                shutdown: false,
            }),
            wake: Condvar::new(),
        });
        let evaluate = Arc::new(evaluate);
        let mut threads = Vec::with_capacity(limits.workers);
        for _ in 0..limits.workers {
            let shared = shared.clone();
            let evaluate = evaluate.clone();
            threads.push(std::thread::spawn(move || worker(shared, evaluate)));
        }
        Ok(Self {
            shared,
            threads,
            capabilities,
            limits,
        })
    }
    /// Capability negotiation is explicit; this executor is always a local simulator.
    pub fn capabilities(&self) -> &JobCapabilities {
        &self.capabilities
    }
    /// Submit after checking provenance, calibration freshness, readouts and limits.
    /// `now_unix_seconds` is explicit for reproducible tests; providers should use
    /// an authenticated wall clock. Repeating an id with identical content is safe.
    pub fn submit(
        &self,
        id: JobId,
        request: SimulationRequest,
        now_unix_seconds: u64,
    ) -> Result<()> {
        request.identity.validate()?;
        let c = &self.capabilities;
        if !c.programs.contains(&request.identity.program)
            || request.compiler != c.compiler
            || request.physical_model != c.physical_model
            || request.version != 1
            || request.identity.calibration != c.calibration
            || request.identity.executor != c.executor
            || now_unix_seconds >= c.calibration_expires_at
            || request.identity.attempts > c.max_attempts
            || request.bindings.len() > c.max_parameters
            || request.bindings.iter().any(|x| !x.is_finite())
            || request.readouts.is_empty()
            || request.readouts.iter().any(|r| !c.readouts.contains(r))
            || !c.precision_bits.contains(&request.precision_bits)
            || !request.error_budget.is_finite()
            || !(0.0..=1.).contains(&request.error_budget)
            || request.max_result_bytes == 0
            || request.max_result_bytes > self.limits.result_bytes
            || request.timeout_ms == 0
        {
            return Err(Error::InvalidValue(
                "simulation request fails capability/calibration/budget contract".into(),
            ));
        }
        let mut state = self.shared.state.lock().map_err(poison)?;
        if let Some(existing) = state.entries.get(&id) {
            return if existing.request == request {
                Ok(())
            } else {
                Err(Error::InvalidValue(
                    "job id reused with different immutable request".into(),
                ))
            };
        }
        if state.entries.len() >= self.limits.retained || state.queue.len() >= self.limits.queued {
            return Err(Error::ResourceLimit {
                resource: "asynchronous job queue/retention",
                required: state.entries.len() + 1,
                limit: self.limits.retained,
            });
        }
        state.entries.insert(
            id,
            Entry {
                request,
                status: JobStatus::Queued,
                shots: vec![],
                bytes: 0,
                submitted: Instant::now(),
                calibration_lease: Duration::from_secs(c.calibration_expires_at - now_unix_seconds),
                cancel: Arc::new(AtomicBool::new(false)),
            },
        );
        state.queue.push_back(id);
        self.shared.wake.notify_one();
        Ok(())
    }
    /// Read current status without waiting on evaluator execution.
    pub fn status(&self, id: JobId) -> Result<JobStatus> {
        let s = self.shared.state.lock().map_err(poison)?;
        Ok(s.entries.get(&id).ok_or_else(unknown)?.status.clone())
    }
    /// Snapshot raw completed attempts, including terminal partial jobs.
    pub fn result(&self, id: JobId) -> Result<SimulationResult> {
        let s = self.shared.state.lock().map_err(poison)?;
        let e = s.entries.get(&id).ok_or_else(unknown)?;
        Ok(SimulationResult {
            request: e.request.clone(),
            execution_kind: "local_simulator".into(),
            status: e.status.clone(),
            shots: e.shots.clone(),
            encoded_bytes: e.bytes,
            wall_seconds: e.submitted.elapsed().as_secs_f64(),
        })
    }
    /// Cancel a queued job immediately or an active job at the next shot boundary.
    pub fn cancel(&self, id: JobId) -> Result<()> {
        let mut s = self.shared.state.lock().map_err(poison)?;
        let e = s.entries.get_mut(&id).ok_or_else(unknown)?;
        e.cancel.store(true, Ordering::Release);
        if e.status == JobStatus::Queued {
            e.status = JobStatus::Cancelled;
            s.queue.retain(|v| *v != id);
        }
        Ok(())
    }
    /// Release a terminal result so retention limits can admit future jobs.
    pub fn forget(&self, id: JobId) -> Result<()> {
        let mut s = self.shared.state.lock().map_err(poison)?;
        let e = s.entries.get(&id).ok_or_else(unknown)?;
        if matches!(e.status, JobStatus::Queued | JobStatus::Running) {
            return Err(Error::InvalidValue(
                "cannot forget active simulation job".into(),
            ));
        }
        s.entries.remove(&id);
        Ok(())
    }
}
impl Drop for LocalSimulationJobs {
    fn drop(&mut self) {
        if let Ok(mut s) = self.shared.state.lock() {
            s.shutdown = true;
            for e in s.entries.values() {
                e.cancel.store(true, Ordering::Release);
            }
            self.shared.wake.notify_all();
        }
        for t in self.threads.drain(..) {
            let _ = t.join();
        }
    }
}
fn poison(e: impl std::fmt::Display) -> Error {
    Error::InvalidValue(format!("job service lock: {e}"))
}
fn unknown() -> Error {
    Error::InvalidValue("unknown simulation job".into())
}
fn worker<F>(shared: Arc<Shared>, evaluate: Arc<F>)
where
    F: Fn(&SimulationRequest, u64, u64, &AtomicBool) -> Result<RawSimulationShot>
        + Send
        + Sync
        + 'static,
{
    loop {
        let (id, request, cancel, start, lease) = {
            let Ok(mut s) = shared.state.lock() else {
                return;
            };
            while s.queue.is_empty() && !s.shutdown {
                s = match shared.wake.wait(s) {
                    Ok(v) => v,
                    Err(_) => return,
                };
            }
            if s.shutdown {
                return;
            }
            let id = s.queue.pop_front().unwrap();
            let e = s.entries.get_mut(&id).unwrap();
            e.status = JobStatus::Running;
            (
                id,
                e.request.clone(),
                e.cancel.clone(),
                e.submitted,
                e.calibration_lease,
            )
        };
        let timeout = Duration::from_millis(request.timeout_ms);
        let mut terminal = JobStatus::Completed;
        for attempt in 0..request.identity.attempts {
            if cancel.load(Ordering::Acquire) {
                terminal = JobStatus::Cancelled;
                break;
            }
            if start.elapsed() >= lease {
                terminal =
                    JobStatus::Failed("calibration expired during queued/running job".into());
                break;
            }
            if start.elapsed() >= timeout {
                terminal = JobStatus::TimedOut;
                break;
            }
            let answer = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                evaluate(
                    &request,
                    attempt,
                    shot_seed(request.identity.seed, attempt, 0),
                    &cancel,
                )
            }));
            let answer = match answer {
                Ok(Ok(v)) => v,
                Ok(Err(e)) => {
                    terminal = JobStatus::Failed(e.to_string());
                    break;
                }
                Err(_) => {
                    terminal = JobStatus::Failed("simulator evaluator panicked".into());
                    break;
                }
            };
            if answer.values.len() != request.readouts.len()
                || answer.values.iter().any(|v| !v.is_finite())
            {
                terminal = JobStatus::Failed("invalid raw simulation readout".into());
                break;
            }
            // Bound raw vector lengths before encoding, then bound exact encoded size.
            if answer.raw.len() > request.max_result_bytes / 8
                || answer.values.len() > request.max_result_bytes / 8
            {
                terminal = JobStatus::Failed("raw simulation record budget".into());
                break;
            }
            let size = match serde_json::to_vec(&(attempt, &answer)) {
                Ok(v) => v.len(),
                Err(e) => {
                    terminal = JobStatus::Failed(e.to_string());
                    break;
                }
            };
            let Ok(mut s) = shared.state.lock() else {
                return;
            };
            let e = s.entries.get_mut(&id).unwrap();
            if size > request.max_result_bytes.saturating_sub(e.bytes) {
                terminal = JobStatus::Failed("retained simulation record budget".into());
                break;
            }
            e.bytes += size;
            e.shots.push((attempt, answer));
            if cancel.load(Ordering::Acquire) {
                terminal = JobStatus::Cancelled;
                break;
            }
            // Keep an actual completed experiment even if its callback crossed the
            // wall deadline. Mark the partial/late result honestly and stop work.
            if start.elapsed() >= lease {
                terminal =
                    JobStatus::Failed("calibration expired during queued/running job".into());
                break;
            }
            if start.elapsed() >= timeout {
                terminal = JobStatus::TimedOut;
                break;
            }
        }
        if let Ok(mut s) = shared.state.lock() {
            let entry = s.entries.get_mut(&id).unwrap();
            // Linearize completion against cancel(), which holds this same lock.
            entry.status =
                if terminal == JobStatus::Completed && entry.cancel.load(Ordering::Acquire) {
                    JobStatus::Cancelled
                } else {
                    terminal
                };
        } else {
            return;
        }
    }
}