Skip to main content

Quantar

Quantar Demo

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

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=0 disables automatic builds.
  • QUANTAR_RUST_SOURCE_DIR points at the repo root or its rust/ directory.
  • QUANTAR_CARGO selects a specific cargo executable.
  • QUANTAR_RUST_BACKEND_PATH loads a specific .pyd/.so instead.
  • QUANTAR_RUST_BUILD_CACHE changes 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 a cp314t extension automatically.
  • Python 3.15+ uses PEP 803's abi3t stable 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-only abi3 extension 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: .py imports and .qtr module 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-py312 on GIL-enabled CPython and can build for free-threaded interpreters (cp314t or abi3t-py315). If the extension is missing or stale, Quantar can build it locally when cargo and the Rust source tree are available.
  • CLI: qtr can run or compile .qtr files.
  • 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-abi3 wheel from this checkout; Linux/macOS wheels and prebuilt free-threaded wheels are planned. Free-threaded users can install from source today.
  • The .qtr import 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.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Quantar 1.2.2
File Size Uploaded Provenance Actions
quantar-1.2.2.tar.gz 440.3 kB Without provenance Details for quantar-1.2.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for Quantar 1.2.2
File Interpreter ABI Platform Actions
quantar-1.2.2-cp315-abi3.abi3t-win_amd64.whl CPython 3.15 abi3, abi3t Windows x86-64 Details for quantar-1.2.2-cp315-abi3.abi3t-win_amd64.whl

Total release size: 1.2 MB

Release files / quantar-1.2.2.tar.gz

Download URL quantar-1.2.2.tar.gz
Size 440.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4bcbfcb2631ed30eac0ce90b2639895fc74da01110eddc1127317fb5142247a6
BLAKE2b-256 checksum
How to use checksums
41a36c7eecc73ca5eee53a229131543cbda9287560231e02888d184a3043d6eb
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.2.2-cp315-abi3.abi3t-win_amd64.whl

Download URL quantar-1.2.2-cp315-abi3.abi3t-win_amd64.whl
Size 758.9 kB
Tags CPython 3.15 Windows x86-64 abi3 abi3t
SHA-256 checksum
How to use checksums
9216e08498576fe510c4974a200562c16c26d394d95bbb99703f4c6bc30e202e
BLAKE2b-256 checksum
How to use checksums
c280840780d709407087b60197ae688c0f63176544104dd9b654db119b7faea1
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.2.2

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page