Skip to content

Commit

Permalink
add some docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasberent committed Nov 19, 2024
1 parent bffd7dd commit 9f06935
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def compile_decoder_for_dem(self, *, dem: stim.DetectorErrorModel) -> CompiledDe
)
return SinterCompiledDecoderMaxSat(maxsat, **self.maxsat_kwargs)

def decode_via_files(
def decode_via_files( # noqa: PLR6301
self,
*,
num_shots: int, # noqa: ARG002
Expand Down
12 changes: 9 additions & 3 deletions test/python/cc_decoder/test_color_code_stim.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
""" tests for the color code stim decoder main functionality """
from __future__ import annotations

from typing import TYPE_CHECKING
Expand All @@ -19,14 +20,16 @@

@pytest.fixture
def hamming_code() -> NDArray[bool]:
""" return hamming code parity check matrix. """
return np.array([
[True, True, False, True, True, False, False],
[False, True, True, False, True, True, False],
[False, False, False, True, True, True, True],
])


def test_gen_pcm_and_logical(hamming_code) -> None:
def test_gen_pcm_and_logical(hamming_code:NDArray[bool]) -> None:
""" test parity check matrix and logical matrix generation"""
distance = 3
expected_logicals = {2, 5, 6}

Expand All @@ -37,12 +40,14 @@ def test_gen_pcm_and_logical(hamming_code) -> None:


def test_neighbours() -> None:
""" test neighbour computation for color code grid. """
input_perm = np.array([1, 2, 3])
expected = np.array([[2, 2, 2], [1, 3, 2], [0, 3, 3], [0, 2, 4], [1, 1, 4], [2, 1, 3]])
assert array_equal(expected, neighbors(input_perm))


def test_add_checks_one_round(hamming_code) -> None:
def test_add_checks_one_round(hamming_code:NDArray[bool]) -> None:
""" test stim circuit generation for one stabilizer round """
expected_circuit = stim.Circuit()
circuit = stim.Circuit()
expected_circuit.append_from_stim_program_text("MPP Z0*Z1*Z3*Z4")
Expand All @@ -51,7 +56,8 @@ def test_add_checks_one_round(hamming_code) -> None:
assert expected_circuit == add_checks_one_round(hamming_code, circuit, False, 0)


def test_gen_stim_memory_experiment(hamming_code) -> None:
def test_gen_stim_memory_experiment(hamming_code:NDArray[bool]) -> None:
""" test generation of stim circuit for a memory experiment. """
expected_circuit = stim.Circuit()
stim.Circuit()
expected_circuit.append_from_stim_program_text("R 0 1 2 3 4 5 6")
Expand Down
29 changes: 21 additions & 8 deletions test/python/cc_decoder/test_dem_to_matrices.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
""" testing detector error model (dem) to check matries glue code. """
from __future__ import annotations

from typing import TYPE_CHECKING
Expand All @@ -17,6 +18,7 @@

@pytest.fixture
def dem_matrix() -> NDArray[NDArray[int]]:
""" return detector error model matrix for d=3 color code. """
return np.array([
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
Expand All @@ -35,6 +37,7 @@ def dem_matrix() -> NDArray[NDArray[int]]:

@pytest.fixture
def hyperedges() -> dict[int, frozenset[int]]:
""" return adjacency dict of hyperedges for d=3 color code dem. """
return {
0: frozenset({0, 1, 2}),
1: frozenset({0, 1}),
Expand Down Expand Up @@ -71,11 +74,13 @@ def hyperedges() -> dict[int, frozenset[int]]:

@pytest.fixture
def hypergraph_shape() -> tuple[int, int]:
""" return hypergraph shape for d=3 color code dem. """
return (12, 30)


@pytest.fixture
def priors() -> NDArray[np.float32]:
""" return list of priors for dem errors. """
return np.array([
0.5,
0.5,
Expand Down Expand Up @@ -112,6 +117,7 @@ def priors() -> NDArray[np.float32]:

@pytest.fixture
def hyperedge_to_edge_matrix() -> NDArray[NDArray[np.int32]]:
""" return hyperedge to edge matrix for dem. """
return np.array([
[
0,
Expand Down Expand Up @@ -982,11 +988,13 @@ def hyperedge_to_edge_matrix() -> NDArray[NDArray[np.int32]]:

@pytest.fixture
def edge_obsbl_matrix() -> NDArray[NDArray[np.int32]]:
""" return the edge observable matrix. """
return np.array([[1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]])


@pytest.fixture
def edge_check_matrix() -> NDArray[NDArray.np.int32]:
""" return the edge adjacency matrix. """
return np.array([
[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
Expand All @@ -1005,11 +1013,13 @@ def edge_check_matrix() -> NDArray[NDArray.np.int32]:

@pytest.fixture
def obsble_matrix() -> NDArray[NDArray[np.int32]]:
""" return the observable matrix"""
return np.array([[0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]])


@pytest.fixture
def hamming_code() -> NDArray[bool]:
""" return hamming code check matrix. """
return np.array([
[True, True, False, True, True, False, False],
[False, True, True, False, True, True, False],
Expand All @@ -1019,6 +1029,7 @@ def hamming_code() -> NDArray[bool]:

@pytest.fixture
def detector_error_model() -> stim.DetectorErrorModel:
""" return d=3 color code dem. """
return stim.DetectorErrorModel("""
error(0.5) D0 D1 D2
error(0.5) D0 D1 L0
Expand Down Expand Up @@ -1323,21 +1334,23 @@ def detector_error_model() -> stim.DetectorErrorModel:


def test_dict_to_csc_matrix(
hypergraph_shape, hyperedges: dict[int, frozenset[int]], dem_matrix: NDArray[NDArray[int]]
hypergraph_shape:Tuple[int,int], hyperedges: dict[int, frozenset[int]], dem_matrix: NDArray[NDArray[int]]
) -> None:
""" test the dictionary to sparse matrix function. """
result = dict_to_csc_matrix(hyperedges, hypergraph_shape).todense()
assert array_equal(result, dem_matrix)


def test_detector_error_model_to_check_matrices(
detector_error_model,
priors,
dem_matrix,
obsble_matrix,
edge_check_matrix,
edge_obsbl_matrix,
hyperedge_to_edge_matrix,
detector_error_model:stim.DetectorErrorModel,
priors:NDArray[np.float32],
dem_matrix:NDArray[NDArray[int]],
obsble_matrix:NDArray[NDArray[int]],
edge_check_matrix:NDArray[NDArray[int]],
edge_obsbl_matrix:NDArray[NDArray[int]],
hyperedge_to_edge_matrix:NDArray[NDArray[int]],
) -> None:
""" test dem to check matrices function. """
result = detector_error_model_to_check_matrices(detector_error_model, True)
assert array_equal(result.priors, priors)
assert array_equal(result.check_matrix.todense(), dem_matrix)
Expand Down
Loading

0 comments on commit 9f06935

Please sign in to comment.