Skip to content

Commit 6f8e31a

Browse files
Refactoring: Project structure improvements and documentation updates (#3)
* some refactor + improved readme * Refactored project structure + removed unused stuff * s'more refactorin' * removed some plots. improved readme * Added disclaimer * Added plots to README.md
1 parent 46ebc80 commit 6f8e31a

41 files changed

Lines changed: 292 additions & 1202 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 105 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,91 @@
1-
# Tesseract Code
1+
# Stim implementation of the [[16,4,4]] Tesseract Code
22

3-
This repository contains implementations and simulations of the Tesseract quantum error correction code using Stim and PyMatching.
3+
This repository contains implementations and simulations of the Tesseract quantum error correction code [[1]](#references) using Stim [[3]](#references).
44

5-
## Features
5+
## Overview
6+
### Motivation
7+
The 16-qubit tesseract subsystem color code offers a useful comprosmise between protection against errors and overhead. It encodes 4 logical qubits with a code rate of 1/4. By reducing 2 logical qubit from the original [[16,6,4]] code, this code achieves single-shot error correction, with only 2 ancilla qubits. This makes this code practical for current trapped-ion platforms. Recent experiments on Quantinuum hardware[[1]](#references) demonstrated preparing high-fidelity encoded graph states of up to 12 logical qubits, as well as protecting encoded states through 5 rounds of error correction. This repository reproduces these results in simulation, providing modular Stim circuits, noise modelling, and verification tests to support further research on low-overhead fault tolerance.
68

7-
- Circuit implementation of the Tesseract code
8-
- Error simulation and correction
9-
- Jupyter notebooks with various experiments and visualizations
10-
- Utility functions for circuit manipulation and analysis
9+
![Figure 1: Tesseract code structure (from [1])](docs/images/fig1_tesseract_code.png)
1110

12-
## Installation
11+
### Project status
12+
This codebase is an active work-in-progress.
13+
• All building blocks (encoding, noise, measurement, decoder) are implemented.
14+
• The end-to-end error-correction success rate is **not yet at the target level**.
15+
Community testing, bug-fixes, and feature PRs are highly appreciated!
16+
17+
**Disclaimer:** This repository is an independent, community-driven implementation.
18+
It is **not** affiliated with Microsoft, Quantinuum, nor the authors of the original tesseract-code paper.
19+
20+
### Features
21+
22+
- Circuit implementation of the [[16,4,4]] Tesseract subsystem color code [[2]](#references) in Stim, including encoding, error correction rounds and final measurements.
23+
- Simulation of an error correction experiment with configurable noise setting, rounds, shot and more.
24+
- Plotting: sweeping of different parameters and obtaining acceptance rate and logical success rate.
25+
26+
### Implementation details
27+
28+
In order to mimic the original paper's error correction, the different parts of the experiment are implemented in the gate level, and not using Stim's `MPP` stabilizer measurements, or detectors, for example.
29+
The experiment implemented here is an error correction experiment based on Microsoft's paper[[1]](#references), and resembles the experimental setup shown in Figure 8 below.
30+
31+
![Figure 8: Error correction experiment (from [1])](docs/images/fig8_error_correction_experiment.png)
32+
33+
The experiment goes as follows:
34+
35+
1. **Encoding** - The initial state is encoded using the circuits in Fig. 9a or 9b. This part is noiseless for simplicity.
36+
2. **Channel Noise** - Optional noise is applied on all qubits.
37+
3. **Error correction rounds** - Each round is composed of measureing rows/columns and X/Z stabilizers. Measurements results are saved.
38+
4. **Logical measurements** - Qubits are measured by breaking apart the code into two smaller codes. Each code is the [[8,3,2]] color code [[4]](#references). See [measure_logical_operators_tesseract](tesseract_sim/error_correction/measurement_rounds.py) and [verify_final_state](tesseract_sim/error_correction/decoder_manual.py) for more details.
39+
5. **Post processing** - Each shot is accepted or not (based on the error correction rounds); For accepted rounds, the qubits are corrected based on Pauli frame (if enabled in simulation). Next, logical qubits are measured and validated to determine the logical error rate.
40+
41+
### Code
42+
#### Structure
43+
44+
```
45+
tesseract-code-stim/
46+
├── tesseract_sim/ # Main simulation package
47+
│ ├── common/ # Shared utilities and base components
48+
│ │ ├── circuit_base.py # Basic circuit operations and initialization
49+
│ │ └── code_commons.py # Tesseract code definitions (stabilizers, operators)
50+
│ ├── encoding/ # State encoding implementations
51+
│ │ ├── encoding_manual_9a.py # |++0000⟩ encoding (Fig 9a)
52+
│ │ └── encoding_manual_9b.py # |+0+0+0⟩ encoding (Fig 9b)
53+
│ ├── error_correction/ # Error correction and measurement
54+
│ │ ├── correction_rules.py # Correction logic for different error types
55+
│ │ ├── decoder_manual.py # Manual decoder implementation
56+
│ │ └── measurement_rounds.py # Stabilizer measurements and rounds
57+
│ ├── noise/ # Noise modeling and injection
58+
│ │ ├── noise_cfg.py # Noise configuration dataclass
59+
│ │ └── noise_utils.py # Noise injection utilities
60+
│ ├── plotting/ # Visualization and analysis
61+
│ │ └── plot_acceptance_rates.py # Generate acceptance/success rate plots
62+
│ └── run.py # Main simulation entry point
63+
├── notebooks/ # Jupyter notebooks for experiments
64+
│ ├── encoding_circuits_visualization.ipynb # Circuit visualization
65+
│ ├── entire_experiment_circuit.ipynb # Complete experiment demo
66+
│ └── tesseract_stim_simulation_real_decoder.ipynb # Full simulation
67+
├── tests/ # Test suite
68+
│ ├── encoding/ # Encoding tests
69+
│ ├── noise/ # Noise injection tests
70+
│ └── test_*.py # Various experiment and functionality tests
71+
├── plots/ # Generated plot outputs
72+
├── requirements.txt # Python dependencies
73+
└── setup.py # Package configuration
74+
```
75+
76+
#### Key Components
77+
78+
- **`tesseract_sim/run.py`**: Main entry point for running simulations with configurable noise parameters
79+
- **`tesseract_sim/encoding/`**: Two encoding modes based on paper figures 9a and 9b
80+
- **`tesseract_sim/error_correction/`**: Manual decoder with correction rules and measurement rounds
81+
- **`tesseract_sim/noise/`**: Configurable noise injection for encoding and error correction phases
82+
- **`tesseract_sim/plotting/`**: Analysis and visualization tools for acceptance rates and logical success rates
83+
- **`notebooks/`**: Interactive Jupyter notebooks for experiments and visualization
84+
- **`tests/`**: Comprehensive test suite covering all major functionality
85+
86+
## Quick Start
87+
88+
### Installation
1389

1490
```bash
1591
# Clone the repository
@@ -24,19 +100,6 @@ source venv/bin/activate # On Windows: venv\Scripts\activate
24100
pip install -r requirements.txt
25101
```
26102

27-
## Structure
28-
29-
- `circuit_commons.py`: Core circuit components and helper functions
30-
- `stim_simulation_utils.py`: Simulation and plotting utilities
31-
- `stim_utils.py`: Stim-specific helper functions
32-
- Notebooks:
33-
- `01_stim_playground.ipynb`: Initial Stim experiments
34-
- `03_tesseract_stim_simulation_pymatching.ipynb`: Main simulation with PyMatching
35-
- `04_stim_example_5_qubit_code.ipynb`: Five-qubit code reference
36-
- And more...
37-
38-
## Usage
39-
40103
The main workflow is through Jupyter notebooks. After installation:
41104

42105
```bash
@@ -45,9 +108,9 @@ jupyter notebook
45108

46109
Then navigate to one of the notebooks to run experiments and simulations.
47110

48-
### Running Simulations with Noise
111+
### Running Simulations
49112

50-
The `tesseract_sim/run.py` script now supports configurable noise injection during encoding and error correction phases. You can control whether noise is active and set independent 1-qubit and 2-qubit error rates for each phase.
113+
The `tesseract_sim/run.py` script supports configurable noise injection during encoding and error correction phases. You can control whether noise is active and set independent 1-qubit and 2-qubit error rates for each phase.
51114

52115
```bash
53116
python -m tesseract_sim.run --help
@@ -88,33 +151,47 @@ The `plotting/plot_acceptance_rates.py` script generates acceptance and logical
88151

89152
* **Generate plots with Pauli frame correction enabled and 9a encoding:**
90153
```bash
91-
python plotting/plot_acceptance_rates.py --apply_pauli_frame true --encoding-mode 9a
154+
python tesseract_sim/plotting/plot_acceptance_rates.py --apply_pauli_frame true --encoding-mode 9a
92155
```
93156

94157
* **Generate plots with Pauli frame correction disabled and 9a encoding:**
95158
```bash
96-
python plotting/plot_acceptance_rates.py --apply_pauli_frame false --encoding-mode 9a
159+
python tesseract_sim/plotting/plot_acceptance_rates.py --apply_pauli_frame false --encoding-mode 9a
97160
```
98161

99162
* **Generate plots with 9b encoding and custom shot count:**
100163
```bash
101-
python plotting/plot_acceptance_rates.py --apply_pauli_frame true --encoding-mode 9b --shots 5000
164+
python tesseract_sim/plotting/plot_acceptance_rates.py --apply_pauli_frame true --encoding-mode 9b --shots 5000
102165
```
103166

104167
* **Generate plots with channel noise sweep instead of EC noise:**
105168
```bash
106-
python plotting/plot_acceptance_rates.py --apply_pauli_frame true --encoding-mode 9a --sweep-channel-noise
169+
python tesseract_sim/plotting/plot_acceptance_rates.py --apply_pauli_frame true --encoding-mode 9a --sweep-channel-noise
107170
```
108171

109172
* **Specify custom output directory:**
110173
```bash
111-
python plotting/plot_acceptance_rates.py --apply_pauli_frame true --encoding-mode 9a --out-dir ./custom_plots
174+
python tesseract_sim/plotting/plot_acceptance_rates.py --apply_pauli_frame true --encoding-mode 9a --out-dir ./custom_plots
112175
```
113176

114177
The script generates two types of plots:
115178
- **Acceptance Rate Plots**: Show how well the error correction accepts states across different noise levels and rounds
116179
- **Logical Success Rate Plots**: Show the conditional probability of logical success given acceptance
117180

181+
**Example Results:**
182+
183+
![Acceptance Rates vs Error Correction Noise](plots/acceptance_rates_ec_noise_ec_experiment.png)
184+
185+
## References
186+
187+
[1] B. W. Reichardt et al., "Demonstration of quantum computation and error correction with a tesseract code", (2024) [arXiv:2409.04628](https://arxiv.org/abs/2409.04628)
188+
189+
[2] "\([[16,6,4]]\) Tesseract color code", The Error Correction Zoo (V. V. Albert & P. Faist, eds.), 2024. https://errorcorrectionzoo.org/c/stab_16_6_4
190+
191+
[3] C. Gidney, "Stim: a fast stabilizer circuit simulator", Quantum 5, 497 (2021). https://doi.org/10.22331/q-2021-07-06-497
192+
193+
[4] E. Campbell, "The smallest interesting colour code", (2016). https://earltcampbell.com/2016/09/26/the-smallest-interesting-colour-code/
194+
118195
## License
119196

120197
MIT License
200 KB
Loading
229 KB
Loading

docs/images/fig9_encoding.png

265 KB
Loading

notebooks/encoding_circuits_visualization.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
}
1919
},
2020
"source": [
21-
"import stim\n",
2221
"\n",
23-
"from tesseract_sim.circuit_base import init_circuit\n",
24-
"from tesseract_sim.encoding_manual_9a import encode_manual_fig9a\n",
25-
"from tesseract_sim.encoding_manual_9b import encode_manual_fig9b\n",
26-
"from tesseract_sim.noise_cfg import NO_NOISE\n"
22+
"\n",
23+
"from tesseract_sim.common.circuit_base import init_circuit\n",
24+
"from tesseract_sim.encoding.encoding_manual_9a import encode_manual_fig9a\n",
25+
"from tesseract_sim.encoding.encoding_manual_9b import encode_manual_fig9b\n",
26+
"from tesseract_sim.noise.noise_cfg import NO_NOISE\n"
2727
],
2828
"outputs": [],
2929
"execution_count": 2

notebooks/entire_experiment_circuit.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
],
100100
"source": [
101101
"from tesseract_sim.run import build_circuit_ec_experiment\n",
102-
"from tesseract_sim.noise_cfg import NO_NOISE\n",
102+
"from tesseract_sim.noise.noise_cfg import NO_NOISE\n",
103103
"\n",
104104
"# Initialize circuit with experiment 2 parameters\n",
105105
"circuit = build_circuit_ec_experiment(rounds=1, cfg=NO_NOISE, encoding_mode='9a')\n",

notebooks/tesseract_stim_simulation_real_decoder.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"%autoreload 2\n",
1313
"\n",
1414
"import stim\n",
15-
"import numpy as np\n",
15+
"\n",
1616
"print(stim.__version__)"
1717
]
1818
},
@@ -74,7 +74,7 @@
7474
"SHOTS = 100000\n",
7575
"\n",
7676
"# Create noise configuration\n",
77-
"from tesseract_sim.noise_cfg import NoiseCfg\n",
77+
"from tesseract_sim.noise.noise_cfg import NoiseCfg\n",
7878
"noise_cfg = NoiseCfg(\n",
7979
" ec_active=True, # Enable noise during error correction\n",
8080
" ec_rate_1q=0.001, # 0.1% error rate for 1-qubit gates\n",
@@ -123,7 +123,7 @@
123123
"SHOTS = 100000\n",
124124
"\n",
125125
"# Create noise configuration\n",
126-
"from tesseract_sim.noise_cfg import NoiseCfg\n",
126+
"from tesseract_sim.noise.noise_cfg import NoiseCfg\n",
127127
"noise_cfg = NoiseCfg(\n",
128128
" ec_active=True, # Enable noise during error correction\n",
129129
" ec_rate_1q=0.001, # 0.1% error rate for 1-qubit gates\n",

0 commit comments

Comments
 (0)