From cea529dff10d460b516e34c24b5bdc724bf84f82 Mon Sep 17 00:00:00 2001 From: Iftach Yakar Date: Mon, 22 Sep 2025 00:02:37 +0300 Subject: [PATCH] Added stim circuits --- README.md | 9 ++ stim_circuits/README.md | 118 +++++++++++++++ stim_circuits/complete_experiment_9a.stim | 172 ++++++++++++++++++++++ stim_circuits/complete_experiment_9b.stim | 170 +++++++++++++++++++++ stim_circuits/decoding.stim | 38 +++++ stim_circuits/encoding_9a.stim | 45 ++++++ stim_circuits/encoding_9b.stim | 43 ++++++ stim_circuits/error_correction_round.stim | 75 ++++++++++ stim_circuits/generate_stim_files.py | 150 +++++++++++++++++++ 9 files changed, 820 insertions(+) create mode 100644 stim_circuits/README.md create mode 100644 stim_circuits/complete_experiment_9a.stim create mode 100644 stim_circuits/complete_experiment_9b.stim create mode 100644 stim_circuits/decoding.stim create mode 100644 stim_circuits/encoding_9a.stim create mode 100644 stim_circuits/encoding_9b.stim create mode 100644 stim_circuits/error_correction_round.stim create mode 100644 stim_circuits/generate_stim_files.py diff --git a/README.md b/README.md index 9c36fa7..807ee93 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ python tesseract_sim/plotting/plot_acceptance_rates.py \ ### Features - Circuit implementation of the [[16,4,4]] Tesseract subsystem color code [[1]](#references) in Stim, including encoding, error correction rounds and final measurements. +- Pre-generated stim circuit files for common operations (see [`stim_circuits/`](stim_circuits/README.md) directory). - Simulation of an error correction experiment with configurable noise setting, rounds, shot and more. - Plotting: sweeping of different parameters and obtaining acceptance rate and logical success rate. @@ -82,6 +83,13 @@ tesseract-code-stim/ │ ├── plotting/ # Visualization and analysis │ │ └── plot_acceptance_rates.py # Generate acceptance/success rate plots │ └── run.py # Main simulation entry point +├── stim_circuits/ # Pre-generated stim circuit files +│ ├── encoding_9a.stim # Encoding circuit for |++0000⟩ state +│ ├── encoding_9b.stim # Encoding circuit for |+0+0+0⟩ state +│ ├── error_correction_round.stim # Single EC round circuit +│ ├── decoding.stim # Final measurement and decoding +│ ├── complete_experiment_*.stim # Full experiment pipelines +│ └── README.md # Stim circuits documentation and usage ├── notebooks/ # Jupyter notebooks for experiments │ ├── encoding_circuits_visualization.ipynb # Circuit visualization │ ├── entire_experiment_circuit.ipynb # Complete experiment demo @@ -102,6 +110,7 @@ tesseract-code-stim/ - **`tesseract_sim/error_correction/`**: Manual decoder with correction rules and measurement rounds - **`tesseract_sim/noise/`**: Configurable noise injection for encoding and error correction phases - **`tesseract_sim/plotting/`**: Analysis and visualization tools for acceptance rates and logical success rates +- **`stim_circuits/`**: Pre-generated stim circuit files for direct use with stim or other simulators - **`notebooks/`**: Interactive Jupyter notebooks for experiments and visualization - **`tests/`**: Comprehensive test suite covering all major functionality diff --git a/stim_circuits/README.md b/stim_circuits/README.md new file mode 100644 index 0000000..44dbb71 --- /dev/null +++ b/stim_circuits/README.md @@ -0,0 +1,118 @@ +# Tesseract Code Stim Circuit Files + +This directory contains pre-generated stim circuit files for common tesseract code operations. These files can be used independently with stim. +**An important note** is that the calculation of the Pauli frame is not included in these files, since it is achieved by classical computation done using Python code, outside of the Stim runtime. This means that running these circuits will not do the actual correction at the end. Check the Python code for the complete implementation. + +## Generated Files + +### 1. Encoding Circuits + +- **`encoding_9a.stim`** - Encoding circuit for the |++0000⟩ state (Fig 9a) + - 23 qubits total (16 data + 7 ancillas) + - Prepares the tesseract code in the |++0000⟩ logical state + +- **`encoding_9b.stim`** - Encoding circuit for the |+0+0+0⟩ state (Fig 9b) + - 18 qubits total (16 data + 2 ancillas) + - Prepares the tesseract code in the |+0+0+0⟩ logical state + +### 2. Error Correction + +- **`error_correction_round.stim`** - Single round of error correction + - 18 qubits total (16 data + 2 ancillas) + - Measures stabilizers for both rows and columns + - Includes flagged syndrome extraction + +### 3. Decoding + +- **`decoding.stim`** - Final measurement and decoding by diving to two [[8,3,2]] color codes + - 18 qubits total (16 data + 2 ancillas) + - Splits the tesseract code into two [[8,3,2]] color codes + - Measures top half in X basis, bottom half in Z basis + +### 4. Complete Experiments + +- **`complete_experiment_9a.stim`** - Full experiment with 9a encoding + - Encoding (9a) → 3 rounds of error correction → decoding + - 23 qubits total + +- **`complete_experiment_9b.stim`** - Full experiment with 9b encoding + - Encoding (9b) → 3 rounds of error correction → decoding + - 18 qubits total + +## Qubit Layout + +All circuits use a consistent qubit layout: + +``` +Data qubits (0-15) arranged in 4×4 grid: +Row 1: 0 1 2 3 (y=0, x=0-3) +Row 2: 4 5 6 7 (y=1, x=0-3) +Row 3: 8 9 10 11 (y=2, x=0-3) +Row 4: 12 13 14 15 (y=3, x=0-3) + +Ancilla qubits: 16, 17, ... (at x=5, y=0,1,2,...) +``` + +## Usage Examples + +### Using with stim directly + +```bash +# Simulate the complete 9b experiment with 1000 shots +stim sample --shots 1000 --in complete_experiment_9a.stim +``` + +### Using in Python + +```python +import stim + +# Load a circuit +circuit = stim.Circuit.from_file("encoding_9a.stim") + +# Run simulation +sampler = circuit.compile_sampler() +samples = sampler.sample(shots=1000) + +# Get detector error model +dem = circuit.detector_error_model() +``` + +### Combining circuits + +```python +import stim + +# Load individual components +encoding = stim.Circuit.from_file("encoding_9a.stim") +ec_round = stim.Circuit.from_file("error_correction_round.stim") +decoding = stim.Circuit.from_file("decoding.stim") + +# Combine into custom experiment +custom_circuit = stim.Circuit() +custom_circuit += encoding + +# Add noise channel +custom_circuit.append("DEPOLARIZE1", range(16), 0.001) + +# Add multiple EC rounds +for _ in range(5): # 5 rounds instead of 3 + custom_circuit += ec_round + +custom_circuit += decoding +``` + +## Generation + +These files were generated using `generate_stim_files.py`, which uses the existing tesseract-code-stim Python implementation to build the circuits and export them to stim format. + +To regenerate the files: + +```bash +python stim_circuits/generate_stim_files.py +``` + +## Related Papers + +- "Demonstration of quantum computation and error correction with a tesseract code" - http://arxiv.org/abs/2409.04628 +- "The smallest interesting colour code" by Earl Campbell - https://earltcampbell.com/2016/09/26/the-smallest-interesting-colour-code/ diff --git a/stim_circuits/complete_experiment_9a.stim b/stim_circuits/complete_experiment_9a.stim new file mode 100644 index 0000000..b6fd427 --- /dev/null +++ b/stim_circuits/complete_experiment_9a.stim @@ -0,0 +1,172 @@ +# Complete experiment: encoding 9a -> 3 EC rounds -> decoding +# Generated from tesseract-code-stim project +# Circuit has 23 qubits total +# +# Qubit layout: +# - Qubits 0-15: Data qubits arranged in 4x4 grid +# - Qubits 16+: Ancilla qubits for measurements +# +# Coordinate system: +# - Qubits 0-3: row 1 (y=0, x=0-3) +# - Qubits 4-7: row 2 (y=1, x=0-3) +# - Qubits 8-11: row 3 (y=2, x=0-3) +# - Qubits 12-15: row 4 (y=3, x=0-3) +# + +QUBIT_COORDS(0, 0) 0 +QUBIT_COORDS(1, 0) 1 +QUBIT_COORDS(2, 0) 2 +QUBIT_COORDS(3, 0) 3 +QUBIT_COORDS(0, 1) 4 +QUBIT_COORDS(1, 1) 5 +QUBIT_COORDS(2, 1) 6 +QUBIT_COORDS(3, 1) 7 +QUBIT_COORDS(0, 2) 8 +QUBIT_COORDS(1, 2) 9 +QUBIT_COORDS(2, 2) 10 +QUBIT_COORDS(3, 2) 11 +QUBIT_COORDS(0, 3) 12 +QUBIT_COORDS(1, 3) 13 +QUBIT_COORDS(2, 3) 14 +QUBIT_COORDS(3, 3) 15 +QUBIT_COORDS(5, 0) 16 +QUBIT_COORDS(5, 1) 17 +QUBIT_COORDS(5, 2) 18 +QUBIT_COORDS(5, 3) 19 +QUBIT_COORDS(5, 4) 20 +QUBIT_COORDS(5, 5) 21 +QUBIT_COORDS(5, 6) 22 +TICK # Start encoding based on Fig. 9a +H 0 1 2 3 4 8 12 +CX 4 20 4 5 4 6 4 7 4 20 8 21 8 9 8 10 8 11 8 21 12 22 12 13 12 14 12 15 12 22 0 16 1 17 2 18 3 19 0 4 1 5 2 6 3 7 0 8 1 9 2 10 3 11 0 12 1 13 2 14 3 15 0 16 1 17 2 18 3 19 +R 18 19 +H 19 +CX 19 18 19 0 19 1 19 2 19 3 19 18 +TICK # Start error correction round 1 +R 16 17 # Start measure row 1 +H 16 +CX 0 17 16 1 1 17 16 0 2 17 16 3 3 17 16 2 +H 16 +M 16 17 +R 16 17 # Start measure row 2 +H 16 +CX 4 17 16 5 5 17 16 4 6 17 16 7 7 17 16 6 +H 16 +M 16 17 +R 16 17 # Start measure row 3 +H 16 +CX 8 17 16 9 9 17 16 8 10 17 16 11 11 17 16 10 +H 16 +M 16 17 +R 16 17 # Start measure row 4 +H 16 +CX 12 17 16 13 13 17 16 12 14 17 16 15 15 17 16 14 +H 16 +M 16 17 +R 16 17 # Start measure column 1 +H 16 +CX 0 17 16 4 4 17 16 0 8 17 16 12 12 17 16 8 +H 16 +M 16 17 +R 16 17 # Start measure column 2 +H 16 +CX 1 17 16 5 5 17 16 1 9 17 16 13 13 17 16 9 +H 16 +M 16 17 +R 16 17 # Start measure column 3 +H 16 +CX 2 17 16 6 6 17 16 2 10 17 16 14 14 17 16 10 +H 16 +M 16 17 +R 16 17 # Start measure column 4 +H 16 +CX 3 17 16 7 7 17 16 3 11 17 16 15 15 17 16 11 +H 16 +M 16 17 +TICK # Start error correction round 2 +R 16 17 # Start measure row 1 +H 16 +CX 0 17 16 1 1 17 16 0 2 17 16 3 3 17 16 2 +H 16 +M 16 17 +R 16 17 # Start measure row 2 +H 16 +CX 4 17 16 5 5 17 16 4 6 17 16 7 7 17 16 6 +H 16 +M 16 17 +R 16 17 # Start measure row 3 +H 16 +CX 8 17 16 9 9 17 16 8 10 17 16 11 11 17 16 10 +H 16 +M 16 17 +R 16 17 # Start measure row 4 +H 16 +CX 12 17 16 13 13 17 16 12 14 17 16 15 15 17 16 14 +H 16 +M 16 17 +R 16 17 # Start measure column 1 +H 16 +CX 0 17 16 4 4 17 16 0 8 17 16 12 12 17 16 8 +H 16 +M 16 17 +R 16 17 # Start measure column 2 +H 16 +CX 1 17 16 5 5 17 16 1 9 17 16 13 13 17 16 9 +H 16 +M 16 17 +R 16 17 # Start measure column 3 +H 16 +CX 2 17 16 6 6 17 16 2 10 17 16 14 14 17 16 10 +H 16 +M 16 17 +R 16 17 # Start measure column 4 +H 16 +CX 3 17 16 7 7 17 16 3 11 17 16 15 15 17 16 11 +H 16 +M 16 17 +TICK # Start error correction round 3 +R 16 17 # Start measure row 1 +H 16 +CX 0 17 16 1 1 17 16 0 2 17 16 3 3 17 16 2 +H 16 +M 16 17 +R 16 17 # Start measure row 2 +H 16 +CX 4 17 16 5 5 17 16 4 6 17 16 7 7 17 16 6 +H 16 +M 16 17 +R 16 17 # Start measure row 3 +H 16 +CX 8 17 16 9 9 17 16 8 10 17 16 11 11 17 16 10 +H 16 +M 16 17 +R 16 17 # Start measure row 4 +H 16 +CX 12 17 16 13 13 17 16 12 14 17 16 15 15 17 16 14 +H 16 +M 16 17 +R 16 17 # Start measure column 1 +H 16 +CX 0 17 16 4 4 17 16 0 8 17 16 12 12 17 16 8 +H 16 +M 16 17 +R 16 17 # Start measure column 2 +H 16 +CX 1 17 16 5 5 17 16 1 9 17 16 13 13 17 16 9 +H 16 +M 16 17 +R 16 17 # Start measure column 3 +H 16 +CX 2 17 16 6 6 17 16 2 10 17 16 14 14 17 16 10 +H 16 +M 16 17 +R 16 17 # Start measure column 4 +H 16 +CX 3 17 16 7 7 17 16 3 11 17 16 15 15 17 16 11 +H 16 +M 16 17 +TICK +CX 0 12 1 13 2 14 3 15 4 8 5 9 6 10 7 11 # row-transversal CNOT from row 1 -> 4, row 2 -> 3 +H 0 1 2 3 4 5 6 7 # Change top half's basis so its measured in the X basis +M 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # Measure all physical qubits +TICK \ No newline at end of file diff --git a/stim_circuits/complete_experiment_9b.stim b/stim_circuits/complete_experiment_9b.stim new file mode 100644 index 0000000..ccd283b --- /dev/null +++ b/stim_circuits/complete_experiment_9b.stim @@ -0,0 +1,170 @@ +# Complete experiment: encoding 9b -> 3 EC rounds -> decoding +# Generated from tesseract-code-stim project +# Circuit has 18 qubits total +# +# Qubit layout: +# - Qubits 0-15: Data qubits arranged in 4x4 grid +# - Qubits 16+: Ancilla qubits for measurements +# +# Coordinate system: +# - Qubits 0-3: row 1 (y=0, x=0-3) +# - Qubits 4-7: row 2 (y=1, x=0-3) +# - Qubits 8-11: row 3 (y=2, x=0-3) +# - Qubits 12-15: row 4 (y=3, x=0-3) +# + +QUBIT_COORDS(0, 0) 0 +QUBIT_COORDS(1, 0) 1 +QUBIT_COORDS(2, 0) 2 +QUBIT_COORDS(3, 0) 3 +QUBIT_COORDS(0, 1) 4 +QUBIT_COORDS(1, 1) 5 +QUBIT_COORDS(2, 1) 6 +QUBIT_COORDS(3, 1) 7 +QUBIT_COORDS(0, 2) 8 +QUBIT_COORDS(1, 2) 9 +QUBIT_COORDS(2, 2) 10 +QUBIT_COORDS(3, 2) 11 +QUBIT_COORDS(0, 3) 12 +QUBIT_COORDS(1, 3) 13 +QUBIT_COORDS(2, 3) 14 +QUBIT_COORDS(3, 3) 15 +QUBIT_COORDS(5, 0) 16 +QUBIT_COORDS(5, 1) 17 +TICK # Start encoding based on Fig. 9b +H 17 0 3 6 7 +CX 0 1 6 2 0 4 6 5 0 5 6 1 3 2 7 4 3 4 7 1 3 5 7 2 17 0 1 16 17 1 0 16 17 2 3 16 17 3 2 16 +H 17 +M 17 16 +H 0 1 2 3 4 5 6 7 17 8 11 14 15 +CX 8 9 14 10 8 12 14 13 8 13 14 9 11 10 15 12 11 12 15 9 11 13 15 10 17 8 9 16 17 9 8 16 17 10 11 16 17 11 10 16 +H 17 +M 17 16 +TICK # Start error correction round 1 +R 16 17 # Start measure row 1 +H 16 +CX 0 17 16 1 1 17 16 0 2 17 16 3 3 17 16 2 +H 16 +M 16 17 +R 16 17 # Start measure row 2 +H 16 +CX 4 17 16 5 5 17 16 4 6 17 16 7 7 17 16 6 +H 16 +M 16 17 +R 16 17 # Start measure row 3 +H 16 +CX 8 17 16 9 9 17 16 8 10 17 16 11 11 17 16 10 +H 16 +M 16 17 +R 16 17 # Start measure row 4 +H 16 +CX 12 17 16 13 13 17 16 12 14 17 16 15 15 17 16 14 +H 16 +M 16 17 +R 16 17 # Start measure column 1 +H 16 +CX 0 17 16 4 4 17 16 0 8 17 16 12 12 17 16 8 +H 16 +M 16 17 +R 16 17 # Start measure column 2 +H 16 +CX 1 17 16 5 5 17 16 1 9 17 16 13 13 17 16 9 +H 16 +M 16 17 +R 16 17 # Start measure column 3 +H 16 +CX 2 17 16 6 6 17 16 2 10 17 16 14 14 17 16 10 +H 16 +M 16 17 +R 16 17 # Start measure column 4 +H 16 +CX 3 17 16 7 7 17 16 3 11 17 16 15 15 17 16 11 +H 16 +M 16 17 +TICK # Start error correction round 2 +R 16 17 # Start measure row 1 +H 16 +CX 0 17 16 1 1 17 16 0 2 17 16 3 3 17 16 2 +H 16 +M 16 17 +R 16 17 # Start measure row 2 +H 16 +CX 4 17 16 5 5 17 16 4 6 17 16 7 7 17 16 6 +H 16 +M 16 17 +R 16 17 # Start measure row 3 +H 16 +CX 8 17 16 9 9 17 16 8 10 17 16 11 11 17 16 10 +H 16 +M 16 17 +R 16 17 # Start measure row 4 +H 16 +CX 12 17 16 13 13 17 16 12 14 17 16 15 15 17 16 14 +H 16 +M 16 17 +R 16 17 # Start measure column 1 +H 16 +CX 0 17 16 4 4 17 16 0 8 17 16 12 12 17 16 8 +H 16 +M 16 17 +R 16 17 # Start measure column 2 +H 16 +CX 1 17 16 5 5 17 16 1 9 17 16 13 13 17 16 9 +H 16 +M 16 17 +R 16 17 # Start measure column 3 +H 16 +CX 2 17 16 6 6 17 16 2 10 17 16 14 14 17 16 10 +H 16 +M 16 17 +R 16 17 # Start measure column 4 +H 16 +CX 3 17 16 7 7 17 16 3 11 17 16 15 15 17 16 11 +H 16 +M 16 17 +TICK # Start error correction round 3 +R 16 17 # Start measure row 1 +H 16 +CX 0 17 16 1 1 17 16 0 2 17 16 3 3 17 16 2 +H 16 +M 16 17 +R 16 17 # Start measure row 2 +H 16 +CX 4 17 16 5 5 17 16 4 6 17 16 7 7 17 16 6 +H 16 +M 16 17 +R 16 17 # Start measure row 3 +H 16 +CX 8 17 16 9 9 17 16 8 10 17 16 11 11 17 16 10 +H 16 +M 16 17 +R 16 17 # Start measure row 4 +H 16 +CX 12 17 16 13 13 17 16 12 14 17 16 15 15 17 16 14 +H 16 +M 16 17 +R 16 17 # Start measure column 1 +H 16 +CX 0 17 16 4 4 17 16 0 8 17 16 12 12 17 16 8 +H 16 +M 16 17 +R 16 17 # Start measure column 2 +H 16 +CX 1 17 16 5 5 17 16 1 9 17 16 13 13 17 16 9 +H 16 +M 16 17 +R 16 17 # Start measure column 3 +H 16 +CX 2 17 16 6 6 17 16 2 10 17 16 14 14 17 16 10 +H 16 +M 16 17 +R 16 17 # Start measure column 4 +H 16 +CX 3 17 16 7 7 17 16 3 11 17 16 15 15 17 16 11 +H 16 +M 16 17 +TICK +CX 0 12 1 13 2 14 3 15 4 8 5 9 6 10 7 11 # row-transversal CNOT from row 1 -> 4, row 2 -> 3 +H 0 1 2 3 4 5 6 7 # Change top half's basis so its measured in the X basis +M 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # Measure all physical qubits +TICK \ No newline at end of file diff --git a/stim_circuits/decoding.stim b/stim_circuits/decoding.stim new file mode 100644 index 0000000..4e9ac65 --- /dev/null +++ b/stim_circuits/decoding.stim @@ -0,0 +1,38 @@ +# Decoding with two [[8,3,2]] color codes + measurement +# Generated from tesseract-code-stim project +# Circuit has 18 qubits total +# +# Qubit layout: +# - Qubits 0-15: Data qubits arranged in 4x4 grid +# - Qubits 16+: Ancilla qubits for measurements +# +# Coordinate system: +# - Qubits 0-3: row 1 (y=0, x=0-3) +# - Qubits 4-7: row 2 (y=1, x=0-3) +# - Qubits 8-11: row 3 (y=2, x=0-3) +# - Qubits 12-15: row 4 (y=3, x=0-3) +# + +QUBIT_COORDS(0, 0) 0 +QUBIT_COORDS(1, 0) 1 +QUBIT_COORDS(2, 0) 2 +QUBIT_COORDS(3, 0) 3 +QUBIT_COORDS(0, 1) 4 +QUBIT_COORDS(1, 1) 5 +QUBIT_COORDS(2, 1) 6 +QUBIT_COORDS(3, 1) 7 +QUBIT_COORDS(0, 2) 8 +QUBIT_COORDS(1, 2) 9 +QUBIT_COORDS(2, 2) 10 +QUBIT_COORDS(3, 2) 11 +QUBIT_COORDS(0, 3) 12 +QUBIT_COORDS(1, 3) 13 +QUBIT_COORDS(2, 3) 14 +QUBIT_COORDS(3, 3) 15 +QUBIT_COORDS(5, 0) 16 +QUBIT_COORDS(5, 1) 17 +TICK +CX 0 12 1 13 2 14 3 15 4 8 5 9 6 10 7 11 # row-transversal CNOT from row 1 -> 4, row 2 -> 3 +H 0 1 2 3 4 5 6 7 # Change top half's basis so its measured in the X basis +M 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # Measure # Measure all physical qubits +TICK \ No newline at end of file diff --git a/stim_circuits/encoding_9a.stim b/stim_circuits/encoding_9a.stim new file mode 100644 index 0000000..5be6176 --- /dev/null +++ b/stim_circuits/encoding_9a.stim @@ -0,0 +1,45 @@ +# Encoding 9a circuit (|++0000> state) +# Generated from tesseract-code-stim project +# Circuit has 23 qubits total +# +# Qubit layout: +# - Qubits 0-15: Data qubits arranged in 4x4 grid +# - Qubits 16+: Ancilla qubits for measurements +# +# Coordinate system: +# - Qubits 0-3: row 1 (y=0, x=0-3) +# - Qubits 4-7: row 2 (y=1, x=0-3) +# - Qubits 8-11: row 3 (y=2, x=0-3) +# - Qubits 12-15: row 4 (y=3, x=0-3) +# + +QUBIT_COORDS(0, 0) 0 +QUBIT_COORDS(1, 0) 1 +QUBIT_COORDS(2, 0) 2 +QUBIT_COORDS(3, 0) 3 +QUBIT_COORDS(0, 1) 4 +QUBIT_COORDS(1, 1) 5 +QUBIT_COORDS(2, 1) 6 +QUBIT_COORDS(3, 1) 7 +QUBIT_COORDS(0, 2) 8 +QUBIT_COORDS(1, 2) 9 +QUBIT_COORDS(2, 2) 10 +QUBIT_COORDS(3, 2) 11 +QUBIT_COORDS(0, 3) 12 +QUBIT_COORDS(1, 3) 13 +QUBIT_COORDS(2, 3) 14 +QUBIT_COORDS(3, 3) 15 +QUBIT_COORDS(5, 0) 16 +QUBIT_COORDS(5, 1) 17 +QUBIT_COORDS(5, 2) 18 +QUBIT_COORDS(5, 3) 19 +QUBIT_COORDS(5, 4) 20 +QUBIT_COORDS(5, 5) 21 +QUBIT_COORDS(5, 6) 22 +TICK # Start encoding based on Fig. 9a +H 0 1 2 3 4 8 12 +CX 4 20 4 5 4 6 4 7 4 20 8 21 8 9 8 10 8 11 8 21 12 22 12 13 12 14 12 15 12 22 0 16 1 17 2 18 3 19 0 4 1 5 2 6 3 7 0 8 1 9 2 10 3 11 0 12 1 13 2 14 3 15 0 16 1 17 2 18 3 19 +R 18 19 +H 19 +CX 19 18 19 0 19 1 19 2 19 3 19 18 +TICK \ No newline at end of file diff --git a/stim_circuits/encoding_9b.stim b/stim_circuits/encoding_9b.stim new file mode 100644 index 0000000..d3fbed2 --- /dev/null +++ b/stim_circuits/encoding_9b.stim @@ -0,0 +1,43 @@ +# Encoding 9b circuit (|+0+0+0> state) +# Generated from tesseract-code-stim project +# Circuit has 18 qubits total +# +# Qubit layout: +# - Qubits 0-15: Data qubits arranged in 4x4 grid +# - Qubits 16+: Ancilla qubits for measurements +# +# Coordinate system: +# - Qubits 0-3: row 1 (y=0, x=0-3) +# - Qubits 4-7: row 2 (y=1, x=0-3) +# - Qubits 8-11: row 3 (y=2, x=0-3) +# - Qubits 12-15: row 4 (y=3, x=0-3) +# + +QUBIT_COORDS(0, 0) 0 +QUBIT_COORDS(1, 0) 1 +QUBIT_COORDS(2, 0) 2 +QUBIT_COORDS(3, 0) 3 +QUBIT_COORDS(0, 1) 4 +QUBIT_COORDS(1, 1) 5 +QUBIT_COORDS(2, 1) 6 +QUBIT_COORDS(3, 1) 7 +QUBIT_COORDS(0, 2) 8 +QUBIT_COORDS(1, 2) 9 +QUBIT_COORDS(2, 2) 10 +QUBIT_COORDS(3, 2) 11 +QUBIT_COORDS(0, 3) 12 +QUBIT_COORDS(1, 3) 13 +QUBIT_COORDS(2, 3) 14 +QUBIT_COORDS(3, 3) 15 +QUBIT_COORDS(5, 0) 16 +QUBIT_COORDS(5, 1) 17 +TICK # Start encoding based on Fig. 9b +H 17 0 3 6 7 +CX 0 1 6 2 0 4 6 5 0 5 6 1 3 2 7 4 3 4 7 1 3 5 7 2 17 0 1 16 17 1 0 16 17 2 3 16 17 3 2 16 +H 17 +M 17 16 +H 0 1 2 3 4 5 6 7 17 8 11 14 15 +CX 8 9 14 10 8 12 14 13 8 13 14 9 11 10 15 12 11 12 15 9 11 13 15 10 17 8 9 16 17 9 8 16 17 10 11 16 17 11 10 16 +H 17 +M 17 16 +TICK \ No newline at end of file diff --git a/stim_circuits/error_correction_round.stim b/stim_circuits/error_correction_round.stim new file mode 100644 index 0000000..511bfbf --- /dev/null +++ b/stim_circuits/error_correction_round.stim @@ -0,0 +1,75 @@ +# Single error correction round (rows + columns) +# Generated from tesseract-code-stim project +# Circuit has 18 qubits total +# +# Qubit layout: +# - Qubits 0-15: Data qubits arranged in 4x4 grid +# - Qubits 16+: Ancilla qubits for measurements +# +# Coordinate system: +# - Qubits 0-3: row 1 (y=0, x=0-3) +# - Qubits 4-7: row 2 (y=1, x=0-3) +# - Qubits 8-11: row 3 (y=2, x=0-3) +# - Qubits 12-15: row 4 (y=3, x=0-3) +# + +QUBIT_COORDS(0, 0) 0 +QUBIT_COORDS(1, 0) 1 +QUBIT_COORDS(2, 0) 2 +QUBIT_COORDS(3, 0) 3 +QUBIT_COORDS(0, 1) 4 +QUBIT_COORDS(1, 1) 5 +QUBIT_COORDS(2, 1) 6 +QUBIT_COORDS(3, 1) 7 +QUBIT_COORDS(0, 2) 8 +QUBIT_COORDS(1, 2) 9 +QUBIT_COORDS(2, 2) 10 +QUBIT_COORDS(3, 2) 11 +QUBIT_COORDS(0, 3) 12 +QUBIT_COORDS(1, 3) 13 +QUBIT_COORDS(2, 3) 14 +QUBIT_COORDS(3, 3) 15 +QUBIT_COORDS(5, 0) 16 +QUBIT_COORDS(5, 1) 17 +TICK +R 16 17 # Start measure row 1 +H 16 +CX 0 17 16 1 1 17 16 0 2 17 16 3 3 17 16 2 +H 16 +M 16 17 +R 16 17 # Start measure row 2 +H 16 +CX 4 17 16 5 5 17 16 4 6 17 16 7 7 17 16 6 +H 16 +M 16 17 +R 16 17 # Start measure row 3 +H 16 +CX 8 17 16 9 9 17 16 8 10 17 16 11 11 17 16 10 +H 16 +M 16 17 +R 16 17 # Start measure row 4 +H 16 +CX 12 17 16 13 13 17 16 12 14 17 16 15 15 17 16 14 +H 16 +M 16 17 +R 16 17 # Start measure column 1 +H 16 +CX 0 17 16 4 4 17 16 0 8 17 16 12 12 17 16 8 +H 16 +M 16 17 +R 16 17 # Start measure column 2 +H 16 +CX 1 17 16 5 5 17 16 1 9 17 16 13 13 17 16 9 +H 16 +M 16 17 +R 16 17 # Start measure column 3 +H 16 +CX 2 17 16 6 6 17 16 2 10 17 16 14 14 17 16 10 +H 16 +M 16 17 +R 16 17 # Start measure column 4 +H 16 +CX 3 17 16 7 7 17 16 3 11 17 16 15 15 17 16 11 +H 16 +M 16 17 +TICK \ No newline at end of file diff --git a/stim_circuits/generate_stim_files.py b/stim_circuits/generate_stim_files.py new file mode 100644 index 0000000..da8c4ef --- /dev/null +++ b/stim_circuits/generate_stim_files.py @@ -0,0 +1,150 @@ +#!/usr/bin/env python3 +""" +Script to generate stim circuit files for common tesseract code circuits. +This script uses the existing circuit building functions from run.py to generate +.stim files that can be used independently. +""" + +import sys +import os +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from tesseract_sim.run import build_encoding_circuit, build_error_correction_circuit +from tesseract_sim.common.circuit_base import init_circuit +from tesseract_sim.encoding.encoding_manual_9a import encode_manual_fig9a +from tesseract_sim.encoding.encoding_manual_9b import encode_manual_fig9b +from tesseract_sim.error_correction.measurement_rounds import ( + error_correction_round_rows, + error_correction_round_columns, + measure_logical_operators_tesseract +) +from tesseract_sim.noise.noise_cfg import NO_NOISE + + +def generate_encoding_9a_stim(): + """Generate stim file for encoding 9a (|++0000> state)""" + circuit = init_circuit(qubits=16, ancillas=7) # Need extra ancillas for encoding 9a + encode_manual_fig9a(circuit, cfg=NO_NOISE) + return circuit + + +def generate_encoding_9b_stim(): + """Generate stim file for encoding 9b (|+0+0+0> state)""" + circuit = init_circuit(qubits=16, ancillas=2) + encode_manual_fig9b(circuit, cfg=NO_NOISE) + return circuit + + +def generate_error_correction_round_stim(): + """Generate stim file for 1 error correction round (rows + columns)""" + circuit = init_circuit(qubits=16, ancillas=2) + error_correction_round_rows(circuit, cfg=NO_NOISE) + error_correction_round_columns(circuit, cfg=NO_NOISE) + circuit.append_operation("TICK") + return circuit + + +def generate_decoding_stim(): + """Generate stim file for decoding with two [[8,3,2]] color codes + measurement""" + circuit = init_circuit(qubits=16, ancillas=2) + measure_logical_operators_tesseract(circuit, cfg=NO_NOISE) + return circuit + + +def generate_complete_experiment_9a_stim(): + """Generate stim file for complete experiment with 9a encoding""" + circuit = init_circuit(qubits=16, ancillas=7) + + # Add encoding comment + circuit.append("TICK") + circuit.append_operation("# Start encoding based on Fig. 9a", []) + + # Encoding 9a + encode_manual_fig9a(circuit, cfg=NO_NOISE) + + # 3 rounds of error correction + for i in range(3): + circuit.append_operation("# Start EC round {}".format(i+1), []) + error_correction_round_rows(circuit, cfg=NO_NOISE) + error_correction_round_columns(circuit, cfg=NO_NOISE) + circuit.append_operation("TICK") + + # Final measurements + circuit.append_operation("# Start decoding", []) + measure_logical_operators_tesseract(circuit, cfg=NO_NOISE) + + return circuit + + +def generate_complete_experiment_9b_stim(): + """Generate stim file for complete experiment with 9b encoding""" + circuit = init_circuit(qubits=16, ancillas=2) + + # Encoding 9b + encode_manual_fig9b(circuit, cfg=NO_NOISE) + + # 3 rounds of error correction + for i in range(3): + error_correction_round_rows(circuit, cfg=NO_NOISE) + error_correction_round_columns(circuit, cfg=NO_NOISE) + circuit.append_operation("TICK") + + # Final measurements + measure_logical_operators_tesseract(circuit, cfg=NO_NOISE) + + return circuit + + +def save_stim_file(circuit, filename, description): + """Save a stim circuit to a file with header comments""" + stim_str = str(circuit) + + # Add header comments + header = f"""# {description} +# Generated from tesseract-code-stim project +# Circuit has {circuit.num_qubits} qubits total +# +# Qubit layout: +# - Qubits 0-15: Data qubits arranged in 4x4 grid +# - Qubits 16+: Ancilla qubits for measurements +# +# Coordinate system: +# - Qubits 0-3: row 1 (y=0, x=0-3) +# - Qubits 4-7: row 2 (y=1, x=0-3) +# - Qubits 8-11: row 3 (y=2, x=0-3) +# - Qubits 12-15: row 4 (y=3, x=0-3) +# + +""" + + with open(filename, 'w') as f: + f.write(header) + f.write(stim_str) + + print(f"Generated {filename} - {description}") + + +def main(): + """Generate all stim circuit files""" + os.makedirs('stim_circuits', exist_ok=True) + + circuits = [ + (generate_encoding_9a_stim(), 'stim_circuits/encoding_9a.stim', 'Encoding 9a circuit (|++0000> state)'), + (generate_encoding_9b_stim(), 'stim_circuits/encoding_9b.stim', 'Encoding 9b circuit (|+0+0+0> state)'), + (generate_error_correction_round_stim(), 'stim_circuits/error_correction_round.stim', 'Single error correction round (rows + columns)'), + (generate_decoding_stim(), 'stim_circuits/decoding.stim', 'Decoding with two [[8,3,2]] color codes + measurement'), + (generate_complete_experiment_9a_stim(), 'stim_circuits/complete_experiment_9a.stim', 'Complete experiment: encoding 9a -> 3 EC rounds -> decoding'), + (generate_complete_experiment_9b_stim(), 'stim_circuits/complete_experiment_9b.stim', 'Complete experiment: encoding 9b -> 3 EC rounds -> decoding'), + ] + + for circuit, filename, description in circuits: + save_stim_file(circuit, filename, description) + + print(f"\nAll stim files generated successfully!") + print("Files created:") + for _, filename, _ in circuits: + print(f" - {filename}") + + +if __name__ == "__main__": + main()