|
1 | | -# Repository Guidelines |
| 1 | +# AGENTS.md — QECTOR Decoder v3 |
2 | 2 |
|
3 | | -## Project Structure & Module Organization |
| 3 | +Project-specific instructions for working in this repository. |
4 | 4 |
|
5 | | -This repository packages QECTOR Decoder v3 as a Rust/Python project. Python source lives in `python/qector_decoder_v3/`, with public modules such as `backend.py`, `dem.py`, `routing.py`, and `workbench.py`. Python tests are in `python/tests/` and follow the `test_*.py` pattern configured in `pyproject.toml`. Rust packaging and extension metadata are in `Cargo.toml`, `Cargo.lock`, `build.rs`, and `src/lib.rs`; the public `src/` tree is a stub for licensed source builds. Examples are under `examples/`, benchmark and validation utilities under `scripts/`, protobuf definitions under `proto/`, and design/reproducibility notes under `docs/`. |
| 5 | +## Project Overview |
6 | 6 |
|
7 | | -## Build, Test, and Development Commands |
| 7 | +QECTOR Decoder v3 is a high-performance, source-available Rust/Python platform for quantum error correction (QEC) decoding. |
8 | 8 |
|
9 | | -Create a local environment before development: |
| 9 | +- **Core**: Rust (PyO3 cdylib) implementing Union-Find, Blossom (exact MWPM), Sparse Blossom, BP-OSD, sliding-window, streaming, batch decoders, lookup tables, neural/GNN predecoders, hybrid decoders. |
| 10 | +- **Python package**: `qector_decoder_v3` (published on PyPI). |
| 11 | +- **Bindings**: maturin + PyO3. Zero-copy NumPy arrays, GIL-free hot paths where possible. |
| 12 | +- **Hardware**: CPU (Rayon), optional CUDA batch, optional OpenCL batch. Runtime detection + graceful fallback. |
| 13 | +- **Ecosystem**: PyMatching-compatible, Stim/Sinter integration, belief-matching, Qiskit plugin, REST/gRPC + full-featured MCP server (25 tools in the QectorWorkbench companion). |
| 14 | +- **Emphasis**: Syndrome faithfulness, CPU/GPU bit-identical results, extensive regression + artifact-backed benchmarks, reproducibility. |
| 15 | + |
| 16 | +**Current version**: 0.6.4 (Cargo + Python packaging). |
| 17 | + |
| 18 | +**Workspace layout** (this checkout): |
| 19 | + |
| 20 | +- `src/` — Rust core (all decoder implementations, batch engines, kernels, utils). |
| 21 | +- `python/qector_decoder_v3/` — Python package sources (`__init__.py`, `backend.py`, `belief_matching.py`, `bposd.py`, `stim_compat.py`, `sinter_compat.py`, `workbench.py`, `dem.py`, etc.). |
| 22 | +- `python/tests/` — 100+ pytest tests covering correctness, faithfulness, performance, GPU parity, edge cases, benchmarks. |
| 23 | +- `proto/` — gRPC `.proto` (only for `grpc` / `full` features). |
| 24 | +- `lib/` — Windows import libs (OpenCL etc.). |
| 25 | +- `build.rs` — Handles optional protoc for gRPC + link paths. |
| 26 | +- `Cargo.toml` / `pyproject.toml` — maturin build. |
| 27 | + |
| 28 | +Note: This appears to be a full-source "build" checkout (src/ contains real implementations + .cu kernels). |
| 29 | + |
| 30 | +## Build & Install (Windows PowerShell) |
| 31 | + |
| 32 | +Use a virtual environment. |
10 | 33 |
|
11 | 34 | ```powershell |
12 | | -py -3.11 -m venv .venv |
13 | | -.\.venv\Scripts\python.exe -m pip install --upgrade pip maturin |
14 | | -.\.venv\Scripts\python.exe -m pip install -e ".[dev,stim,bench]" |
| 35 | +python -m venv .venv |
| 36 | +.\.venv\Scripts\Activate.ps1 |
| 37 | +python -m pip install --upgrade pip maturin |
| 38 | +python -m pip install -e ".[stim,bench]" # or [all], [cuda], [opencl] etc. |
15 | 39 | ``` |
16 | 40 |
|
17 | | -Build the Python extension in-place with: |
| 41 | +- For iterative Rust+Python dev: `maturin develop --release` (or without --release for faster debug). |
| 42 | +- Features are controlled in Cargo (default includes opencl + cuda in this workspace). |
| 43 | +- gRPC/MCP: `--features full` or `maturin develop --features full`. |
| 44 | +- CUDA kernels are compiled at runtime via NVRTC (no special build step for kernels). |
| 45 | +- After changes to Rust, re-run `maturin develop` (or restart Python interpreter). |
| 46 | + |
| 47 | +Verify: |
18 | 48 |
|
19 | 49 | ```powershell |
20 | | -.\.venv\Scripts\python.exe -m maturin develop --release --no-default-features |
| 50 | +python -c "from qector_decoder_v3 import UnionFindDecoder, BlossomDecoder, CUDABatchDecoder; print('CUDA available:', CUDABatchDecoder.is_available() if hasattr(CUDABatchDecoder,'is_available') else 'check'); import qector_decoder_v3; print(qector_decoder_v3.__version__)" |
21 | 51 | ``` |
22 | 52 |
|
23 | | -Run the full Python test suite with `.\.venv\Scripts\python.exe -m pytest python/tests -q --tb=short`. Run targeted tests by passing a file, for example `python/tests/test_backend.py`. Use `cargo test --release --lib` for Rust library tests when licensed Rust sources are available. |
| 53 | +## Testing |
| 54 | + |
| 55 | +Run the full Python test suite (primary validation lives here): |
| 56 | + |
| 57 | +```powershell |
| 58 | +pytest python/tests/ -q |
| 59 | +``` |
| 60 | + |
| 61 | +Useful filters: |
| 62 | + |
| 63 | +- `pytest python/tests/ -k "unionfind or blossom or faithfulness"` |
| 64 | +- `pytest python/tests/test_gpu* -q` (GPU paths) |
| 65 | +- `pytest python/tests/test_bposd* python/tests/test_belief*` |
| 66 | +- `pytest python/tests/test_clean_venv_install.py` (install hygiene) |
| 67 | +- `pytest python/tests/test_public_api_imports.py test_type_hints.py` |
| 68 | + |
| 69 | +Many tests are **correctness audits** (syndrome faithful, bit-identical across backends, regression against known good results). Do not weaken or skip them lightly. |
| 70 | + |
| 71 | +Rust unit tests (if present): |
| 72 | + |
| 73 | +```powershell |
| 74 | +cargo test |
| 75 | +``` |
| 76 | + |
| 77 | +Before claiming performance or correctness improvements, re-run relevant benchmark scripts (see README) and the due-diligence style tests. |
| 78 | + |
| 79 | +## Coding Conventions & Style |
| 80 | + |
| 81 | +### Rust |
| 82 | + |
| 83 | +- Keep hot paths allocation-free or with reusable scratch (see `uf_core`, batch workspaces). |
| 84 | +- Use `rayon` for data-parallel batch paths. |
| 85 | +- Feature-gate GPU modules (`#[cfg(feature = "cuda")]`, `#[cfg(feature = "opencl")]`). |
| 86 | +- Strong error messages for invalid inputs (see decoder constructors). |
| 87 | +- Preserve exact numerical behavior across CPU/GPU; bit-identical results are a hard requirement in many tests. |
| 88 | +- Update `build.rs` only when adding new rerun-if-changed or build-time dependencies. |
| 89 | + |
| 90 | +### Python |
| 91 | + |
| 92 | +- NumPy-centric. Use `numpy.typing`, keep dtypes explicit (uint8 for syndromes usually). |
| 93 | +- Public API re-exports from the compiled extension + pure-Python shims/fallbacks. |
| 94 | +- Backend selection logic lives in `backend.py` (AutoDecoder, calibration). |
| 95 | +- Compat layers (`stim_compat`, `sinter_compat`, `pymatching_compat`) must stay faithful to the wrapped behavior. |
| 96 | +- Type hints + mypy/ruff clean (dev deps include them). |
| 97 | +- Avoid holding GIL during long Rust calls where the extension releases it. |
| 98 | + |
| 99 | +### General |
| 100 | + |
| 101 | +- Prefer adding tests over comments for invariants. |
| 102 | +- Benchmark numbers and claims must be reproducible via checked-in scripts + artifacts. |
| 103 | +- GPU code must degrade gracefully; never assume a device is present. |
| 104 | +- Changes touching DEM handling, observables, or sliding window must touch the corresponding faithfulness tests. |
| 105 | + |
| 106 | +## Common Tasks |
| 107 | + |
| 108 | +- Add a new decoder variant: implement in Rust (new `Py*` class + registration in `lib.rs`), expose in `__init__.py`, add Python wrapper if needed, add tests under `python/tests/`. |
| 109 | +- Fix a bug reported by a specific test: run that test in isolation first, then broader faithfulness suite. |
| 110 | +- Change Python-only logic (e.g. `belief_matching.py`): no rebuild needed. |
| 111 | +- Touch CUDA: ensure `cuda_is_available` and fallback paths still work; run GPU parity tests. |
| 112 | +- Update public API: also update `test_public_api_imports.py`, docs if present, and type stubs if any. |
| 113 | +- Release prep: version bumps in `Cargo.toml` + `pyproject.toml`, update `__fallback_version__` in `__init__.py`. |
| 114 | + |
| 115 | +## Things to Watch Out For |
24 | 116 |
|
25 | | -## Coding Style & Naming Conventions |
| 117 | +- The compiled extension (`qector_decoder_v3*.pyd` or .so) must be rebuilt after any Rust change. Stale .pyd is a common source of confusion. |
| 118 | +- Feature flags affect what symbols are present (CUDA/OpenCL/gRPC may be missing). |
| 119 | +- Windows: OpenCL uses the pre-bundled .lib in `lib/`. |
| 120 | +- Hyperedges: some fast decoders reject them (see validation code). |
| 121 | +- Memory: batch decoders and large distance codes can be memory-heavy; tests include growth/leak checks. |
| 122 | +- Never claim "faster than X" or "better LER" without running the competitive scripts on this machine and recording artifacts. |
26 | 123 |
|
27 | | -Python targets 3.9+ and uses Ruff with a 120-character line length. Keep modules and functions in `snake_case`, classes in `PascalCase`, and tests named `test_<behavior>.py` with `test_<expected_result>` functions. Preserve lazy optional imports for GPU and ecosystem dependencies. Rust uses edition 2021; prefer `cargo fmt` style and feature-gated code for optional CUDA, OpenCL, and gRPC paths. |
| 124 | +## When Running Commands |
28 | 125 |
|
29 | | -## Testing Guidelines |
| 126 | +- Prefer `python -m pytest ...` over bare `pytest` for consistency. |
| 127 | +- Use the activated venv. |
| 128 | +- For long-running tests/benchmarks use the monitor tool or background where appropriate. |
| 129 | +- When editing across Rust/Python boundary, verify roundtrips (encode/decode result objects, JSON, etc.). |
30 | 130 |
|
31 | | -Use `pytest` and `hypothesis` where property coverage is useful. Add tests beside related coverage in `python/tests/`, especially for decoder correctness, API compatibility, fallback behavior, and reproducibility commands. GPU tests must tolerate unavailable hardware unless explicitly marked or guarded by availability checks. |
| 131 | +## Project Rules Priority |
32 | 132 |
|
33 | | -## Commit & Pull Request Guidelines |
| 133 | +These instructions apply in addition to any higher-level or user `AGENTS.md` / rules. Deeper directory rules (if added) take precedence for files within them. |
34 | 134 |
|
35 | | -Git history is unavailable in this environment, so use concise imperative commit subjects, for example `Fix batch decode dtype handling`. Pull requests should include a scoped description, commands run, environment details for benchmark claims, linked issues when applicable, and screenshots or artifacts for Workbench or report-output changes. Follow `CONTRIBUTING.md` for licensing, benchmark evidence, and security disclosure rules. |
| 135 | +Follow the spirit of the existing extensive test suite: prioritize correctness, reproducibility, and faithful behavior over micro-optimizations. |
36 | 136 |
|
37 | | -## Security & Configuration Tips |
| 137 | +--- |
38 | 138 |
|
39 | | -Do not publish exploit details in issues; follow `SECURITY.md`. Treat performance claims as hardware- and build-specific. Include raw outputs, hashes, and environment blocks for benchmark submissions as described in `docs/REPRODUCIBILITY_CHECKLIST.md`. |
| 139 | +Generated by /init on first session in this workspace. Update this file as conventions evolve. |
0 commit comments