File size: 7,915 Bytes
8a46533
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
---
license: mit
base_model: microsoft/bitnet-b1.58-2B-4T-bf16
tags:
  - quantum-computing
  - bitnet
  - lora
  - peft
  - algorithm-recommendation
  - research-prototype
library_name: peft
pipeline_tag: text-generation
---

# QARE — Quantum Algorithm Recommendation Engine (BitNet b1.58 2B + LoRA)

A LoRA adapter for `microsoft/bitnet-b1.58-2B-4T-bf16` that recommends a quantum,
hybrid, or classical algorithm for a computational problem given hardware
constraints, and explains the choice in a fixed format.

> **Research prototype.** Read the Limitations section before using or citing
> this. Reported accuracy is **not** a measure of quantum-computing correctness.

## ⚠️ This model is not standalone

The adapter was trained on prompts that already contain a **precomputed
resource-requirements block**. Prompted without it, the model **fabricates
requirements** — measured: it claimed Grover's Algorithm needs *~2057 qubits* for
a 20-bit search space (the correct value is **21**), then reasoned validly from
its own false premise to the wrong recommendation.

QARE is therefore a **hybrid system**:

| Component | Role |
|---|---|
| `knowledge_base.py` | Deterministic resource estimation + feasibility — **makes the decision** |
| BitNet + this LoRA | Ranking, reasoning, natural-language explanation — **narrates the decision** |

**Always use `qare_engine.py` (included in this repo).** It builds the required
prompt. Prompting the weights directly is a misuse.

## Usage

```python
# files ship with this repo
from qare_engine import QAREEngine

eng = QAREEngine(
    base="microsoft/bitnet-b1.58-2B-4T-bf16",
    adapter="USERNAME/qare-bitnet-lora",
)

out = eng.recommend(
    problem_type="unstructured_search",
    size=20,                  # log2 of the database size
    available_qubits=1000,
    noise="none",
    max_depth=100000,
    hardware="simulator",
    desired_accuracy=0.9,
)
print(out["text"])          # model's explanation
print(out["kb_reference"])  # deterministic KB answer (audit trail)
```

The engine constructs prompts of this form:

```
Problem: unstructured search on a 20-item database (log2) instance.
Hardware: simulator
Available qubits: 1000
Noise level: none
Max circuit depth: 100000
Desired accuracy: 0.9

Computed resource requirements (from a resource estimator):
  Grover's Algorithm: needs 21 qubits, depth 805 -> FITS
  Quantum Walk Search: needs 10 qubits, depth 121 -> FITS
  Classical algorithm: 0 qubits, no circuit -> always available
```

Output format: `Primary Algorithm · Confidence Score · Quantum Practical ·
Reasoning · Alternative Algorithms (ranked) · Estimated Hardware Requirements ·
Expected Advantages · Expected Limitations · References`.

**Coverage:** Shor, Grover, QAOA, VQE, QPE, HHL, Quantum Walk, Quantum
Annealing, Quantum Kernel/VQC, Gaussian Boson Sampling, Trotterization, and
classical fallbacks across 11 problem types and 7 hardware families.

## Training

| | |
|---|---|
| Base | `microsoft/bitnet-b1.58-2B-4T-bf16` (BF16 master weights) |
| Method | LoRA r=16, α=32, dropout 0.05, on q/k/v/o/gate/up/down projections |
| Trainable | 21.6M / 2.43B (0.89%) |
| Data | 6,000 synthetic examples (4,796 train / 594 val / 610 test), stratified |
| Schedule | 3 epochs, 900 steps, effective batch 16, lr 1e-4 cosine, maxlen 768 |
| Precision | **bf16 (required)** — fp16 overflows BitLinear → `loss=0`, `grad_norm=nan` |
| Loss | **Completion-only masking** (loss on the answer, not the templated prompt) |
| Hardware | ~26 min on a single A100 |

### Dataset
No public problem→quantum-algorithm recommendation dataset exists. Data is
**synthetic**, generated by sampling problem/hardware configurations and
labeling them with a deterministic rule-based knowledge base encoding standard
complexity and NISQ-era hardware facts (Shor 1994; Grover 1996; Farhi et al.
2014; Peruzzo et al. 2014; Harrow–Hassidim–Lloyd 2009; Preskill 2018; Nielsen &
Chuang). No copyrighted text is reproduced — only structured facts. ~60% of
labels are "use a classical algorithm," which reflects genuine NISQ-era
practicality.

## Evaluation — read this carefully

Top-1 accuracy on 30 held-out test examples, vs the untrained base model:

| Prompt variant | Zero-shot | Fine-tuned | Δ | McNemar p | 95% CI |
|---|---|---|---|---|---|
| v1 — no computed requirements | 0.233 | 0.133 | −0.10 | 0.51 | [−0.30, +0.10] |
| v3 — requirements **+ FITS/BLOCKED verdict** | 0.700 | **1.000** | **+0.30** | **0.0039** | [+0.13, +0.47] |

v3 fixed 9 examples and broke 0. The improvement is statistically significant.

### 🚨 The headline number is inflated by label leakage

The v3 prompt includes the knowledge base's own feasibility verdict
(`-> FITS` / `-> BLOCKED`). That **is** the decision. The task partly collapses to
"pick the first item marked FITS," solvable without any quantum knowledge —
which is exactly why the **zero-shot** score jumped from 0.233 to 0.700 for a
model that knows nothing about QARE.

**Do not read 1.00 as quantum-algorithm competence.** A cleaner ablation
(numbers supplied, verdict withheld, forcing the model to *compare* rather than
*derive*) is the correct next experiment and has not been run.

### The interesting finding
Training loss reached **0.09** while test top-1 was **0.133** — near-perfect loss
alongside near-total task failure. Two causes, both instructive:

1. Loss originally included the highly templated prompt, so it was dominated by
   trivially predictable tokens (fixed via completion-only masking).
2. The model learned the **form** of technical reasoning long before the
   **content** — it produced correctly-structured justifications built on
   fabricated numbers.

**Takeaway: a 2B instruction-tuned model can learn a technical schema without
learning the underlying quantitative constraints. Low loss on templated output
can mask complete failure.** Inspecting raw generations caught what the metric hid.

## Limitations

- **Not standalone** — requires `qare_engine.py`; otherwise it invents requirements.
- **Leaked evaluation** — the 1.00 reflects a partly-given answer (see above).
- **Circular ground truth** — test labels come from the same KB as training data.
  This measures *learning the KB*, **not** objective correctness. A real accuracy
  claim needs an expert-reviewed test set.
- **Small eval** — n=30 (BitNet generation in transformers is slow). Wide CIs.
- **Order-of-magnitude KB** — qubit/depth formulas are approximations, not
  compiler-exact. Swap in Qiskit / Azure QRE for hardened estimates.
- **Not ternary at runtime** — this is the BF16 master-weights path, a dense
  ~2.4B model. `transformers` has **no** optimized BitNet kernels; 1.58-bit
  efficiency requires [`bitnet.cpp`](https://github.com/microsoft/BitNet) with
  the packed repo.
- **Not hardware-validated** — no recommendation here has been checked against a
  real quantum device. Do not use for research or procurement decisions.

## Intended use

Research and educational demonstration of (a) instruction-tuning a 1.58-bit
model with LoRA, and (b) hybrid symbolic + LM system design where a
deterministic engine owns correctness and the LM owns explanation.

**Out of scope:** production quantum-algorithm selection, hardware procurement,
or any decision where a wrong recommendation carries cost.

## Reproducing

Full pipeline (data generation → training → evaluation → significance testing →
inference) is in the project's all-in-one Colab notebook.

## Citation

```bibtex
@software{qare_bitnet_2026,
  title  = {QARE: A Quantum Algorithm Recommendation Engine via LoRA
            Fine-Tuning of BitNet b1.58},
  year   = {2026},
  note   = {Research prototype. Reported accuracy is affected by
            evaluation label leakage; see model card.}
}
```

## License
MIT (inherited from `microsoft/bitnet-b1.58-2B-4T`).