Skip to content

Commit

Permalink
implement example sinter bposd decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-janderks committed Dec 20, 2023
1 parent 56f3d64 commit 344c921
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ A C++ rewrite of the `LDPC` package for decoding low density parity check checks
- Belief-find with inversion solving (first implementation I am aware of).
- An implementation of the Kuo and Lai memory belief propagation decoder (https://arxiv.org/abs/2104.13659)
- Flip and P-flip decoders (https://aps.arxiv.org/abs/2212.06985)
- STIM integration for circuit level noise.

## ToDos

Expand All @@ -22,7 +23,6 @@ A C++ rewrite of the `LDPC` package for decoding low density parity check checks
- Functions need to be properly documented (in progress)
- Proper test coverage is required (C++ has 100%, Python tests still need to expanded).
- The Peeling version of union-find only works for the Toric code. A routine for matching to the boundary needs to be implemented.
- STIM integration for circuit level noise.

## Python - Installation from source

Expand Down
67 changes: 67 additions & 0 deletions examples/sinter_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import sinter
from ldpc.sinter_decoders import SinterBpOsdDecoder
import stim
from matplotlib import pyplot as plt
import numpy as np

def generate_example_tasks():
for p in np.arange(0.001,0.01,0.001):
for d in [5,7,9]:
sc_circuit = stim.Circuit.generated(
rounds=d,
distance=d,
after_clifford_depolarization=p,
after_reset_flip_probability=p,
before_measure_flip_probability=p,
before_round_data_depolarization=p,
code_task=f'surface_code:rotated_memory_z',
)
yield sinter.Task(
circuit=sc_circuit,
json_metadata={
'p': p,
'd': d,
'rounds':d,
},
)

def main():
samples = sinter.collect(
num_workers=10,
max_shots=10_000,
max_errors=200,
tasks=generate_example_tasks(),
decoders=['bposd' ],
custom_decoders={'bposd': SinterBpOsdDecoder()},
print_progress=True,
save_resume_filepath=f'bposd_surface_code.csv',
)


# Print samples as CSV data.
print(sinter.CSV_HEADER)
for sample in samples:
print(sample.to_csv_line())

# Render a matplotlib plot of the data.
fig, ax = plt.subplots(1, 1)
sinter.plot_error_rate(
ax=ax,
stats=samples,
group_func=lambda stat: f"Rotated Surface Code d={stat.json_metadata['d']} dec={stat.decoder}",
x_func=lambda stat: stat.json_metadata['p'],
)
ax.loglog()
ax.grid()
ax.set_title('Logical Error Rate vs Physical Error Rate')
ax.set_ylabel('Logical Error Probability (per shot)')
ax.set_xlabel('Physical Error Rate')
ax.legend()

# Save to file and also open in a window.
fig.savefig('plot.png')
plt.show()


if __name__ == '__main__':
main()
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ dependencies = [
"scipy>=1.9.3",
"tqdm",
"pytest"
"stim"
"sinter"
]
version = "2.0.12"

Expand Down
14 changes: 6 additions & 8 deletions src_python/ldpc/sinter_decoders/sinter_bposd_decoder.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import stim
import numpy as np
import pathlib
from BeliefMatching import detector_error_model_to_check_matrices
from ldpc.sinter_decoders import SinterDecoder
from beliefmatching import detector_error_model_to_check_matrices
from ldpc.bposd_decoder import BpOsdDecoder
from sinter import Decoder


class SinterBpOsdDecoder(SinterDecoder):
class SinterBpOsdDecoder(Decoder):
def __init__(self,
bp_method="ms",
ms_scaling_factor=0.625,
Expand All @@ -30,7 +29,7 @@ def decode_via_files(
) -> None:
self.dem = stim.DetectorErrorModel.from_file(dem_path)
self.matrices = detector_error_model_to_check_matrices(self.dem)
self.bposd = BpOsdDecoder(self.matrices.check_matrix, error_channel=self.matrices.priors, max_iter=5, bp_method="ms", ms_scaling_factor = 0.625, schedule="parallel", osd_method = "osd0")
self.bposd = BpOsdDecoder(self.matrices.check_matrix, error_channel=list(self.matrices.priors), max_iter=5, bp_method="ms", ms_scaling_factor = 0.625, schedule="parallel", osd_method = "osd0")

shots = stim.read_shot_data_file(
path=dets_b8_in_path, format="b8", num_detectors=self.dem.num_detectors
Expand All @@ -48,6 +47,5 @@ def decode_via_files(


def decode(self, syndrome: np.ndarray) -> np.ndarray:
corr = self.matrices.decode(syndrome)
if self._bpd.converge:
return (self.matrices.observables_matrix @ corr) % 2
corr = self.bposd.decode(syndrome)
return (self.matrices.observables_matrix @ corr) % 2

0 comments on commit 344c921

Please sign in to comment.