Quantar
Quantar is a strict superset of Python for quantum computing and physics
simulations. It keeps Python syntax and adds custom infix operators such as
a <-> b, plus a library of quantum gates, circuits, algorithms, and physics
helpers.
Requirements
- Python 3.12 or newer
- Rust (to install it, click here and follow the commands)
- NumPy, Numba, and
openqasm3[parser](installed automatically)
Installation
pip install quantar
# or with uv
uv pip install quantar
Optional extras:
# alternative quantum backends
pip install quantar[backends]
# SciPy-powered COBYLA optimization
pip install quantar[optimization]
# LSP support
pip install quantar[lsp]
also install the VS code extension here for better coloring and errors or just use the ext command as follows:
ext install Gauthamrgb.quantar-lang
Starting a project
qtr new my-project
cd my-project
qtr run
qtr check
qtr compile
qtr new NAME creates NAME/ with pyproject.toml, src/main.qtr, and
README.md. Run qtr init instead to scaffold the current directory.
The generated pyproject.toml has quantar in dependencies and a
[tool.quantar] table with entry, output, compile-to, mode, rust, and
jobs. Plain qtr run, qtr check, and qtr compile read those settings, so
you do not need to repeat the filename. Command-line options override them, and
--rust/--no-rust overrides [tool.quantar].rust.
Running Quantar
qtr path/to/program.qtr
Compile a file to ordinary Python instead of executing it:
qtr path/to/program.qtr --compile path/to/program.py
During development, the same runner is available as a module:
python -m lang_systems.runner path/to/program.qtr
Small registers use vectorized NumPy so short programs skip Numba's startup and
compilation cost. Registers with at least 1,048,576 amplitudes use the JIT. Set
QUANTAR_NUMBA_MIN_AMPLITUDES to change that cutoff, or QUANTAR_NUMBA=0 to
disable Numba entirely.
VS Code extension
The extension lives in extension/. It provides Python-based syntax
highlighting for .qtr and .quantar files, parser diagnostics, and semantic
highlighting for registered Quantar operators. Operators use one consistent
cyan accent; the rest of the syntax follows the active VS Code theme.
python -m pip install -e ".[lsp]"
cd extension
npm install
Open the project root in VS Code and press F5 to launch the Run Quantar Extension development configuration. The extension uses the Python
interpreter selected by the VS Code Python extension. Install the LSP extra in
that interpreter; for a local checkout, run the install command above from the
project root.
Parsing Quantar
lang_systems.parse() returns pegen's raw parse tree. This is the low-level
form used by tooling and tests:
from lang_systems import parse
tree = parse("a <-> b\nx = 1 + 2\n")
lang_systems.parse_ast() returns a normal Python AST with custom QuantumOp
nodes:
import ast
from lang_systems import OperatorCollector, parse_ast
module = parse_ast("x = a <-> b\n")
print(ast.dump(module, indent=2))
collector = OperatorCollector()
collector.visit(module)
print(collector.operators) # [("<->", "binary")]
Custom operators are registered with lang_systems.operators.operator:
from lang_systems.operators import operator
@operator.binary("><")
def reverse(left, right):
return right, left
If the operators live in another module, import that module before parsing:
from lang_systems import load_operators, parse
load_operators("my_package.quantar_ops")
tree = parse("a >< b\n")
Quantar as a library
quantar is the umbrella package. It loads the language, quantum, and physics
packages lazily, so importing it stays lightweight:
import quantar
q = quantar.QuantumComputer(2)
tree = quantar.parse_ast("a <-> b\n")
print(quantar.__version__)
quantar.lang_systems
quantar.quantum
quantar.stl
Common algorithms and physics helpers are re-exported from quantar and stl
too:
import quantar
from stl import shors_alg, grovers_alg, Quantity
# The implementation modules are still available directly:
from stl.quantum_algs.shors_alg_lib import shors_alg as shors_alg_impl
from stl.physics_algs.units import Quantity as QuantityImpl
Physics helpers share the same shortcut: quantar.VectorField,
quantar.momentum, and quantar.angular_momentum resolve lazily. Numeric hot
paths in stl.physics_algs (kinetic energy, Lorentz forces, finite differences,
and Verlet steps) use compiled Rust kernels when available and identical NumPy
fallbacks otherwise. Check with stl.physics_algs.rust_available().
See docs/API.md for the full import cheat sheet, public API,
and backend/seed details.
Selecting a simulator backend
QuantumComputer, Qubit, and Gate follow the active backend. The default is
the pure-Python simulator; switch to the compiled Rust backend at runtime when
the extension is available:
import quantum
print(quantum.get_backend()) # "python"
quantum.use_backend("rust") # or "python" to switch back
qc = quantum.QuantumComputer(2) # quantum.quantar_kernels.QuantumComputer
quantum.load_rust_backend() # import/validate the extension directly
quantum.available_backends() # e.g. ("python", "rust")
The backend can also be chosen before startup with QUANTAR_BACKEND=rust. The
bundled extension is built with PyO3's abi3-py312 feature, so the same binary
works on CPython 3.12+ and wheels are tagged cp312-abi3-*. In the source tree
it is named src/quantum/quantar_kernels.pyd, with a copy kept in the root
kernels/ directory.
If that extension is missing or stale, Quantar automatically builds it locally
when cargo and the rust/ source tree are available:
import quantum
quantum.rust_build_available() # True when cargo + rust/Cargo.toml are found
quantum.build_rust_backend() # force a fresh release build and load it
quantum.use_backend("rust") # auto-builds if needed
Environment overrides:
QUANTAR_RUST_AUTO_BUILD=0disables automatic builds.QUANTAR_RUST_SOURCE_DIRpoints at the repo root or itsrust/directory.QUANTAR_CARGOselects a specific cargo executable.QUANTAR_RUST_BACKEND_PATHloads a specific.pyd/.soinstead.QUANTAR_RUST_BUILD_CACHEchanges the fallback output directory when the installed package directory is not writable.
Reproducible sampling
Both backends share one seed. set_seed affects measurement, sample,
sample_parallel, measure_pauli, and the Rust per-computer RNG:
import quantum
quantum.set_seed(1234)
print(quantum.get_seed()) # 1234
qc = quantum.QuantumComputer(3)
# Recreate the same stream later:
quantum.set_seed(1234)
qc = quantum.QuantumComputer(3)
quantum.get_seed() generates and remembers a seed from OS entropy on first
use, so any run can be reproduced afterward. The new seed applies to computers
and samples created after the call; existing computers keep their generator.
The compiled extension exposes quantum.quantar_kernels.set_seed() /
get_seed() as aliases of the same shared state.
Build output
Normal uv build, python -m build, and pip install . runs are quiet.
Dependency deprecation warnings (for example num-traits 0.2.19's deprecated
core::f32::NAN constants) are suppressed because they come from dependencies:
$env:QUANTAR_VERBOSE_BUILD = "1" # detailed maturin/cargo output
uv build
Remove-Item Env:QUANTAR_VERBOSE_BUILD
$env:QUANTAR_STRICT_RUST = "1" # keep dependency warnings visible
uv build
Building the Rust backend manually
$env:PYO3_PYTHON = "C:\path\to\your\python.exe"
cargo build --release --manifest-path rust/Cargo.toml
Copy-Item rust\target\release\quantar_kernels.dll `
src\quantum\quantar_kernels.pyd -Force
Copy-Item rust\target\release\quantar_kernels.dll `
kernels\quantar_kernels.pyd -Force
For distribution, prefer wheels over copying .pyd files by hand. The
GIL-enabled wheel is tagged cp312-abi3-*; free-threaded builds need their own
wheels as described below.
Free-threaded Python
Quantar can build and load the Rust backend on free-threaded CPython. The ABI matrix is:
| Interpreter | Extension built from source | Prebuilt wheel to publish |
|---|---|---|
| CPython 3.12–3.14, GIL enabled | abi3 (abi3-py312) |
cp312-abi3-* |
| CPython 3.14t, free-threaded | version-specific cp314t |
cp314t-* |
| CPython 3.15/3.15t and newer | abi3t (abi3t-py315) |
abi3t-* |
Important details:
- The existing
cp312-abi3-*wheel cannot be installed on a free-threaded interpreter; pip rejects it as unsupported. Install a free-threaded wheel or install from source with the free-threaded interpreter. python3.14t -m pip install .compiles acp314textension automatically.- Python 3.15+ uses PEP 803's
abi3tstable ABI, so one binary works with both GIL-enabled and free-threaded interpreters. - PyO3 0.30 no longer supports CPython 3.13t; use 3.14t or newer.
- The runtime loader detects
Py_GIL_DISABLED, so local auto-builds never write a GIL-onlyabi3extension for a free-threaded interpreter.
uv python install 3.14t
uv venv --python 3.14t .venv-3.14t
.\.venv-3.14t\Scripts\python.exe -m pip install . --no-deps
.\.venv-3.14t\Scripts\python.exe -c "import quantum; print(quantum.is_free_threaded()); quantum.use_backend('rust')"
The extension declares gil_used = false: NumPy access uses borrow guards and
the heavy kernels run after py.detach, so importing it does not force the
interpreter to re-enable the GIL.
Imports in Quantar programs
Regular Python imports work inside .qtr files, and Quantar can import other
.qtr modules too:
# gates.qtr
from lang_systems.operators import operator
@operator.binary("<~>")
def reverse(left, right):
return right, left
VALUE = 42
# main.qtr
from gates import VALUE
import gates
a, b = 1, 2
print(a <~> b) # operator is registered before parsing
print(VALUE)
Run it with qtr main.qtr. Directory packages with __init__.qtr support
relative imports such as from .gates import VALUE. The runner adds the source
file's directory to sys.path, so sibling .py and .qtr modules are found
without extra setup. Compiled .py output installs the same import hook.
See the modules under src/quantum/ and src/stl/ for the available gates,
circuits, algorithms, and physics helpers.
Development
The Python packages live under src/. Install the project in editable mode
before running the CLI or LSP from a checkout:
pip install -e ".[lsp]"
Regenerate the parser after changing src/lang_systems/quantar.gram:
python -m lang_systems.pegen_gram_gen
Build the wheel and source distribution:
uv build
The generated parser is src/lang_systems/quantar_parser.py; the build hook in
hatch_build.py regenerates it automatically. The parser is generated from
CPython's Grammar/python.gram plus the operator overlay in
src/lang_systems/quantar.gram.
Project maturity
Quantar is usable today as a Python superset for quantum programs, but it is still a maturing project. The core language frontend, simulator, Rust backend, and CLI are in place; tooling, platform coverage, and the compiled execution pipeline are still being expanded. See roadmap.md for the planned work.
What is stable enough to use
- Language frontend: the parser and AST builder handle Python syntax plus Quantar's custom infix operators.
- Imports:
.pyimports and.qtrmodule imports work, including operator preloading, sibling modules, and relative imports inside packages. - Simulation: the pure-Python/NumPy backend is the default and supports the main state-vector operations, circuits, QFT helpers, measurement, sampling, and QASM export.
- Rust backend: the optional compiled backend mirrors the Python API. It
uses
abi3-py312on GIL-enabled CPython and can build for free-threaded interpreters (cp314torabi3t-py315). If the extension is missing or stale, Quantar can build it locally whencargoand the Rust source tree are available. - CLI:
qtrcan run or compile.qtrfiles. - LSP basics: the VS Code extension provides syntax highlighting, operator highlighting, and parser diagnostics.
What is still evolving
- The Rust backend API is additive and tested for parity, but its surface is still growing.
- Distribution currently produces a Windows
cp312-abi3wheel from this checkout; Linux/macOS wheels and prebuilt free-threaded wheels are planned. Free-threaded users can install from source today. - The
.qtrimport hook works for common cases, but namespace packages, reloads, and circular-import diagnostics are still being refined. - Error messages and source locations are being improved across the parser, runner, and LSP.
- Performance work is ongoing, especially for multi-controlled gates, common 1-qubit/2-qubit kernels, and Python-to-Rust callback reduction.
What is not implemented yet
- A dedicated Quantar bytecode interpreter.
- A debugger with breakpoints, stepping, and state inspection.
- GPU, distributed, or tensor-network execution backends.
- Full LSP support for completions, hover documentation, and cross-file go-to-definition.
In short: Quantar is ready for experimentation, teaching, prototyping, and library use on supported platforms. It is not yet a finished production toolchain, and the roadmap tracks the work needed to get there.
Feedback
Feedback and bug reports are welcome through GitHub issues or by contacting the maintainer. The main contributor is Gautham_rgb.
Notes on AI
Most of the code was written by people. Some AI assistance was used for boilerplate and for explaining the mathematics behind topics such as Kronecker products and gate matrices and for understanding Rust.
Metadata
Release files for Quantar 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| quantar-1.3.0.tar.gz | 455.8 kB | Without provenance | Details for quantar-1.3.0.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| quantar-1.3.0-cp315-abi3.abi3t-win_amd64.whl | CPython 3.15 | abi3t, abi3 | Windows x86-64 | Details for quantar-1.3.0-cp315-abi3.abi3t-win_amd64.whl |
| quantar-1.3.0-cp314-cp314t-win_amd64.whl | CPython 3.14 | CPython 3.14 free-threading | Windows x86-64 | Details for quantar-1.3.0-cp314-cp314t-win_amd64.whl |
| quantar-1.3.0-cp312-abi3-win_amd64.whl | CPython 3.12 | abi3 | Windows x86-64 | Details for quantar-1.3.0-cp312-abi3-win_amd64.whl |
Total release size: 2.7 MB
Release files / quantar-1.3.0.tar.gz
| Download URL | quantar-1.3.0.tar.gz |
|---|---|
| Size | 455.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f88544f0312b17fc042d7c9d76373a3d0f7127b086e551ffb8710494a931ca0d
|
|
BLAKE2b-256 checksum How to use checksums |
29f846824eae8b0fc9077993372016f26c6bd0a838020972e8a31fa48d1f8eb4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.2
|
Release files / quantar-1.3.0-cp315-abi3.abi3t-win_amd64.whl
| Download URL | quantar-1.3.0-cp315-abi3.abi3t-win_amd64.whl |
|---|---|
| Size | 778.2 kB |
| Tags | CPython 3.15 Windows x86-64 abi3 abi3t |
|
SHA-256 checksum How to use checksums |
c2dfdf352695a937bf511466c43d3e5af4e5aeb0d35229ef70fd3ee439b546a5
|
|
BLAKE2b-256 checksum How to use checksums |
ced3852f00773b4c078d583c3022fa424f2100c80058c80801397c54f73a8df4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.2
|
Release files / quantar-1.3.0-cp314-cp314t-win_amd64.whl
| Download URL | quantar-1.3.0-cp314-cp314t-win_amd64.whl |
|---|---|
| Size | 755.6 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading Windows x86-64 |
|
SHA-256 checksum How to use checksums |
fce587d830b9b316f9485bebe433a765179a964cb87e472751823f72ccf59072
|
|
BLAKE2b-256 checksum How to use checksums |
606ba4a32075cbc4def5f58c1c14f69aa774913d10521aa1a5246d05a3fdb1bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.2
|
Release files / quantar-1.3.0-cp312-abi3-win_amd64.whl
| Download URL | quantar-1.3.0-cp312-abi3-win_amd64.whl |
|---|---|
| Size | 740.3 kB |
| Tags | CPython 3.12 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
f7cd6378e2865383c0b75518f6baae062d7ef081504cdcb9f3c013fd081d899f
|
|
BLAKE2b-256 checksum How to use checksums |
64c3069a2f3a98102d5e1ccf1b14c3c0f27d4abb5f2f30d9919c6d6d2fa4c736
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.2
|
Security for Quantar 1.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.