Know when your LLM is confident — and when it's guessing.
🎯 What is Q-GPT?
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.
The Problem
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.
The Solution
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.
Extract Hidden States — When the LLM generates a response, we capture its internal representation (hidden states from the last layer).
Compress — The high-dimensional hidden states (2880 dimensions for GPT-OSS) are compressed to 4 values using a small neural network.
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.
Variational Layers — The qubits pass through multiple layers of:
Rotation gates (trainable parameters that learn patterns)
CNOT gates (create entanglement between qubits)
Measurement — We measure the expectation value ⟨Z⟩ of each qubit, giving us 4 numbers between -1 and +1.
Confidence Output — A final layer converts these measurements into a confidence score (0-1) and an uncertainty estimate.
Why Quantum?
Entanglement captures complex correlations in the data that classical networks struggle with
Superposition allows exploring multiple states simultaneously
1from quantum_head import load_qgpt
23# Load model with quantum head4model, tokenizer = load_qgpt("squ11z1/gpt-oss-9b-reasoning")56# Prepare input7prompt ="What is the capital of France?"8inputs = tokenizer(prompt, return_tensors="pt").to(model.device)910# Generate with confidence11outputs = model.generate_with_confidence(12 inputs.input_ids,13 max_new_tokens=5014)1516# Check results17print(f"Response: {tokenizer.decode(outputs['sequences'][0])}")18print(f"Confidence: {outputs['confidence_label']}")# "high"19print(f"Should refuse: {outputs['should_refuse']}")# False
Using Just the Quantum Head
python
1from quantum_head import QuantumHead
2import torch
34# Create quantum head for your model's hidden size5head = QuantumHead(hidden_size=2880)67# Get hidden states from your model8# hidden_states shape: [batch_size, hidden_size]9hidden_states = torch.randn(1,2880)1011# Get confidence12output = head(hidden_states)13print(f"Confidence: {output['confidence'].item():.2%}")
🎓 Training the Quantum Head
The quantum head can be trained on examples where you know if the model was correct:
1{"text":"What is 2+2? The answer is 4.","confidence":0.95,"is_correct":true}2{"text":"The moon is made of cheese.","confidence":0.2,"is_correct":false}
📁 Files
File
Description
quantum_head.py
Main implementation (QuantumHead, QGPT, load_qgpt)
train.py
Training script for the quantum head
__init__.py
Package initialization
🔬 Technical Details
Parameter
Value
Qubits
4
Variational Layers
3
Trainable Parameters
~2,000 (quantum) + ~200,000 (classical)
Framework
PennyLane + PyTorch
Fallback
Classical approximation if PennyLane unavailable
⚠️ Limitations
Not perfect — Confidence estimation is inherently uncertain
Training data dependent — Quality depends on training examples
Simulation — Currently runs on quantum simulator, not real hardware
Latency — Adds ~10-50ms per inference (quantum circuit execution)
📖 Citation
bibtex
1@misc{qgpt2026,
2 title={Q-GPT: Quantum-Enhanced Confidence Estimation for Language Models},
3 author={squ11z1},
4 year={2026},
5 url={https://huggingface.co/squ11z1/Q-GPT}
6}