rmatching 0.3.0

Rust minimum-weight perfect-matching decoder for detector-error-model workflows
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
use std::num::Wrapping;

use crate::interop::CompressedEdge;
use crate::search::search_graph::SearchGraph;
use crate::types::*;
use crate::util::radix_heap::{HasTime, RadixHeapQueue};

// ---------------------------------------------------------------------------
// Search-specific event type for the radix heap
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy)]
pub enum SearchEvent {
    NoEvent,
    LookAtNode { node: SearchNodeIdx, time: Wrapping<u32> },
}

impl HasTime for SearchEvent {
    fn time(&self) -> Wrapping<u32> {
        match self {
            SearchEvent::NoEvent => Wrapping(0),
            SearchEvent::LookAtNode { time, .. } => *time,
        }
    }
    fn no_event() -> Self {
        SearchEvent::NoEvent
    }
    fn is_no_event(&self) -> bool {
        matches!(self, SearchEvent::NoEvent)
    }
}

// ---------------------------------------------------------------------------
// Collision edge returned by the search
// ---------------------------------------------------------------------------

/// The edge on which two search regions collided.
#[derive(Debug, Clone, Copy)]
pub struct SearchGraphEdge {
    pub node: Option<SearchNodeIdx>,
    pub neighbor_index: usize,
}

// ---------------------------------------------------------------------------
// Target type for the search
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TargetType {
    DetectorNode,
    Boundary,
    NoTarget,
}

// ---------------------------------------------------------------------------
// SearchFlooder
// ---------------------------------------------------------------------------

/// Bidirectional Dijkstra on the search graph.
///
/// Used by Mwpm to extract the actual shortest path between matched nodes
/// after the flooding phase finds the matching.
pub struct SearchFlooder {
    pub graph: SearchGraph,
    pub queue: RadixHeapQueue<SearchEvent>,
    reached_nodes: Vec<SearchNodeIdx>,
    target_type: TargetType,
}

impl SearchFlooder {
    pub fn new(graph: SearchGraph) -> Self {
        SearchFlooder {
            graph,
            queue: RadixHeapQueue::new(),
            reached_nodes: Vec::new(),
            target_type: TargetType::NoTarget,
        }
    }

    // -- internal helpers ---------------------------------------------------

    /// Find the best next event (neighbor index, collision time) for a node.
    fn find_next_event(
        &self,
        node_idx: SearchNodeIdx,
    ) -> (Option<usize>, CumulativeTime) {
        let node = &self.graph.nodes[node_idx.0 as usize];
        let mut best_time: CumulativeTime = CumulativeTime::MAX;
        let mut best_neighbor: Option<usize> = None;

        let mut start = 0usize;

        // Check boundary neighbor (always at index 0 if present).
        if !node.neighbors.is_empty() && node.neighbors[0].is_none() {
            if self.target_type == TargetType::Boundary {
                let weight = node.neighbor_weights[0] as CumulativeTime;
                let covered = self.queue.cur_time - node.distance_from_source;
                let collision_time = self.queue.cur_time + weight - covered;
                if collision_time < best_time {
                    best_time = collision_time;
                    best_neighbor = Some(0);
                }
            }
            start = 1;
        }

        // Non-boundary neighbors.
        for i in start..node.neighbors.len() {
            let weight = node.neighbor_weights[i] as CumulativeTime;
            let nb_idx = node.neighbors[i].unwrap();
            let nb = &self.graph.nodes[nb_idx.0 as usize];

            let collision_time;
            if nb.reached_from_source == node.reached_from_source {
                // Same source -- skip.
                continue;
            } else if nb.reached_from_source.is_none() {
                // Unreached neighbor.
                let covered = self.queue.cur_time - node.distance_from_source;
                collision_time = self.queue.cur_time + weight - covered;
            } else {
                // Reached from different source -- two fronts meeting.
                let covered_this = self.queue.cur_time - node.distance_from_source;
                let covered_nb = self.queue.cur_time - nb.distance_from_source;
                collision_time =
                    self.queue.cur_time + (weight - covered_this - covered_nb) / 2;
            }

            if collision_time < best_time {
                best_time = collision_time;
                best_neighbor = Some(i);
            }
        }

        (best_neighbor, best_time)
    }

    /// Schedule the next event for a node.
    fn reschedule_events(&mut self, node_idx: SearchNodeIdx) {
        let (best_nb, best_time) = self.find_next_event(node_idx);
        let tracker =
            &mut self.graph.nodes[node_idx.0 as usize].node_event_tracker;
        match best_nb {
            None => tracker.set_no_desired_event(),
            Some(_) => {
                let event = SearchEvent::LookAtNode {
                    node: node_idx,
                    time: Wrapping(best_time as u32),
                };
                tracker.set_desired_event(event, &mut self.queue);
            }
        }
    }

    /// Start a search from an empty (unreached) node.
    fn start_at_empty_node(&mut self, src: SearchNodeIdx) {
        {
            let node = &mut self.graph.nodes[src.0 as usize];
            node.reached_from_source = Some(src);
            node.index_of_predecessor = None;
            node.distance_from_source = 0;
        }
        self.reached_nodes.push(src);
        self.reschedule_events(src);
    }

    /// Explore an unreached neighbor.
    fn explore_empty_node(
        &mut self,
        empty_idx: SearchNodeIdx,
        empty_to_from_index: usize,
    ) {
        let from_idx =
            self.graph.nodes[empty_idx.0 as usize].neighbors[empty_to_from_index]
                .unwrap();
        let from_source =
            self.graph.nodes[from_idx.0 as usize].reached_from_source;
        let from_dist =
            self.graph.nodes[from_idx.0 as usize].distance_from_source;
        let weight = self.graph.nodes[empty_idx.0 as usize].neighbor_weights
            [empty_to_from_index] as CumulativeTime;

        {
            let empty = &mut self.graph.nodes[empty_idx.0 as usize];
            empty.reached_from_source = from_source;
            empty.index_of_predecessor = Some(empty_to_from_index);
            empty.distance_from_source = weight + from_dist;
        }
        self.reached_nodes.push(empty_idx);
        self.reschedule_events(empty_idx);
    }

    /// Process a "look at node" event. Returns a collision edge if found.
    fn do_look_at_node_event(
        &mut self,
        node_idx: SearchNodeIdx,
    ) -> SearchGraphEdge {
        let (next_nb, next_time) = self.find_next_event(node_idx);

        if let Some(nb_i) = next_nb {
            if next_time == self.queue.cur_time {
                let dst_opt =
                    self.graph.nodes[node_idx.0 as usize].neighbors[nb_i];
                match dst_opt {
                    None => {
                        // Boundary collision.
                        return SearchGraphEdge {
                            node: Some(node_idx),
                            neighbor_index: nb_i,
                        };
                    }
                    Some(dst_idx) => {
                        let dst_reached = self.graph.nodes[dst_idx.0 as usize]
                            .reached_from_source;
                        if dst_reached.is_none() {
                            // Explore the empty neighbor.
                            let reverse_idx = self.graph.nodes
                                [dst_idx.0 as usize]
                                .index_of_neighbor(Some(node_idx));
                            self.explore_empty_node(dst_idx, reverse_idx);
                            // Revisit this node immediately.
                            let tracker = &mut self.graph.nodes
                                [node_idx.0 as usize]
                                .node_event_tracker;
                            let event = SearchEvent::LookAtNode {
                                node: node_idx,
                                time: Wrapping(self.queue.cur_time as u32),
                            };
                            tracker.set_desired_event(
                                event,
                                &mut self.queue,
                            );
                            return SearchGraphEdge {
                                node: None,
                                neighbor_index: NO_NEIGHBOR,
                            };
                        } else {
                            // Two-front collision.
                            return SearchGraphEdge {
                                node: Some(node_idx),
                                neighbor_index: nb_i,
                            };
                        }
                    }
                }
            } else {
                // Revisit later.
                let tracker =
                    &mut self.graph.nodes[node_idx.0 as usize]
                        .node_event_tracker;
                let event = SearchEvent::LookAtNode {
                    node: node_idx,
                    time: Wrapping(next_time as u32),
                };
                tracker.set_desired_event(event, &mut self.queue);
            }
        }

        SearchGraphEdge {
            node: None,
            neighbor_index: NO_NEIGHBOR,
        }
    }

    // -- public API ---------------------------------------------------------

    /// Run bidirectional Dijkstra from `src` to `dst`.
    /// `dst` is `None` for boundary search.
    /// Returns the collision edge.
    pub fn run_until_collision(
        &mut self,
        src: SearchNodeIdx,
        dst: Option<SearchNodeIdx>,
    ) -> SearchGraphEdge {
        match dst {
            None => {
                self.target_type = TargetType::Boundary;
            }
            Some(d) => {
                self.target_type = TargetType::DetectorNode;
                self.start_at_empty_node(d);
            }
        }
        self.start_at_empty_node(src);

        while !self.queue.is_empty() {
            let ev = self.queue.dequeue();
            if let SearchEvent::LookAtNode { node, .. } = ev {
                let n = &mut self.graph.nodes[node.0 as usize];
                let should_process = n.node_event_tracker.dequeue_decision(
                    &ev,
                    &mut self.queue,
                    |t| SearchEvent::LookAtNode { node, time: t },
                );
                if should_process {
                    let edge = self.do_look_at_node_event(node);
                    if edge.node.is_some() {
                        return edge;
                    }
                }
            }
        }

        SearchGraphEdge {
            node: None,
            neighbor_index: NO_NEIGHBOR,
        }
    }

    /// Trace back from a node to its source, collecting edges.
    fn trace_back_from_node(
        &self,
        start: SearchNodeIdx,
    ) -> Vec<SearchGraphEdge> {
        let mut edges = Vec::new();
        let mut cur = start;
        loop {
            let pred = self.graph.nodes[cur.0 as usize].index_of_predecessor;
            match pred {
                None => break,
                Some(pred_idx) => {
                    edges.push(SearchGraphEdge {
                        node: Some(cur),
                        neighbor_index: pred_idx,
                    });
                    cur = self.graph.nodes[cur.0 as usize].neighbors[pred_idx]
                        .unwrap();
                }
            }
        }
        edges
    }

    /// Iterate edges on the shortest path from `src` to `dst` (in order),
    /// calling `callback` with `(from: Option<SearchNodeIdx>, to: Option<SearchNodeIdx>, obs_mask)`.
    pub fn iter_edges_on_shortest_path(
        &mut self,
        src: usize,
        dst: Option<usize>,
        mut callback: impl FnMut(Option<SearchNodeIdx>, Option<SearchNodeIdx>, ObsMask),
    ) {
        let src_idx = SearchNodeIdx(src as u32);
        let dst_idx = dst.map(|d| SearchNodeIdx(d as u32));

        let collision_edge = self.run_until_collision(src_idx, dst_idx);

        if collision_edge.node.is_none() {
            self.reset();
            return;
        }

        let collision_node = collision_edge.node.unwrap();

        // Path 1: trace back from collision node.
        let path1 = self.trace_back_from_node(collision_node);

        // The collision edge itself.
        let other_opt = self.graph.nodes[collision_node.0 as usize].neighbors
            [collision_edge.neighbor_index];

        // Path 2: trace back from the other side of the collision edge.
        let mut path2 = vec![collision_edge];
        if let Some(other) = other_opt {
            let mut more = self.trace_back_from_node(other);
            path2.append(&mut more);
        }

        // Determine which path leads back to src.
        let last_of_path2 = {
            let last_edge = path2.last().unwrap();
            self.graph.nodes[last_edge.node.unwrap().0 as usize].neighbors
                [last_edge.neighbor_index]
        };

        let leads_to_src = last_of_path2 == Some(src_idx);

        if leads_to_src {
            // Reverse path2 (it goes collision->src, we want src->collision).
            self.emit_reversed(&path2, &mut callback);
            // Path1 goes collision->dst, emit in order.
            self.emit_forward(&path1, &mut callback);
        } else {
            // Reverse path1 (it goes collision->src, we want src->collision).
            self.emit_reversed(&path1, &mut callback);
            // Path2 goes collision->dst, emit in order.
            self.emit_forward(&path2, &mut callback);
        }

        self.reset();
    }

    /// Emit edges in forward order (node -> neighbor).
    fn emit_forward(
        &self,
        edges: &[SearchGraphEdge],
        callback: &mut impl FnMut(Option<SearchNodeIdx>, Option<SearchNodeIdx>, ObsMask),
    ) {
        for e in edges {
            let from = e.node;
            let node_i = e.node.unwrap().0 as usize;
            let to = self.graph.nodes[node_i].neighbors[e.neighbor_index];
            let obs = self.graph.nodes[node_i].neighbor_observables
                [e.neighbor_index];
            callback(from, to, obs);
        }
    }

    /// Emit edges in reversed order (neighbor -> node, traversed backwards).
    fn emit_reversed(
        &self,
        edges: &[SearchGraphEdge],
        callback: &mut impl FnMut(Option<SearchNodeIdx>, Option<SearchNodeIdx>, ObsMask),
    ) {
        for e in edges.iter().rev() {
            let node_i = e.node.unwrap().0 as usize;
            let nb_opt = self.graph.nodes[node_i].neighbors[e.neighbor_index];
            // Reversed: from = neighbor, to = node.
            let from = nb_opt;
            let to = e.node;
            // Find the reverse edge's observable.
            let obs = if let Some(nb_idx) = nb_opt {
                let reverse_i = self.graph.nodes[nb_idx.0 as usize]
                    .index_of_neighbor(e.node);
                self.graph.nodes[nb_idx.0 as usize].neighbor_observables
                    [reverse_i]
            } else {
                // Boundary edge -- use the same observable.
                self.graph.nodes[node_i].neighbor_observables
                    [e.neighbor_index]
            };
            callback(from, to, obs);
        }
    }

    /// Build a `CompressedEdge` for the shortest path between two nodes.
    pub fn find_shortest_path(
        &mut self,
        src: usize,
        dst: Option<usize>,
    ) -> CompressedEdge {
        let mut obs_mask: ObsMask = 0;
        self.iter_edges_on_shortest_path(src, dst, |_, _, obs| {
            obs_mask ^= obs;
        });
        CompressedEdge {
            loc_from: Some(NodeIdx(src as u32)),
            loc_to: dst.map(|d| NodeIdx(d as u32)),
            obs_mask,
        }
    }

    /// Reset the graph and queue for the next search.
    pub fn reset(&mut self) {
        for &idx in &self.reached_nodes {
            self.graph.nodes[idx.0 as usize].reset();
        }
        self.reached_nodes.clear();
        self.queue.reset();
        self.target_type = TargetType::NoTarget;
    }
}