E-graph T-count optimizer for Clifford+T (Python bindings)
Project description
tsimpl
Python bindings for a Rust e-graph optimizer that minimizes T-count in Clifford+T circuits. Stack: Rust ·
egge-graphs · cost-based extraction · PyO3 (abi3) · maturin.
Highlights
- T-count minimization with rewrite sets: T-parity cancel, Clifford normalize, RZ commutation, CNOT identities.
- Cost model: minimize T-count → tie-break by depth → then gate count.
- Two-liner API + CLI for batch use; emits OpenQASM 2.0.
- Wheels built with
abi3(Python 3.9+); works on Apple Silicon/macOS and Linux (when wheels are provided).
Install (local dev)
pip install maturin
maturin develop
After publishing to PyPI you’ll be able to do:
pip install tsimpl
Quickstart
Python API
import tsimpl as ts
qasm = """OPENQASM 2.0;
include "qelib1.inc";
qreg q[1];
t q[0];
t q[0];
h q[0];
z q[0];
h q[0];
"""
opt_qasm, stats = ts.optimize_qasm(qasm)
print(stats)
print(opt_qasm)
Example output:
{'t_before': 2, 't_after': 0, 'depth_before': 5, 'depth_after': 2, 'gates_before': 5, 'gates_after': 2}
OPENQASM 2.0;
include "qelib1.inc";
qreg q[1];
s q[0];
x q[0];
CLI
tsimpl --in input.qasm --out output.qasm --stats
Example:
tsimpl --in examples/qft3.qasm --stats
# {'t_before': 2, 't_after': 0, 'depth_before': 6, 'depth_after': 3, 'gates_before': 6, 'gates_after': 3}
# (optimized OpenQASM printed to stdout)
What it does (under the hood)
-
Language/AST:
H, S, T, X, Z, CNOT, RZ(π/2^k), Seq(...). -
Rewrite rules (subset):
- T-parity:
T · T ⇒ S - Clifford norms:
H·Z·H ⇒ X,H·X·H ⇒ Z,S·S ⇒ Z, etc. - Commutation: move
RZ(π/2^k)past gates when legal to expose cancellations. - CNOT identities:
CNOT(a,b); CNOT(a,b) ⇒ Iand adjacent cleanups.
- T-parity:
-
Extractor: custom cost function prioritizes T-count → depth → gate count.
When to use
- Reducing non-Clifford budget before synthesis/mapping.
- Cleaning small/medium building blocks (QFT slices, QAOA layers, adders) before backend passes.
- Teaching/demo for e-graphs in quantum compilers.
Development
# create/edit Rust bindings
cargo build
# run Python-side install in editable mode
maturin develop
# quick Python API test
python - <<'PY'
import tsimpl as ts, sys
opt, s = ts.optimize_qasm("OPENQASM 2.0;\ninclude \"qelib1.inc\";\nqreg q[1];\nt q[0];\nt q[0];")
print(s); print(opt)
PY
Tests & examples
- Keep small QASM samples in
examples/(e.g.,qft3.qasm,qaoa_p1.qasm,adder2.qasm). - Add property tests (semantic equality via external simulators) in future versions.
Benchmarks (optional)
- Use
criterionon the Rust core crate for: time to fixpoint, rule fire counts, success rate on blocks.
Roadmap
- More phase-polynomial/phase-gadget rules.
- Equivalence checks vs simulators (Qiskit Aer/
statevector) in CI. - Manylinux/macOS wheels via GitHub Actions +
maturin build. - QASM3/JSON IR I/O, multi-qubit RZ patterns, light peephole mapping.
License
MIT © 2025 Harshit Lakum
Citation
If this tool helps your research, please cite:
Harshit Lakum, "tsimpl: E-graph T-count Optimizer for Clifford+T", 2025.
Acknowledgements
egge-graph framework and the broader equality saturation community.- OpenQASM community tooling inspiring the simple I/O used here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tsimpl-0.1.0-cp39-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: tsimpl-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 332.2 kB
- Tags: CPython 3.9+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b75ec6b29c6c5f23c5bcd720b26e120ad6e3819540357278f2b5c1dbd11bc2c0
|
|
| MD5 |
c2f11bdd64d694a2d05b03dd392b0fe1
|
|
| BLAKE2b-256 |
f9c408d527157b63465f10aba2506da35e21ad858a4dac7a156f840203f01031
|