Skip to content

Commit 8939ff9

Browse files
Build initial production v0 of rqm-optimize package
Co-authored-by: RQM-Technologies-dev <267137213+RQM-Technologies-dev@users.noreply.github.com>
1 parent eb59356 commit 8939ff9

14 files changed

Lines changed: 1373 additions & 1 deletion

AGENTS.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# AGENTS.md — rqm-optimize repo discipline
2+
3+
## Purpose
4+
5+
`rqm-optimize` is the **optimization layer** in the RQM Technologies quantum software stack.
6+
7+
Its job is to reduce circuit depth and gate count by compressing single-qubit gate runs through SU(2)-aware fusion, and to provide a foundation for backend-aware optimization in future versions.
8+
9+
---
10+
11+
## Stack position
12+
13+
```text
14+
rqm-core → canonical quaternion / SU(2) / Bloch math
15+
rqm-compiler → circuit construction / normalization
16+
rqm-qiskit → Qiskit lowering / execution bridge
17+
rqm-braket → Braket lowering / execution bridge
18+
rqm-optimize → optimization / compression layer ← this repo
19+
```
20+
21+
---
22+
23+
## What belongs here
24+
25+
- Single-qubit run fusion and compression logic
26+
- SU(2)-aware decomposition helpers that are specific to optimization workflows
27+
- Gate count metrics and optimization quality metrics
28+
- Future: backend-aware native-axis alignment
29+
- Future: drift-aware path selection
30+
- Future: quaternionic error metrics
31+
- Future: Braket circuit support (adapter only, not execution)
32+
33+
---
34+
35+
## What does NOT belong here
36+
37+
- **Canonical quaternion or SU(2) math** — that belongs in `rqm-core`. Do not duplicate it here.
38+
- **Qiskit execution** — that belongs in `rqm-qiskit`.
39+
- **Hardware execution** — never here.
40+
- **Backend calibration mutation** — never here.
41+
- **Full transpilation** — this is an optimizer, not a transpiler.
42+
- **Circuit construction** — that belongs in `rqm-compiler`.
43+
44+
---
45+
46+
## Module responsibilities
47+
48+
| Module | Responsibility |
49+
|--------|---------------|
50+
| `optimizer.py` | Public API, type dispatch, strategy validation, result packaging |
51+
| `fusion.py` | Scanning circuits, identifying single-qubit runs, fusing them |
52+
| `geometry.py` | SU(2) / global-phase normalization helpers (small, rigorous) |
53+
| `metrics.py` | Gate count metrics, matrix error norms, stubs for future quaternionic metrics |
54+
| `qiskit_adapter.py` | Qiskit-specific instruction inspection, matrix extraction, Euler emission |
55+
56+
---
57+
58+
## Public API rules
59+
60+
- The public API lives in `src/rqm_optimize/__init__.py` and is controlled by `__all__`.
61+
- Keep the public API small: `optimize` and `OptimizationResult` are the surface area.
62+
- New public symbols require deliberate addition to `__all__`.
63+
- Do not expose internal helpers in the public API.
64+
65+
---
66+
67+
## Code discipline
68+
69+
- All public functions must have type hints and docstrings.
70+
- All behavior changes require tests.
71+
- No placeholder TODO spam in production code.
72+
- No speculative claims in code comments.
73+
- No dead code.
74+
- Functions should be small and do one thing.
75+
- Prefer readability over cleverness.
76+
77+
---
78+
79+
## Testing
80+
81+
- Tests live in `tests/`.
82+
- Use `pytest` for all tests.
83+
- Equivalence tests must use tolerance-aware comparison (`numpy.allclose` or `Operator` fidelity).
84+
- Circuits with measurements cannot be compared by unitary — compare structure or unitary prefix.
85+
- All regression bugs get a test.
86+
87+
---
88+
89+
## Versioning
90+
91+
- v0.1.x: Qiskit single-qubit run fusion only.
92+
- v0.2.x: Backend-aware native-axis alignment (planned).
93+
- v1.0: Stable public API, Braket support, quaternionic metrics.

README.md

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,140 @@
11
# rqm-optimize
2-
rqm-optimize compresses single-qubit gate runs into shorter SU(2)-equivalent forms, reducing unnecessary depth without changing the user workflow.
2+
3+
> `rqm-optimize` improves quantum circuits by compressing single-qubit gate runs through SU(2)-aware fusion, reducing unnecessary gate depth while preserving circuit behavior.
4+
5+
[![Python 3.9+](https://img.shields.io/badge/python-3.9%2B-blue.svg)](https://www.python.org/)
6+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
7+
8+
---
9+
10+
## Purpose
11+
12+
`rqm-optimize` is the **optimization layer** of the RQM Technologies quantum software stack. It accepts a Qiskit `QuantumCircuit`, scans it for contiguous single-qubit gate runs, fuses those runs into minimal SU(2)-equivalent operations, and returns a simplified circuit that is unitary-equivalent to the original up to global phase.
13+
14+
---
15+
16+
## Stack placement
17+
18+
```text
19+
rqm-core → canonical quaternion / SU(2) / Bloch math
20+
rqm-compiler → circuit construction / normalization
21+
rqm-qiskit → Qiskit lowering / execution bridge
22+
rqm-braket → Braket lowering / execution bridge
23+
rqm-optimize → optimization / compression layer ← this package
24+
```
25+
26+
---
27+
28+
## Installation
29+
30+
```bash
31+
pip install rqm-optimize
32+
```
33+
34+
Or from source:
35+
36+
```bash
37+
git clone https://github.com/RQM-Technologies-dev/rqm-optimize.git
38+
cd rqm-optimize
39+
pip install -e ".[dev]"
40+
```
41+
42+
---
43+
44+
## Quickstart
45+
46+
```python
47+
from qiskit import QuantumCircuit
48+
from rqm_optimize import optimize
49+
50+
qc = QuantumCircuit(1)
51+
qc.rx(0.5, 0)
52+
qc.ry(0.3, 0)
53+
qc.rz(0.2, 0)
54+
55+
# Simple usage — returns an optimized QuantumCircuit.
56+
qc_opt = optimize(qc)
57+
58+
# With metadata.
59+
result = optimize(qc, return_metadata=True)
60+
print(result.original_gate_count) # 3
61+
print(result.optimized_gate_count) # 1
62+
print(result.fused_runs) # 1
63+
print(result.circuit)
64+
```
65+
66+
---
67+
68+
## What v0 does
69+
70+
- Detects **contiguous single-qubit gate runs** on each qubit.
71+
- Fuses each run into a **single SU(2)-equivalent gate** using matrix
72+
multiplication followed by Qiskit's `OneQubitEulerDecomposer`.
73+
- Skips fusion when the decomposition would produce more gates than the
74+
original (i.e., only applies optimizations that reduce or maintain gate count).
75+
- Preserves **barriers, measurements, resets, and multi-qubit gates** exactly
76+
as hard boundaries.
77+
- Never mutates the input circuit.
78+
- Returns deterministic output.
79+
80+
### Supported gates in v0
81+
82+
`rx`, `ry`, `rz`, `u`, `u3`, `u2`, `u1`, `p`, `x`, `y`, `z`, `h`, `s`, `sdg`,
83+
`t`, `tdg`, `id`, `sx`, `sxdg`, `r`, and any generic single-qubit
84+
`UnitaryGate` whose matrix can be extracted.
85+
86+
---
87+
88+
## What v0 does not yet do
89+
90+
- Backend-aware native-axis alignment (planned for v0.2).
91+
- Quaternionic error metrics and drift-aware path selection (planned).
92+
- Braket circuit support (planned).
93+
- Two-qubit gate optimization.
94+
95+
---
96+
97+
## Architecture
98+
99+
```
100+
src/rqm_optimize/
101+
├── __init__.py # Public API: optimize, OptimizationResult
102+
├── optimizer.py # Type dispatch, strategy validation, result packaging
103+
├── fusion.py # Single-qubit run identification and matrix fusion
104+
├── geometry.py # SU(2) / global-phase normalization helpers
105+
├── metrics.py # Gate count metrics, matrix error norms
106+
├── qiskit_adapter.py # Qiskit instruction inspection, matrix extraction, Euler emission
107+
└── py.typed # PEP 561 marker
108+
```
109+
110+
The public surface area is intentionally minimal: `optimize()` and
111+
`OptimizationResult`. All internal helpers are private.
112+
113+
---
114+
115+
## Development and testing
116+
117+
```bash
118+
# Install with dev dependencies.
119+
pip install -e ".[dev]"
120+
121+
# Run tests.
122+
pytest
123+
124+
# Run the example.
125+
python examples/basic_optimize.py
126+
```
127+
128+
Tests cover:
129+
130+
- Public API importability and `__all__` contract.
131+
- Fusion correctness (runs compressed, boundaries respected, equivalence up to global phase).
132+
- Integration tests comparing unitaries using `qiskit.quantum_info.Operator`.
133+
- Measurement / barrier / multi-qubit structure preservation.
134+
- Metadata fields and determinism.
135+
136+
---
137+
138+
## License
139+
140+
MIT © RQM Technologies

examples/basic_optimize.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""examples/basic_optimize.py — Minimal runnable example for rqm-optimize.
2+
3+
Demonstrates the core workflow: build a circuit, optimize it, inspect results.
4+
"""
5+
6+
from qiskit import QuantumCircuit
7+
8+
from rqm_optimize import optimize
9+
10+
# Build a simple one-qubit circuit with multiple rotations.
11+
qc = QuantumCircuit(1, name="example")
12+
qc.rx(0.5, 0)
13+
qc.ry(0.3, 0)
14+
qc.rz(0.2, 0)
15+
qc.h(0)
16+
qc.rx(1.1, 0)
17+
qc.rz(0.8, 0)
18+
19+
print("Original circuit:")
20+
print(qc)
21+
print(f"Original gate count: {len(qc.data)}")
22+
23+
# Optimize with metadata.
24+
result = optimize(qc, return_metadata=True)
25+
26+
print("\nOptimized circuit:")
27+
print(result.circuit)
28+
print(f"Optimized gate count: {result.optimized_gate_count}")
29+
print(f"Fused runs: {result.fused_runs}")
30+
print(f"Strategy: {result.strategy}")

pyproject.toml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[build-system]
2+
requires = ["hatchling"]
3+
build-backend = "hatchling.build"
4+
5+
[project]
6+
name = "rqm-optimize"
7+
version = "0.1.0"
8+
description = "SU(2)-aware circuit compression for quantum workflows"
9+
readme = "README.md"
10+
license = { file = "LICENSE" }
11+
authors = [{ name = "RQM Technologies", email = "dev@rqm.tech" }]
12+
keywords = ["quantum", "optimization", "qiskit", "circuit", "su2", "quaternion"]
13+
classifiers = [
14+
"Development Status :: 3 - Alpha",
15+
"Intended Audience :: Science/Research",
16+
"License :: OSI Approved :: MIT License",
17+
"Programming Language :: Python :: 3",
18+
"Programming Language :: Python :: 3.9",
19+
"Programming Language :: Python :: 3.10",
20+
"Programming Language :: Python :: 3.11",
21+
"Programming Language :: Python :: 3.12",
22+
"Topic :: Scientific/Engineering :: Physics",
23+
"Typing :: Typed",
24+
]
25+
requires-python = ">=3.9"
26+
dependencies = [
27+
"qiskit>=1.0",
28+
"numpy>=1.24",
29+
]
30+
31+
[project.optional-dependencies]
32+
dev = ["pytest>=7.0", "pytest-cov"]
33+
34+
[project.urls]
35+
Homepage = "https://github.com/RQM-Technologies-dev/rqm-optimize"
36+
Repository = "https://github.com/RQM-Technologies-dev/rqm-optimize"
37+
Issues = "https://github.com/RQM-Technologies-dev/rqm-optimize/issues"
38+
39+
[tool.hatch.build.targets.wheel]
40+
packages = ["src/rqm_optimize"]
41+
42+
[tool.pytest.ini_options]
43+
testpaths = ["tests"]
44+
addopts = "-v"

src/rqm_optimize/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""rqm-optimize — SU(2)-aware circuit compression for quantum workflows.
2+
3+
Public API::
4+
5+
from rqm_optimize import optimize, OptimizationResult
6+
7+
result = optimize(qc, return_metadata=True)
8+
"""
9+
10+
from .optimizer import OptimizationResult, optimize
11+
12+
__all__ = ["optimize", "OptimizationResult"]

0 commit comments

Comments
 (0)