Skip to content

Commit

Permalink
defualt numpy dtype change to 'np.uint8_t' from 'np.int_t'. Potential…
Browse files Browse the repository at this point in the history
… fix for #40. Alternative to lower bounding numpy version as suggested in #41. Needs testing
  • Loading branch information
quantumgizmos committed Jun 19, 2024
1 parent cb462f0 commit 2e16016
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/ldpc/bp_decoder.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ cdef class bp_decoder:
cdef int MEM_ALLOCATED
cdef int input_vector_type

cpdef np.ndarray[np.int_t, ndim=1] decode(self, input_vector)
cpdef np.ndarray[np.uint8_t, ndim=1] decode(self, input_vector)

cdef char* bp_decode_cy(self)

Expand Down
2 changes: 1 addition & 1 deletion src/ldpc/bp_decoder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ cdef class bp_decoder:
for j in range(self.n): self.channel_probs[j]=error_rate
self.error_rate=error_rate

cpdef np.ndarray[np.int_t, ndim=1] decode(self, input_vector):
cpdef np.ndarray[np.uint8_t, ndim=1] decode(self, input_vector):

"""
Runs the BP decoder for a given input_vector.
Expand Down
8 changes: 4 additions & 4 deletions src/ldpc/c_util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import numpy as np
cimport numpy as np
cimport cython

# cdef char* numpy2char(np.ndarray[np.int_t, ndim=1] np_array,char* char_array)
# cdef char* numpy2char(np.ndarray[np.uint8_t, ndim=1] np_array,char* char_array)
cdef char* numpy2char(np_array,char* char_array)
cdef char* spmatrix2char(matrix,char* char_array)
cdef double* numpy2double(np.ndarray[np.float_t, ndim=1] np_array,double* double_array)
cdef np.ndarray[np.int_t, ndim=1] char2numpy(char* char_array, int n)
cdef np.ndarray[np.float_t, ndim=1] double2numpy(double* char_array, int n)
cdef double* numpy2double(np.ndarray[dtype = np.float_t, ndim=1] np_array,double* double_array)
cdef np.ndarray[dtype = np.uint8_t, ndim=1] char2numpy(char* char_array, int n)
cdef np.ndarray[dtype = np.float_t, ndim=1] double2numpy(double* char_array, int n)


cdef extern from "binary_char.h":
Expand Down
8 changes: 4 additions & 4 deletions src/ldpc/c_util.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
from scipy.sparse import spmatrix

# cdef char* numpy2char(np.ndarray[np.int_t, ndim=1] np_array,char* char_array):
# cdef char* numpy2char(np.ndarray[np.uint8_t, ndim=1] np_array,char* char_array):

# cdef int n = np_array.shape[0]
# for i in range(n): char_array[i]=np_array[i]
Expand All @@ -30,15 +30,15 @@ cdef double* numpy2double(np.ndarray[np.float_t, ndim=1] np_array,double* double
for i in range(n): double_array[i]=np_array[i]
return double_array

cdef np.ndarray[np.int_t, ndim=1] char2numpy(char* char_array, int n):
cdef np.ndarray[np.uint8_t, ndim=1] char2numpy(char* char_array, int n):

cdef np.ndarray[np.int_t, ndim=1] np_array=np.zeros(n).astype(int)
cdef np.ndarray[np.uint8_t, ndim=1] np_array=np.zeros(n).astype(np.uint8)
for i in range(n):np_array[i]=char_array[i]
return np_array

cdef np.ndarray[np.float_t, ndim=1] double2numpy(double* char_array, int n):

cdef np.ndarray[np.float_t, ndim=1] np_array=np.zeros(n)
cdef np.ndarray[np.float_t, ndim=1] np_array=np.zeros(n, dtype=np.float64)
for i in range(n):np_array[i]=char_array[i]
return np_array

2 changes: 1 addition & 1 deletion src/ldpc/experimental/bp_decoder2.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cdef class bp_decoder:
cdef double ms_scaling_factor
cdef int MEM_ALLOCATED

cpdef np.ndarray[np.int_t, ndim=1] bp_decode(self, np.ndarray[np.int_t, ndim=1] syndrome)
cpdef np.ndarray[np.uint8_t, ndim=1] bp_decode(self, np.ndarray[np.uint8_t, ndim=1] syndrome)

cdef char* bp_decode_cy(self)

Expand Down
2 changes: 1 addition & 1 deletion src/ldpc/experimental/bp_decoder2.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ cdef class bp_decoder:
else:
print("Decoder not called")

cpdef np.ndarray[np.int_t, ndim=1] bp_decode(self, np.ndarray[np.int_t, ndim=1] syndrome):
cpdef np.ndarray[np.uint8_t, ndim=1] bp_decode(self, np.ndarray[np.uint8_t, ndim=1] syndrome):
self.synd=numpy2char(syndrome,self.synd)
self.bp_decode_cy()
return char2numpy(self.bp_decoding,self.n)
Expand Down
2 changes: 1 addition & 1 deletion src/ldpc/experimental/pymod2sparse.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ cdef class pymod2sparse():

cpdef iter_row(self,int row_index,int reverse_iterate=False)
cpdef iter_col(self,int col_index,int reverse_iterate=False)
cpdef np.ndarray[np.int_t, ndim=1] mul(self, np.ndarray[np.int_t, ndim=1] vector)
cpdef np.ndarray[np.uint8_t, ndim=1] mul(self, np.ndarray[np.uint8_t, ndim=1] vector)

2 changes: 1 addition & 1 deletion src/ldpc/experimental/pymod2sparse.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ cdef class pymod2sparse():

return self

cpdef np.ndarray[np.int_t, ndim=1] mul(self, np.ndarray[np.int_t, ndim=1] vector):
cpdef np.ndarray[np.uint8_t, ndim=1] mul(self, np.ndarray[np.uint8_t, ndim=1] vector):

if len(vector)!=self.n:
raise ValueError(f'Dimension mismatch. The supplied vector of length {len(vector)} cannot be multiplied by a matrix with dimesnions ({self.m},{self.n})!')
Expand Down
2 changes: 1 addition & 1 deletion src/ldpc/mod2sparse.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ cdef class pymod2sparse():

cpdef iter_row(self,int row_index,int reverse_iterate)
cpdef iter_col(self,int col_index,int reverse_iterate)
cpdef np.ndarray[np.int_t, ndim=1] mul(self, np.ndarray[np.int_t, ndim=1] vector)
cpdef np.ndarray[np.uint8_t, ndim=1] mul(self, np.ndarray[np.uint8_t, ndim=1] vector)


2 changes: 1 addition & 1 deletion src/ldpc/mod2sparse.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ cdef class pymod2sparse():

return self

cpdef np.ndarray[np.int_t, ndim=1] mul(self, np.ndarray[np.int_t, ndim=1] vector):
cpdef np.ndarray[np.uint8_t, ndim=1] mul(self, np.ndarray[np.uint8_t, ndim=1] vector):

if len(vector)!=self.n:
raise ValueError(f'Dimension mismatch. The supplied vector of length {len(vector)} cannot be multiplied by a matrix with dimesnions ({self.m},{self.n})!')
Expand Down
2 changes: 1 addition & 1 deletion src/ldpc/osd.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ cdef class bposd_decoder(bp_decoder):
return self.osdw_decoding


cpdef np.ndarray[np.int_t, ndim=1] decode(self, input_vector):
cpdef np.ndarray[np.uint8_t, ndim=1] decode(self, input_vector):

"""
Runs the BP+OSD decoder for a given syndrome.
Expand Down
15 changes: 15 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import numpy as np
from ldpc.codes import rep_code
from ldpc.bp_decoder import bp_decoder

H = rep_code(3)
print(H)

bpd = bp_decoder(H, error_rate = 0.1, max_iter=0, bp_method='ps', ms_scaling_factor=0.625)

syndrome = np.array([1,0])

decoding = bpd.decode(syndrome)
print(decoding)
print(bpd.bp_decoding)
print(bpd.log_prob_ratios)

0 comments on commit 2e16016

Please sign in to comment.