ruqu 0.1.32

Classical nervous system for quantum machines - real-time coherence assessment via dynamic min-cut
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
//! ruQu Demo Binary - Proof Artifact
//!
//! This is the runnable demonstration of ruQu's capabilities.
//!
//! ## What it does
//!
//! 1. Generates a streaming syndrome feed
//! 2. Runs the coherence gate loop per round
//! 3. Prints live status: round, cut value, risk, region mask
//! 4. Writes metrics file: latency histogram, p50/p99/p999, false alarms
//!
//! ## Usage
//!
//! ```bash
//! # Basic run with defaults
//! cargo run --bin ruqu_demo --release
//!
//! # Custom parameters
//! cargo run --bin ruqu_demo --release -- \
//!     --distance 7 \
//!     --error-rate 0.01 \
//!     --rounds 10000 \
//!     --output metrics.json
//! ```

use std::collections::VecDeque;
use std::fs::File;
use std::io::Write;
use std::time::Instant;

use ruqu::stim::{StimSyndromeSource, SurfaceCodeConfig};
use ruqu::syndrome::DetectorBitmap;

// ============================================================================
// CONFIGURATION
// ============================================================================

#[derive(Debug, Clone)]
struct DemoConfig {
    /// Code distance
    code_distance: usize,
    /// Physical error rate
    error_rate: f64,
    /// Number of rounds to run
    num_rounds: usize,
    /// Random seed
    seed: u64,
    /// Output metrics file
    output_file: Option<String>,
    /// Print interval (every N rounds)
    print_interval: usize,
    /// Gate threshold
    threshold: f64,
}

impl Default for DemoConfig {
    fn default() -> Self {
        Self {
            code_distance: 5,
            error_rate: 0.01,
            num_rounds: 10000,
            seed: 42,
            output_file: Some("ruqu_metrics.json".to_string()),
            print_interval: 1000,
            threshold: 5.0,
        }
    }
}

fn parse_args() -> DemoConfig {
    let args: Vec<String> = std::env::args().collect();
    let mut config = DemoConfig::default();

    let mut i = 1;
    while i < args.len() {
        match args[i].as_str() {
            "--distance" | "-d" => {
                i += 1;
                config.code_distance = args[i].parse().expect("Invalid distance");
            }
            "--error-rate" | "-e" => {
                i += 1;
                config.error_rate = args[i].parse().expect("Invalid error rate");
            }
            "--rounds" | "-r" => {
                i += 1;
                config.num_rounds = args[i].parse().expect("Invalid rounds");
            }
            "--seed" | "-s" => {
                i += 1;
                config.seed = args[i].parse().expect("Invalid seed");
            }
            "--output" | "-o" => {
                i += 1;
                config.output_file = Some(args[i].clone());
            }
            "--threshold" | "-t" => {
                i += 1;
                config.threshold = args[i].parse().expect("Invalid threshold");
            }
            "--help" | "-h" => {
                print_help();
                std::process::exit(0);
            }
            _ => {
                eprintln!("Unknown argument: {}", args[i]);
                std::process::exit(1);
            }
        }
        i += 1;
    }

    config
}

fn print_help() {
    println!(
        r#"
ruQu Demo - Coherence Gate Demonstration

USAGE:
    ruqu_demo [OPTIONS]

OPTIONS:
    -d, --distance <N>      Code distance (default: 5)
    -e, --error-rate <P>    Physical error rate (default: 0.01)
    -r, --rounds <N>        Number of rounds (default: 10000)
    -s, --seed <N>          Random seed (default: 42)
    -o, --output <FILE>     Output metrics file (default: ruqu_metrics.json)
    -t, --threshold <T>     Gate threshold (default: 5.0)
    -h, --help              Print this help message
"#
    );
}

// ============================================================================
// LATENCY TRACKING
// ============================================================================

struct LatencyTracker {
    latencies: Vec<u64>,
    recent: VecDeque<u64>,
    max_recent: usize,
}

impl LatencyTracker {
    fn new(max_recent: usize) -> Self {
        Self {
            latencies: Vec::new(),
            recent: VecDeque::with_capacity(max_recent),
            max_recent,
        }
    }

    fn record(&mut self, latency_ns: u64) {
        self.latencies.push(latency_ns);
        if self.recent.len() >= self.max_recent {
            self.recent.pop_front();
        }
        self.recent.push_back(latency_ns);
    }

    fn percentile(&self, p: f64) -> u64 {
        if self.latencies.is_empty() {
            return 0;
        }
        let mut sorted = self.latencies.clone();
        sorted.sort_unstable();
        let idx = ((p / 100.0) * (sorted.len() - 1) as f64) as usize;
        sorted[idx]
    }

    fn p50(&self) -> u64 {
        self.percentile(50.0)
    }

    fn p99(&self) -> u64 {
        self.percentile(99.0)
    }

    fn p999(&self) -> u64 {
        self.percentile(99.9)
    }

    fn max(&self) -> u64 {
        self.latencies.iter().copied().max().unwrap_or(0)
    }

    fn mean(&self) -> f64 {
        if self.latencies.is_empty() {
            return 0.0;
        }
        let sum: u64 = self.latencies.iter().sum();
        sum as f64 / self.latencies.len() as f64
    }

    fn count(&self) -> usize {
        self.latencies.len()
    }

    fn histogram(&self, num_buckets: usize) -> Vec<(u64, u64, usize)> {
        if self.latencies.is_empty() {
            return vec![];
        }

        let min = *self.latencies.iter().min().unwrap();
        let max = self.max();
        let range = max - min + 1;
        let bucket_size = (range / num_buckets as u64).max(1);

        let mut buckets = vec![0usize; num_buckets];
        for &lat in &self.latencies {
            let bucket = ((lat - min) / bucket_size).min(num_buckets as u64 - 1) as usize;
            buckets[bucket] += 1;
        }

        buckets
            .into_iter()
            .enumerate()
            .map(|(i, count)| {
                let start = min + i as u64 * bucket_size;
                let end = start + bucket_size;
                (start, end, count)
            })
            .collect()
    }
}

// ============================================================================
// SIMPLE MIN-CUT GATE
// ============================================================================

use std::collections::{HashMap, HashSet};

struct MinCutGate {
    threshold: f64,
    grid_size: usize,
    base_weight: f64,
}

impl MinCutGate {
    fn new(code_distance: usize, error_rate: f64, threshold: f64) -> Self {
        Self {
            threshold,
            grid_size: code_distance - 1,
            base_weight: (-error_rate.ln()).max(0.1),
        }
    }

    fn process(&self, syndrome: &DetectorBitmap) -> GateResult {
        let start = Instant::now();

        // Compute min-cut
        let fired_set: HashSet<usize> = syndrome.iter_fired().collect();
        let min_cut = self.compute_min_cut(&fired_set);

        // Compute risk
        let risk = if min_cut < self.threshold {
            1.0 - (min_cut / self.threshold)
        } else {
            0.0
        };

        // Compute region mask (simplified: which quadrants have errors)
        let region_mask = self.compute_region_mask(&fired_set);

        let latency_ns = start.elapsed().as_nanos() as u64;

        GateResult {
            min_cut,
            risk,
            region_mask,
            decision: if min_cut >= self.threshold {
                Decision::Permit
            } else if min_cut >= self.threshold * 0.5 {
                Decision::Defer
            } else {
                Decision::Deny
            },
            latency_ns,
            fired_count: fired_set.len(),
        }
    }

    fn compute_min_cut(&self, fired_set: &HashSet<usize>) -> f64 {
        // Simple s-t min-cut using Edmonds-Karp
        let mut adj: HashMap<u32, Vec<(u32, f64)>> = HashMap::new();
        let fired_weight = 0.01;

        // Build grid
        for row in 0..self.grid_size {
            for col in 0..self.grid_size {
                let node = (row * self.grid_size + col) as u32;
                let is_fired = fired_set.contains(&(node as usize));

                if col + 1 < self.grid_size {
                    let right = (row * self.grid_size + col + 1) as u32;
                    let right_fired = fired_set.contains(&(right as usize));
                    let weight = if is_fired || right_fired {
                        fired_weight
                    } else {
                        self.base_weight
                    };
                    adj.entry(node).or_default().push((right, weight));
                    adj.entry(right).or_default().push((node, weight));
                }

                if row + 1 < self.grid_size {
                    let bottom = ((row + 1) * self.grid_size + col) as u32;
                    let bottom_fired = fired_set.contains(&(bottom as usize));
                    let weight = if is_fired || bottom_fired {
                        fired_weight
                    } else {
                        self.base_weight
                    };
                    adj.entry(node).or_default().push((bottom, weight));
                    adj.entry(bottom).or_default().push((node, weight));
                }
            }
        }

        let source = (self.grid_size * self.grid_size) as u32;
        let sink = source + 1;

        // Connect boundaries
        let boundary_weight = self.base_weight * 2.0;
        for row in 0..self.grid_size {
            let left = (row * self.grid_size) as u32;
            let right = (row * self.grid_size + self.grid_size - 1) as u32;
            adj.entry(source).or_default().push((left, boundary_weight));
            adj.entry(left).or_default().push((source, boundary_weight));
            adj.entry(right).or_default().push((sink, boundary_weight));
            adj.entry(sink).or_default().push((right, boundary_weight));
        }

        // Max-flow = min-cut
        let mut capacity: HashMap<(u32, u32), f64> = HashMap::new();
        for (&u, neighbors) in &adj {
            for &(v, w) in neighbors {
                *capacity.entry((u, v)).or_default() += w;
            }
        }

        let mut max_flow = 0.0;
        loop {
            // BFS for augmenting path
            let mut parent: HashMap<u32, u32> = HashMap::new();
            let mut visited = HashSet::new();
            let mut queue = std::collections::VecDeque::new();

            queue.push_back(source);
            visited.insert(source);

            while let Some(u) = queue.pop_front() {
                if u == sink {
                    break;
                }
                if let Some(neighbors) = adj.get(&u) {
                    for &(v, _) in neighbors {
                        let cap = capacity.get(&(u, v)).copied().unwrap_or(0.0);
                        if !visited.contains(&v) && cap > 1e-10 {
                            visited.insert(v);
                            parent.insert(v, u);
                            queue.push_back(v);
                        }
                    }
                }
            }

            if !parent.contains_key(&sink) {
                break;
            }

            // Find bottleneck
            let mut path_flow = f64::INFINITY;
            let mut v = sink;
            while v != source {
                let u = parent[&v];
                path_flow = path_flow.min(capacity.get(&(u, v)).copied().unwrap_or(0.0));
                v = u;
            }

            // Update capacities
            v = sink;
            while v != source {
                let u = parent[&v];
                *capacity.entry((u, v)).or_default() -= path_flow;
                *capacity.entry((v, u)).or_default() += path_flow;
                v = u;
            }

            max_flow += path_flow;
        }

        max_flow
    }

    fn compute_region_mask(&self, fired_set: &HashSet<usize>) -> u64 {
        // Split into 4 quadrants
        let half = self.grid_size / 2;
        let mut mask = 0u64;

        for &det in fired_set {
            let row = det / self.grid_size;
            let col = det % self.grid_size;
            let quadrant = match (row < half, col < half) {
                (true, true) => 0,   // Top-left
                (true, false) => 1,  // Top-right
                (false, true) => 2,  // Bottom-left
                (false, false) => 3, // Bottom-right
            };
            mask |= 1 << quadrant;
        }

        mask
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum Decision {
    Permit,
    Defer,
    Deny,
}

#[derive(Debug, Clone)]
struct GateResult {
    min_cut: f64,
    risk: f64,
    region_mask: u64,
    decision: Decision,
    latency_ns: u64,
    fired_count: usize,
}

// ============================================================================
// METRICS OUTPUT
// ============================================================================

#[derive(Debug, serde::Serialize)]
struct DemoMetrics {
    config: MetricsConfig,
    summary: MetricsSummary,
    latency: LatencyMetrics,
    decisions: DecisionMetrics,
    histogram: Vec<HistogramBucket>,
}

#[derive(Debug, serde::Serialize)]
struct MetricsConfig {
    code_distance: usize,
    error_rate: f64,
    num_rounds: usize,
    seed: u64,
    threshold: f64,
}

#[derive(Debug, serde::Serialize)]
struct MetricsSummary {
    total_rounds: usize,
    total_time_ms: f64,
    throughput_per_sec: f64,
    total_fired: usize,
    avg_fired_per_round: f64,
}

#[derive(Debug, serde::Serialize)]
struct LatencyMetrics {
    mean_ns: f64,
    p50_ns: u64,
    p99_ns: u64,
    p999_ns: u64,
    max_ns: u64,
}

#[derive(Debug, serde::Serialize)]
struct DecisionMetrics {
    permits: usize,
    defers: usize,
    denies: usize,
    permit_rate: f64,
    deny_rate: f64,
}

#[derive(Debug, serde::Serialize)]
struct HistogramBucket {
    start_ns: u64,
    end_ns: u64,
    count: usize,
}

// ============================================================================
// MAIN
// ============================================================================

fn main() {
    let config = parse_args();

    println!("╔═══════════════════════════════════════════════════════════════════╗");
    println!("║                    ruQu Demo - Proof Artifact                     ║");
    println!("╠═══════════════════════════════════════════════════════════════════╣");
    println!("║ Code Distance: d={}  | Error Rate: {:.4}  | Rounds: {:>6}",
             config.code_distance, config.error_rate, config.num_rounds);
    println!("║ Threshold: {:.2}     | Seed: {:>10}                          ║",
             config.threshold, config.seed);
    println!("╚═══════════════════════════════════════════════════════════════════╝");
    println!();

    // Initialize components
    let surface_config = SurfaceCodeConfig::new(config.code_distance, config.error_rate)
        .with_seed(config.seed);
    let mut syndrome_source = match StimSyndromeSource::new(surface_config) {
        Ok(s) => s,
        Err(e) => {
            eprintln!("Failed to create syndrome source: {:?}", e);
            std::process::exit(1);
        }
    };

    let gate = MinCutGate::new(config.code_distance, config.error_rate, config.threshold);
    let mut latency_tracker = LatencyTracker::new(1000);

    // Counters
    let mut permits = 0usize;
    let mut defers = 0usize;
    let mut denies = 0usize;
    let mut total_fired = 0usize;

    // Run demo
    println!("Round │ Cut   │ Risk  │ Decision │ Regions │ Latency │ Fired");
    println!("──────┼───────┼───────┼──────────┼─────────┼─────────┼──────");

    let start_time = Instant::now();

    for round in 0..config.num_rounds {
        // Get syndrome
        let syndrome: DetectorBitmap = match syndrome_source.sample() {
            Ok(s) => s,
            Err(_) => continue,
        };

        // Process through gate
        let result = gate.process(&syndrome);
        latency_tracker.record(result.latency_ns);
        total_fired += result.fired_count;

        // Update counters
        match result.decision {
            Decision::Permit => permits += 1,
            Decision::Defer => defers += 1,
            Decision::Deny => denies += 1,
        }

        // Print live status
        if round % config.print_interval == 0 || result.decision == Decision::Deny {
            let decision_str = match result.decision {
                Decision::Permit => "\x1b[32mPERMIT\x1b[0m  ",
                Decision::Defer => "\x1b[33mDEFER\x1b[0m   ",
                Decision::Deny => "\x1b[31mDENY\x1b[0m    ",
            };
            println!(
                "{:>5}{:>5.2}{:>5.2}{}{:>07b}{:>5}ns │ {:>3}",
                round,
                result.min_cut,
                result.risk,
                decision_str,
                result.region_mask,
                result.latency_ns,
                result.fired_count
            );
        }
    }

    let total_time = start_time.elapsed();

    // Summary
    println!();
    println!("╔═══════════════════════════════════════════════════════════════════╗");
    println!("║                         RESULTS SUMMARY                           ║");
    println!("╠═══════════════════════════════════════════════════════════════════╣");
    println!("║ Total Time:        {:>10.2} ms                                 ║",
             total_time.as_secs_f64() * 1000.0);
    println!("║ Throughput:        {:>10.0} rounds/sec                         ║",
             config.num_rounds as f64 / total_time.as_secs_f64());
    println!("║ Avg Fired/Round:   {:>10.2}                                    ║",
             total_fired as f64 / config.num_rounds as f64);
    println!("╠═══════════════════════════════════════════════════════════════════╣");
    println!("║ Latency:                                                         ║");
    println!("║   Mean:   {:>8.0} ns                                           ║", latency_tracker.mean());
    println!("║   P50:    {:>8} ns                                           ║", latency_tracker.p50());
    println!("║   P99:    {:>8} ns                                           ║", latency_tracker.p99());
    println!("║   P999:   {:>8} ns                                           ║", latency_tracker.p999());
    println!("║   Max:    {:>8} ns                                           ║", latency_tracker.max());
    println!("╠═══════════════════════════════════════════════════════════════════╣");
    println!("║ Decisions:                                                       ║");
    println!("║   Permits: {:>6} ({:>5.1}%)                                      ║",
             permits, permits as f64 / config.num_rounds as f64 * 100.0);
    println!("║   Defers:  {:>6} ({:>5.1}%)                                      ║",
             defers, defers as f64 / config.num_rounds as f64 * 100.0);
    println!("║   Denies:  {:>6} ({:>5.1}%)                                      ║",
             denies, denies as f64 / config.num_rounds as f64 * 100.0);
    println!("╚═══════════════════════════════════════════════════════════════════╝");

    // Write metrics file
    if let Some(output_file) = &config.output_file {
        let metrics = DemoMetrics {
            config: MetricsConfig {
                code_distance: config.code_distance,
                error_rate: config.error_rate,
                num_rounds: config.num_rounds,
                seed: config.seed,
                threshold: config.threshold,
            },
            summary: MetricsSummary {
                total_rounds: config.num_rounds,
                total_time_ms: total_time.as_secs_f64() * 1000.0,
                throughput_per_sec: config.num_rounds as f64 / total_time.as_secs_f64(),
                total_fired,
                avg_fired_per_round: total_fired as f64 / config.num_rounds as f64,
            },
            latency: LatencyMetrics {
                mean_ns: latency_tracker.mean(),
                p50_ns: latency_tracker.p50(),
                p99_ns: latency_tracker.p99(),
                p999_ns: latency_tracker.p999(),
                max_ns: latency_tracker.max(),
            },
            decisions: DecisionMetrics {
                permits,
                defers,
                denies,
                permit_rate: permits as f64 / config.num_rounds as f64,
                deny_rate: denies as f64 / config.num_rounds as f64,
            },
            histogram: latency_tracker
                .histogram(20)
                .into_iter()
                .map(|(start, end, count)| HistogramBucket {
                    start_ns: start,
                    end_ns: end,
                    count,
                })
                .collect(),
        };

        match File::create(output_file) {
            Ok(mut file) => {
                let json = serde_json::to_string_pretty(&metrics).unwrap();
                file.write_all(json.as_bytes()).unwrap();
                println!("\nMetrics written to: {}", output_file);
            }
            Err(e) => {
                eprintln!("Failed to write metrics file: {}", e);
            }
        }
    }

    // Latency histogram
    println!("\nLatency Histogram:");
    let histogram = latency_tracker.histogram(10);
    let max_count = histogram.iter().map(|(_, _, c)| *c).max().unwrap_or(1);
    for (start, end, count) in histogram {
        let bar_len = (count as f64 / max_count as f64 * 40.0) as usize;
        let bar = "".repeat(bar_len);
        println!("{:>8}-{:<8} │{:<40} {:>5}", start, end, bar, count);
    }
}