squ11z1 commited on
Commit
ec8a0b6
ยท
verified ยท
1 Parent(s): 662c9ff

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +200 -58
README.md CHANGED
@@ -1,99 +1,230 @@
1
- # Q-GPT: Quantum-Enhanced GPT
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- A quantum neural network head that adds confidence estimation to GPT models.
4
 
5
- ## Features
6
 
7
- - ๐Ÿ”ฎ **Variational Quantum Circuit** - Uses PennyLane for true quantum computing simulation
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
- ## Installation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 Q-GPT
26
- model, tokenizer = load_qgpt(
27
- "squ11z1/gpt-oss-9b-reasoning",
28
- torch_dtype="auto",
29
- device="auto",
30
- )
31
 
32
  # Generate with confidence
33
- inputs = tokenizer("What is 2 + 2?", return_tensors="pt").to(model.device)
34
- outputs = model.generate_with_confidence(inputs.input_ids, max_new_tokens=50)
 
 
35
 
 
36
  print(f"Response: {tokenizer.decode(outputs['sequences'][0])}")
37
- print(f"Confidence: {outputs['confidence_label']}") # e.g., "high"
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) # Match your model's hidden size
49
 
50
- # Forward pass with hidden states
51
- hidden_states = torch.randn(1, 2880) # From your model
52
- output = head(hidden_states)
53
 
54
- print(f"Confidence: {output['confidence'].item():.2f}")
55
- print(f"Uncertainty: {output['uncertainty'].item():.2f}")
 
56
  ```
57
 
58
- ### Training
59
 
60
- ```bash
61
- # Create synthetic training data
62
- python train.py --model squ11z1/gpt-oss-9b-reasoning --create-data --data train.jsonl
63
 
64
- # Train quantum head
65
- python train.py --model squ11z1/gpt-oss-9b-reasoning --data train.jsonl --epochs 3
66
- ```
67
 
68
- ## Architecture
 
69
 
 
 
 
 
 
70
  ```
71
- Hidden States โ†’ [Classical Compression] โ†’ [Quantum Circuit] โ†’ [Post-Processing] โ†’ Confidence
72
- โ†“ โ†“ โ†“ โ†“
73
- [B, H] [B, n_qubits] [B, n_qubits] [B, 2]
74
- โ†“
75
- confidence + uncertainty
76
  ```
77
 
78
- ### Quantum Circuit
79
 
80
- ```
81
- |0โŸฉ โ”€ RY(xโ‚€) โ”€ RZ(xโ‚€) โ”€ Rot(ฮธ) โ”€ โ—โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Rot(ฮธ) โ”€ ... โ”€ โŸจZโŸฉ
82
- โ”‚
83
- |0โŸฉ โ”€ RY(xโ‚) โ”€ RZ(xโ‚) โ”€ Rot(ฮธ) โ”€ โŠ• โ”€ โ—โ”€โ”€โ”€ Rot(ฮธ) โ”€ ... โ”€ โŸจZโŸฉ
84
- โ”‚
85
- |0โŸฉ โ”€ RY(xโ‚‚) โ”€ RZ(xโ‚‚) โ”€ Rot(ฮธ) โ”€โ”€โ”€โ”€โ”€ โŠ• โ”€ โ—โ”€ Rot(ฮธ) โ”€ ... โ”€ โŸจZโŸฉ
86
- โ”‚
87
- |0โŸฉ โ”€ RY(xโ‚ƒ) โ”€ RZ(xโ‚ƒ) โ”€ Rot(ฮธ) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โŠ• โ”€ Rot(ฮธ) โ”€ ... โ”€ โŸจZโŸฉ
88
- ```
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
- ## Files
91
 
92
- - `quantum_head.py` - Main implementation (QuantumHead, QGPT, load_qgpt)
93
- - `train.py` - Training script for quantum head
94
- - `quantum_head.pt` - Pre-trained weights (after training)
 
95
 
96
- ## Citation
 
 
97
 
98
  ```bibtex
99
  @misc{qgpt2026,
@@ -104,6 +235,17 @@ Hidden States โ†’ [Classical Compression] โ†’ [Quantum Circuit] โ†’ [Post-Proces
104
  }
105
  ```
106
 
107
- ## License
 
 
 
 
 
 
 
 
 
 
 
108
 
109
- Apache 2.0
 
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
+ [![PennyLane](https://img.shields.io/badge/PennyLane-Quantum_ML-6C3483?style=for-the-badge)](https://pennylane.ai/)
23
+ [![PyTorch](https://img.shields.io/badge/PyTorch-Compatible-EE4C2C?style=for-the-badge&logo=pytorch)](https://pytorch.org/)
24
+ [![License](https://img.shields.io/badge/License-Apache_2.0-green?style=for-the-badge)](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>