Core quaternion, spinor, SU(2), and Bloch mathematics for the RQM Python ecosystem
Project description
RQM Core
Core quaternion, spinor, SU(2), and Bloch mathematics for the RQM Python ecosystem.
🌐 RQM Platform
This repository is part of the RQM Technologies ecosystem.
→ Website: https://rqmtechnologies.com
→ Documentation: https://docs.rqmtechnologies.com
Install
pip install rqm-core
rqm-core is the mathematical foundation layer. It has no RQM-level dependencies — only numpy at runtime.
Where This Fits
rqm-core → rqm-compiler → rqm-qiskit / rqm-braket
rqm-core provides the canonical mathematical layer of the RQM ecosystem, including quaternion, spinor, Bloch, and SU(2) foundations. Higher-level packages (rqm-compiler, rqm-qiskit, rqm-braket) build on top of it.
Next Steps
- Documentation: https://docs.rqmtechnologies.com
- Website: https://rqmtechnologies.com
- Next package in the stack:
rqm-compiler
Why This Package Exists
Higher-level RQM libraries (simulators, compilers, hardware adapters) all require a
common, reliable layer of linear algebra and quantum geometry. rqm-core is that layer.
It provides a single, versioned source of truth for the mathematical primitives shared across the whole ecosystem: no duplication, no conflicting conventions, and no framework lock-in.
Design Principles
| Principle | What it means in practice |
|---|---|
| Tiny | Only implement primitives that are needed by ≥2 packages |
| Stable | Slow to change; breaking changes require a major version bump |
| Dependency-light | Only numpy at runtime |
| Canonical | One correct convention, clearly documented, used everywhere |
| Well-tested | Strong test coverage from the first commit |
What Is Included
- Quaternion primitives – Hamilton product, conjugate, inverse, axis-angle construction, SO(3) and SU(2) conversions
- Spinor helpers – normalization, norm, fidelity, spinor↔quaternion/SU(2) mappings
- SU(2) conversions – construction from quaternions and axis-angle, validation, round-trips
- Bloch sphere mappings – state↔Bloch, Bloch↔state, quaternion rotation to Bloch vector
- Matrix helpers – trace, determinant, conjugate transpose (dagger), norm, closeness checks
- Validation utilities – axis, complex pair, matrix shape, real number, tolerance checks
Mathematical Conventions
The full reference lives in CONVENTIONS.md.
The five items every downstream package needs to know:
1 · SU(2) Convention
A unit quaternion q = w + xi + yj + zk maps to SU(2) as:
U(q) = [[ w − iz , −y − ix ],
[ y − ix , w + iz ]]
Implemented in Quaternion.to_su2_matrix(); inverted by su2_to_quaternion().
2 · Spinor Convention
States are written |ψ⟩ = α|0⟩ + β|1⟩ with |0⟩ as the north-pole
computational-basis ground state. Amplitudes are always passed as the ordered
pair (alpha, beta). Functions that require unit norm normalize internally.
3 · Bloch Sphere Parameterization
|ψ⟩ = cos(θ/2)|0⟩ + e^{iφ} sin(θ/2)|1⟩
theta ∈ [0, π] (polar/colatitude), phi ∈ [0, 2π) (azimuthal).
|0⟩ → north pole (0, 0, +1); |1⟩ → south pole (0, 0, −1).
4 · Global Phase
q and −q represent the same rotation. spinor_to_quaternion encodes
the rotation up to global phase — never rely on the sign of the scalar part.
5 · Default Tolerance and Axis Labels
All closeness checks default to atol = 1e-9 (absolute, no relative
component). Axis labels are "x", "y", "z" (case-insensitive);
all angles are in radians.
What Is Intentionally Not Included
- Qiskit / PennyLane / Cirq adapters
- Backend execution or hardware drivers
- Circuit transpilation or compilation
- Plotting or visualisation
- Cloud workflow integration
- Notebook tooling
- Algorithm frameworks or optimisation workflows
Those belong in higher-level packages.
Installation
pip install rqm-core
Development install (includes pytest and pytest-cov):
pip install "rqm-core[dev]"
Ecosystem Role
rqm-core is the spine of the RQM Python ecosystem. It provides the
single canonical implementation of all shared mathematical primitives so that
downstream packages never need to re-implement or copy them.
How downstream packages declare the dependency (example pyproject.toml excerpt):
[project]
dependencies = [
"rqm-core>=0.1.0",
]
How downstream packages import:
# rqm-qiskit, rqm-circuits, rqm-compiler, etc.
from rqm_core import Quaternion, axis_angle_to_su2, state_to_bloch
Contract for downstream maintainers:
- All conventions are defined in
CONVENTIONS.mdand must not be re-defined or overridden locally. - Breaking changes to
rqm-corerequire a major version bump (1.0.0, …). - Any primitive needed by two or more packages belongs here, not in the individual packages.
Quickstart
from rqm_core import Quaternion, state_to_bloch, axis_angle_to_su2
import math
# 90° rotation around Y
q = Quaternion.from_axis_angle("y", math.pi / 2)
print(q)
# Quaternion(0.7071..., 0.0, 0.7071..., 0.0)
# SU(2) matrix directly from axis-angle
print(axis_angle_to_su2("y", math.pi / 2))
# |+⟩ state on the Bloch sphere → equator at (1, 0, 0)
c = 1 / math.sqrt(2)
x, y, z = state_to_bloch(c, c)
print(x, y, z) # 1.0 0.0 0.0
Package Structure
rqm-core/
CONVENTIONS.md – canonical mathematical conventions reference
pyproject.toml – package metadata and build config
src/rqm_core/
__init__.py – canonical public API (import everything from here)
py.typed – PEP 561 marker (enables type checking in downstream packages)
quaternion.py – Quaternion class (Hamilton algebra, SO(3)/SU(2) conversions)
spinor.py – spinor normalization, fidelity, spinor↔quaternion/SU(2)
su2.py – SU(2) construction, validation, quaternion round-trips
bloch.py – Bloch sphere mappings and validation
linalg.py – matrix helpers (dagger, trace, determinant, closeness)
validation.py – shared validation helpers (axis, matrix shape, tolerances)
types.py – shared type aliases (ComplexVector2, BlochVector, SU2Matrix, …)
utils.py – small math utilities (angle_wrap, safe_norm, is_finite_*)
tests/
test_quaternion.py
test_spinor.py
test_su2.py
test_bloch.py
test_linalg.py
test_utils.py
test_validation.py
test_public_api.py
examples/
quaternion_basics.py – quaternion construction, composition, conversion
spinor_basics.py – spinor normalization, Bloch mapping, fidelity
su2_bloch_demo.py – axis-angle → SU(2) → Bloch pipeline
bloch_mapping_demo.py – canonical Bloch vectors and round-trip checks
su2_rotation_demo.py – SU(2) construction and quaternion round-trip
Testing
# Install the package in editable mode with test dependencies
pip install -e ".[dev]"
# Run the full test suite
pytest
# Run with coverage report
pytest --cov=rqm_core --cov-report=term-missing
# Run a single test file
pytest tests/test_quaternion.py -v
# Run only the public-API contract tests
pytest tests/test_public_api.py -v
Architectural Notes
-
su2.is_unitaryis a thin domain-scoped shim overlinalg.is_unitary. Both exist intentionally:linalg.is_unitaryis the general-purpose helper;su2.is_unitaryis the public entry point for SU(2)-context callers and is whatrqm_core.is_unitaryresolves to in the top-level API. -
qand−qrepresent the same rotation. Round-trip conversionsq → SU(2) → qare tested against bothqand−q. Downstream code must never compare quaternion scalar parts for sign equality. -
The
src/layout meansrqm_coreis only importable after installation (pip install -e .). Running tests directly without installing first will produceModuleNotFoundError. -
py.typedis included in the installed wheel (via[tool.setuptools.package-data]), enablingmypyandpyrightin any package that depends onrqm-core.
Roadmap
rqm-core is intended to remain small and stable while higher-level packages evolve around it.
Planned additions for future minor versions:
- Quaternion SLERP (spherical linear interpolation)
- SO(3) rotation-matrix ↔ quaternion round-trip helpers
- Mixed-state density matrix utilities
- SU(2) Lie-algebra generators and exponential map
- Type stub files (
.pyi) for IDE completions
No Qiskit or framework dependencies will ever be added to this package.
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 Distribution
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 rqm_core-0.1.4.tar.gz.
File metadata
- Download URL: rqm_core-0.1.4.tar.gz
- Upload date:
- Size: 38.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e40711c2ef20632632e24125a45c7e907bfd5039ec6ef164210eb17b03fc447
|
|
| MD5 |
4ef5bfc2e42617fee2b79920b9f45c67
|
|
| BLAKE2b-256 |
a5a70416cd5c5f8a173e42891d576328664edc35c24177b0bf36b93a7f855627
|
Provenance
The following attestation bundles were made for rqm_core-0.1.4.tar.gz:
Publisher:
publish.yml on RQM-Technologies-dev/rqm-core
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rqm_core-0.1.4.tar.gz -
Subject digest:
9e40711c2ef20632632e24125a45c7e907bfd5039ec6ef164210eb17b03fc447 - Sigstore transparency entry: 1140409510
- Sigstore integration time:
-
Permalink:
RQM-Technologies-dev/rqm-core@3b283430ef0ed49d00cc92ab4548bc4f76dec227 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/RQM-Technologies-dev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3b283430ef0ed49d00cc92ab4548bc4f76dec227 -
Trigger Event:
push
-
Statement type:
File details
Details for the file rqm_core-0.1.4-py3-none-any.whl.
File metadata
- Download URL: rqm_core-0.1.4-py3-none-any.whl
- Upload date:
- Size: 22.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f0ae747702432d8bb55fcae2393ee8f1f71d0c06a40c188e52ef7087315c9301
|
|
| MD5 |
6c2de2bc29a3f85b36357fe8d8bd2cb8
|
|
| BLAKE2b-256 |
e773d87f1095cafd23c7d1b9a4b4b74b61664bc68f08d3a244a32df674bad21d
|
Provenance
The following attestation bundles were made for rqm_core-0.1.4-py3-none-any.whl:
Publisher:
publish.yml on RQM-Technologies-dev/rqm-core
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rqm_core-0.1.4-py3-none-any.whl -
Subject digest:
f0ae747702432d8bb55fcae2393ee8f1f71d0c06a40c188e52ef7087315c9301 - Sigstore transparency entry: 1140409587
- Sigstore integration time:
-
Permalink:
RQM-Technologies-dev/rqm-core@3b283430ef0ed49d00cc92ab4548bc4f76dec227 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/RQM-Technologies-dev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3b283430ef0ed49d00cc92ab4548bc4f76dec227 -
Trigger Event:
push
-
Statement type: