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
414
415
416
417
418
419
use std::fs::File;
use std::path::PathBuf;

use clap::{Parser, Subcommand};
#[cfg(feature = "rbposd-runner")]
use rbposd::OsdVariant;

#[cfg(feature = "rbposd-runner")]
use rsinter::bb_circuit_memory::{
    SimulationConfig, export_bravyi_model_audit_for_code, export_comparison_case_for_code,
    export_comparison_case_for_code_with_osd_variant, run_simulation_for_code,
    run_simulation_for_code_with_osd_variant,
};
#[cfg(feature = "plotting")]
use rsinter::bench::bb_compare_csv::read_bb_compare_csv;
use rsinter::bench::merge::merge_result_rows;
#[cfg(feature = "plotting")]
use rsinter::bench::plot::render_benchmark_plot;
use rsinter::bench::registry::build_default_rust_runner_registry;
use rsinter::bench::result::{read_results_jsonl, write_results_jsonl};
use rsinter::bench::run::{BenchRunOptions, run_rust_benchmark_with_options};
use rsinter::bench::spec::BenchmarkSpec;
#[cfg(feature = "plotting")]
use rsinter::bench::surface_compare_csv::read_surface_compare_csv;
use rsinter::replay::{ReplayOptions, run_replay};

#[derive(Parser)]
#[command(
    name = "rsinter",
    version,
    about = "Rust benchmark and sampling harness",
    after_help = "bench subcommands: run, merge, plot, plot-surface-compare-csv, plot-bb-compare-csv"
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Plot a CSV of measured logical failures by distance and physical error rate.
    ///
    /// Requires the plotting feature.
    PlotErrorRateCsv {
        #[arg(long)]
        input: PathBuf,
        #[arg(long)]
        out: PathBuf,
    },
    /// Decode a detector dataset (requires a decoder runner feature).
    ///
    /// Requires one of: rbposd-runner, rmatching-runner, ilp-runner.
    Replay {
        #[arg(long)]
        dem: PathBuf,
        #[arg(long)]
        dets: PathBuf,
        #[arg(long)]
        decoder: String,
        #[arg(long)]
        decoder_config: Option<PathBuf>,
        #[arg(long)]
        predictions_out: PathBuf,
        #[arg(long)]
        stats_out: PathBuf,
        #[arg(long, default_value_t = 65_536)]
        batch_size: usize,
        #[arg(
            long,
            help = "Validate the inferred shot count; required for zero-detector DEMs"
        )]
        shots: Option<usize>,
    },
    /// Run or plot benchmarks (runner features; plots require plotting).
    ///
    /// Decoder runs require their matching runner feature; plots require plotting.
    Bench {
        #[command(subcommand)]
        command: BenchCommands,
    },
    /// Run the built-in bivariate-bicycle memory experiment (requires rbposd-runner).
    BbCircuitBposdMemory {
        #[arg(long, default_value = "bb144")]
        code_id: String,
        #[arg(long, default_value_t = 0.003, allow_hyphen_values = true)]
        physical_error_rate: f64,
        #[arg(long, default_value_t = 12)]
        num_cycles: usize,
        #[arg(long, default_value_t = 50_000)]
        num_trials: u64,
        #[arg(long)]
        seed: Option<u64>,
        #[arg(long, default_value_t = 10_000)]
        max_bp_iterations: usize,
        #[arg(long, default_value_t = 7)]
        osd_order: usize,
        #[arg(long)]
        osd_method: Option<String>,
        #[arg(long)]
        json_model_audit: bool,
        #[arg(long)]
        json_compare_case: bool,
    },
}

#[derive(Subcommand)]
enum BenchCommands {
    /// Run a benchmark spec (decoder implementations require matching runner features).
    Run {
        #[arg(long)]
        spec: String,
        #[arg(long)]
        language: String,
        #[arg(long)]
        out: String,
        #[arg(
            long,
            help = "Resume from existing per-runner test-run/results.jsonl rows under --out"
        )]
        resume: bool,
    },
    Merge {
        #[arg(long)]
        spec: String,
        #[arg(long = "input")]
        input: Vec<String>,
        #[arg(long)]
        out: String,
    },
    /// Plot benchmark JSONL results (requires plotting).
    Plot {
        #[arg(long)]
        spec: String,
        #[arg(long = "input")]
        input: Vec<String>,
        #[arg(long)]
        out: String,
    },
    /// Plot legacy surface-comparison CSV input (requires plotting).
    PlotSurfaceCompareCsv {
        #[arg(long)]
        spec: String,
        #[arg(long)]
        input: String,
        #[arg(long)]
        out: String,
    },
    /// Plot legacy BB-comparison CSV input (requires plotting).
    PlotBbCompareCsv {
        #[arg(long)]
        spec: String,
        #[arg(long)]
        input: String,
        #[arg(long)]
        out: String,
    },
}

#[cfg_attr(not(feature = "rbposd-runner"), allow(dead_code))]
struct BbCircuitBposdMemoryArgs {
    code_id: String,
    physical_error_rate: f64,
    num_cycles: usize,
    num_trials: u64,
    seed: Option<u64>,
    max_bp_iterations: usize,
    osd_order: usize,
    osd_method: Option<String>,
    json_model_audit: bool,
    json_compare_case: bool,
}

fn main() {
    if let Err(err) = run() {
        eprintln!("Error: {err}");
        std::process::exit(1);
    }
}

fn run() -> Result<(), String> {
    let cli = Cli::parse();
    match cli.command {
        Commands::PlotErrorRateCsv { input, out } => {
            rsinter::plot::plot_error_rate_csv(&input, &out)?;
        }
        Commands::Replay {
            dem,
            dets,
            decoder,
            decoder_config,
            predictions_out,
            stats_out,
            batch_size,
            shots,
        } => {
            run_replay(&ReplayOptions {
                dem,
                dets,
                decoder,
                decoder_config,
                predictions_out,
                stats_out,
                batch_size,
                shots,
            })?;
        }
        Commands::Bench { command } => match command {
            BenchCommands::Run {
                spec,
                language,
                out,
                resume,
            } => {
                let spec_path = PathBuf::from(&spec);
                let spec_dir = spec_path
                    .parent()
                    .map(PathBuf::from)
                    .unwrap_or_else(|| PathBuf::from("."));
                let text = std::fs::read_to_string(&spec_path).map_err(|e| e.to_string())?;
                let bench_spec: BenchmarkSpec = toml::from_str(&text).map_err(|e| e.to_string())?;
                bench_spec.validate()?;
                let registry = build_default_rust_runner_registry();
                run_rust_benchmark_with_options(
                    &bench_spec,
                    &language,
                    PathBuf::from(out).as_path(),
                    &registry,
                    &spec_dir,
                    BenchRunOptions { resume },
                )?;
            }
            BenchCommands::Merge {
                spec: _,
                input,
                out,
            } => {
                let mut row_sets = Vec::new();
                for path in input {
                    let data = std::fs::read(path).map_err(|e| e.to_string())?;
                    row_sets.push(read_results_jsonl(&data[..])?);
                }
                let merged = merge_result_rows(row_sets)?;
                ensure_parent_dir(PathBuf::from(&out).as_path())?;
                let mut file = File::create(out).map_err(|e| e.to_string())?;
                write_results_jsonl(&merged, &mut file)?;
            }
            BenchCommands::Plot { spec, input, out } => {
                require_plotting_feature()?;
                run_benchmark_plot_command(spec, input, out)?;
            }
            BenchCommands::PlotSurfaceCompareCsv { spec, input, out } => {
                require_plotting_feature()?;
                run_surface_compare_plot_command(spec, input, out)?;
            }
            BenchCommands::PlotBbCompareCsv { spec, input, out } => {
                require_plotting_feature()?;
                run_bb_compare_plot_command(spec, input, out)?;
            }
        },
        Commands::BbCircuitBposdMemory {
            code_id,
            physical_error_rate,
            num_cycles,
            num_trials,
            seed,
            max_bp_iterations,
            osd_order,
            osd_method,
            json_model_audit,
            json_compare_case,
        } => {
            run_bb_circuit_bposd_memory(BbCircuitBposdMemoryArgs {
                code_id,
                physical_error_rate,
                num_cycles,
                num_trials,
                seed,
                max_bp_iterations,
                osd_order,
                osd_method,
                json_model_audit,
                json_compare_case,
            })?;
        }
    }
    Ok(())
}

#[cfg(not(feature = "plotting"))]
fn require_plotting_feature() -> Result<(), String> {
    Err("requires Cargo feature 'plotting'".into())
}

#[cfg(feature = "plotting")]
fn require_plotting_feature() -> Result<(), String> {
    Ok(())
}

#[cfg(feature = "rbposd-runner")]
fn run_bb_circuit_bposd_memory(args: BbCircuitBposdMemoryArgs) -> Result<(), String> {
    let num_trials = usize::try_from(args.num_trials)
        .map_err(|_| "num_trials exceeds supported platform usize".to_string())?;
    let config = SimulationConfig {
        physical_error_rate: args.physical_error_rate,
        num_cycles: args.num_cycles,
        num_trials,
        seed: args.seed,
        max_bp_iterations: args.max_bp_iterations,
        osd_order: args.osd_order,
    };
    let osd_variant = match args.osd_method.as_deref() {
        Some(method) => Some(OsdVariant::from_method_name(method).map_err(|e| e.to_string())?),
        None => None,
    };
    if args.json_model_audit {
        let export = export_bravyi_model_audit_for_code(&args.code_id, config)?;
        serde_json::to_writer_pretty(std::io::stdout(), &export).map_err(|e| e.to_string())?;
        println!();
    } else if args.json_compare_case {
        let export = match osd_variant {
            Some(osd_variant) => export_comparison_case_for_code_with_osd_variant(
                &args.code_id,
                config,
                osd_variant,
            )?,
            None => export_comparison_case_for_code(&args.code_id, config)?,
        };
        serde_json::to_writer_pretty(std::io::stdout(), &export).map_err(|e| e.to_string())?;
        println!();
    } else {
        let result = match osd_variant {
            Some(osd_variant) => {
                run_simulation_for_code_with_osd_variant(&args.code_id, config, osd_variant)?
            }
            None => run_simulation_for_code(&args.code_id, config)?,
        };
        println!(
            "{}\t{}\t{}\t{}",
            result.physical_error_rate,
            result.num_cycles,
            result.num_trials,
            result.num_failed_trials
        );
    }
    Ok(())
}

#[cfg(not(feature = "rbposd-runner"))]
fn run_bb_circuit_bposd_memory(_args: BbCircuitBposdMemoryArgs) -> Result<(), String> {
    Err("requires Cargo feature 'rbposd-runner'".into())
}

#[cfg(feature = "plotting")]
fn run_benchmark_plot_command(spec: String, input: Vec<String>, out: String) -> Result<(), String> {
    let text = std::fs::read_to_string(&spec).map_err(|e| e.to_string())?;
    let bench_spec: BenchmarkSpec = toml::from_str(&text).map_err(|e| e.to_string())?;
    bench_spec.validate()?;
    let mut rows = Vec::new();
    for path in input {
        let data = std::fs::read(path).map_err(|e| e.to_string())?;
        rows.extend(read_results_jsonl(&data[..])?);
    }
    ensure_parent_dir(PathBuf::from(&out).as_path())?;
    render_benchmark_plot(&bench_spec, &rows, PathBuf::from(out).as_path())
}

#[cfg(not(feature = "plotting"))]
fn run_benchmark_plot_command(
    _spec: String,
    _input: Vec<String>,
    _out: String,
) -> Result<(), String> {
    Err("bench plot requires Cargo feature 'plotting'".into())
}

#[cfg(feature = "plotting")]
fn run_surface_compare_plot_command(
    spec: String,
    input: String,
    out: String,
) -> Result<(), String> {
    let text = std::fs::read_to_string(&spec).map_err(|e| e.to_string())?;
    let bench_spec: BenchmarkSpec = toml::from_str(&text).map_err(|e| e.to_string())?;
    bench_spec.validate()?;
    let rows = read_surface_compare_csv(PathBuf::from(input).as_path(), &bench_spec.name)?;
    ensure_parent_dir(PathBuf::from(&out).as_path())?;
    render_benchmark_plot(&bench_spec, &rows, PathBuf::from(out).as_path())
}

#[cfg(not(feature = "plotting"))]
fn run_surface_compare_plot_command(
    _spec: String,
    _input: String,
    _out: String,
) -> Result<(), String> {
    Err("bench plot-surface-compare-csv requires Cargo feature 'plotting'".into())
}

#[cfg(feature = "plotting")]
fn run_bb_compare_plot_command(spec: String, input: String, out: String) -> Result<(), String> {
    let text = std::fs::read_to_string(&spec).map_err(|e| e.to_string())?;
    let bench_spec: BenchmarkSpec = toml::from_str(&text).map_err(|e| e.to_string())?;
    bench_spec.validate()?;
    let rows = read_bb_compare_csv(PathBuf::from(input).as_path(), &bench_spec.name)?;
    ensure_parent_dir(PathBuf::from(&out).as_path())?;
    render_benchmark_plot(&bench_spec, &rows, PathBuf::from(out).as_path())
}

#[cfg(not(feature = "plotting"))]
fn run_bb_compare_plot_command(_spec: String, _input: String, _out: String) -> Result<(), String> {
    Err("bench plot-bb-compare-csv requires Cargo feature 'plotting'".into())
}

fn ensure_parent_dir(path: &std::path::Path) -> Result<(), String> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
    }
    Ok(())
}