Skip to content

Commit 4fef739

Browse files
committed
clean
1 parent 767e174 commit 4fef739

File tree

16 files changed

+333
-58
lines changed

16 files changed

+333
-58
lines changed
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
11
//! CXX FFI bridge for Chromobius decoder
2+
//!
3+
//! This module provides the low-level FFI bindings to the Chromobius C++ library.
4+
//! Users should prefer the high-level [`ChromobiusDecoder`](crate::ChromobiusDecoder) API.
25
36
#[cxx::bridge]
4-
pub mod ffi {
7+
pub(crate) mod ffi {
58
unsafe extern "C++" {
69
include!("chromobius_bridge.h");
710

811
type ChromobiusDecoderWrapper;
912

13+
/// Create a Chromobius decoder from a detector error model string.
14+
///
15+
/// # Errors
16+
///
17+
/// Returns a CXX exception if the DEM string is malformed or contains
18+
/// unsupported error mechanisms.
1019
fn create_chromobius_decoder(
1120
dem_string: &str,
1221
drop_mobius_errors_involving_remnant_errors: bool,
1322
) -> Result<UniquePtr<ChromobiusDecoderWrapper>>;
1423

24+
/// Decode bit-packed detection events and return the observables mask.
25+
///
26+
/// # Errors
27+
///
28+
/// Returns a CXX exception if decoding fails.
1529
fn decode_detection_events(
1630
decoder: Pin<&mut ChromobiusDecoderWrapper>,
1731
bit_packed_detection_events: &[u8],
1832
) -> Result<u64>;
1933

34+
/// Decode bit-packed detection events, returning observables mask and weight.
35+
///
36+
/// # Errors
37+
///
38+
/// Returns a CXX exception if decoding fails.
2039
fn decode_detection_events_with_weight(
2140
decoder: Pin<&mut ChromobiusDecoderWrapper>,
2241
bit_packed_detection_events: &[u8],
2342
weight_out: &mut f32,
2443
) -> Result<u64>;
2544

45+
/// Get the number of detectors in the error model.
2646
fn chromobius_get_num_detectors(decoder: &ChromobiusDecoderWrapper) -> usize;
47+
48+
/// Get the number of observables in the error model.
2749
fn chromobius_get_num_observables(decoder: &ChromobiusDecoderWrapper) -> usize;
2850
}
2951
}

crates/pecos-chromobius/src/decoder.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ impl ChromobiusDecoder {
119119
/// # }
120120
/// # example().unwrap();
121121
/// ```
122+
///
123+
/// # Errors
124+
///
125+
/// Returns [`ChromobiusError::InitializationFailed`] if:
126+
/// - The DEM string is malformed
127+
/// - The DEM contains unsupported error mechanisms
128+
/// - Memory allocation fails
122129
pub fn new(dem_string: &str, config: ChromobiusConfig) -> Result<Self, ChromobiusError> {
123130
let inner = ffi::create_chromobius_decoder(
124131
dem_string,
@@ -143,6 +150,10 @@ impl ChromobiusDecoder {
143150
///
144151
/// # Returns
145152
/// The decoded observables mask
153+
///
154+
/// # Errors
155+
///
156+
/// Returns [`ChromobiusError::DecodingFailed`] if decoding fails.
146157
pub fn decode_detection_events(
147158
&mut self,
148159
detection_events: &[u8],
@@ -163,6 +174,10 @@ impl ChromobiusDecoder {
163174
///
164175
/// # Returns
165176
/// The decoded observables mask and weight
177+
///
178+
/// # Errors
179+
///
180+
/// Returns [`ChromobiusError::DecodingFailed`] if decoding fails.
166181
pub fn decode_detection_events_with_weight(
167182
&mut self,
168183
detection_events: &[u8],

crates/pecos-chromobius/src/lib.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
//! Chromobius color code decoder for PECOS
2-
3-
// Internal crate - don't require exhaustive docs
4-
#![allow(
5-
clippy::missing_errors_doc,
6-
clippy::missing_panics_doc,
7-
clippy::must_use_candidate
8-
)]
2+
//!
3+
//! This crate provides Rust bindings for the Chromobius decoder, which is designed
4+
//! for decoding color codes in quantum error correction. Chromobius uses a Mobius
5+
//! matching approach to efficiently decode color code syndromes.
96
107
pub mod bridge;
118
pub mod decoder;

crates/pecos-fusion-blossom/examples/fusion_blossom_usage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
226226
max_tree_size: None,
227227
};
228228

229-
let mut decoder_legacy = FusionBlossomDecoder::new(config.clone())?;
229+
let mut decoder_legacy = FusionBlossomDecoder::new(config)?;
230230
config.solver_type = SolverType::Serial;
231231
let mut decoder_serial = FusionBlossomDecoder::new(config)?;
232232

crates/pecos-fusion-blossom/src/decoder.rs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub enum SolverType {
2424
}
2525

2626
/// Configuration for Fusion Blossom decoder
27-
#[derive(Debug, Clone, PartialEq, Eq)]
27+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2828
pub struct FusionBlossomConfig {
2929
/// Number of nodes in the graph
3030
pub num_nodes: Option<usize>,
@@ -48,7 +48,7 @@ impl Default for FusionBlossomConfig {
4848
}
4949

5050
/// Options for decoding
51-
#[derive(Debug, Clone, Default)]
51+
#[derive(Debug, Clone, Copy, Default)]
5252
pub struct DecodingOptions {
5353
/// Whether to include perfect matching details in the result
5454
pub include_perfect_matching: bool,
@@ -122,7 +122,7 @@ impl fmt::Display for DecodingResult {
122122
}
123123

124124
/// Standard QEC code types
125-
#[derive(Debug, Clone)]
125+
#[derive(Debug, Clone, Copy)]
126126
pub enum StandardCode {
127127
/// Code capacity planar code
128128
CodeCapacityPlanar {
@@ -202,6 +202,10 @@ pub struct FusionBlossomDecoder {
202202

203203
impl FusionBlossomDecoder {
204204
/// Create a new decoder with the given configuration
205+
///
206+
/// # Errors
207+
///
208+
/// Returns [`FusionBlossomError::Configuration`] if `num_nodes` is not specified in the config.
205209
pub fn new(config: FusionBlossomConfig) -> Result<Self> {
206210
let num_nodes = config.num_nodes.ok_or_else(|| {
207211
FusionBlossomError::Configuration("num_nodes must be specified".to_string())
@@ -220,6 +224,11 @@ impl FusionBlossomDecoder {
220224
}
221225

222226
/// Create decoder from a standard QEC code
227+
///
228+
/// # Errors
229+
///
230+
/// This function currently does not return errors, but returns `Result` for API
231+
/// consistency and future extensibility.
223232
pub fn from_standard_code(code: StandardCode, config: FusionBlossomConfig) -> Result<Self> {
224233
let example_code: Box<dyn ExampleCode> = match code {
225234
StandardCode::CodeCapacityPlanar {
@@ -318,6 +327,12 @@ impl FusionBlossomDecoder {
318327
}
319328

320329
/// Add an edge to the graph
330+
///
331+
/// # Errors
332+
///
333+
/// Returns [`FusionBlossomError::InvalidGraph`] if:
334+
/// - Either node index is out of bounds
335+
/// - The weight is negative
321336
pub fn add_edge(
322337
&mut self,
323338
node1: usize,
@@ -358,6 +373,18 @@ impl FusionBlossomDecoder {
358373
}
359374

360375
/// Add a boundary edge (connects a node to the boundary)
376+
///
377+
/// # Errors
378+
///
379+
/// Returns [`FusionBlossomError::InvalidGraph`] if:
380+
/// - The node index is out of bounds
381+
/// - The weight is negative
382+
///
383+
/// # Panics
384+
///
385+
/// This function will not panic. The internal `unwrap()` is safe because
386+
/// `boundary_node` is always set before use (either already `Some` or set
387+
/// in the same code path).
361388
pub fn add_boundary_edge(
362389
&mut self,
363390
node: usize,
@@ -404,6 +431,13 @@ impl FusionBlossomDecoder {
404431
}
405432

406433
/// Create decoder from a check matrix
434+
///
435+
/// # Errors
436+
///
437+
/// Returns an error if:
438+
/// - [`FusionBlossomError::Configuration`] if `num_nodes` cannot be set
439+
/// - [`FusionBlossomError::InvalidCheckMatrix`] if a column has more than 2 non-zero entries
440+
/// - [`FusionBlossomError::InvalidGraph`] if edge addition fails
407441
pub fn from_check_matrix(
408442
check_matrix: &Array2<u8>,
409443
weights: Option<&[f64]>,
@@ -498,6 +532,11 @@ impl FusionBlossomDecoder {
498532
}
499533

500534
/// Decode a syndrome with advanced options and decoding options
535+
///
536+
/// # Errors
537+
///
538+
/// This function currently does not return errors, but returns `Result` for API
539+
/// consistency and future extensibility.
501540
pub fn decode_with_options(
502541
&mut self,
503542
syndrome_data: SyndromeData,
@@ -601,11 +640,20 @@ impl FusionBlossomDecoder {
601640
}
602641

603642
/// Decode a syndrome with advanced options (backwards compatibility)
643+
///
644+
/// # Errors
645+
///
646+
/// Returns the same errors as [`Self::decode_with_options`].
604647
pub fn decode_advanced(&mut self, syndrome_data: SyndromeData) -> Result<DecodingResult> {
605648
self.decode_with_options(syndrome_data, DecodingOptions::default())
606649
}
607650

608651
/// Decode a syndrome (simple interface)
652+
///
653+
/// # Errors
654+
///
655+
/// Returns [`FusionBlossomError::InvalidSyndrome`] if the syndrome length doesn't
656+
/// match the number of nodes in the decoder.
609657
pub fn decode(&mut self, syndrome: &ArrayView1<u8>) -> Result<DecodingResult> {
610658
if syndrome.len() != self.num_nodes {
611659
return Err(FusionBlossomError::InvalidSyndrome(format!(

crates/pecos-fusion-blossom/src/lib.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@
99
clippy::cast_precision_loss,
1010
clippy::cast_sign_loss
1111
)]
12-
// Internal crate - don't require exhaustive docs
13-
#![allow(
14-
clippy::missing_errors_doc,
15-
clippy::missing_panics_doc,
16-
clippy::must_use_candidate,
17-
clippy::needless_pass_by_value
18-
)]
1912

2013
pub mod core_traits;
2114
pub mod decoder;

0 commit comments

Comments
 (0)