rsinter 0.4.0

Experimental parallel sampling and decoder benchmark harness for RustQEC
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
use std::collections::BTreeMap;
use std::path::PathBuf;

use toml::Value;

use crate::bench::result::BenchmarkResultRow;
use crate::bench::runners::predict_zero::PredictZeroRunner;
use crate::bench::runners::rbposd::RbposdRunner;
use crate::bench::runners::rilpqec::RilpqecRunner;
use crate::bench::runners::rmatching::RmatchingRunner;

#[derive(Debug)]
pub struct BenchCasePoint {
    pub input_type: String,
    pub code_id: Option<String>,
    pub distance: Option<usize>,
    pub rounds: usize,
    pub p: f64,
    pub seed: u64,
    pub basis: Option<String>,
    pub schedule: Option<String>,
    pub hx_path: Option<String>,
    pub hz_path: Option<String>,
    pub observables_path: Option<String>,
    pub max_shots: u64,
    pub max_errors: u64,
    pub max_wall_seconds: Option<f64>,
    pub batch_size: usize,
    pub decoder_params: BTreeMap<String, Value>,
}

const DEFAULT_BENCH_SEED: u64 = 12_345;

pub struct BenchRunContext {
    pub benchmark_name: String,
    pub runner_name: String,
    pub language: String,
    pub seed: u64,
    pub spec_dir: PathBuf,
}

pub trait RustBenchRunner: Send + Sync {
    fn name(&self) -> &'static str;
    fn preflight_point(&self, _point: &BenchCasePoint) -> Result<(), String> {
        Ok(())
    }
    fn plan_point_identity(
        &self,
        _point: &BenchCasePoint,
        _ctx: &BenchRunContext,
    ) -> Result<String, String> {
        Err("pre-run identity planning is unsupported for this runner".into())
    }
    fn run_point(
        &self,
        point: &BenchCasePoint,
        ctx: &BenchRunContext,
    ) -> Result<BenchmarkResultRow, String>;
}

pub type RustRunnerRegistry = BTreeMap<String, Box<dyn RustBenchRunner>>;

struct SplitRunnerParams {
    generic: BTreeMap<String, Value>,
    decoder: BTreeMap<String, Value>,
}

pub fn default_rust_runner_names() -> Vec<String> {
    ["rmatching", "rbposd", "rilpqec", "predict-zero"]
        .into_iter()
        .map(|name| name.to_string())
        .collect()
}

pub fn build_default_rust_runner_registry() -> RustRunnerRegistry {
    let mut registry: RustRunnerRegistry = BTreeMap::new();
    registry.insert("rmatching".into(), Box::new(RmatchingRunner));
    registry.insert("rbposd".into(), Box::new(RbposdRunner));
    registry.insert("rilpqec".into(), Box::new(RilpqecRunner));
    registry.insert("predict-zero".into(), Box::new(PredictZeroRunner));
    registry
}

pub fn expand_runner_points(
    params: &BTreeMap<String, Value>,
) -> Result<Vec<BenchCasePoint>, String> {
    expand_runner_points_for_runner("generic", params)
}

pub fn expand_runner_points_for_runner(
    runner_name: &str,
    params: &BTreeMap<String, Value>,
) -> Result<Vec<BenchCasePoint>, String> {
    let split = split_runner_params(runner_name, params)?;
    expand_generic_runner_points(&split.generic, split.decoder)
}

fn expand_generic_runner_points(
    params: &BTreeMap<String, Value>,
    decoder_params: BTreeMap<String, Value>,
) -> Result<Vec<BenchCasePoint>, String> {
    let input_type =
        optional_string(params, "input_type")?.unwrap_or_else(|| "surface_rotated_memory_x".into());
    let rounds = require_array(params, "rounds")?;
    let ps = require_array(params, "p")?;
    let seed = optional_u64(params, "seed")?.unwrap_or(DEFAULT_BENCH_SEED);
    let max_shots = require_u64(params, "max_shots")?;
    let max_errors = require_u64(params, "max_errors")?;
    let max_wall_seconds = optional_f64(params, "max_wall_seconds")?;
    validate_max_wall_seconds(max_wall_seconds)?;
    let batch_size = require_usize(params, "batch_size")?;
    if rounds.is_empty() {
        return Err("rounds must not be empty".into());
    }
    if ps.is_empty() {
        return Err("p must not be empty".into());
    }
    if batch_size == 0 {
        return Err("batch_size must be positive".into());
    }

    match input_type.as_str() {
        "surface_rotated_memory_x" | "surface_rotated_memory_z" | "memory-x" | "memory-z" => {
            expand_surface_points(
                &input_type,
                params,
                rounds,
                ps,
                seed,
                max_shots,
                max_errors,
                max_wall_seconds,
                batch_size,
                decoder_params,
            )
        }
        "css" => expand_css_points(
            params,
            rounds,
            ps,
            seed,
            max_shots,
            max_errors,
            max_wall_seconds,
            batch_size,
            decoder_params,
        ),
        other => Err(format!("unknown input_type: {other}")),
    }
}

fn expand_surface_points(
    input_type: &str,
    params: &BTreeMap<String, Value>,
    rounds: &[Value],
    ps: &[Value],
    seed: u64,
    max_shots: u64,
    max_errors: u64,
    max_wall_seconds: Option<f64>,
    batch_size: usize,
    decoder_params: BTreeMap<String, Value>,
) -> Result<Vec<BenchCasePoint>, String> {
    let distances = require_array(params, "distance")?;
    if distances.is_empty() {
        return Err("distance must not be empty".into());
    }

    let mut points = Vec::new();
    for distance in distances {
        for round in rounds {
            for p in ps {
                let distance = value_as_usize(distance, "distance entry")?;
                let rounds = value_as_usize(round, "round entry")?;
                if distance < 2 {
                    return Err("distance entry must be >= 2".into());
                }
                if rounds < 1 {
                    return Err("round entry must be >= 1".into());
                }
                points.push(BenchCasePoint {
                    input_type: input_type.to_string(),
                    code_id: None,
                    distance: Some(distance),
                    rounds,
                    p: value_as_f64(p, "p entry")?,
                    seed,
                    basis: None,
                    schedule: None,
                    hx_path: None,
                    hz_path: None,
                    observables_path: None,
                    max_shots,
                    max_errors,
                    max_wall_seconds,
                    batch_size,
                    decoder_params: decoder_params.clone(),
                });
            }
        }
    }
    Ok(points)
}

fn expand_css_points(
    params: &BTreeMap<String, Value>,
    rounds: &[Value],
    ps: &[Value],
    seed: u64,
    max_shots: u64,
    max_errors: u64,
    max_wall_seconds: Option<f64>,
    batch_size: usize,
    decoder_params: BTreeMap<String, Value>,
) -> Result<Vec<BenchCasePoint>, String> {
    let basis = require_string(params, "basis")?;
    let schedule = optional_string(params, "schedule")?.unwrap_or_else(|| "greedy".to_string());
    let hx_path = require_string(params, "hx")?;
    let hz_path = require_string(params, "hz")?;
    let observables_path = optional_string(params, "observables")?;
    let code_id = optional_string(params, "code_id")?;

    let mut points = Vec::new();
    for round in rounds {
        for p in ps {
            let rounds = value_as_usize(round, "round entry")?;
            if rounds < 1 {
                return Err("round entry must be >= 1".into());
            }
            points.push(BenchCasePoint {
                input_type: "css".into(),
                code_id: code_id.clone(),
                distance: None,
                rounds,
                p: value_as_f64(p, "p entry")?,
                seed,
                basis: Some(basis.clone()),
                schedule: Some(schedule.clone()),
                hx_path: Some(hx_path.clone()),
                hz_path: Some(hz_path.clone()),
                observables_path: observables_path.clone(),
                max_shots,
                max_errors,
                max_wall_seconds,
                batch_size,
                decoder_params: decoder_params.clone(),
            });
        }
    }
    Ok(points)
}

fn split_runner_params(
    runner_name: &str,
    params: &BTreeMap<String, Value>,
) -> Result<SplitRunnerParams, String> {
    let mut generic = BTreeMap::new();
    let mut decoder = BTreeMap::new();
    for (key, value) in params {
        if is_decoder_param_entry(runner_name, key, value) {
            decoder.insert(key.clone(), value.clone());
        } else if is_generic_param_key(key) {
            generic.insert(key.clone(), value.clone());
        } else {
            return Err(format!("unknown {runner_name} runner param: {key}"));
        }
    }
    Ok(SplitRunnerParams { generic, decoder })
}

fn is_generic_param_key(key: &str) -> bool {
    matches!(
        key,
        "input_type"
            | "distance"
            | "rounds"
            | "p"
            | "seed"
            | "max_shots"
            | "max_errors"
            | "max_wall_seconds"
            | "batch_size"
            | "basis"
            | "schedule"
            | "hx"
            | "hz"
            | "observables"
            | "code_id"
    )
}

fn is_decoder_param_entry(runner_name: &str, key: &str, value: &Value) -> bool {
    match runner_name {
        "rbposd" => {
            matches!(
                key,
                "bp_algorithm"
                    | "bp_method"
                    | "bp_iters"
                    | "max_bp_iterations"
                    | "early_stop"
                    | "osd_method"
                    | "osd_order"
                    | "lsd_method"
                    | "lsd_order"
            ) || is_rbposd_bp_schedule_entry(key, value)
        }
        "rilpqec" => matches!(
            key,
            "backend" | "time_limit_s" | "mip_gap" | "threads" | "verbose"
        ),
        "rmatching" | "generic" => false,
        _ => false,
    }
}

fn is_rbposd_bp_schedule_entry(key: &str, value: &Value) -> bool {
    key == "schedule" && !matches!(value.as_str(), Some("greedy" | "sequential"))
}

fn optional_string(params: &BTreeMap<String, Value>, key: &str) -> Result<Option<String>, String> {
    match params.get(key) {
        None => Ok(None),
        Some(value) => value
            .as_str()
            .map(str::to_string)
            .map(Some)
            .ok_or_else(|| format!("{key} must be a string")),
    }
}

fn require_string(params: &BTreeMap<String, Value>, key: &str) -> Result<String, String> {
    require_param(params, key)?
        .as_str()
        .map(str::to_string)
        .ok_or_else(|| format!("{key} must be a string"))
}

fn require_array<'a>(
    params: &'a BTreeMap<String, Value>,
    key: &str,
) -> Result<&'a Vec<Value>, String> {
    require_param(params, key)?
        .as_array()
        .ok_or_else(|| format!("{key} must be an array"))
}

fn require_u64(params: &BTreeMap<String, Value>, key: &str) -> Result<u64, String> {
    let value = require_param(params, key)?
        .as_integer()
        .ok_or_else(|| format!("{key} must be an integer"))?;
    u64::try_from(value).map_err(|_| format!("{key} must be non-negative"))
}

fn optional_u64(params: &BTreeMap<String, Value>, key: &str) -> Result<Option<u64>, String> {
    match params.get(key) {
        None => Ok(None),
        Some(value) => {
            let integer = value
                .as_integer()
                .ok_or_else(|| format!("{key} must be an integer"))?;
            u64::try_from(integer)
                .map(Some)
                .map_err(|_| format!("{key} must be non-negative"))
        }
    }
}

fn optional_f64(params: &BTreeMap<String, Value>, key: &str) -> Result<Option<f64>, String> {
    match params.get(key) {
        None => Ok(None),
        Some(value) => value_as_f64(value, key).map(Some),
    }
}

fn validate_max_wall_seconds(max_wall_seconds: Option<f64>) -> Result<(), String> {
    if let Some(seconds) = max_wall_seconds {
        if !seconds.is_finite() || seconds <= 0.0 {
            return Err("max_wall_seconds must be positive".into());
        }
    }
    Ok(())
}

fn require_usize(params: &BTreeMap<String, Value>, key: &str) -> Result<usize, String> {
    let value = require_param(params, key)?
        .as_integer()
        .ok_or_else(|| format!("{key} must be an integer"))?;
    usize::try_from(value).map_err(|_| format!("{key} must be non-negative"))
}

fn require_param<'a>(params: &'a BTreeMap<String, Value>, key: &str) -> Result<&'a Value, String> {
    params
        .get(key)
        .ok_or_else(|| format!("missing runner param: {key}"))
}

fn value_as_usize(value: &Value, label: &str) -> Result<usize, String> {
    let value = value
        .as_integer()
        .ok_or_else(|| format!("{label} must be an integer"))?;
    usize::try_from(value).map_err(|_| format!("{label} must be non-negative"))
}

fn value_as_f64(value: &Value, label: &str) -> Result<f64, String> {
    if let Some(value) = value.as_float() {
        return Ok(value);
    }
    if let Some(value) = value.as_integer() {
        return Ok(value as f64);
    }
    Err(format!("{label} must be numeric"))
}