rustqec-cli 0.3.3

Command-line tools for quantum error-correction circuit simulation and decoding
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
//! Pipeline verbs routed through the unified `rustqec` command.
//!
//! Each verb wraps the corresponding `rstim` library entry point (the same
//! ones the `rstim` CLI uses) and adds the automation-facing contract:
//! structured errors with stable codes, a versioned JSON success envelope,
//! and declared artifacts.

use std::fs;
use std::io::{BufWriter, Read, Write};
use std::path::{Path, PathBuf};

use serde::Serialize;

use crate::{CommandError, ErrorFormat};

pub const CIRCUIT_GEN_COMMAND: &str = "circuit.gen";
pub const CIRCUIT_SAMPLE_COMMAND: &str = "circuit.sample";
pub const CIRCUIT_DETECT_COMMAND: &str = "circuit.detect";
pub const CIRCUIT_DEM_COMMAND: &str = "circuit.dem";
pub const DATASET_EXPORT_COMMAND: &str = "dataset.export";
pub const DATASET_IMPORT_COMMAND: &str = "dataset.import";

#[derive(Clone, Copy, Debug, clap::ValueEnum)]
pub enum PipelineFormat {
    Human,
    Json,
}

impl PipelineFormat {
    pub fn is_json(self, error_format: Option<ErrorFormat>) -> bool {
        match error_format {
            Some(ErrorFormat::Human) => false,
            Some(ErrorFormat::Json) => true,
            None => matches!(self, PipelineFormat::Json),
        }
    }
}

#[derive(Debug)]
pub struct GenOptions {
    pub code: String,
    pub task: String,
    pub distance: usize,
    pub rounds: usize,
    pub noise: f64,
    pub before_round_data_depolarization: f64,
    pub before_round_data_loss_probability: f64,
    pub before_measure_flip_probability: f64,
    pub after_reset_flip_probability: f64,
    pub after_clifford_loss_probability: f64,
    pub operation_loss_probability: f64,
    pub measurement_loss_probability: f64,
    pub out: PathBuf,
    pub format: PipelineFormat,
}

#[derive(Serialize)]
pub struct GenResult {
    pub code: String,
    pub task: String,
    pub distance: usize,
    pub rounds: usize,
    pub circuit: String,
}

#[derive(Debug)]
pub struct SampleOptions {
    pub shots: u64,
    pub input: Option<PathBuf>,
    pub out: PathBuf,
    pub out_format: String,
    pub seed: Option<u64>,
    pub skip_reference_sample: bool,
    pub format: PipelineFormat,
}

#[derive(Serialize)]
pub struct SampleResult {
    pub shots: u64,
    pub num_measurements: usize,
    pub out: String,
    pub out_format: String,
}

#[derive(Debug)]
pub struct DetectOptions {
    pub shots: u64,
    pub input: Option<PathBuf>,
    pub out: PathBuf,
    pub out_format: String,
    pub seed: Option<u64>,
    pub append_observables: bool,
    pub obs_out: Option<PathBuf>,
    pub obs_out_format: String,
    pub format: PipelineFormat,
}

#[derive(Serialize)]
pub struct DetectResult {
    pub shots: u64,
    pub num_detectors: usize,
    pub out: String,
    pub out_format: String,
    pub observables_out: Option<String>,
}

#[derive(Debug)]
pub struct DemOptions {
    pub input: Option<PathBuf>,
    pub out: PathBuf,
    pub approximate_disjoint_errors: bool,
    pub allow_gauge_detectors: bool,
    pub decompose_errors: bool,
    pub format: PipelineFormat,
}

#[derive(Serialize)]
pub struct DemResult {
    pub num_detectors: usize,
    pub num_observables: usize,
    pub out: String,
}

#[derive(Debug)]
pub struct DatasetImportOptions {
    pub circuit: PathBuf,
    pub shots: PathBuf,
    pub shots_format: String,
    pub out: PathBuf,
    pub loss_log: Option<PathBuf>,
    pub format: PipelineFormat,
}

#[derive(Serialize)]
pub struct DatasetImportResult {
    pub shots: usize,
    pub measurements: usize,
    pub loss_flags: usize,
    pub out: String,
}

/// Packages a third-party-produced circuit and shot payload into a public
/// decoder dataset bundle. The staged bundle is validated with exactly the
/// checks `decode` performs (manifest structure, hashes, row widths, and the
/// full loss-visible circuit subset compilation) before it is published
/// atomically; a failed import leaves no output behind.
pub fn run_dataset_import(
    options: &DatasetImportOptions,
    error_format: Option<ErrorFormat>,
) -> Result<DatasetImportResult, CommandError> {
    let json = options.format.is_json(error_format);
    let text = fs::read_to_string(&options.circuit).map_err(|error| {
        command_error(
            DATASET_IMPORT_COMMAND,
            "input_error",
            format!("failed to read {}: {error}", options.circuit.display()),
            json,
        )
    })?;
    validate_circuit(DATASET_IMPORT_COMMAND, &text, json)?;
    let summary = rstim::stats::summarize_text(&text).map_err(|message| {
        command_error(DATASET_IMPORT_COMMAND, "invalid_circuit", message, json)
    })?;
    let row_bits = summary.num_measurements;
    if row_bits == 0 {
        return Err(command_error(
            DATASET_IMPORT_COMMAND,
            "invalid_arguments",
            "circuit has no measurement records to import".to_string(),
            json,
        ));
    }
    let shots_b8 = read_shots_payload(options, row_bits, json)?;
    let row_bytes = row_bits.div_ceil(8);
    let shots = shots_b8.len() / row_bytes;
    if shots == 0 {
        return Err(command_error(
            DATASET_IMPORT_COMMAND,
            "invalid_arguments",
            "shots payload is empty".to_string(),
            json,
        ));
    }
    if options.out.exists() {
        return Err(command_error(
            DATASET_IMPORT_COMMAND,
            "output_error",
            format!("{} already exists", options.out.display()),
            json,
        ));
    }
    let staging = options.out.with_file_name(format!(
        ".{}.import-staging-{}",
        options
            .out
            .file_name()
            .map(|name| name.to_string_lossy().into_owned())
            .unwrap_or_else(|| "dataset".to_string()),
        std::process::id()
    ));
    fs::create_dir(&staging).map_err(|error| {
        command_error(
            DATASET_IMPORT_COMMAND,
            "output_error",
            format!("failed to create {}: {error}", staging.display()),
            json,
        )
    })?;
    let staged = (|| -> Result<Vec<usize>, CommandError> {
        crate::decode::write_public_bundle(&staging, &text, &shots_b8, shots).map_err(
            |failure| command_error(DATASET_IMPORT_COMMAND, failure.code, failure.message, json),
        )?;
        let loss_flags = crate::decode::validate_public_bundle(&staging).map_err(|failure| {
            command_error(DATASET_IMPORT_COMMAND, failure.code, failure.message, json)
        })?;
        if let Some(loss_log) = &options.loss_log {
            check_loss_log(loss_log, &loss_flags, &shots_b8, row_bits, shots, json)?;
        }
        Ok(loss_flags)
    })();
    let loss_flags = match staged {
        Ok(loss_flags) => loss_flags,
        Err(error) => {
            let _ = fs::remove_dir_all(&staging);
            return Err(error);
        }
    };
    fs::rename(&staging, &options.out).map_err(|error| {
        let _ = fs::remove_dir_all(&staging);
        command_error(
            DATASET_IMPORT_COMMAND,
            "output_error",
            format!("failed to publish {}: {error}", options.out.display()),
            json,
        )
    })?;
    Ok(DatasetImportResult {
        shots,
        measurements: row_bits,
        loss_flags: loss_flags.len(),
        out: options.out.display().to_string(),
    })
}

fn read_shots_payload(
    options: &DatasetImportOptions,
    row_bits: usize,
    json: bool,
) -> Result<Vec<u8>, CommandError> {
    let invalid =
        |message: String| command_error(DATASET_IMPORT_COMMAND, "invalid_arguments", message, json);
    match options.shots_format.as_str() {
        "01" => {
            let text = fs::read_to_string(&options.shots).map_err(|error| {
                command_error(
                    DATASET_IMPORT_COMMAND,
                    "input_error",
                    format!("failed to read {}: {error}", options.shots.display()),
                    json,
                )
            })?;
            let row_bytes = row_bits.div_ceil(8);
            let mut packed = Vec::new();
            for (line_number, line) in text.lines().enumerate() {
                let line = line.trim();
                if line.is_empty() {
                    continue;
                }
                if line.len() != row_bits || !line.chars().all(|c| c == '0' || c == '1') {
                    return Err(invalid(format!(
                        "01 row {} must contain exactly {row_bits} bits",
                        line_number + 1
                    )));
                }
                let mut row = vec![0u8; row_bytes];
                for (bit, c) in line.chars().enumerate() {
                    if c == '1' {
                        row[bit / 8] |= 1 << (bit % 8);
                    }
                }
                packed.extend_from_slice(&row);
            }
            Ok(packed)
        }
        "b8" => {
            let bytes = fs::read(&options.shots).map_err(|error| {
                command_error(
                    DATASET_IMPORT_COMMAND,
                    "input_error",
                    format!("failed to read {}: {error}", options.shots.display()),
                    json,
                )
            })?;
            let row_bytes = row_bits.div_ceil(8);
            if bytes.len() % row_bytes != 0 {
                return Err(invalid(format!(
                    "b8 payload has {} bytes, not a multiple of the {row_bytes}-byte row",
                    bytes.len()
                )));
            }
            let padding = row_bits % 8;
            if padding != 0 {
                let mask = 0xffu8 << padding;
                for (shot, row) in bytes.chunks_exact(row_bytes).enumerate() {
                    if row[row_bytes - 1] & mask != 0 {
                        return Err(invalid(format!("b8 row {shot} has nonzero padding bits")));
                    }
                }
            }
            Ok(bytes)
        }
        other => Err(invalid(format!(
            "unknown shots format {other:?}; expected 01 or b8"
        ))),
    }
}

/// Cross-checks a loss sidecar (`rustqec.loss-log.v1`: per-shot lists of
/// loss-visible readout ordinals) against the flag bits actually present in
/// the packed shots payload.
fn check_loss_log(
    path: &Path,
    loss_flags: &[usize],
    shots_b8: &[u8],
    row_bits: usize,
    shots: usize,
    json: bool,
) -> Result<(), CommandError> {
    let text = fs::read_to_string(path).map_err(|error| {
        command_error(
            DATASET_IMPORT_COMMAND,
            "input_error",
            format!("failed to read {}: {error}", path.display()),
            json,
        )
    })?;
    let log: serde_json::Value = serde_json::from_str(&text).map_err(|error| {
        command_error(
            DATASET_IMPORT_COMMAND,
            "invalid_arguments",
            format!("invalid loss log: {error}"),
            json,
        )
    })?;
    if log["schema_version"] != "rustqec.loss-log.v1" {
        return Err(command_error(
            DATASET_IMPORT_COMMAND,
            "invalid_arguments",
            "loss log must declare schema_version rustqec.loss-log.v1".to_string(),
            json,
        ));
    }
    let entries = log["shots"].as_array().ok_or_else(|| {
        command_error(
            DATASET_IMPORT_COMMAND,
            "invalid_arguments",
            "loss log must contain a shots array".to_string(),
            json,
        )
    })?;
    if entries.len() != shots {
        return Err(command_error(
            DATASET_IMPORT_COMMAND,
            "loss_log_mismatch",
            format!(
                "loss log lists {} shots, payload carries {shots}",
                entries.len()
            ),
            json,
        ));
    }
    let row_bytes = row_bits.div_ceil(8);
    for (shot, entry) in entries.iter().enumerate() {
        let declared = entry.as_array().ok_or_else(|| {
            command_error(
                DATASET_IMPORT_COMMAND,
                "invalid_arguments",
                format!("loss log shot {shot} must be an array of readout ordinals"),
                json,
            )
        })?;
        let mut declared: Vec<usize> = declared
            .iter()
            .map(|value| {
                value
                    .as_u64()
                    .map(|ordinal| ordinal as usize)
                    .ok_or_else(|| {
                        command_error(
                            DATASET_IMPORT_COMMAND,
                            "invalid_arguments",
                            format!("loss log shot {shot} contains a non-integer ordinal"),
                            json,
                        )
                    })
            })
            .collect::<Result<_, _>>()?;
        declared.sort_unstable();
        let row = &shots_b8[shot * row_bytes..(shot + 1) * row_bytes];
        let actual: Vec<usize> = loss_flags
            .iter()
            .enumerate()
            .filter_map(|(ordinal, &flag)| {
                ((row[flag / 8] >> (flag % 8)) & 1 == 1).then_some(ordinal)
            })
            .collect();
        if declared != actual {
            return Err(command_error(
                DATASET_IMPORT_COMMAND,
                "loss_log_mismatch",
                format!(
                    "loss log shot {shot} declares readouts {declared:?} but flag bits mark {actual:?}"
                ),
                json,
            ));
        }
    }
    Ok(())
}

#[derive(Debug)]
pub struct DatasetExportOptions {
    pub circuit: PathBuf,
    pub shots: u64,
    pub mode: String,
    pub public_out: PathBuf,
    pub private_out: PathBuf,
    pub seed: Option<u64>,
    pub logical_x_qubits: Option<String>,
    pub logical_z_qubits: Option<String>,
    pub error_trace: bool,
    pub format: PipelineFormat,
}

#[derive(Serialize)]
pub struct DatasetExportResult {
    pub shots: u64,
    pub mode: String,
    pub public_out: String,
    pub private_out: String,
}

fn command_error(
    command: &'static str,
    code: &'static str,
    message: String,
    json: bool,
) -> CommandError {
    CommandError {
        command,
        code,
        message,
        json,
        exit_code: 2,
    }
}

fn read_circuit_text(
    command: &'static str,
    input: Option<&Path>,
    stdin: &mut dyn Read,
    json: bool,
) -> Result<String, CommandError> {
    match input {
        Some(path) => fs::read_to_string(path).map_err(|error| {
            command_error(
                command,
                "input_error",
                format!("failed to read {}: {error}", path.display()),
                json,
            )
        }),
        None => {
            let mut text = String::new();
            stdin
                .read_to_string(&mut text)
                .map_err(|error| {
                    command_error(
                        command,
                        "input_error",
                        format!("failed to read stdin: {error}"),
                        json,
                    )
                })
                .map(|_| text)
        }
    }
}

/// Pre-validates the circuit so parse and semantic failures carry the stable
/// `invalid_circuit` code instead of leaking into a generic execution error.
fn validate_circuit(command: &'static str, text: &str, json: bool) -> Result<(), CommandError> {
    rstim::validation::parse_and_validate(text)
        .map(|_| ())
        .map_err(|message| command_error(command, "invalid_circuit", message, json))
}

fn write_artifact(
    command: &'static str,
    bytes: &[u8],
    out: &Path,
    json: bool,
) -> Result<(), CommandError> {
    let file = fs::File::create(out).map_err(|error| {
        command_error(
            command,
            "output_error",
            format!("failed to create {}: {error}", out.display()),
            json,
        )
    })?;
    let mut writer = BufWriter::new(file);
    writer
        .write_all(bytes)
        .and_then(|_| writer.flush())
        .map_err(|error| {
            command_error(
                command,
                "output_error",
                format!("failed to write {}: {error}", out.display()),
                json,
            )
        })
}

pub fn run_gen(
    options: &GenOptions,
    error_format: Option<ErrorFormat>,
) -> Result<GenResult, CommandError> {
    let json = options.format.is_json(error_format);
    for (name, value) in [
        ("noise", options.noise),
        (
            "before_round_data_depolarization",
            options.before_round_data_depolarization,
        ),
        (
            "before_round_data_loss_probability",
            options.before_round_data_loss_probability,
        ),
        (
            "before_measure_flip_probability",
            options.before_measure_flip_probability,
        ),
        (
            "after_reset_flip_probability",
            options.after_reset_flip_probability,
        ),
        (
            "after_clifford_loss_probability",
            options.after_clifford_loss_probability,
        ),
        (
            "operation_loss_probability",
            options.operation_loss_probability,
        ),
        (
            "measurement_loss_probability",
            options.measurement_loss_probability,
        ),
    ] {
        if !value.is_finite() || !(0.0..=1.0).contains(&value) {
            return Err(command_error(
                CIRCUIT_GEN_COMMAND,
                "invalid_arguments",
                format!("{name} must be finite and in [0, 1], got {value}"),
                json,
            ));
        }
    }
    let is_midswap = options.code == "surface_code" && options.task == "rotated_memory_z_midswap";
    let is_conventional = options.code == "surface_code" && options.task == "rotated_memory_z";
    if !is_midswap && options.before_round_data_loss_probability != 0.0 {
        return Err(command_error(
            CIRCUIT_GEN_COMMAND,
            "invalid_arguments",
            "before_round_data_loss_probability is only valid for surface_code/rotated_memory_z_midswap"
                .to_string(),
            json,
        ));
    }
    let mut buffer = Vec::new();
    if is_midswap {
        if options.after_clifford_loss_probability != 0.0 {
            return Err(command_error(
                CIRCUIT_GEN_COMMAND,
                "invalid_arguments",
                "after_clifford_loss_probability is not used by the Mid-SWAP task; use operation_loss_probability"
                    .to_string(),
                json,
            ));
        }
        let circuit = rstim::codegen::rotated_memory_z_midswap(rstim::codegen::MidSwapConfig {
            distance: options.distance,
            rounds: options.rounds,
            before_round_data_depolarization: options.before_round_data_depolarization,
            before_round_data_loss_probability: options.before_round_data_loss_probability,
            after_clifford_depolarization: options.noise,
            before_measure_flip_probability: options.before_measure_flip_probability,
            after_reset_flip_probability: options.after_reset_flip_probability,
            operation_loss_probability: options.operation_loss_probability,
            measurement_loss_probability: options.measurement_loss_probability,
        })
        .map_err(|error| {
            command_error(
                CIRCUIT_GEN_COMMAND,
                "invalid_arguments",
                error.to_string(),
                json,
            )
        })?;
        buffer.extend_from_slice(circuit.as_bytes());
    } else if is_conventional
        && (options.operation_loss_probability != 0.0
            || options.measurement_loss_probability != 0.0)
    {
        let circuit = rstim::codegen::surface_code::rotated_memory_z_loss_visible(
            options.distance,
            options.rounds,
            rstim::codegen::surface_code::RotatedMemoryZLossConfig {
                before_round_data_depolarization: options.before_round_data_depolarization,
                after_clifford_depolarization: options.noise,
                before_measure_flip_probability: options.before_measure_flip_probability,
                after_reset_flip_probability: options.after_reset_flip_probability,
                operation_loss_probability: options.operation_loss_probability,
                measurement_loss_probability: options.measurement_loss_probability,
                after_clifford_loss_probability: options.after_clifford_loss_probability,
            },
        )
        .map_err(|message| {
            command_error(CIRCUIT_GEN_COMMAND, "invalid_arguments", message, json)
        })?;
        buffer.extend_from_slice(circuit.as_bytes());
    } else {
        if options.operation_loss_probability != 0.0 || options.measurement_loss_probability != 0.0
        {
            return Err(command_error(
                CIRCUIT_GEN_COMMAND,
                "invalid_arguments",
                "operation_loss_probability and measurement_loss_probability are only valid for surface_code/rotated_memory_z and surface_code/rotated_memory_z_midswap"
                    .to_string(),
                json,
            ));
        }
        let params = rstim::codegen::NoiseParams {
            before_round_data_depolarization: options.before_round_data_depolarization,
            after_clifford_depolarization: options.noise,
            before_measure_flip_probability: options.before_measure_flip_probability,
            after_reset_flip_probability: options.after_reset_flip_probability,
            after_clifford_loss_probability: options.after_clifford_loss_probability,
        };
        rstim::operations::run_gen_with_params(
            &options.code,
            &options.task,
            options.distance,
            options.rounds,
            params,
            &mut buffer,
        )
        .map_err(|message| {
            command_error(CIRCUIT_GEN_COMMAND, "invalid_arguments", message, json)
        })?;
    }
    write_artifact(CIRCUIT_GEN_COMMAND, &buffer, &options.out, json)?;
    Ok(GenResult {
        code: options.code.clone(),
        task: options.task.clone(),
        distance: options.distance,
        rounds: options.rounds,
        circuit: options.out.display().to_string(),
    })
}

pub fn run_sample(
    options: &SampleOptions,
    error_format: Option<ErrorFormat>,
    stdin: &mut dyn Read,
) -> Result<SampleResult, CommandError> {
    let json = options.format.is_json(error_format);
    let text = read_circuit_text(
        CIRCUIT_SAMPLE_COMMAND,
        options.input.as_deref(),
        stdin,
        json,
    )?;
    validate_circuit(CIRCUIT_SAMPLE_COMMAND, &text, json)?;
    let shots = usize::try_from(options.shots).map_err(|_| {
        command_error(
            CIRCUIT_SAMPLE_COMMAND,
            "invalid_arguments",
            "--shots is too large for this platform".to_string(),
            json,
        )
    })?;
    let summary = rstim::stats::summarize_text(&text).map_err(|message| {
        command_error(CIRCUIT_SAMPLE_COMMAND, "invalid_circuit", message, json)
    })?;
    let mut buffer = Vec::new();
    rstim::operations::run_sample(
        &text,
        shots,
        &options.out_format,
        options.seed,
        options.skip_reference_sample,
        &mut buffer,
    )
    .map_err(|message| {
        let code = if message.starts_with("unknown output format") {
            "invalid_arguments"
        } else {
            "execution_error"
        };
        command_error(CIRCUIT_SAMPLE_COMMAND, code, message, json)
    })?;
    write_artifact(CIRCUIT_SAMPLE_COMMAND, &buffer, &options.out, json)?;
    Ok(SampleResult {
        shots: options.shots,
        num_measurements: summary.num_measurements,
        out: options.out.display().to_string(),
        out_format: options.out_format.clone(),
    })
}

pub fn run_detect(
    options: &DetectOptions,
    error_format: Option<ErrorFormat>,
    stdin: &mut dyn Read,
) -> Result<DetectResult, CommandError> {
    let json = options.format.is_json(error_format);
    let text = read_circuit_text(
        CIRCUIT_DETECT_COMMAND,
        options.input.as_deref(),
        stdin,
        json,
    )?;
    validate_circuit(CIRCUIT_DETECT_COMMAND, &text, json)?;
    let shots = usize::try_from(options.shots).map_err(|_| {
        command_error(
            CIRCUIT_DETECT_COMMAND,
            "invalid_arguments",
            "--shots is too large for this platform".to_string(),
            json,
        )
    })?;
    let summary = rstim::stats::summarize_text(&text).map_err(|message| {
        command_error(CIRCUIT_DETECT_COMMAND, "invalid_circuit", message, json)
    })?;
    let mut buffer = Vec::new();
    match &options.obs_out {
        Some(obs_out) => {
            let mut obs_buffer = Vec::new();
            rstim::operations::run_detect_with_obs(
                &text,
                shots,
                &options.out_format,
                options.seed,
                options.append_observables,
                &mut buffer,
                &mut obs_buffer,
                &options.obs_out_format,
            )
            .map_err(|message| detect_error_code(message, json))?;
            write_artifact(CIRCUIT_DETECT_COMMAND, &buffer, &options.out, json)?;
            write_artifact(CIRCUIT_DETECT_COMMAND, &obs_buffer, obs_out, json)?;
        }
        None => {
            rstim::operations::run_detect(
                &text,
                shots,
                &options.out_format,
                options.seed,
                options.append_observables,
                &mut buffer,
            )
            .map_err(|message| detect_error_code(message, json))?;
            write_artifact(CIRCUIT_DETECT_COMMAND, &buffer, &options.out, json)?;
        }
    }
    Ok(DetectResult {
        shots: options.shots,
        num_detectors: summary.num_detectors,
        out: options.out.display().to_string(),
        out_format: options.out_format.clone(),
        observables_out: options
            .obs_out
            .as_ref()
            .map(|path| path.display().to_string()),
    })
}

fn detect_error_code(message: String, json: bool) -> CommandError {
    let code = if message.starts_with("unknown output format") {
        "invalid_arguments"
    } else {
        "execution_error"
    };
    command_error(CIRCUIT_DETECT_COMMAND, code, message, json)
}

pub fn run_dem(
    options: &DemOptions,
    error_format: Option<ErrorFormat>,
    stdin: &mut dyn Read,
) -> Result<DemResult, CommandError> {
    let json = options.format.is_json(error_format);
    let text = read_circuit_text(CIRCUIT_DEM_COMMAND, options.input.as_deref(), stdin, json)?;
    validate_circuit(CIRCUIT_DEM_COMMAND, &text, json)?;
    let summary = rstim::stats::summarize_text(&text)
        .map_err(|message| command_error(CIRCUIT_DEM_COMMAND, "invalid_circuit", message, json))?;
    let mut buffer = Vec::new();
    rstim::operations::run_analyze_errors_with_flags(
        &text,
        options.approximate_disjoint_errors,
        options.allow_gauge_detectors,
        options.decompose_errors,
        &mut buffer,
    )
    .map_err(|message| command_error(CIRCUIT_DEM_COMMAND, "execution_error", message, json))?;
    write_artifact(CIRCUIT_DEM_COMMAND, &buffer, &options.out, json)?;
    Ok(DemResult {
        num_detectors: summary.num_detectors,
        num_observables: summary.num_observables,
        out: options.out.display().to_string(),
    })
}

pub fn run_dataset_export(
    options: &DatasetExportOptions,
    error_format: Option<ErrorFormat>,
) -> Result<DatasetExportResult, CommandError> {
    let json = options.format.is_json(error_format);
    if options.logical_x_qubits.is_some() && options.logical_z_qubits.is_some() {
        return Err(command_error(
            DATASET_EXPORT_COMMAND,
            "invalid_arguments",
            "--logical-x-qubits and --logical-z-qubits are mutually exclusive".to_string(),
            json,
        ));
    }
    let text = fs::read_to_string(&options.circuit).map_err(|error| {
        command_error(
            DATASET_EXPORT_COMMAND,
            "input_error",
            format!("failed to read {}: {error}", options.circuit.display()),
            json,
        )
    })?;
    validate_circuit(DATASET_EXPORT_COMMAND, &text, json)?;
    let circuit_path = options.circuit.display().to_string();
    let public_out = options.public_out.display().to_string();
    let private_out = options.private_out.display().to_string();
    let logical_flip = match (
        options.logical_x_qubits.as_deref(),
        options.logical_z_qubits.as_deref(),
    ) {
        (Some(value), None) => Some(rstim::decoder_dataset::LogicalFlip::parse(
            rstim::decoder_dataset::LogicalPauli::X,
            value,
        )),
        (None, Some(value)) => Some(rstim::decoder_dataset::LogicalFlip::parse(
            rstim::decoder_dataset::LogicalPauli::Z,
            value,
        )),
        _ => None,
    }
    .transpose()
    .map_err(|message| command_error(DATASET_EXPORT_COMMAND, "invalid_arguments", message, json))?;
    rstim::operations::run_export_decoder_dataset_with_logical_flip(
        &circuit_path,
        options.shots,
        &options.mode,
        logical_flip,
        &public_out,
        &private_out,
        options.seed,
        options.error_trace,
    )
    .map_err(|message| {
        let code = if message.contains("unknown decoder dataset mode") {
            "invalid_arguments"
        } else {
            "execution_error"
        };
        command_error(DATASET_EXPORT_COMMAND, code, message, json)
    })?;
    Ok(DatasetExportResult {
        shots: options.shots,
        mode: options.mode.clone(),
        public_out,
        private_out,
    })
}