Skip to content

Commit 0ebbcf8

Browse files
Update documentation for Python interface (#77)
1 parent aaa0292 commit 0ebbcf8

File tree

1 file changed

+80
-7
lines changed

1 file changed

+80
-7
lines changed

src/py/README.md

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,80 @@ print(f"Custom configuration detection penalty: {config2.det_beam}")
6565
This is the main class that implements the Tesseract decoding logic.
6666
* `TesseractDecoder(config: tesseract.TesseractConfig)`
6767
* `decode_to_errors(detections: list[int])`
68-
* `decode_to_errors(detections: list[int], detector_order: int, detector_beam: int)`
68+
* `decode_to_errors(detections: list[int], det_order: int, det_beam: int)`
6969
* `get_observables_from_errors(predicted_errors: list[int]) -> list[bool]`
7070
* `cost_from_errors(predicted_errors: list[int]) -> float`
7171
* `decode(detections: list[int]) -> list[bool]`
7272

73+
Explanation of each method:
74+
#### `decode_to_errors(detections: list[int])`
75+
76+
Decodes a single measurement shot to predict a list of errors.
77+
78+
* **Parameters:** `detections` is a list of integers that represent the indices of the detectors that have fired in a single shot.
79+
80+
* **Returns:** A list of integers, where each integer is the index of a predicted error.
81+
82+
#### `decode_to_errors(detections: list[int], det_order: int, det_beam: int)`
83+
84+
An overloaded version of the `decode_to_errors` method that allows for a different decoding strategy.
85+
86+
* **Parameters:**
87+
88+
* `detections` is a list of integers representing the indices of the fired detectors.
89+
90+
* `det_order` is an integer that specifies a different ordering of detectors to use for the decoding.
91+
92+
* `det_beam` is an integer that specifies the beam size to use for the decoding.
93+
94+
* **Returns:** A list of integers, where each integer is the index of a predicted error.
95+
96+
#### `get_observables_from_errors(predicted_errors: list[int]) -> list[bool]`
97+
98+
Converts a list of predicted error indices into a list of flipped logical observables.
99+
100+
* **Parameters:** `predicted_errors` is a list of integers representing the predicted error indices.
101+
102+
* **Returns:** A list of booleans. Each boolean corresponds to a logical observable and is `True` if the observable was flipped, and `False` otherwise.
103+
104+
#### `cost_from_errors(predicted_errors: list[int]) -> float`
105+
106+
Calculates the total logarithmic probability cost for a given set of predicted errors. The cost is a measure of how likely a set of errors is.
107+
108+
* **Parameters:** `predicted_errors` is a list of integers representing the predicted error indices.
109+
110+
* **Returns:** A float representing the total logarithmic probability cost.
111+
112+
#### `decode_from_detection_events(detections: list[int]) -> numpy.ndarray`
113+
114+
This method decodes a single shot from a list of detection events. This is an alternative to the `decode` method that takes a NumPy array.
115+
116+
* **Parameters:** `detections` is a list of integers representing the indices of the detectors that were fired.
117+
118+
* **Returns:** A 1D NumPy array of booleans. Each boolean indicates whether the corresponding logical observable has been flipped by the decoded error.
119+
120+
#### `decode(syndrome: numpy.ndarray) -> numpy.ndarray`
121+
122+
A convenience function that decodes a single shot and returns the flipped logical observables directly. It combines the functionality of `decode_to_errors` and `get_observables_from_errors`.
123+
124+
* **Parameters:** `syndrome` is a 1D NumPy array of booleans representing the detector outcomes for a single shot. The length of the array should match the number of detectors in the DEM.
125+
126+
* **Returns:** A 1D NumPy array of booleans, where each boolean corresponds to a logical observable and is `True` if the observable was flipped.
127+
128+
#### `decode_batch(syndromes: numpy.ndarray) -> numpy.ndarray`
129+
130+
Decodes a batch of shots at once.
131+
132+
* **Parameters:** `syndromes` is a 2D NumPy array of booleans where each row represents a single shot's detector outcomes. The shape should be `(num_shots, num_detectors)`.
133+
134+
* **Returns:** A 2D NumPy array of booleans with the shape `(num_shots, num_observables)`. Each row is the decoder's prediction of which observables were flipped in the shot.
135+
73136
**Example Usage**:
74137

75138
```python
76139
import tesseract_decoder.tesseract as tesseract
77140
import stim
78-
import tesseract_decoder.common as common
141+
import numpy as np
79142

80143
# Create a DEM and a configuration
81144
dem = stim.DetectorErrorModel("""
@@ -89,21 +152,31 @@ config = tesseract.TesseractConfig(dem=dem)
89152
# Create the decoder
90153
decoder = tesseract.TesseractDecoder(config)
91154

92-
# Decode the detections and get flipped observables
155+
# --- Decode a single shot using detection events (list of integers) ---
93156
detections = [1]
94-
flipped_observables = decoder.decode(detections)
95-
print(f"Flipped observables for detections {detections}: {flipped_observables}")
157+
flipped_observables_events = decoder.decode_from_detection_events(detections)
158+
print(f"Decoded (from events) flipped observables for detections {detections}: {flipped_observables_events}")
96159

97-
# Access predicted errors after decoding
160+
# Access predicted errors
98161
predicted_errors = decoder.predicted_errors_buffer
99-
print(f"Predicted errors: {predicted_errors}")
162+
print(f"\nPredicted errors after single-shot decode: {predicted_errors}")
100163

101164
# Calculate cost for predicted errors
102165
cost = decoder.cost_from_errors(predicted_errors)
103166
print(f"Cost of predicted errors: {cost}")
104167

105168
# Check the low confidence flag
106169
print(f"Decoder low confidence: {decoder.low_confidence_flag}")
170+
171+
# --- Decode a single shot using a syndrome array (NumPy array of booleans) ---
172+
syndrome_array = np.array([False, True])
173+
flipped_observables_syndrome = decoder.decode(syndrome_array)
174+
print(f"Decoded (from syndrome) flipped observables for syndrome {syndrome_array}: {flipped_observables_syndrome}")
175+
176+
# --- Decode a batch of shots using a syndrome array (2D NumPy array of booleans) ---
177+
syndromes_batch = np.array([[False, True], [True, False]])
178+
flipped_observables_batch = decoder.decode_batch(syndromes_batch)
179+
print(f"Decoded (batch) flipped observables for syndromes:\n{syndromes_batch}\nResult:\n{flipped_observables_batch}")
107180
```
108181

109182
### `tesseract_decoder.simplex` Module

0 commit comments

Comments
 (0)