Skip to content

Commit

Permalink
first try
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasberent committed Feb 8, 2024
1 parent fe9cf34 commit eed95ef
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 55 deletions.
5 changes: 4 additions & 1 deletion cpp_test/TestLsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,14 @@ TEST(LsdDecoder, test_cluster_stats) {
auto lsd = LsdDecoder(pcm);
lsd.set_do_stats(true);
auto syndrome = std::vector<uint8_t>({1, 1, 0, 0, 0});

lsd.setLsdMethod(ldpc::osd::OsdMethod::EXHAUSTIVE);
auto decoding = lsd.lsd_decode(syndrome, bp.log_prob_ratios, 1, true, 0);

auto stats = lsd.statistics;
std::cout << stats.toString() << std::endl;
ASSERT_TRUE(lsd.get_do_stats());
ASSERT_TRUE(stats.lsd_method = ldpc::osd::OsdMethod::EXHAUSTIVE);
ASSERT_TRUE(stats.lsd_order == 0);
// check that there is one timestep with two entries in the statistics
ASSERT_TRUE(stats.individual_cluster_stats.size() == 2);
ASSERT_TRUE(stats.global_timestep_bit_history.size() == 1);
Expand Down
54 changes: 27 additions & 27 deletions python_test/test_qcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,35 +74,35 @@ def test_400_16_6_hgp():
print("...................................................")
print()

decoder = BpOsdDecoder(hx, error_rate=error_rate, max_iter=max_iter, bp_method="ms", ms_scaling_factor = 0.625, schedule="parallel", osd_method = "osd0")
ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder, "Min-sum osd-0 parallel schedule")

decoder = BpOsdDecoder(hx, error_rate=error_rate, max_iter=max_iter, bp_method="ms", ms_scaling_factor=0.625,
schedule="parallel", osd_method="osd_cs", osd_order=osd_order)
ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,
f"Min-sum osd-cs-{osd_order} parallel schedule")

decoder = BpOsdDecoder(hx, error_rate=error_rate, max_iter=max_iter, bp_method="ms", ms_scaling_factor=0.625,
schedule="parallel", osd_method="osd_e", osd_order=osd_order)
ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,
f"Min-sum osd-e-{osd_order} parallel schedule")

decoder = BpOsdDecoder(hx, error_rate=error_rate, max_iter=max_iter, bp_method="ms", ms_scaling_factor = 0.625, schedule="serial", osd_method = "osd0")
ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,"Min-sum osd-0 serial schedule")

decoder = BpOsdDecoder(hx, error_rate=error_rate, max_iter=max_iter, bp_method="ps", schedule="parallel", osd_method = "osd0")
ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,"Prod-sum osd-0 parallel schedule")

decoder = BeliefFindDecoder(hx, error_rate=error_rate, max_iter=max_iter, bp_method="ms", ms_scaling_factor=0.625, schedule="parallel", uf_method="inversion", bits_per_step=1)
ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,"Belief-find parallel schedule")

decoder = BpLsdDecoder(hx, error_rate=error_rate, max_iter=max_iter, bp_method="ms", ms_scaling_factor=0.625,
schedule="parallel", bits_per_step=1, lsd_order=0)
ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,
f"Min-sum LSD-0 parallel schedule")
# decoder = BpOsdDecoder(hx, error_rate=error_rate, max_iter=max_iter, bp_method="ms", ms_scaling_factor = 0.625, schedule="parallel", osd_method = "osd0")
# ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder, "Min-sum osd-0 parallel schedule")
#
# decoder = BpOsdDecoder(hx, error_rate=error_rate, max_iter=max_iter, bp_method="ms", ms_scaling_factor=0.625,
# schedule="parallel", osd_method="osd_cs", osd_order=osd_order)
# ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,
# f"Min-sum osd-cs-{osd_order} parallel schedule")
#
# decoder = BpOsdDecoder(hx, error_rate=error_rate, max_iter=max_iter, bp_method="ms", ms_scaling_factor=0.625,
# schedule="parallel", osd_method="osd_e", osd_order=osd_order)
# ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,
# f"Min-sum osd-e-{osd_order} parallel schedule")
#
# decoder = BpOsdDecoder(hx, error_rate=error_rate, max_iter=max_iter, bp_method="ms", ms_scaling_factor = 0.625, schedule="serial", osd_method = "osd0")
# ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,"Min-sum osd-0 serial schedule")
#
# decoder = BpOsdDecoder(hx, error_rate=error_rate, max_iter=max_iter, bp_method="ps", schedule="parallel", osd_method = "osd0")
# ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,"Prod-sum osd-0 parallel schedule")
#
# decoder = BeliefFindDecoder(hx, error_rate=error_rate, max_iter=max_iter, bp_method="ms", ms_scaling_factor=0.625, schedule="parallel", uf_method="inversion", bits_per_step=1)
# ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,"Belief-find parallel schedule")

# decoder = BpLsdDecoder(hx, error_rate=error_rate, max_iter=max_iter, bp_method="ms", ms_scaling_factor=0.625,
# schedule="parallel", bits_per_step=1, lsd_order=0, lsd_method="osd_cs")
# ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,
# f"Min-sum LSD-0 parallel schedule")

decoder = BpLsdDecoder(hx, error_rate=error_rate, max_iter=5, bp_method="ms", ms_scaling_factor=0.625,
schedule="parallel", bits_per_step=1, lsd_order=osd_order)
schedule="parallel", bits_per_step=1, lsd_order=osd_order, lsd_method="osd_cs")
decoder.set_do_stats(True)
ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,
f"Min-sum LSD-{osd_order} parallel schedule")
Expand Down
31 changes: 26 additions & 5 deletions src_cpp/lsd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ namespace ldpc::lsd {
std::unordered_map<int, ClusterStatistics> individual_cluster_stats; // clusterid <> stats
std::unordered_map<int, std::unordered_map<int, std::vector<int>>> global_timestep_bit_history; //timestep <> (clusterid <> added bits)
long elapsed_time;
osd::OsdMethod lsd_method;
int lsd_order;

void clear() {
this->individual_cluster_stats.clear();
Expand All @@ -413,6 +415,8 @@ namespace ldpc::lsd {
// build json like string object from individual cluster stats and global timestep bit history
std::string result = "{";
result += "\"elapsed_time_mu\":" + std::to_string(this->elapsed_time) + ",";
result += "\"lsd_method\":" + std::to_string(static_cast<int>(this->lsd_method)) + ",";
result += "\"lsd_order\":" + std::to_string(this->lsd_order) + ",";
result += "\"individual_cluster_stats\":{";
for (auto &kv: this->individual_cluster_stats) {
result += "\"" + std::to_string(kv.first) + "\":{";
Expand Down Expand Up @@ -469,13 +473,27 @@ namespace ldpc::lsd {
std::vector<uint8_t> decoding;
Statistics statistics{};
int bit_count;

explicit LsdDecoder(ldpc::bp::BpSparse &parity_check_matrix) : pcm(parity_check_matrix) {
osd::OsdMethod lsd_method;
int lsd_order;

explicit LsdDecoder(ldpc::bp::BpSparse &parity_check_matrix,
osd::OsdMethod lsdMethod = osd::OsdMethod::COMBINATION_SWEEP,
int lsd_order = 0) : pcm(parity_check_matrix),
lsd_method(lsdMethod),
lsd_order(lsd_order) {
this->bit_count = pcm.n;
this->decoding.resize(this->bit_count);
this->do_stats = false;
}

osd::OsdMethod getLsdMethod() const {
return lsd_method;
}

void setLsdMethod(osd::OsdMethod lsdMethod) {
lsd_method = lsdMethod;
}

void set_do_stats(const bool on) {
this->do_stats = on;
}
Expand Down Expand Up @@ -518,8 +536,7 @@ namespace ldpc::lsd {
lsd_decode(std::vector<uint8_t> &syndrome,
const std::vector<double> &bit_weights = NULL_DOUBLE_VECTOR,
const int bits_per_step = 1,
const bool is_on_the_fly = true,
const int lsd_order = 0) {
const bool is_on_the_fly = true) {
auto start_time = std::chrono::high_resolution_clock::now();
this->statistics.clear();

Expand Down Expand Up @@ -574,6 +591,8 @@ namespace ldpc::lsd {
}

if (lsd_order == 0) {
this->statistics.lsd_order = 0;
this->statistics.lsd_method = osd::OSD_0;
for (auto cl: clusters) {
if (do_stats) {
this->update_final_stats(cl);
Expand All @@ -590,6 +609,8 @@ namespace ldpc::lsd {
delete cl; //delete the cluster now that we have the solution.
}
} else {
this->statistics.lsd_order = lsd_order;
this->statistics.lsd_method = this->lsd_method;
this->apply_lsdw(clusters, lsd_order, bit_weights);
}
auto end_time = std::chrono::high_resolution_clock::now();
Expand Down Expand Up @@ -643,7 +664,7 @@ namespace ldpc::lsd {
auto cl_osd_decoder = osd::DenseOsdDecoder(
cl->cluster_pcm,
cl->pluDecomposition,
osd::OsdMethod::EXHAUSTIVE,
this->lsd_method,
lsd_order,
cl->bit_nodes.size(),
cl->check_nodes.size(),
Expand Down
3 changes: 1 addition & 2 deletions src_python/ldpc/belief_find_decoder/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,4 @@ class BeliefFindDecoder(BpDecoderBase):


@property
def uf_method(self):
def set_uf_method(self, uf_method: str):
def uf_method(self):
69 changes: 68 additions & 1 deletion src_python/ldpc/bplsd_decoder/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
from scipy.sparse import spmatrix
import json
from ldpc.bposd_decoder cimport OsdMethod
import warnings
class BpLsdDecoder(BpDecoderBase):
"""
A class representing a decoder that combines Belief Propagation (BP) with the Localised Statistics Decoder (LSD) algorithm.
Expand Down Expand Up @@ -34,7 +36,11 @@ class BpLsdDecoder(BpDecoderBase):
by default None.
bits_per_step : int, optional, NotImplemented
Specifies the number of bits added to the cluster in each step of the LSD algorithm. If no value is provided, this is set the block length of the code.
lsd_order: int, optional
The order of the OSD algorithm applied to each cluster. Must be greater than or equal to 0, by default 0.
lsd_method: OsdMethod
The OSD method of the OSD algorithm applied to each cluster. Must be one of {'OSD_0', 'OSD_E', 'OSD_CS'}.
By default 'OSD_0'.
Notes
-----
The `BpLsdDecoder` class leverages soft information outputted by the BP decoder to guide the cluster growth
Expand Down Expand Up @@ -99,3 +105,64 @@ class BpLsdDecoder(BpDecoderBase):
Whether the statistics are being collected.
"""


@property
def lsd_method(self) -> Optional[str]:
"""
The Ordered Statistic Decoding (OSD) method used.
Returns
-------
Optional[str]
A string representing the OSD method used. Must be one of {'OSD_0', 'OSD_E', 'OSD_CS'}. If no OSD method
has been set, returns `None`.
"""


@lsd_method.setter
def lsd_method(self, method: Union[str, int, float]) -> None:
"""
Sets the OSD method used.
Parameters
----------
method : Union[str, int, float]
A string, integer or float representing the OSD method to use. Must be one of {'OSD_0', 'OSD_E', 'OSD_CS'}, corresponding to
OSD order-0, OSD Exhaustive or OSD-Cominbation-Sweep.
"""


@property
def lsd_order(self) -> int:
"""
The OSD order used.
Returns
-------
int
An integer representing the OSD order used.
"""


@lsd_order.setter
def lsd_order(self, order: int) -> None:
"""
Set the order for the OSD method.
Parameters
----------
order : int
The order for the OSD method. Must be a positive integer.
Raises
------
ValueError
If order is less than 0.
Warns
-----
UserWarning
If the OSD method is 'OSD_E' and the order is greater than 15.
"""

13 changes: 8 additions & 5 deletions src_python/ldpc/bplsd_decoder/_bplsd_decoder.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cimport numpy as np
from ldpc.bp_decoder cimport BpSparse, BpEntry, BpDecoderBase
ctypedef np.uint8_t uint8_t
from libcpp.unordered_map cimport unordered_map as cpp_map

from ldpc.bposd_decoder cimport OsdMethod

cdef extern from "lsd.hpp" namespace "ldpc::lsd":

Expand All @@ -32,18 +32,21 @@ cdef extern from "lsd.hpp" namespace "ldpc::lsd":
cpp_map[int, ClusterStatistics] individual_cluster_stats
cpp_map[int, cpp_map[int, vector[int]]] global_timestep_bit_history
long elapsed_time
int lsd_order
OsdMethod lsd_method

cdef cppclass LsdDecoderCpp "ldpc::lsd::LsdDecoder":
LsdDecoderCpp(BpSparse& pcm) except +
vector[uint8_t]& lsd_decode(vector[uint8_t]& syndrome, const vector[double]& bit_weights, int bits_per_step, bool on_the_fly_decode, int lsd_order)
LsdDecoderCpp(BpSparse& pcm, OsdMethod lsd_method, int lsd_order) except +
vector[uint8_t]& lsd_decode(vector[uint8_t]& syndrome, const vector[double]& bit_weights, int bits_per_step, bool on_the_fly_decode)
vector[uint8_t] decoding
Statistics statistics
bool do_stats
OsdMethod lsd_method
int lsd_order
bool get_do_stats()
void set_do_stats(bool do_stats)

cdef class BpLsdDecoder(BpDecoderBase):
cdef LsdDecoderCpp* lsd
cdef int bits_per_step
cdef vector[uint8_t] bplsd_decoding
cdef int lsd_order
cdef vector[uint8_t] bplsd_decoding
Loading

0 comments on commit eed95ef

Please sign in to comment.