use std::collections::HashMap;
use ndarray::{Array1, Array2, ArrayView2};
use pymatching_cxx::EdgeRecord as RawEdgeRecord;
#[derive(Debug, Clone, PartialEq)]
pub struct EdgeData {
pub fault_ids: Vec<usize>,
pub weight: f64,
pub error_probability: Option<f64>,
}
impl EdgeData {
#[must_use]
pub fn new(fault_ids: Vec<usize>) -> Self {
Self {
fault_ids,
weight: 1.0,
error_probability: None,
}
}
#[must_use]
pub fn with_weight(mut self, weight: f64) -> Self {
self.weight = weight;
self
}
#[must_use]
pub fn with_error_probability(mut self, error_probability: f64) -> Self {
self.error_probability = Some(error_probability);
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct EdgeRecord {
pub node1: usize,
pub node2: Option<usize>,
pub data: EdgeData,
}
impl EdgeRecord {
#[must_use]
pub fn new(node1: usize, node2: Option<usize>, data: EdgeData) -> Self {
Self { node1, node2, data }
}
#[must_use]
pub(crate) fn from_raw(record: RawEdgeRecord) -> Self {
Self {
node1: record.node1 as usize,
node2: (!record.is_boundary).then_some(record.node2 as usize),
data: EdgeData {
fault_ids: record.fault_ids.into_iter().map(|id| id as usize).collect(),
weight: record.weight,
error_probability: normalize_error_probability(record.error_probability),
},
}
}
}
fn normalize_error_probability(error_probability: f64) -> Option<f64> {
(0.0..=1.0)
.contains(&error_probability)
.then_some(error_probability)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MergeStrategy {
Disallow,
Independent,
SmallestWeight,
KeepOriginal,
Replace,
}
pub type MatchedDetectors = HashMap<usize, Option<usize>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DecodeOptions {
pub(crate) return_weight: bool,
pub(crate) bit_packed_shots: bool,
pub(crate) bit_packed_predictions: bool,
pub(crate) enable_correlations: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct GapPrediction {
pub prediction: Array1<u8>,
pub gaps: Array1<f64>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct GapBatchPrediction {
pub predictions: Array2<u8>,
pub gaps: Array2<f64>,
}
impl Default for DecodeOptions {
fn default() -> Self {
Self::new()
}
}
impl DecodeOptions {
#[must_use]
pub fn new() -> Self {
Self {
return_weight: false,
bit_packed_shots: false,
bit_packed_predictions: false,
enable_correlations: false,
}
}
#[must_use]
pub fn with_return_weight(mut self, return_weight: bool) -> Self {
self.return_weight = return_weight;
self
}
#[must_use]
pub fn with_bit_packed_shots(mut self, bit_packed_shots: bool) -> Self {
self.bit_packed_shots = bit_packed_shots;
self
}
#[must_use]
pub fn with_bit_packed_predictions(mut self, bit_packed_predictions: bool) -> Self {
self.bit_packed_predictions = bit_packed_predictions;
self
}
#[must_use]
pub fn with_enable_correlations(mut self, enable_correlations: bool) -> Self {
self.enable_correlations = enable_correlations;
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct DecodeResult {
pub prediction: Array1<u8>,
pub weight: Option<f64>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct BatchDecodeResult {
pub predictions: Array2<u8>,
pub weights: Option<Array1<f64>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct NoiseSample {
pub errors: Array1<u8>,
pub syndrome: Array1<u8>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CheckMatrixOptions {
pub(crate) weights: Option<Vec<f64>>,
pub(crate) error_probabilities: Option<Vec<f64>>,
pub(crate) faults_matrix: Option<StoredBinaryMatrix>,
pub(crate) merge_strategy: MergeStrategy,
pub(crate) repetitions: usize,
pub(crate) timelike_weights: Option<Vec<f64>>,
pub(crate) measurement_error_probabilities: Option<Vec<f64>>,
pub(crate) use_virtual_boundary_node: bool,
}
impl Default for CheckMatrixOptions {
fn default() -> Self {
Self::new()
}
}
impl CheckMatrixOptions {
#[must_use]
pub fn new() -> Self {
Self {
weights: None,
error_probabilities: None,
faults_matrix: None,
merge_strategy: MergeStrategy::SmallestWeight,
repetitions: 1,
timelike_weights: None,
measurement_error_probabilities: None,
use_virtual_boundary_node: false,
}
}
#[must_use]
pub fn with_weights(mut self, weights: Vec<f64>) -> Self {
self.weights = Some(weights);
self
}
#[must_use]
pub fn with_error_probabilities(mut self, error_probabilities: Vec<f64>) -> Self {
self.error_probabilities = Some(error_probabilities);
self
}
#[must_use]
pub fn with_faults_matrix(mut self, faults_matrix: ArrayView2<'_, u8>) -> Self {
self.faults_matrix = Some(StoredBinaryMatrix::from_array_view(faults_matrix));
self
}
#[must_use]
pub fn with_merge_strategy(mut self, merge_strategy: MergeStrategy) -> Self {
self.merge_strategy = merge_strategy;
self
}
#[must_use]
pub fn with_repetitions(mut self, repetitions: usize) -> Self {
self.repetitions = repetitions;
self
}
#[must_use]
pub fn with_timelike_weights(mut self, timelike_weights: Vec<f64>) -> Self {
self.timelike_weights = Some(timelike_weights);
self
}
#[must_use]
pub fn with_measurement_error_probabilities(
mut self,
measurement_error_probabilities: Vec<f64>,
) -> Self {
self.measurement_error_probabilities = Some(measurement_error_probabilities);
self
}
#[must_use]
pub fn with_virtual_boundary_node(mut self, use_virtual_boundary_node: bool) -> Self {
self.use_virtual_boundary_node = use_virtual_boundary_node;
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct StoredBinaryMatrix {
rows: usize,
cols: usize,
data: Vec<u8>,
}
impl StoredBinaryMatrix {
fn from_array_view(matrix: ArrayView2<'_, u8>) -> Self {
Self {
rows: matrix.nrows(),
cols: matrix.ncols(),
data: matrix.iter().copied().collect(),
}
}
pub(crate) fn nrows(&self) -> usize {
self.rows
}
pub(crate) fn ncols(&self) -> usize {
self.cols
}
pub(crate) fn get(&self, row: usize, col: usize) -> u8 {
self.data[row * self.cols + col]
}
}