deltakit-core 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,16 @@
1
+ # (c) Copyright Riverlane 2020-2025.
2
+ """Description of ``deltakit.core`` namespace here."""
3
+
4
+ import importlib.metadata
5
+
6
+ # It looks like these were intended to be separate, public modules.
7
+ # For now, import them as such. This can be reconsidered during API review.
8
+ from . import decoding_graphs
9
+
10
+ __version__ = importlib.metadata.version(__package__)
11
+
12
+ # Prevent import of `importlib` (and any other non-public objects) from this module.
13
+ del importlib
14
+
15
+ # List only public members in `__all__`.
16
+ __all__ = ["decoding_graphs"]
@@ -0,0 +1,33 @@
1
+ # (c) Copyright Riverlane 2020-2025.
2
+ """
3
+ Sub-package for data formats and converting them to other data types
4
+ """
5
+
6
+ from deltakit_core.data_formats._b801_parsers import (
7
+ to_bytearray,
8
+ b8_to_syndromes,
9
+ b8_to_measurements,
10
+ b8_to_logical_flip,
11
+ syndromes_to_b8_file,
12
+ logical_flips_to_b8_file,
13
+ parse_01_to_logical_flips,
14
+ parse_01_to_syndromes,
15
+ )
16
+ from deltakit_core.data_formats._measurements import (
17
+ split_input_data_to_c64,
18
+ c64_to_addressed_input_words,
19
+ )
20
+
21
+ # List only public members in `__all__`.
22
+ __all__ = [
23
+ "to_bytearray",
24
+ "b8_to_syndromes",
25
+ "b8_to_measurements",
26
+ "b8_to_logical_flip",
27
+ "syndromes_to_b8_file",
28
+ "logical_flips_to_b8_file",
29
+ "parse_01_to_logical_flips",
30
+ "parse_01_to_syndromes",
31
+ "split_input_data_to_c64",
32
+ "c64_to_addressed_input_words",
33
+ ]
@@ -0,0 +1,141 @@
1
+ # (c) Copyright Riverlane 2020-2025.
2
+ from __future__ import annotations
3
+
4
+ from pathlib import Path
5
+ from typing import Iterator, Tuple
6
+
7
+ import numpy as np
8
+ from deltakit_core.decoding_graphs import Bitstring, OrderedSyndrome
9
+
10
+
11
+ def to_bytearray(data: Path | bytes) -> bytearray:
12
+ """Converts either the content of a file or a bytes object to
13
+ a bytearray object. A key motivation for this is that np.unpackbits
14
+ accepts bytearray but not bytes."""
15
+ if isinstance(data, Path):
16
+ with open(data, "rb") as b8_data:
17
+ return bytearray(b8_data.read())
18
+ return bytearray(data)
19
+
20
+
21
+ def b8_to_syndromes(
22
+ b8_input: Path | bytes, detector_num: int
23
+ ) -> Iterator[OrderedSyndrome]:
24
+ """Given a b8 input (either a file path containing b8 data or bytes),
25
+ and the number of detectors that should be in each syndrome, return a
26
+ generator of syndromes.
27
+
28
+ Informed by https://github.com/quantumlib/Stim/blob/main/doc/result_formats.md#b8
29
+ """
30
+ bytes_per_shot = (detector_num + 7) // 8
31
+ b8_byte_arr: bytearray = to_bytearray(b8_input)
32
+ for offset in range(0, len(b8_byte_arr), bytes_per_shot):
33
+ yield OrderedSyndrome.from_bitstring(
34
+ np.unpackbits(
35
+ np.frombuffer(b8_byte_arr[offset : offset + bytes_per_shot],
36
+ dtype=np.uint8),
37
+ bitorder="little",
38
+ ).tolist()
39
+ )
40
+
41
+
42
+ def b8_to_measurements(b8_input: Path, measurement_num: int) -> Iterator[Bitstring]:
43
+ """Given a b8 input (a file path containing b8 data),
44
+ and the number of measurements in each shot,
45
+ return a generator of measurement bitstrings.
46
+
47
+ Informed by https://github.com/quantumlib/Stim/blob/main/doc/result_formats.md#b8
48
+ """
49
+ bytes_per_shot = (measurement_num + 7) // 8
50
+ b8_byte_arr: bytearray = to_bytearray(b8_input)
51
+ for offset in range(0, len(b8_byte_arr), bytes_per_shot):
52
+ yield Bitstring.from_bits(
53
+ np.unpackbits(
54
+ np.frombuffer(b8_byte_arr[offset : offset + bytes_per_shot],
55
+ dtype=np.uint8),
56
+ bitorder="little",
57
+ ).tolist()
58
+ )
59
+
60
+
61
+ def b8_to_logical_flip(
62
+ b8_input: Path | bytes, num_logicals: int = 1
63
+ ) -> Iterator[Tuple[bool, ...]]:
64
+ """Given a b8 input (either a file path containing b8 data or bytes),
65
+ and the number of detectors that should be in each syndrome, return a
66
+ generator of logical flips.
67
+
68
+ Informed by https://github.com/quantumlib/Stim/blob/main/doc/result_formats.md#b8
69
+ """
70
+ bytes_per_shot = (num_logicals + 7) // 8
71
+ b8_byte_arr: bytearray = to_bytearray(b8_input)
72
+ for offset in range(0, len(b8_byte_arr), bytes_per_shot):
73
+ yield tuple(
74
+ np.unpackbits(
75
+ np.frombuffer(b8_byte_arr[offset : offset + bytes_per_shot],
76
+ dtype=np.uint8),
77
+ bitorder="little",
78
+ )[:num_logicals].tolist(),
79
+ )
80
+
81
+
82
+ def syndromes_to_b8_file(
83
+ syndrome_b8_out: Path, detector_num: int, syndromes: Iterator[OrderedSyndrome]
84
+ ):
85
+ """Given a syndrome generator, this will output them to file in the b8 format.
86
+ Informed by https://github.com/quantumlib/Stim/blob/main/doc/result_formats.md#b8
87
+ """
88
+ with open(syndrome_b8_out, "wb") as b8_out_handle:
89
+ for syndrome in syndromes:
90
+ b8_out_handle.write(
91
+ bytes(
92
+ np.packbits(syndrome.as_bitstring(detector_num), bitorder="little")
93
+ )
94
+ )
95
+
96
+
97
+ def logical_flips_to_b8_file(
98
+ logical_flips_b8_out: Path, logical_flips: Iterator[Tuple[bool, ...]]
99
+ ):
100
+ """Given a logical flip generator, this will output them to file in the b8 format.
101
+ b8 is a byte-padded bitstring representation for logical flips. This is more
102
+ efficient than the string-based 01 representation.
103
+ Informed by https://github.com/quantumlib/Stim/blob/main/doc/result_formats.md#b8
104
+ """
105
+ with open(logical_flips_b8_out, "wb") as data_b8:
106
+ for logical_flip in logical_flips:
107
+ data_b8.write(bytes(np.packbits(logical_flip, bitorder="little")))
108
+
109
+
110
+ def parse_01_to_logical_flips(
111
+ logical_flips_01_file: Path,
112
+ ) -> Iterator[Tuple[bool, ...]]:
113
+ """Given a filepath for a 01 file containing target logical flips, return a
114
+ generator of logical flips as booleans.
115
+
116
+ 01 files are string based, where each line is a different shot. Where 1
117
+ indicates the reference logical is flipped.
118
+
119
+ Use logical_flips_to_b8_file on the result of this function to translate 01
120
+ data to b8 data that can be decoded via the B8DecoderManager.
121
+ """
122
+ with open(logical_flips_01_file, "r", encoding="utf-8") as logical_flips_handle:
123
+ for bit_string in logical_flips_handle.read().splitlines():
124
+ yield tuple(char != "0" for char in bit_string)
125
+
126
+
127
+ def parse_01_to_syndromes(
128
+ logical_flips_01_file: Path,
129
+ ) -> Iterator[OrderedSyndrome]:
130
+ """Given a filepath for a 01 file containing syndromes, return a
131
+ generator of OrderedSyndromes.
132
+
133
+ 01 files are string based, where each line is a different target logical as a
134
+ string of 0s and 1s. Where 1 indicates the reference logical is flipped.
135
+
136
+ Use logical_flips_to_b8_file on the result of this function to translate 01
137
+ data to b8 data that can be decoded via the B8DecoderManager.
138
+ """
139
+ with open(logical_flips_01_file, "r", encoding="utf-8") as logical_flips_handle:
140
+ for bit_string in logical_flips_handle.read().splitlines():
141
+ yield OrderedSyndrome([i for i, v in enumerate(bit_string) if v == "1"])
@@ -0,0 +1,58 @@
1
+ # (c) Copyright Riverlane 2020-2025.
2
+ import csv
3
+ from itertools import cycle
4
+ from pathlib import Path
5
+ from typing import Iterable, Iterator, List, Tuple
6
+
7
+ from deltakit_core.decoding_graphs import Bitstring
8
+
9
+
10
+ def split_input_data_to_c64(
11
+ input_data: Iterable[Iterable[Bitstring]], output_path: Path
12
+ ):
13
+ """
14
+ Given a list of round-split decoder input data, this will output them to a file
15
+ as comma separated 64-bit integers. Each line is a new shot.
16
+
17
+ The decoder input data could be measurements or syndromes.
18
+ """
19
+ with open(output_path, "w", encoding="ascii") as output_handle:
20
+ cw = csv.writer(output_handle)
21
+ for input_datum in input_data:
22
+ words = [
23
+ int(item)
24
+ for line in [
25
+ input_datum_round.to_words(64) for input_datum_round in input_datum
26
+ ]
27
+ for item in line
28
+ ]
29
+ cw.writerow(words)
30
+
31
+
32
+ def c64_to_addressed_input_words(
33
+ input_path: Path, round_width: int
34
+ ) -> Iterator[List[Tuple[int, int]]]:
35
+ """Given a path to a c64 file, return, for each line, the words within the line
36
+ and the index of each word in each round. These are appropriate for input to a
37
+ hardware decoder, they could be syndromes or measurement data.
38
+
39
+ Parameters
40
+ ----------
41
+ input_path : Path
42
+ Path to the c64 file to read.
43
+ round_width : int
44
+ The number of detectors or measurements per round.
45
+
46
+ Yields
47
+ ------
48
+ List[Tuple[int, int]]
49
+ List of addressed 64bit words
50
+ """
51
+ with open(input_path, "r", encoding="ascii") as input_handle:
52
+ syndrome_reader = csv.reader(input_handle)
53
+ for row in syndrome_reader:
54
+ round_indices = cycle(range(round_width // 64 + 1))
55
+ yield [
56
+ (word_index, int(word_64))
57
+ for word_index, word_64 in zip(round_indices, row, strict=False)
58
+ ]
@@ -0,0 +1,122 @@
1
+ # (c) Copyright Riverlane 2020-2025.
2
+ """
3
+ Sub-package for defining decoding graphs and related data types; these structures are
4
+ used by many decoders to define their understanding of the code and noise model being
5
+ decoded.
6
+ """
7
+
8
+ from deltakit_core.decoding_graphs._data_qubits import (
9
+ DecodingEdge,
10
+ DecodingHyperEdge,
11
+ EdgeRecord,
12
+ EdgeT,
13
+ OrderedDecodingEdges,
14
+ errors_to_syndrome,
15
+ )
16
+
17
+ from deltakit_core.decoding_graphs._decoding_graph import (
18
+ AnyEdgeT,
19
+ DecodingCode,
20
+ DecodingHyperGraph,
21
+ DecodingHyperMultiGraph,
22
+ HyperLogicals,
23
+ HyperMultiGraph,
24
+ NXCode,
25
+ NXDecodingGraph,
26
+ NXDecodingMultiGraph,
27
+ NXLogicals,
28
+ )
29
+
30
+ from deltakit_core.decoding_graphs._dem_parsing import (
31
+ DemParser,
32
+ DetectorCounter,
33
+ DetectorRecorder,
34
+ LogicalsInEdges,
35
+ dem_to_decoding_graph_and_logicals,
36
+ dem_to_hypergraph_and_logicals,
37
+ observable_warning,
38
+ )
39
+
40
+ from deltakit_core.decoding_graphs._syndromes import (
41
+ Bitstring,
42
+ DetectorRecord,
43
+ FixedWidthBitstring,
44
+ OrderedSyndrome,
45
+ get_round_words,
46
+ )
47
+
48
+ from deltakit_core.decoding_graphs._weighted_graphs import (
49
+ change_graph_error_probabilities,
50
+ vector_weights,
51
+ )
52
+
53
+ from deltakit_core.decoding_graphs._decoding_graph_tools import (
54
+ compute_graph_distance,
55
+ compute_graph_distance_for_logical,
56
+ filter_to_data_edges,
57
+ filter_to_measure_edges,
58
+ graph_to_json,
59
+ has_contiguous_nodes,
60
+ hypergraph_to_weighted_edge_list,
61
+ inverse_logical_at_boundary,
62
+ is_single_connected_component,
63
+ single_boundary_is_last_node,
64
+ unweight_graph,
65
+ worst_case_num_detectors,
66
+ )
67
+
68
+ from deltakit_core.decoding_graphs._explained_dem_parsing import (
69
+ extract_logicals,
70
+ parse_explained_dem,
71
+ )
72
+
73
+ from deltakit_core.decoding_graphs._hypergraph_decomposition import decompositions
74
+
75
+ # List only public members in `__all__`.
76
+ __all__ = [
77
+ "DecodingEdge",
78
+ "DecodingHyperEdge",
79
+ "EdgeRecord",
80
+ "EdgeT",
81
+ "OrderedDecodingEdges",
82
+ "errors_to_syndrome",
83
+ "AnyEdgeT",
84
+ "DecodingCode",
85
+ "DecodingHyperGraph",
86
+ "DecodingHyperMultiGraph",
87
+ "HyperLogicals",
88
+ "HyperMultiGraph",
89
+ "NXCode",
90
+ "NXDecodingGraph",
91
+ "NXDecodingMultiGraph",
92
+ "NXLogicals",
93
+ "DemParser",
94
+ "DetectorCounter",
95
+ "DetectorRecorder",
96
+ "LogicalsInEdges",
97
+ "dem_to_decoding_graph_and_logicals",
98
+ "dem_to_hypergraph_and_logicals",
99
+ "observable_warning",
100
+ "Bitstring",
101
+ "DetectorRecord",
102
+ "FixedWidthBitstring",
103
+ "OrderedSyndrome",
104
+ "get_round_words",
105
+ "change_graph_error_probabilities",
106
+ "vector_weights",
107
+ "compute_graph_distance",
108
+ "compute_graph_distance_for_logical",
109
+ "filter_to_data_edges",
110
+ "filter_to_measure_edges",
111
+ "graph_to_json",
112
+ "has_contiguous_nodes",
113
+ "hypergraph_to_weighted_edge_list",
114
+ "inverse_logical_at_boundary",
115
+ "is_single_connected_component",
116
+ "single_boundary_is_last_node",
117
+ "unweight_graph",
118
+ "worst_case_num_detectors",
119
+ "extract_logicals",
120
+ "parse_explained_dem",
121
+ "decompositions",
122
+ ]