forked from quantumgizmos/bp_osd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weighted_bp_osd_example.py
54 lines (44 loc) · 1.4 KB
/
weighted_bp_osd_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'''
Example demonstrating how to run bp+osd with a non-uniform initial error channel.
Before running, install `bposd' and `ldpc`
`pip install -U ldpc bposd`
'''
import numpy as np
from bposd.hgp import hgp
from bposd import bposd_decoder
from ldpc.codes import rep_code
''''
Distance 2 surface code
'''
#build surface code via hypergraph product
h=rep_code(2) #repetition code seed
surface_code = hgp(h,h) #build surface code
hz = surface_code.hz #z stabilisers parity check matrix
print("Distance-2 surface code hz parity check matrix")
print(hz)
'''
Example error
'''
m,n=hz.shape
error = np.zeros(n).astype(int)
error[[2]] = 1
syndrome = hz@error % 2
print("Error", error)
print("Syndrome", syndrome)
'''
Decoding with uniform initial error channel
'''
rho = 0.1*np.ones(n)
# rho[0]=0.0
bpd = bposd_decoder(hz, channel_probs=rho, bp_method="product_sum", osd_method="osd_e", osd_order=3, max_iter=5)
decoding = bpd.decode(syndrome)
print("Uniform error channel", rho)
print("Decoding (uniform initial error channel)", decoding)
'''
Decoding with non-uniform initial error channel
'''
rho[2]=0.05 #here I'm setting the probability on qubit 2 to be lower than the other qubits
bpd = bposd_decoder(hz, channel_probs=rho, bp_method="product_sum", osd_method="osd_e", osd_order=3, max_iter=5)
decoding = bpd.decode(syndrome)
print("Non-uniform error channel", rho)
print("Decoding (non-uniform initial error channel)", decoding)