quantumgizmos / ldpc

Software for decoding classical and quantum codes
MIT License
76 stars 27 forks source link

Python interpreter crashes from syndrome dtype mismatch #8

Closed oscarhiggott closed 2 years ago

oscarhiggott commented 2 years ago

Firstly, thank you for creating this useful package.

When running the following code:

from ldpc.codes import rep_code
from bposd.hgp import hgp
import numpy as np
from ldpc import bposd_decoder

h=rep_code(3)
surface_code=hgp(h1=h,h2=h,compute_distance=True) #nb. set compute_distance=False for larger codes

bpd=bposd_decoder(
    surface_code.hz,#the parity check matrix
    error_rate=0.05,
    channel_probs=[None], #assign error_rate to each qubit. This will override "error_rate" input variable
    max_iter=surface_code.N, #the maximum number of iterations for BP)
    bp_method="ms",
    ms_scaling_factor=0, #min sum scaling factor. If set to zero the variable scaling factor method is used
    osd_method="osd_cs", #the OSD method. Choose from:  1) "osd_e", "osd_cs", "osd0"
    osd_order=7 #the osd search depth
    )

error=np.zeros(surface_code.N).astype(int)
error[[5,12]]=1
syndrome=(surface_code.hz@error %2).astype(np.uint8)
bpd.decode(syndrome)

I get the following exception, along with a segmentation fault causing the python interpreter to crash:

ValueError: Buffer dtype mismatch, expected 'int_t' but got 'unsigned char'
Exception ignored in: 'ldpc.c_util.numpy2char'
Traceback (most recent call last):
  File "/Users/oscarhiggott/Desktop/bposd_example.py", line 23, in <module>
    bpd.decode(syndrome)
ValueError: Buffer dtype mismatch, expected 'int_t' but got 'unsigned char'
zsh: segmentation fault  python3.9 bposd_example.py

And in case it helps here's some of the crash log:

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   bp_decoder.cpython-39-darwin.so        0x10bf93990 __pyx_f_4ldpc_10bp_decoder_10bp_decoder_bp_decode_log_prob_ratios + 816
1   bp_decoder.cpython-39-darwin.so        0x10bf92cba __pyx_f_4ldpc_10bp_decoder_10bp_decoder_bp_decode_cy + 42
2   osd.cpython-39-darwin.so               0x11075f3a4 __pyx_f_4ldpc_3osd_13bposd_decoder_decode_cy + 20
3   osd.cpython-39-darwin.so               0x11075d90a __pyx_f_4ldpc_3osd_13bposd_decoder_decode + 2986
4   osd.cpython-39-darwin.so               0x11075f56e __pyx_pw_4ldpc_3osd_13bposd_decoder_3decode + 14
5   Python                                 0x10b7bbfed method_vectorcall_O + 94
6   Python                                 0x10b8a6f70 call_function + 168
7   Python                                 0x10b89f6cb _PyEval_EvalFrameDefault + 23803
8   Python                                 0x10b8988d6 _PyEval_EvalCode + 411
9   Python                                 0x10b8f3d8d run_eval_code_obj + 128
10  Python                                 0x10b8f3cdb run_mod + 96
11  Python                                 0x10b8f150a pyrun_file + 167
12  Python                                 0x10b8f0efb pyrun_simple_file + 271
13  Python                                 0x10b8f0dc6 PyRun_SimpleFileExFlags + 67
14  Python                                 0x10b910aae pymain_run_file + 326
15  Python                                 0x10b9102f2 Py_RunMain + 990
16  Python                                 0x10b9113b4 pymain_main + 35
17  Python                                 0x10b91168a Py_BytesMain + 42
18  dyld                                   0x202f764fe start + 462

The issue arises if in the line syndrome=(surface_code.hz@error %2).astype(np.uint8) if I use any numpy dtype other than int (e.g. I used np.uint8 here). It would be nice to support np.uint8 anyway, but regardless the interpreter shouldn't crash if the dtype is incorrect.

quantumgizmos commented 2 years ago

Hi Oscar. Thanks for pointing out this issue. It appears as though cython doesn't yet natively support np.uint8. I've changed the ldpc.c_util.numpy2char so that it now accepts a python numpy object (rather than the cython one). Fortunately, this doesn't seem to noticeably alter performance. Could you test the latest version in the dev branch. If all works, I'll push the fix to pypi.

oscarhiggott commented 2 years ago

Great just tested it and it works now, thanks!