mobiu-q 3.0.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mobiu_q-3.0.3/PKG-INFO +415 -0
- mobiu_q-3.0.3/README.md +380 -0
- mobiu_q-3.0.3/mobiu_q/__init__.py +130 -0
- mobiu_q-3.0.3/mobiu_q/cli.py +48 -0
- mobiu_q-3.0.3/mobiu_q/core.py +1435 -0
- mobiu_q-3.0.3/mobiu_q/experimental/__init__.py +83 -0
- mobiu_q-3.0.3/mobiu_q/experimental/layers.py +317 -0
- mobiu_q-3.0.3/mobiu_q/experimental/layers_fast.py +320 -0
- mobiu_q-3.0.3/mobiu_q.egg-info/PKG-INFO +415 -0
- mobiu_q-3.0.3/mobiu_q.egg-info/SOURCES.txt +15 -0
- mobiu_q-3.0.3/mobiu_q.egg-info/dependency_links.txt +1 -0
- mobiu_q-3.0.3/mobiu_q.egg-info/entry_points.txt +2 -0
- mobiu_q-3.0.3/mobiu_q.egg-info/requires.txt +10 -0
- mobiu_q-3.0.3/mobiu_q.egg-info/top_level.txt +1 -0
- mobiu_q-3.0.3/pyproject.toml +56 -0
- mobiu_q-3.0.3/setup.cfg +4 -0
- mobiu_q-3.0.3/setup.py +73 -0
mobiu_q-3.0.3/PKG-INFO
ADDED
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mobiu-q
|
|
3
|
+
Version: 3.0.3
|
|
4
|
+
Summary: Soft Algebra Optimizer for Quantum & Complex Optimization
|
|
5
|
+
Home-page: https://github.com/mobiu-ai/mobiu-q
|
|
6
|
+
Author: Mobiu Technologies
|
|
7
|
+
Author-email: Mobiu Technologies <ai@mobiu.ai>
|
|
8
|
+
License: Proprietary
|
|
9
|
+
Project-URL: Homepage, https://app.mobiu.ai
|
|
10
|
+
Project-URL: Documentation, https://pypi.org/project/mobiu-q/
|
|
11
|
+
Keywords: quantum,optimization,VQE,QAOA,machine-learning
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
15
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Requires-Python: >=3.8
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
Requires-Dist: numpy>=1.21.0
|
|
25
|
+
Requires-Dist: requests>=2.25.0
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
28
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
29
|
+
Provides-Extra: full
|
|
30
|
+
Requires-Dist: scipy>=1.7.0; extra == "full"
|
|
31
|
+
Requires-Dist: qiskit>=0.40.0; extra == "full"
|
|
32
|
+
Dynamic: author
|
|
33
|
+
Dynamic: home-page
|
|
34
|
+
Dynamic: requires-python
|
|
35
|
+
|
|
36
|
+
# Mobiu-Q v3.0.3
|
|
37
|
+
|
|
38
|
+
**Soft Algebra for Optimization & Attention**
|
|
39
|
+
|
|
40
|
+
[](https://pypi.org/project/mobiu-q/)
|
|
41
|
+
[](LICENSE)
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Overview
|
|
46
|
+
|
|
47
|
+
Mobiu-Q is a framework built on **Soft Algebra** (nilpotent ε²=0) that provides:
|
|
48
|
+
|
|
49
|
+
1. **MobiuOptimizer** - Stable optimization in noisy environments
|
|
50
|
+
2. **MobiuAttention** 🧪 - O(N) linear attention for long sequences
|
|
51
|
+
|
|
52
|
+
Both share the same mathematical foundation but serve different purposes.
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Installation
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install mobiu-q
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Quick Start
|
|
65
|
+
|
|
66
|
+
### MobiuOptimizer (Stable API)
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from mobiu_q import MobiuOptimizer
|
|
70
|
+
import torch
|
|
71
|
+
|
|
72
|
+
# Wrap any PyTorch optimizer
|
|
73
|
+
model = MyModel()
|
|
74
|
+
base_opt = torch.optim.Adam(model.parameters(), lr=0.0003)
|
|
75
|
+
opt = MobiuOptimizer(base_opt, method="adaptive", use_soft_algebra=True)
|
|
76
|
+
|
|
77
|
+
for batch in dataloader:
|
|
78
|
+
loss = criterion(model(batch))
|
|
79
|
+
loss.backward()
|
|
80
|
+
opt.step(loss.item()) # Pass loss for Soft Algebra
|
|
81
|
+
|
|
82
|
+
opt.end() # Important: release resources
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### MobiuAttention (🧪 Experimental)
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from mobiu_q.experimental import MobiuAttention, MobiuBlock
|
|
89
|
+
|
|
90
|
+
# Drop-in replacement for nn.MultiheadAttention
|
|
91
|
+
attn = MobiuAttention(d_model=512, num_heads=8)
|
|
92
|
+
out = attn(x) # x: [batch, seq, dim]
|
|
93
|
+
|
|
94
|
+
# Or use complete block
|
|
95
|
+
block = MobiuBlock(d_model=512, num_heads=8)
|
|
96
|
+
out = block(x)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## MobiuOptimizer
|
|
102
|
+
|
|
103
|
+
### Methods
|
|
104
|
+
|
|
105
|
+
| Method | Use Case | Default LR |
|
|
106
|
+
|------------|---------------------------------------------|------------|
|
|
107
|
+
| `standard` | Smooth landscapes, chemistry, physics | 0.01 |
|
|
108
|
+
| `deep` | Deep circuits, noisy hardware, complex opt | 0.1 |
|
|
109
|
+
| `adaptive` | RL, LLM fine-tuning, high-variance problems | 0.0003 |
|
|
110
|
+
|
|
111
|
+
### Benchmarks
|
|
112
|
+
|
|
113
|
+
| Domain | Improvement | Win Rate | p-value |
|
|
114
|
+
|-------------------------|-------------|----------|---------|
|
|
115
|
+
| Crypto Trading 🆕 | **+56% profit** | **100%** | **<0.001** |
|
|
116
|
+
| LunarLander-v3 | +128% | 97% | <0.001 |
|
|
117
|
+
| MuJoCo InvertedPendulum | +111% | 100% | <0.001 |
|
|
118
|
+
| VQE H₂ (FakeFez) | +52% | 100% | <0.001 |
|
|
119
|
+
| QAOA MaxCut | +45% | 95% | <0.001 |
|
|
120
|
+
|
|
121
|
+
#### Crypto Trading Details
|
|
122
|
+
|
|
123
|
+
Tested on synthetic crypto market with regime switching (bull/bear), flash crashes, and high volatility:
|
|
124
|
+
|
|
125
|
+
| Metric | Adam Baseline | Mobiu Optimizer |
|
|
126
|
+
|--------|---------------|-----------------|
|
|
127
|
+
| Profit | -0.9% | **+55.9%** |
|
|
128
|
+
| Episode Return | -0.17 | **+0.46** |
|
|
129
|
+
|
|
130
|
+
*500 episodes × 10 seeds, p < 0.001*
|
|
131
|
+
|
|
132
|
+
### Maximize vs Minimize
|
|
133
|
+
|
|
134
|
+
By default, Mobiu-Q assumes you're **minimizing** (loss, energy). For RL/Trading where you **maximize** (reward, profit), use the explicit keyword arguments:
|
|
135
|
+
```python
|
|
136
|
+
# Loss minimization - for supervised learning, VQE
|
|
137
|
+
opt.step(loss=loss.item()) # Lower is better
|
|
138
|
+
|
|
139
|
+
# Reward maximization - for RL, trading
|
|
140
|
+
opt.step(reward=episode_return) # Higher is better
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
| Use Case | What to pass | Example |
|
|
144
|
+
|----------|--------------|---------|
|
|
145
|
+
| Supervised Learning | `loss=` | `opt.step(loss=loss.item())` |
|
|
146
|
+
| VQE / QAOA | `loss=` | `opt.step(loss=energy)` |
|
|
147
|
+
| RL (policy gradient) | `reward=` | `opt.step(reward=episode_return)` |
|
|
148
|
+
| Trading | `reward=` | `opt.step(reward=profit)` |
|
|
149
|
+
|
|
150
|
+
**Legacy API** (still supported):
|
|
151
|
+
```python
|
|
152
|
+
# Old way - use maximize flag at init
|
|
153
|
+
opt = MobiuOptimizer(base_opt, method="adaptive", maximize=True)
|
|
154
|
+
opt.step(episode_return)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**Why does this matter?** Soft Algebra tracks the "direction of improvement". Passing reward as loss (or vice versa) confuses the optimizer.
|
|
158
|
+
|
|
159
|
+
### A/B Testing
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
# Test with Soft Algebra
|
|
163
|
+
opt_on = MobiuOptimizer(base_opt, use_soft_algebra=True)
|
|
164
|
+
|
|
165
|
+
# Test without (baseline)
|
|
166
|
+
opt_off = MobiuOptimizer(base_opt, use_soft_algebra=False)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## Base Optimizers
|
|
172
|
+
|
|
173
|
+
Mobiu-Q enhances these base optimizers with Soft Algebra:
|
|
174
|
+
|
|
175
|
+
| Optimizer | Description | Best For |
|
|
176
|
+
|-----------|-------------|----------|
|
|
177
|
+
| `Adam` | Adaptive moments, most popular | Default, most cases |
|
|
178
|
+
| `AdamW` | Adam with decoupled weight decay | LLM, Transformers |
|
|
179
|
+
| `NAdam` | Adam with Nesterov momentum | Alternative to Adam |
|
|
180
|
+
| `AMSGrad` | Adam with max(v) for stability | Drug discovery, unstable loss |
|
|
181
|
+
| `SGD` | Simple gradient descent | QAOA, convex problems |
|
|
182
|
+
| `Momentum` | SGD with momentum | RL, LLM fine-tuning |
|
|
183
|
+
| `LAMB` | Layer-wise adaptive scaling | Large batch training |
|
|
184
|
+
|
|
185
|
+
### Choosing an Optimizer
|
|
186
|
+
|
|
187
|
+
**PyTorch mode** - Choose your optimizer when creating the base optimizer:
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
import torch
|
|
191
|
+
from mobiu_q import MobiuOptimizer
|
|
192
|
+
|
|
193
|
+
# Using Adam (default, recommended for most cases)
|
|
194
|
+
base_opt = torch.optim.Adam(model.parameters(), lr=0.0003)
|
|
195
|
+
opt = MobiuOptimizer(base_opt, method="adaptive")
|
|
196
|
+
|
|
197
|
+
# Using AdamW (recommended for LLM/Transformers)
|
|
198
|
+
base_opt = torch.optim.AdamW(model.parameters(), lr=0.0003, weight_decay=0.01)
|
|
199
|
+
opt = MobiuOptimizer(base_opt, method="adaptive")
|
|
200
|
+
|
|
201
|
+
# Using SGD with Momentum (recommended for RL)
|
|
202
|
+
base_opt = torch.optim.SGD(model.parameters(), lr=0.02, momentum=0.9)
|
|
203
|
+
opt = MobiuOptimizer(base_opt, method="adaptive", maximize=True)
|
|
204
|
+
|
|
205
|
+
# Using NAdam
|
|
206
|
+
base_opt = torch.optim.NAdam(model.parameters(), lr=0.0003)
|
|
207
|
+
opt = MobiuOptimizer(base_opt, method="deep")
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
**Quantum mode** - Choose your optimizer via the `base_optimizer` parameter:
|
|
211
|
+
|
|
212
|
+
```python
|
|
213
|
+
from mobiu_q import MobiuOptimizer
|
|
214
|
+
import numpy as np
|
|
215
|
+
|
|
216
|
+
params = np.random.randn(10)
|
|
217
|
+
|
|
218
|
+
# Using Adam (default)
|
|
219
|
+
opt = MobiuOptimizer(params, method="standard")
|
|
220
|
+
|
|
221
|
+
# Using NAdam
|
|
222
|
+
opt = MobiuOptimizer(params, method="standard", base_optimizer="NAdam")
|
|
223
|
+
|
|
224
|
+
# Using AMSGrad
|
|
225
|
+
opt = MobiuOptimizer(params, method="deep", base_optimizer="AMSGrad")
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
**⚠️ Important:** In Quantum mode, optimizer names are **case-sensitive!**
|
|
229
|
+
|
|
230
|
+
```python
|
|
231
|
+
# ✅ Correct
|
|
232
|
+
opt = MobiuOptimizer(params, base_optimizer="NAdam")
|
|
233
|
+
|
|
234
|
+
# ❌ Wrong - will fall back to Adam
|
|
235
|
+
opt = MobiuOptimizer(params, base_optimizer="nadam")
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## 🛠️ Troubleshooting
|
|
241
|
+
|
|
242
|
+
If optimization is not improving or diverging, try these adjustments:
|
|
243
|
+
|
|
244
|
+
### 1. Switch Base Optimizer
|
|
245
|
+
|
|
246
|
+
Different optimizers work better for different problems:
|
|
247
|
+
|
|
248
|
+
| Problem Type | Recommended Optimizer |
|
|
249
|
+
|--------------|----------------------|
|
|
250
|
+
| LoRA / LLM | `Momentum` or `AdamW` |
|
|
251
|
+
| VQE / Chemistry | `Adam` |
|
|
252
|
+
| QAOA | `NAdam` |
|
|
253
|
+
| RL / Trading | `Momentum` |
|
|
254
|
+
| Drug Discovery | `AMSGrad` |
|
|
255
|
+
| Large Batch | `LAMB` |
|
|
256
|
+
|
|
257
|
+
```python
|
|
258
|
+
# PyTorch: If Adam isn't working, try Momentum:
|
|
259
|
+
base_opt = torch.optim.SGD(model.parameters(), lr=0.02, momentum=0.9)
|
|
260
|
+
opt = MobiuOptimizer(base_opt, method="adaptive")
|
|
261
|
+
|
|
262
|
+
# Quantum: If Adam isn't working, try NAdam:
|
|
263
|
+
opt = MobiuOptimizer(params, base_optimizer="NAdam", method="adaptive")
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### 2. Switch Method
|
|
267
|
+
|
|
268
|
+
| If This Fails | Try This |
|
|
269
|
+
|---------------|----------|
|
|
270
|
+
| `standard` | `adaptive` |
|
|
271
|
+
| `adaptive` | `deep` |
|
|
272
|
+
| `deep` | `standard` |
|
|
273
|
+
|
|
274
|
+
```python
|
|
275
|
+
# If standard isn't working for your problem:
|
|
276
|
+
opt = MobiuOptimizer(base_opt, method="adaptive")
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### 3. Switch Mode (Quantum only)
|
|
280
|
+
|
|
281
|
+
| If This Fails | Try This |
|
|
282
|
+
|---------------|----------|
|
|
283
|
+
| `simulation` | `hardware` |
|
|
284
|
+
|
|
285
|
+
```python
|
|
286
|
+
opt = MobiuOptimizer(params, method="standard", mode="hardware")
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### 4. Adjust Learning Rate
|
|
290
|
+
|
|
291
|
+
```python
|
|
292
|
+
# Try lower LR if diverging
|
|
293
|
+
base_opt = torch.optim.Adam(model.parameters(), lr=0.0001)
|
|
294
|
+
|
|
295
|
+
# Try higher LR if stuck
|
|
296
|
+
base_opt = torch.optim.Adam(model.parameters(), lr=0.001)
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### 5. Common Fixes by Domain
|
|
300
|
+
|
|
301
|
+
| Domain | Common Issue | Fix |
|
|
302
|
+
|--------|--------------|-----|
|
|
303
|
+
| **LoRA** | SGD + high LR diverges | Use `Momentum` + LR=0.02 |
|
|
304
|
+
| **Drug Discovery** | BCE loss unstable | Use `AMSGrad` + `standard` method |
|
|
305
|
+
| **Crypto/RL** | High variance | Use `Momentum` + `adaptive` method |
|
|
306
|
+
| **QAOA** | Local minima | Use `NAdam` + `deep` method |
|
|
307
|
+
|
|
308
|
+
---
|
|
309
|
+
|
|
310
|
+
## MobiuAttention 🧪
|
|
311
|
+
|
|
312
|
+
### Why?
|
|
313
|
+
|
|
314
|
+
Standard Transformer attention is O(N²) in sequence length. MobiuAttention is **O(N)**.
|
|
315
|
+
|
|
316
|
+
| Seq Length | Transformer | MobiuAttention | Speedup |
|
|
317
|
+
|------------|-------------|----------------|---------|
|
|
318
|
+
| 2,048 | 21s | 9s | 2.3x |
|
|
319
|
+
| 4,096 | 39s | 10s | 3.9x |
|
|
320
|
+
| 8,192 | 42s | 7s | 6.0x |
|
|
321
|
+
| 16,384 | **OOM** 💥 | 5s ✅ | ∞ |
|
|
322
|
+
|
|
323
|
+
### Quality (Same as Transformer)
|
|
324
|
+
|
|
325
|
+
| Benchmark | Transformer | MobiuAttention |
|
|
326
|
+
|----------------------|-------------|----------------|
|
|
327
|
+
| Shakespeare PPL | 12.8 | 13.5 |
|
|
328
|
+
| ListOps Accuracy | 81% | 82% |
|
|
329
|
+
| Needle-in-Haystack | 100% | 100% |
|
|
330
|
+
|
|
331
|
+
### Usage
|
|
332
|
+
|
|
333
|
+
```python
|
|
334
|
+
from mobiu_q.experimental import MobiuBlock
|
|
335
|
+
|
|
336
|
+
class LongContextLM(nn.Module):
|
|
337
|
+
def __init__(self, vocab, d=512, h=8, layers=6):
|
|
338
|
+
super().__init__()
|
|
339
|
+
self.embed = nn.Embedding(vocab, d)
|
|
340
|
+
self.blocks = nn.Sequential(*[MobiuBlock(d, h) for _ in range(layers)])
|
|
341
|
+
self.head = nn.Linear(d, vocab)
|
|
342
|
+
|
|
343
|
+
def forward(self, x):
|
|
344
|
+
return self.head(self.blocks(self.embed(x)))
|
|
345
|
+
|
|
346
|
+
# Works with 16K+ tokens!
|
|
347
|
+
model = LongContextLM(50000)
|
|
348
|
+
x = torch.randint(0, 50000, (1, 16384))
|
|
349
|
+
out = model(x) # No OOM!
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
### ⚠️ Experimental Status
|
|
353
|
+
|
|
354
|
+
- Functional and tested
|
|
355
|
+
- API may change in future versions
|
|
356
|
+
- Feedback welcome!
|
|
357
|
+
|
|
358
|
+
---
|
|
359
|
+
|
|
360
|
+
## How It Works
|
|
361
|
+
|
|
362
|
+
### Soft Algebra
|
|
363
|
+
|
|
364
|
+
Both optimizer and attention use the nilpotent property ε²=0:
|
|
365
|
+
|
|
366
|
+
```
|
|
367
|
+
SoftNumber multiplication: (a,b) × (c,d) = (ad + bc, bd)
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
This enables tracking both "potential" and "realized" components.
|
|
371
|
+
|
|
372
|
+
### In Optimization
|
|
373
|
+
|
|
374
|
+
```python
|
|
375
|
+
lr_t = base_lr × (1 + soft_component)
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
Soft Algebra adapts learning rate based on loss landscape curvature.
|
|
379
|
+
|
|
380
|
+
### In Attention
|
|
381
|
+
|
|
382
|
+
```python
|
|
383
|
+
S(t) = γ·S(t-1) + k_t ⊗ v_t # O(N) state update
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
Instead of O(N²) pairwise attention, we track state with O(N) complexity.
|
|
387
|
+
|
|
388
|
+
---
|
|
389
|
+
|
|
390
|
+
## License
|
|
391
|
+
|
|
392
|
+
Free tier: 20 API calls/month (optimizer only)
|
|
393
|
+
Pro tier: Unlimited - https://app.mobiu.ai
|
|
394
|
+
|
|
395
|
+
**Note:** MobiuAttention runs locally, no API calls required.
|
|
396
|
+
|
|
397
|
+
---
|
|
398
|
+
|
|
399
|
+
## Links
|
|
400
|
+
|
|
401
|
+
- [PyPI](https://pypi.org/project/mobiu-q/)
|
|
402
|
+
- [GitHub Issues](https://github.com/mobiu-ai/mobiu-q/issues)
|
|
403
|
+
|
|
404
|
+
---
|
|
405
|
+
|
|
406
|
+
## Citation
|
|
407
|
+
|
|
408
|
+
```bibtex
|
|
409
|
+
@software{mobiu_q,
|
|
410
|
+
title={Mobiu-Q: Soft Algebra for Optimization and Attention},
|
|
411
|
+
author={Mobiu Technologies},
|
|
412
|
+
year={2026},
|
|
413
|
+
url={https://github.com/mobiu-ai/mobiu-q}
|
|
414
|
+
}
|
|
415
|
+
```
|