Dist m4ri 0.0.1.alpha
Computing distance of a classical or quantum CSS code
Loading...
Searching...
No Matches
Classes | Functions | Variables
util_io.c File Reference
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include "util_io.h"
Include dependency graph for util_io.c:

Go to the source code of this file.

Classes

struct  dem_program_t
 

Functions

void var_init (int argc, char **argv, params_t *const p)
 Initialize parameters and load matrices from command line arguments.
 
void var_kill (params_t *const p)
 Clean up and free memory allocated in the params_t structure.
 
void read_dem_file (char *fnam, csr_t **p_spaH, csr_t **p_spaL, double pmin, int debug)
 Read a Detector Error Model (DEM) file and construct H and L matrices.
 
FILE * nzlist_w_new (const char fnam[], const char comment[])
 
int nzlist_w_append (FILE *f, const cw_vec_t *const vec)
 
FILE * nzlist_r_open (const char fnam[], long long int *lineno)
 
cw_vec_tnzlist_r_one (FILE *f, cw_vec_t *vec, const char fnam[], long long int *lineno)
 
cw_vec_tcodeword_add_maybe (params_t *const p, const int arr[], int weight)
 Add a candidate codeword to the hash table if it meets weight limits.
 
long long int nzlist_read (const char fnam[], params_t *p)
 Read codewords from a .nz list file and add them to the codeword hash.
 
long long int nzlist_write (const char fnam[], const char comment[], params_t *p)
 Write the found codewords from the hash table to a .nz file.
 

Variables

params_t prm
 
params_t *const p = &prm
 

Function Documentation

◆ codeword_add_maybe()

cw_vec_t * codeword_add_maybe ( params_t *const  p,
const int  arr[],
int  weight 
)

Add a candidate codeword to the hash table if it meets weight limits.

Compares the candidate codeword weight with the current minimum weight and dW limit. If it is within the limits, it is added to the hash. If a new strictly smaller minimum weight is found, it updates the global minimum weight and prunes heavier codewords from the hash.

Parameters
pPointer to the params_t structure.
arrArray of indices representing the support of the codeword.
weightWeight of the codeword (length of arr).
Returns
Pointer to the added/existing codeword structure, or NULL if not added.

Definition at line 797 of file util_io.c.

References CW_VEC_T::arr, CW_VEC_T::cnt, params_t::codewords, params_t::dW, ERROR, HASH_ADD, HASH_DEL, HASH_FIND, HASH_ITER, params_t::maxC, params_t::min_w, params_t::num_cws, p, and CW_VEC_T::weight.

Referenced by do_CC_dist(), do_RW_dist(), nzlist_read(), and start_CC_recurs().

◆ nzlist_r_one()

cw_vec_t * nzlist_r_one ( FILE *  f,
cw_vec_t vec,
const char  fnam[],
long long int *  lineno 
)

Definition at line 744 of file util_io.c.

References CW_VEC_T::arr, CW_VEC_T::cnt, ERROR, and CW_VEC_T::weight.

Referenced by nzlist_read().

◆ nzlist_r_open()

FILE * nzlist_r_open ( const char  fnam[],
long long int *  lineno 
)

Definition at line 724 of file util_io.c.

Referenced by nzlist_read().

◆ nzlist_read()

long long int nzlist_read ( const char  fnam[],
params_t p 
)

Read codewords from a .nz list file and add them to the codeword hash.

Reads the file, verifies that each codeword satisfies the code requirements (orthogonal to H, not orthogonal to L for quantum codes), and adds valid ones to the hash table in params_t.

Parameters
fnamPath to the .nz file.
pPointer to the params_t structure containing the code matrices and hash.
Returns
Number of valid codewords successfully read and added, or -1 on error.

Definition at line 840 of file util_io.c.

References CW_VEC_T::arr, codeword_add_maybe(), params_t::codewords, params_t::debug, ERROR, params_t::maxC, params_t::num_cws, nzlist_r_one(), nzlist_r_open(), params_t::outC, p, params_t::spaH, params_t::spaL, CW_VEC_T::weight, and params_t::wmax.

Referenced by main().

◆ nzlist_w_append()

int nzlist_w_append ( FILE *  f,
const cw_vec_t *const  vec 
)

Definition at line 712 of file util_io.c.

References CW_VEC_T::arr, ERROR, and CW_VEC_T::weight.

Referenced by nzlist_write().

◆ nzlist_w_new()

FILE * nzlist_w_new ( const char  fnam[],
const char  comment[] 
)

Definition at line 700 of file util_io.c.

References ERROR.

Referenced by nzlist_write().

◆ nzlist_write()

long long int nzlist_write ( const char  fnam[],
const char  comment[],
params_t p 
)

Write the found codewords from the hash table to a .nz file.

Exports all codewords currently stored in the hash table to a file in NZLIST format.

Parameters
fnamPath to the output .nz file.
commentAn optional comment string to include in the file header.
pPointer to the params_t structure containing the codeword hash.
Returns
Number of codewords written, or -1 on error.

Definition at line 893 of file util_io.c.

References params_t::codewords, CW_VEC_T::hh, UT_hash_handle::next, nzlist_w_append(), nzlist_w_new(), and p.

Referenced by main().

◆ read_dem_file()

void read_dem_file ( char *  fnam,
csr_t **  p_spaH,
csr_t **  p_spaL,
double  pmin,
int  debug 
)

Read a Detector Error Model (DEM) file and construct H and L matrices.

Parses a DEM file (e.g. from Stim), filters error events based on pmin, and builds the corresponding sparse check matrix H and logical matrix L.

Parameters
fnamPath to the DEM file.
p_spaHPointer to store the constructed sparse check matrix H.
p_spaLPointer to store the constructed sparse logical matrix L.
pminMinimum error probability threshold to keep an error event.
debugDebug print level bitmap.

Definition at line 663 of file util_io.c.

References csr_from_pairs(), and ERROR.

Referenced by var_init().

◆ var_init()

void var_init ( int  argc,
char **  argv,
params_t *const  p 
)

Initialize parameters and load matrices from command line arguments.

Parses command line arguments, sets up the parameter structure, loads matrices from specified files (Matrix Market or DEM), constructs logical matrices if needed, and performs consistency checks.

Parameters
argcNumber of command line arguments.
argvArray of command line argument strings.
pPointer to the params_t structure to initialize.

debug

finH

< allow space before file name

finL

< allow space before file name

finG degeneracy generator matrix

< allow space before file name

create Lx

WARNING: this does not necessarily have minimal row weights

Definition at line 54 of file util_io.c.

References BRIEF_HELP, params_t::cbeg, params_t::cend, params_t::classical, csr_t::cols, csr_csr_mul_non_zero(), csr_free(), csr_mm_read(), csr_print(), params_t::css, params_t::debug, params_t::dexp, params_t::dmax, params_t::dmin, params_t::dW, ERROR, params_t::fdem, params_t::fin, params_t::finC, params_t::finG, params_t::finH, params_t::finL, Lx_for_CSS_code(), MAX_W, params_t::maxC, params_t::method, params_t::n0, params_t::nchk, params_t::noscan, params_t::nvar, params_t::outC, p, params_t::pmin, read_dem_file(), csr_t::rows, params_t::seed, params_t::smax, params_t::spaG, params_t::spaH, params_t::spaL, params_t::start, params_t::steps, params_t::swei, params_t::threads, params_t::timeout, USAGE, params_t::wmax, and params_t::wmin.

Referenced by main().

◆ var_kill()

void var_kill ( params_t *const  p)

Clean up and free memory allocated in the params_t structure.

Frees sparse matrices (spaH, spaG, spaL) and codeword lists.

Parameters
pPointer to the params_t structure to clean up.

Definition at line 473 of file util_io.c.

References params_t::codewords, csr_free(), params_t::fin, params_t::finG, params_t::finH, HASH_DEL, HASH_ITER, p, params_t::spaG, params_t::spaH, and params_t::spaL.

Referenced by main().

Variable Documentation

◆ p

params_t* const p = &prm

◆ prm

params_t prm

Definition at line 7 of file util_io.c.

Referenced by main(), and mzd_generator_from_csr().