Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,99 +1,230 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
-
- ๐ **Confidence Estimation** - Estimates how confident the model is in its response
|
| 9 |
-
- ๐ซ **Refusal Detection** - Identifies when the model should refuse to answer
|
| 10 |
-
- โก **Classical Fallback** - Works without PennyLane using classical approximation
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
```bash
|
| 15 |
pip install pennylane torch transformers
|
| 16 |
```
|
| 17 |
|
| 18 |
-
## Usage
|
| 19 |
-
|
| 20 |
### Quick Start
|
| 21 |
|
| 22 |
```python
|
| 23 |
from quantum_head import load_qgpt
|
| 24 |
|
| 25 |
-
# Load
|
| 26 |
-
model, tokenizer = load_qgpt(
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
)
|
| 31 |
|
| 32 |
# Generate with confidence
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
|
|
|
| 36 |
print(f"Response: {tokenizer.decode(outputs['sequences'][0])}")
|
| 37 |
-
print(f"Confidence: {outputs['confidence_label']}") #
|
| 38 |
-
print(f"Should refuse: {outputs['should_refuse']}")
|
| 39 |
```
|
| 40 |
|
| 41 |
-
### Just the Quantum Head
|
| 42 |
|
| 43 |
```python
|
| 44 |
from quantum_head import QuantumHead
|
| 45 |
import torch
|
| 46 |
|
| 47 |
-
# Create quantum head
|
| 48 |
-
head = QuantumHead(hidden_size=2880)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
hidden_states
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
```
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
# Create synthetic training data
|
| 62 |
-
python train.py --model squ11z1/gpt-oss-9b-reasoning --create-data --data train.jsonl
|
| 63 |
|
| 64 |
-
|
| 65 |
-
python train.py --model squ11z1/gpt-oss-9b-reasoning --data train.jsonl --epochs 3
|
| 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 |
```bibtex
|
| 99 |
@misc{qgpt2026,
|
|
@@ -104,6 +235,17 @@ Hidden States โ [Classical Compression] โ [Quantum Circuit] โ [Post-Proces
|
|
| 104 |
}
|
| 105 |
```
|
| 106 |
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
library_name: transformers
|
| 6 |
+
tags:
|
| 7 |
+
- quantum
|
| 8 |
+
- confidence-estimation
|
| 9 |
+
- uncertainty
|
| 10 |
+
- pennylane
|
| 11 |
+
- gpt-oss
|
| 12 |
+
- hallucination-detection
|
| 13 |
+
pipeline_tag: text-classification
|
| 14 |
+
---
|
| 15 |
|
| 16 |
+
<div align="center">
|
| 17 |
|
| 18 |
+
# ๐ฎ Q-GPT
|
| 19 |
|
| 20 |
+
### Quantum-Enhanced Confidence Estimation for Language Models
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
[](https://pennylane.ai/)
|
| 23 |
+
[](https://pytorch.org/)
|
| 24 |
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
| 25 |
+
|
| 26 |
+
**Know when your LLM is confident โ and when it's guessing.**
|
| 27 |
+
|
| 28 |
+
</div>
|
| 29 |
+
|
| 30 |
+
---
|
| 31 |
+
|
| 32 |
+
## ๐ฏ What is Q-GPT?
|
| 33 |
+
|
| 34 |
+
Q-GPT is a **quantum neural network head** that attaches to any language model and estimates how confident the model is in its response. It helps you detect when the model might be "hallucinating" or making up information.
|
| 35 |
+
|
| 36 |
+
### The Problem
|
| 37 |
+
|
| 38 |
+
Large Language Models (LLMs) always produce fluent text โ even when they don't know the answer. They sound confident even when they're wrong. This makes it hard to trust their outputs in critical applications.
|
| 39 |
+
|
| 40 |
+
### The Solution
|
| 41 |
+
|
| 42 |
+
Q-GPT analyzes the internal hidden states of the model using a **variational quantum circuit**. Quantum computing naturally captures complex patterns and uncertainties that classical networks might miss. The result: a confidence score that tells you whether to trust the response.
|
| 43 |
+
|
| 44 |
+
---
|
| 45 |
+
|
| 46 |
+
## ๐ง How It Works
|
| 47 |
+
|
| 48 |
+
```
|
| 49 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 50 |
+
โ Q-GPT Architecture โ
|
| 51 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
|
| 52 |
+
โ โ
|
| 53 |
+
โ LLM Hidden States Quantum Circuit โ
|
| 54 |
+
โ [2880 dimensions] [4 qubits] โ
|
| 55 |
+
โ โ โ โ
|
| 56 |
+
โ โผ โ โ
|
| 57 |
+
โ โโโโโโโโโโโโโโโ โ โ
|
| 58 |
+
โ โ Compress โ โโโโโโโโโโโโโโโโโโโบ โ โ
|
| 59 |
+
โ โ to 4 dims โ โ โ
|
| 60 |
+
โ โโโโโโโโโโโโโโโ โผ โ
|
| 61 |
+
โ โโโโโโโโโโโโโโโโโโโ โ
|
| 62 |
+
โ โ RY RZ โ โ
|
| 63 |
+
โ โ โ โ โ Layer 1 โ
|
| 64 |
+
โ โ Rot โโโ CNOT โ โ
|
| 65 |
+
โ โโโโโโโโโโโโโโโโโโโค โ
|
| 66 |
+
โ โ Rot โโโ CNOT โ Layer 2 โ
|
| 67 |
+
โ โโโโโโโโโโโโโโโโโโโค โ
|
| 68 |
+
โ โ Rot โโโ CNOT โ Layer 3 โ
|
| 69 |
+
โ โโโโโโโโโโโโโโโโโโโ โ
|
| 70 |
+
โ โ โ
|
| 71 |
+
โ โผ โ
|
| 72 |
+
โ โโโโโโโโโโโโโโโโโโโ โ
|
| 73 |
+
โ โ Measure โจZโฉ โ โ
|
| 74 |
+
โ โ on each qubit โ โ
|
| 75 |
+
โ โโโโโโโโโโโโโโโโโโโ โ
|
| 76 |
+
โ โ โ
|
| 77 |
+
โ โผ โ
|
| 78 |
+
โ โโโโโโโโโโโโโโโโโโโ โ
|
| 79 |
+
โ โ Confidence โ โ
|
| 80 |
+
โ โ 0.0 โ 1.0 โ โ
|
| 81 |
+
โ โโโโโโโโโโโโโโโโโโโ โ
|
| 82 |
+
โ โ
|
| 83 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
### Step by Step:
|
| 87 |
+
|
| 88 |
+
1. **Extract Hidden States** โ When the LLM generates a response, we capture its internal representation (hidden states from the last layer).
|
| 89 |
+
|
| 90 |
+
2. **Compress** โ The high-dimensional hidden states (2880 dimensions for GPT-OSS) are compressed to 4 values using a small neural network.
|
| 91 |
+
|
| 92 |
+
3. **Quantum Encoding** โ These 4 values are encoded into quantum states using rotation gates (RY, RZ). Each value controls the angle of rotation for one qubit.
|
| 93 |
+
|
| 94 |
+
4. **Variational Layers** โ The qubits pass through multiple layers of:
|
| 95 |
+
- **Rotation gates** (trainable parameters that learn patterns)
|
| 96 |
+
- **CNOT gates** (create entanglement between qubits)
|
| 97 |
+
|
| 98 |
+
5. **Measurement** โ We measure the expectation value โจZโฉ of each qubit, giving us 4 numbers between -1 and +1.
|
| 99 |
+
|
| 100 |
+
6. **Confidence Output** โ A final layer converts these measurements into a confidence score (0-1) and an uncertainty estimate.
|
| 101 |
+
|
| 102 |
+
### Why Quantum?
|
| 103 |
+
|
| 104 |
+
- **Entanglement** captures complex correlations in the data that classical networks struggle with
|
| 105 |
+
- **Superposition** allows exploring multiple states simultaneously
|
| 106 |
+
- **Inherent probabilistic nature** naturally represents uncertainty
|
| 107 |
+
- **Compact representation** โ 4 qubits can represent 16-dimensional state space
|
| 108 |
+
|
| 109 |
+
---
|
| 110 |
+
|
| 111 |
+
## ๐ What You Get
|
| 112 |
+
|
| 113 |
+
| Output | Description |
|
| 114 |
+
|--------|-------------|
|
| 115 |
+
| `confidence` | Score from 0.0 to 1.0 โ how sure the model is |
|
| 116 |
+
| `uncertainty` | Quantum-derived uncertainty measure |
|
| 117 |
+
| `should_refuse` | Boolean โ True if confidence < 0.3 (model should decline to answer) |
|
| 118 |
+
| `confidence_label` | Human-readable: "very high", "high", "moderate", "low", "very low" |
|
| 119 |
+
|
| 120 |
+
---
|
| 121 |
+
|
| 122 |
+
## ๐ป Usage
|
| 123 |
+
|
| 124 |
+
### Installation
|
| 125 |
|
| 126 |
```bash
|
| 127 |
pip install pennylane torch transformers
|
| 128 |
```
|
| 129 |
|
|
|
|
|
|
|
| 130 |
### Quick Start
|
| 131 |
|
| 132 |
```python
|
| 133 |
from quantum_head import load_qgpt
|
| 134 |
|
| 135 |
+
# Load model with quantum head
|
| 136 |
+
model, tokenizer = load_qgpt("squ11z1/gpt-oss-9b-reasoning")
|
| 137 |
+
|
| 138 |
+
# Prepare input
|
| 139 |
+
prompt = "What is the capital of France?"
|
| 140 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 141 |
|
| 142 |
# Generate with confidence
|
| 143 |
+
outputs = model.generate_with_confidence(
|
| 144 |
+
inputs.input_ids,
|
| 145 |
+
max_new_tokens=50
|
| 146 |
+
)
|
| 147 |
|
| 148 |
+
# Check results
|
| 149 |
print(f"Response: {tokenizer.decode(outputs['sequences'][0])}")
|
| 150 |
+
print(f"Confidence: {outputs['confidence_label']}") # "high"
|
| 151 |
+
print(f"Should refuse: {outputs['should_refuse']}") # False
|
| 152 |
```
|
| 153 |
|
| 154 |
+
### Using Just the Quantum Head
|
| 155 |
|
| 156 |
```python
|
| 157 |
from quantum_head import QuantumHead
|
| 158 |
import torch
|
| 159 |
|
| 160 |
+
# Create quantum head for your model's hidden size
|
| 161 |
+
head = QuantumHead(hidden_size=2880)
|
| 162 |
|
| 163 |
+
# Get hidden states from your model
|
| 164 |
+
# hidden_states shape: [batch_size, hidden_size]
|
| 165 |
+
hidden_states = torch.randn(1, 2880)
|
| 166 |
|
| 167 |
+
# Get confidence
|
| 168 |
+
output = head(hidden_states)
|
| 169 |
+
print(f"Confidence: {output['confidence'].item():.2%}")
|
| 170 |
```
|
| 171 |
|
| 172 |
+
---
|
| 173 |
|
| 174 |
+
## ๐ Training the Quantum Head
|
|
|
|
|
|
|
| 175 |
|
| 176 |
+
The quantum head can be trained on examples where you know if the model was correct:
|
|
|
|
|
|
|
| 177 |
|
| 178 |
+
```python
|
| 179 |
+
from train import train_quantum_head
|
| 180 |
|
| 181 |
+
train_quantum_head(
|
| 182 |
+
model_name="squ11z1/gpt-oss-9b-reasoning",
|
| 183 |
+
train_data_path="train_data.jsonl", # {text, confidence, is_correct}
|
| 184 |
+
epochs=3,
|
| 185 |
+
)
|
| 186 |
```
|
| 187 |
+
|
| 188 |
+
Training data format (JSONL):
|
| 189 |
+
```json
|
| 190 |
+
{"text": "What is 2+2? The answer is 4.", "confidence": 0.95, "is_correct": true}
|
| 191 |
+
{"text": "The moon is made of cheese.", "confidence": 0.2, "is_correct": false}
|
| 192 |
```
|
| 193 |
|
| 194 |
+
---
|
| 195 |
|
| 196 |
+
## ๐ Files
|
| 197 |
+
|
| 198 |
+
| File | Description |
|
| 199 |
+
|------|-------------|
|
| 200 |
+
| `quantum_head.py` | Main implementation (QuantumHead, QGPT, load_qgpt) |
|
| 201 |
+
| `train.py` | Training script for the quantum head |
|
| 202 |
+
| `__init__.py` | Package initialization |
|
| 203 |
+
|
| 204 |
+
---
|
| 205 |
+
|
| 206 |
+
## ๐ฌ Technical Details
|
| 207 |
+
|
| 208 |
+
| Parameter | Value |
|
| 209 |
+
|-----------|-------|
|
| 210 |
+
| Qubits | 4 |
|
| 211 |
+
| Variational Layers | 3 |
|
| 212 |
+
| Trainable Parameters | ~2,000 (quantum) + ~200,000 (classical) |
|
| 213 |
+
| Framework | PennyLane + PyTorch |
|
| 214 |
+
| Fallback | Classical approximation if PennyLane unavailable |
|
| 215 |
+
|
| 216 |
+
---
|
| 217 |
|
| 218 |
+
## โ ๏ธ Limitations
|
| 219 |
|
| 220 |
+
- **Not perfect** โ Confidence estimation is inherently uncertain
|
| 221 |
+
- **Training data dependent** โ Quality depends on training examples
|
| 222 |
+
- **Simulation** โ Currently runs on quantum simulator, not real hardware
|
| 223 |
+
- **Latency** โ Adds ~10-50ms per inference (quantum circuit execution)
|
| 224 |
|
| 225 |
+
---
|
| 226 |
+
|
| 227 |
+
## ๐ Citation
|
| 228 |
|
| 229 |
```bibtex
|
| 230 |
@misc{qgpt2026,
|
|
|
|
| 235 |
}
|
| 236 |
```
|
| 237 |
|
| 238 |
+
---
|
| 239 |
+
|
| 240 |
+
## ๐ Acknowledgments
|
| 241 |
+
|
| 242 |
+
- [PennyLane](https://pennylane.ai/) โ Quantum ML framework
|
| 243 |
+
- [GPT-OSS](https://huggingface.co/squ11z1/gpt-oss-9b-reasoning) โ Base model
|
| 244 |
+
|
| 245 |
+
---
|
| 246 |
+
|
| 247 |
+
<div align="center">
|
| 248 |
+
|
| 249 |
+
**Built with ๐ฎ Quantum Computing and โค๏ธ**
|
| 250 |
|
| 251 |
+
</div>
|