Metadata-Version: 2.4
Name: gnn_decoders
Version: 0.1.0
Summary: GraphSAGE-based decoders for the Stim/Sinter quantum error correction framework.
Author: Quantum Error Correction Research Project
License: MIT License
        
        Copyright (c) 2026 Quantum Error Correction Research Project
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/your-org/quantum-error-correction
Project-URL: Issues, https://github.com/your-org/quantum-error-correction/issues
Keywords: quantum,error-correction,stim,sinter,decoder,graph-neural-network,graphsage
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.22
Requires-Dist: stim>=1.13
Requires-Dist: sinter>=1.13
Requires-Dist: torch>=2.0
Requires-Dist: torch-geometric>=2.4
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Requires-Dist: pymatching>=2.1; extra == "dev"
Provides-Extra: examples
Requires-Dist: pymatching>=2.1; extra == "examples"
Requires-Dist: matplotlib>=3.5; extra == "examples"
Dynamic: license-file

# gnn_decoders

GraphSAGE-based decoders for the [Stim](https://github.com/quantumlib/Stim) /
[Sinter](https://pypi.org/project/sinter/) quantum-error-correction
benchmarking framework.

This package wraps the trained GraphSAGE models from the
`quantum-error-correction` research project and exposes them as
`sinter.Decoder` instances, so they can be benchmarked head-to-head against
`pymatching`, `fusion_blossom`, and other Sinter-supported decoders in a
single `sinter.collect` call.

## Scope of v0.1

* **Codes:** rotated-surface-code memory-Z experiments only.
* **Distances:** d ∈ {3, 5, 7} (the bundled baseline checkpoints).
* **Seeds per distance:** 1-5 (15 checkpoints total, ~6.7 MB bundled).
* **Single observable.** Multi-observable DEMs raise `NotImplementedError`.

If Sinter hands the decoder a DEM whose detector count does not correspond to
a bundled checkpoint, or whose `num_observables != 1`, compilation fails fast
with a clear error rather than producing misleading predictions.

## Install

```bash
pip install -e .
```

Dependencies (`stim`, `sinter`, `torch`, `torch-geometric`, `numpy`) are
declared in `pyproject.toml`. `torch-geometric` typically needs a
torch-version-specific install — see
<https://pytorch-geometric.readthedocs.io/en/latest/install/installation.html>.

## Quickstart

```python
import stim
import sinter
from gnn_decoders import GraphSAGEDecoder

circuit = stim.Circuit.generated(
    "surface_code:rotated_memory_z",
    rounds=5,
    distance=5,
    after_clifford_depolarization=0.005,
    after_reset_flip_probability=0.005,
    before_measure_flip_probability=0.005,
    before_round_data_depolarization=0.005,
)

stats = sinter.collect(
    num_workers=4,
    tasks=[sinter.Task(circuit=circuit, json_metadata={"d": 5, "p": 0.005})],
    decoders=["graphsage"],
    custom_decoders={"graphsage": GraphSAGEDecoder(seed=1)},
    max_shots=10_000,
)
for s in stats:
    print(s)
```

See `examples/benchmark_vs_pymatching.py` for the full apples-to-apples
comparison against PyMatching across all bundled (d, p) combinations.

## API

### `GraphSAGEDecoder(seed=1, k_neighbors=6, device="auto", batch_size=1024)`

A `sinter.Decoder` subclass.

| arg | meaning |
| --- | --- |
| `seed` | Which trained seed to use (1-5). Use `available_checkpoints()` to inspect. |
| `k_neighbors` | k-NN parameter for the sparse-graph encoding. Must match training (6). |
| `device` | `"auto"`, `"cpu"`, `"cuda"`, or a `torch.device`. |
| `batch_size` | Shots per PyG `Batch` per forward pass. Tune for GPU memory. |

### `available_checkpoints() -> dict`
Returns the parsed `MANIFEST.json` describing every bundled `.pt` file.

### `supported_distances() -> list[int]`, `supported_seeds(distance) -> list[int]`
Introspection helpers.

## What's bundled

Each `.pt` in `src/gnn_decoders/checkpoints/` is a dict
`{state_dict, config, nickname, timestamp}` saved by the original
`GraphSAGE.save` method in the research project's `code/models.py`. Each
checkpoint carries its own architectural hyperparameters (`in_channels`,
`hidden_dim`, `num_layers`, `dropout`, `aggr`), so the model is
reconstructed exactly as it was trained — the decoder does not assume any
particular configuration.

The bundled "baseline" checkpoints use:

```
in_channels=5, hidden_dim=128, num_layers=4, dropout=0.0, aggr='mean'
```

The "best tuned" configuration discovered in
`code/gSAGE/tuning/best_model_config.json` (`hidden_dim=256, num_layers=5,
dropout=0.1, aggr='max'`) is not bundled here because it was only trained at
d=7 in the source project. It can be added in a future release by retraining
across d ∈ {3, 5, 7} and copying the resulting `.pt` files into
`src/gnn_decoders/checkpoints/`.

## License

MIT.
