Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Peeling union find with boundaries #29

Merged
merged 10 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 89 additions & 1 deletion cpp_test/TestUnionFind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "union_find.hpp"
#include "util.hpp"
#include "bp.hpp"
#include "robin_set.h"

using namespace std;
using namespace ldpc::uf;
Expand Down Expand Up @@ -111,15 +112,102 @@ TEST(UfDecoder, ring_code3){

auto syndrome = vector<uint8_t>{1, 0, 1};

auto decoding = bpd.peel_decode(syndrome, ldpc::uf::NULL_DOUBLE_VECTOR,3);
auto decoding = bpd.peel_decode(syndrome, ldpc::uf::EMPTY_DOUBLE_VECTOR, 3);

ASSERT_TRUE(bpd.pcm_max_bit_degree_2);
tsl::robin_set<int> boundary_bits = {};
ASSERT_EQ(bpd.planar_code_boundary_bits,boundary_bits);

}


TEST(UfDecoder, rep_code){

for(int n = 5; n < 20; n++){

auto pcm = ldpc::gf2codes::rep_code<ldpc::bp::BpEntry>(n);
auto bpd = UfDecoder(pcm);
ASSERT_TRUE(bpd.pcm_max_bit_degree_2);
tsl::robin_set<int> boundary_bits = {0,n-1};
ASSERT_EQ(bpd.planar_code_boundary_bits,boundary_bits);
auto syndrome = vector<uint8_t>(n,0);
syndrome[0] = 1;
syndrome[n-2] = 1;
auto decoding = bpd.peel_decode(syndrome, ldpc::uf::EMPTY_DOUBLE_VECTOR, 1);
auto expected_decoding = vector<uint8_t>(n,0);
expected_decoding[0] = 1;
expected_decoding[n-1] = 1;
ASSERT_EQ(decoding,expected_decoding);

}

}

TEST(UfDecoder, peeling_with_boundaries){


auto pcm = ldpc::gf2sparse::GF2Sparse<ldpc::bp::BpEntry>(2,7);
pcm.insert_entry(0,0);
pcm.insert_entry(0,1);
pcm.insert_entry(0,2);
pcm.insert_entry(0,3);
pcm.insert_entry(1,3);
pcm.insert_entry(1,4);
pcm.insert_entry(1,5);
pcm.insert_entry(1,6);

// ldpc::sparse_matrix_util::print_sparse_matrix(pcm);

auto bpd = UfDecoder(pcm);
ASSERT_TRUE(bpd.pcm_max_bit_degree_2);
tsl::robin_set<int> boundary_bits = {0,1,2,4,5,6};
ASSERT_EQ(bpd.planar_code_boundary_bits,boundary_bits);
auto syndrome = vector<uint8_t>{1,1};
auto weights = vector<double>{-1,0,0,10,0,0,-1};
auto decoding = bpd.peel_decode(syndrome, weights,1);

// ldpc::sparse_matrix_util::print_vector(decoding);
auto expected_decoding = vector<uint8_t>(7,0);
expected_decoding[0] = 1;
expected_decoding[6] = 1;
ASSERT_EQ(decoding,expected_decoding);



}

TEST(UfDecoder, peeling_with_boundaries_edge_case){

auto pcm = ldpc::gf2sparse::GF2Sparse<ldpc::bp::BpEntry>(20,41);
pcm.insert_entry(0,0);pcm.insert_entry(0,5);pcm.insert_entry(0,25);pcm.insert_entry(1,1);pcm.insert_entry(1,6);pcm.insert_entry(1,25);pcm.insert_entry(1,26);pcm.insert_entry(2,2);pcm.insert_entry(2,7);pcm.insert_entry(2,26);pcm.insert_entry(2,27);pcm.insert_entry(3,3);pcm.insert_entry(3,8);pcm.insert_entry(3,27);pcm.insert_entry(3,28);pcm.insert_entry(4,4);pcm.insert_entry(4,9);pcm.insert_entry(4,28);pcm.insert_entry(5,5);pcm.insert_entry(5,10);pcm.insert_entry(5,29);pcm.insert_entry(6,6);pcm.insert_entry(6,11);pcm.insert_entry(6,29);pcm.insert_entry(6,30);pcm.insert_entry(7,7);pcm.insert_entry(7,12);pcm.insert_entry(7,30);pcm.insert_entry(7,31);pcm.insert_entry(8,8);pcm.insert_entry(8,13);pcm.insert_entry(8,31);pcm.insert_entry(8,32);pcm.insert_entry(9,9);pcm.insert_entry(9,14);pcm.insert_entry(9,32);pcm.insert_entry(10,10);pcm.insert_entry(10,15);pcm.insert_entry(10,33);pcm.insert_entry(11,11);pcm.insert_entry(11,16);pcm.insert_entry(11,33);pcm.insert_entry(11,34);pcm.insert_entry(12,12);pcm.insert_entry(12,17);pcm.insert_entry(12,34);pcm.insert_entry(12,35);pcm.insert_entry(13,13);pcm.insert_entry(13,18);pcm.insert_entry(13,35);pcm.insert_entry(13,36);pcm.insert_entry(14,14);pcm.insert_entry(14,19);pcm.insert_entry(14,36);pcm.insert_entry(15,15);pcm.insert_entry(15,20);pcm.insert_entry(15,37);pcm.insert_entry(16,16);pcm.insert_entry(16,21);pcm.insert_entry(16,37);pcm.insert_entry(16,38);pcm.insert_entry(17,17);pcm.insert_entry(17,22);pcm.insert_entry(17,38);pcm.insert_entry(17,39);pcm.insert_entry(18,18);pcm.insert_entry(18,23);pcm.insert_entry(18,39);pcm.insert_entry(18,40);pcm.insert_entry(19,19);pcm.insert_entry(19,24);pcm.insert_entry(19,40);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you indicate what matrix that is? would be important for intuition and to keep the test 'alive' in the future

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. This was just a specific test from the Python Monte Carlo tests that was failing. I ported the pcm and the syndrome over to CPP for debugging. It seems to be resolved now so this test can be deleted.


auto error_rate = std::vector<double>(41,0.18);
auto bpd = ldpc::bp::BpDecoder(pcm, error_rate, 1, ldpc::bp::MINIMUM_SUM, ldpc::bp::PARALLEL, 0.625);

auto syndrome_sparse = std::vector<int>{3, 5, 7, 8, 12, 17, 18, 19};

auto syndrome = std::vector<uint8_t>(pcm.m,0);
for(int i : syndrome_sparse){
syndrome[i] = 1;
}

bpd.decode(syndrome);

ASSERT_FALSE(bpd.converge);

auto ufd = UfDecoder(pcm);

// ldpc::sparse_matrix_util::print_vector(bpd.log_prob_ratios);

auto decoding = ufd.peel_decode(syndrome, bpd.log_prob_ratios,1);

auto decoding_syndrome = pcm.mulvec(decoding);

ASSERT_EQ(decoding_syndrome,syndrome);



}



Expand Down
Binary file added python_test/pcms/hx_surface_2.npz
Binary file not shown.
Binary file added python_test/pcms/hx_surface_20.npz
Binary file not shown.
Binary file added python_test/pcms/hx_surface_3 copy.npz
Binary file not shown.
Binary file added python_test/pcms/hx_surface_3.npz
Binary file not shown.
Binary file added python_test/pcms/hx_surface_4.npz
Binary file not shown.
Binary file added python_test/pcms/hx_surface_5.npz
Binary file not shown.
Binary file added python_test/pcms/lx_surface_2.npz
Binary file not shown.
Binary file added python_test/pcms/lx_surface_20.npz
Binary file not shown.
Binary file added python_test/pcms/lx_surface_3.npz
Binary file not shown.
Binary file added python_test/pcms/lx_surface_4.npz
Binary file not shown.
Binary file added python_test/pcms/lx_surface_5.npz
Binary file not shown.
27 changes: 27 additions & 0 deletions python_test/test_belief_find.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
import numpy as np
from ldpc.belief_find_decoder import BeliefFindDecoder
from ldpc.codes import hamming_code, rep_code, ring_code

def test_peeling_input():

pcm = rep_code(3)
decoder = BeliefFindDecoder(pcm, error_rate = 0.1, uf_method="peeling")

pcm = ring_code(3)
decoder = BeliefFindDecoder(pcm, error_rate = 0.1, uf_method="peeling")

pcm = hamming_code(3)

# decoder = BeliefFindDecoder(pcm, error_rate = 0.1, uf_method="peeling")

with pytest.raises(ValueError):
decoder = BeliefFindDecoder(pcm, error_rate = 0.1, uf_method="peeling")


if __name__ == "__main__":
test_peeling_input()




47 changes: 42 additions & 5 deletions python_test/test_qcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def quantum_mc_sim(hx, lx, error_rate, run_count, seed, DECODER, run_label, DEBU


def test_400_16_6_hgp():
hx = scipy.sparse.load_npz("/home/luca/Documents/codeRepos/ldpc/python_test/pcms/hx_400_16_6.npz")
lx = scipy.sparse.load_npz("/home/luca/Documents/codeRepos/ldpc/python_test/pcms/lx_400_16_6.npz")
hx = scipy.sparse.load_npz("pcms/hx_400_16_6.npz")
lx = scipy.sparse.load_npz("pcms/lx_400_16_6.npz")

error_rate = 0.03
run_count = 1000
Expand Down Expand Up @@ -99,7 +99,7 @@ def test_400_16_6_hgp():
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 parallel schedule osd={osd_order}")
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)
Expand All @@ -110,7 +110,7 @@ def test_toric_20():
hx = scipy.sparse.load_npz("python_test/pcms/hx_toric_20.npz")
lx = scipy.sparse.load_npz("python_test/pcms/lx_toric_20.npz")

error_rate = 0.05
error_rate = 0.03
run_count = 500
seed = 42
max_iter = 10
Expand Down Expand Up @@ -141,6 +141,43 @@ def test_toric_20():
schedule="parallel", bits_per_step=1, osd_order=5)
ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,"Min-sum LSD parallel schedule")

def test_surface_20():
hx = scipy.sparse.load_npz("python_test/pcms/hx_surface_20.npz")
lx = scipy.sparse.load_npz("python_test/pcms/lx_surface_20.npz")

error_rate = 0.01
run_count = 10000
seed = 42
max_iter = 5

print(hx.shape)

print(f"Code: [[761, 1, 20]] Surface, error rate: {error_rate}, bp iterations:, {max_iter}, run count: {run_count}, seed: {seed}")
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 = 5)
ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,"Min-sum osd-cs-5 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 = 5)
ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,"Min-sum osd-e-5 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="peeling", 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=5)
ler, min_logical, speed, _ = quantum_mc_sim(hx, lx, error_rate, run_count, seed, decoder,"Min-sum LSD parallel schedule")


def test_cl_size():
hx = scipy.sparse.load_npz("python_test/pcms/hx_400_16_6.npz")
Expand Down Expand Up @@ -210,4 +247,4 @@ def test_failing_case_lsd_w():

if __name__ == "__main__":

test_failing_case_lsd_w()
test_surface_20()
Empty file added python_test/test_union_find.py
Empty file.
Loading