|
1 | 1 | # 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 | +[](https://www.python.org/) |
| 6 | +[](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 |
0 commit comments