Skip to content

Commit

Permalink
some additional fixes to stubfile generation to preserve blank lines …
Browse files Browse the repository at this point in the history
…in docstrings and reformat 'cimport' as 'import'. Fixes for #48
  • Loading branch information
quantumgizmos committed Nov 29, 2024
1 parent d2bbdbc commit e0fbfbd
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 10 deletions.
18 changes: 12 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ def generate_cython_stub_file(pyx_filepath: str, output_filepath: str) -> None:
# load file contents
pyx_content = open(pyx_filepath, "r").read()

# strip cython syntax, empty lines, and comments
# Replace 'cimport' with 'import'
pyx_content = re.sub(r'\bcimport\b', 'import', pyx_content)

# strip cython syntax and comments
pyx_content = re.sub("cdef ", "", pyx_content)
pyx_content = re.sub(r"^\s*\n", "", pyx_content, flags=re.MULTILINE)
pyx_content = re.sub(r"^\s*#.*\n", "", pyx_content, flags=re.MULTILINE)

# identify top-level import lines
# identify top-level import lines (including modified 'cimport' lines)
pattern = re.compile(r"^(import|from)\s+.*\n", re.MULTILINE)
for match in pattern.finditer(pyx_content):
pyi_content += pyx_content[match.start() : match.end()]
Expand All @@ -33,19 +35,23 @@ def generate_cython_stub_file(pyx_filepath: str, output_filepath: str) -> None:
docstring_double = r"\"\"\".*?\"\"\""
docstring_single = r"'''.*?'''"
docstring = rf"\s*?(?:{docstring_double}|{docstring_single})\s*?\n"
pattern = re.compile(rf"({decorator})?({declaration})({docstring})?", re.DOTALL | re.MULTILINE)
pattern = re.compile(
rf"({decorator})?({declaration})({docstring})?", re.DOTALL | re.MULTILINE
)
for match in pattern.finditer(pyx_content):
content = pyx_content[match.start() : match.end()]
if not ignore_pattern.match(content, re.MULTILINE):
pyi_content += content.rstrip() # strip trailing whitespace

# If there is a docstring, we only need to add a newline character. Otherwise, we also
# need to add ellipses as a placeholder for the class/method "body".
# If there is a docstring, we only need to add a newline character.
# Otherwise, we also need to add ellipses as a placeholder for the class/method "body".
suffix = "\n" if match.group(3) else " ...\n"
pyi_content += suffix

open(output_filepath, "w").write(pyi_content)



## BUILD

if sys.platform == "darwin":
Expand Down
20 changes: 20 additions & 0 deletions src_python/ldpc/belief_find_decoder/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import numpy as np
from scipy.sparse import spmatrix

class BeliefFindDecoder(BpDecoderBase):

"""
A class representing a decoder that combines Belief Propagation (BP) with the Union Find Decoder (UFD) algorithm.
The BeliefFindDecoder is designed to decode binary linear codes by initially attempting BP decoding, and if that fails,
it falls back to the Union Find Decoder algorithm. The UFD algorithm is based on the principles outlined in
https://arxiv.org/abs/1709.06218, with an option to utilise a more general version as described in
https://arxiv.org/abs/2103.08049 for LDPC codes by setting `uf_method=True`.
Parameters
----------
pcm : Union[np.ndarray, scipy.sparse.spmatrix]
Expand Down Expand Up @@ -37,29 +41,45 @@ class BeliefFindDecoder(BpDecoderBase):
The inversion method can be applied to any parity check matrix.
bits_per_step : int, optional
Specifies the number of bits added to the cluster in each step of the UFD algorithm. If no value is provided, this is set the block length of the code.
Notes
-----
The `BeliefFindDecoder` class leverages soft information outputted by the BP decoder to guide the cluster growth
in the UFD algorithm. The number of bits added to the cluster in each step is controlled by the `bits_per_step` parameter.
The `uf_method` parameter activates a more general version of the UFD algorithm suitable for LDPC codes when set to True.
"""

def __cinit__(self, pcm: Union[np.ndarray, scipy.sparse.spmatrix], error_rate: Optional[float] = None,
error_channel: Optional[List[float]] = None, max_iter: Optional[int] = 0, bp_method: Optional[str] = 'minimum_sum',
ms_scaling_factor: Optional[float] = 1.0, schedule: Optional[str] = 'parallel', omp_thread_count: Optional[int] = 1,
random_schedule_seed: Optional[int] = 0, serial_schedule_order: Optional[List[int]] = None, uf_method: str = "peeling", bits_per_step:int = 0, input_vector_type: str = "syndrome"): ...

def __del__(self): ...

def decode(self,syndrome):

"""
Decodes the input syndrome using the belief propagation and UFD decoding methods.
Initially, the method attempts to decode the syndrome using belief propagation. If this fails to converge,
it falls back to the UFD algorithm.
Parameters
----------
syndrome : np.ndarray
The input syndrome to decode.
Returns
-------
np.ndarray
The decoded output.
Raises
------
ValueError
If the length of the input syndrome is not equal to the length of the code.
"""


@property
def uf_method(self): ...
Loading

0 comments on commit e0fbfbd

Please sign in to comment.