CoolFace
Modelpublic

squ11z1/Q-GPT

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
3likes
Model Card

<div align="center">

Q-GPT

qgpt

Quantum-Enhanced Confidence Estimation for Language Models

![PennyLane](https://pennylane.ai/) ![PyTorch](https://pytorch.org/) ![License](https://www.apache.org/licenses/LICENSE-2.0)

Know when your LLM is confident โ€” and when it's guessing.

</div>


๐ŸŽฏ 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.


๐Ÿง  How It Works

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                        Q-GPT Architecture                        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                   โ”‚
โ”‚  LLM Hidden States                Quantum Circuit                 โ”‚
โ”‚  [2880 dimensions]                [4 qubits]                      โ”‚
โ”‚         โ”‚                              โ”‚                          โ”‚
โ”‚         โ–ผ                              โ”‚                          โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                       โ”‚                          โ”‚
โ”‚  โ”‚  Compress   โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ  โ”‚                          โ”‚
โ”‚  โ”‚  to 4 dims  โ”‚                       โ”‚                          โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                       โ–ผ                          โ”‚
โ”‚                               โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                 โ”‚
โ”‚                               โ”‚   RY   RZ       โ”‚                 โ”‚
โ”‚                               โ”‚   โ”‚    โ”‚        โ”‚  Layer 1        โ”‚
โ”‚                               โ”‚   Rot โ”€โ—โ”€ CNOT  โ”‚                 โ”‚
โ”‚                               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค                 โ”‚
โ”‚                               โ”‚   Rot โ”€โ—โ”€ CNOT  โ”‚  Layer 2        โ”‚
โ”‚                               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค                 โ”‚
โ”‚                               โ”‚   Rot โ”€โ—โ”€ CNOT  โ”‚  Layer 3        โ”‚
โ”‚                               โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                 โ”‚
โ”‚                                        โ”‚                          โ”‚
โ”‚                                        โ–ผ                          โ”‚
โ”‚                               โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                 โ”‚
โ”‚                               โ”‚  Measure โŸจZโŸฉ    โ”‚                 โ”‚
โ”‚                               โ”‚  on each qubit  โ”‚                 โ”‚
โ”‚                               โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                 โ”‚
โ”‚                                        โ”‚                          โ”‚
โ”‚                                        โ–ผ                          โ”‚
โ”‚                               โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                 โ”‚
โ”‚                               โ”‚   Confidence    โ”‚                 โ”‚
โ”‚                               โ”‚   0.0 โ€” 1.0     โ”‚                 โ”‚
โ”‚                               โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                 โ”‚
โ”‚                                                                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Step by Step:

  1. 1.Extract Hidden States โ€” When the LLM generates a response, we capture its internal representation (hidden states from the last layer).
  1. 1.Compress โ€” The high-dimensional hidden states (2880 dimensions for GPT-OSS) are compressed to 4 values using a small neural network.
  1. 1.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.
  1. 1.Variational Layers โ€” The qubits pass through multiple layers of:
  2. 2.Rotation gates (trainable parameters that learn patterns)
  3. 3.CNOT gates (create entanglement between qubits)
  1. 1.Measurement โ€” We measure the expectation value โŸจZโŸฉ of each qubit, giving us 4 numbers between -1 and +1.
  1. 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
  • โ€”Inherent probabilistic nature naturally represents uncertainty
  • โ€”Compact representation โ€” 4 qubits can represent 16-dimensional state space

๐Ÿ“Š What You Get

OutputDescription
confidenceScore from 0.0 to 1.0 โ€” how sure the model is
uncertaintyQuantum-derived uncertainty measure
should_refuseBoolean โ€” True if confidence < 0.3 (model should decline to answer)
confidence_labelHuman-readable: "very high", "high", "moderate", "low", "very low"

๐Ÿ’ป Usage

Installation

bash
pip install pennylane torch transformers

Quick Start

python
from quantum_head import load_qgpt

# Load model with quantum head
model, tokenizer = load_qgpt("squ11z1/gpt-oss-9b-reasoning")

# Prepare input
prompt = "What is the capital of France?"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

# Generate with confidence
outputs = model.generate_with_confidence(
    inputs.input_ids,
    max_new_tokens=50
)

# Check results
print(f"Response: {tokenizer.decode(outputs['sequences'][0])}")
print(f"Confidence: {outputs['confidence_label']}")  # "high"
print(f"Should refuse: {outputs['should_refuse']}")  # False

Using Just the Quantum Head

python
from quantum_head import QuantumHead
import torch

# Create quantum head for your model's hidden size
head = QuantumHead(hidden_size=2880)

# Get hidden states from your model
# hidden_states shape: [batch_size, hidden_size]
hidden_states = torch.randn(1, 2880)

# Get confidence
output = head(hidden_states)
print(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:

python
from train import train_quantum_head

train_quantum_head(
    model_name="squ11z1/gpt-oss-9b-reasoning",
    train_data_path="train_data.jsonl",  # {text, confidence, is_correct}
    epochs=3,
)

Training data format (JSONL):

json
{"text": "What is 2+2? The answer is 4.", "confidence": 0.95, "is_correct": true}
{"text": "The moon is made of cheese.", "confidence": 0.2, "is_correct": false}

๐Ÿ“ Files

FileDescription
quantum_head.pyMain implementation (QuantumHead, QGPT, load_qgpt)
train.pyTraining script for the quantum head
__init__.pyPackage initialization

๐Ÿ”ฌ Technical Details

ParameterValue
Qubits4
Variational Layers3
Trainable Parameters~2,000 (quantum) + ~200,000 (classical)
FrameworkPennyLane + PyTorch
FallbackClassical 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
@misc{qgpt2026,
  title={Q-GPT: Quantum-Enhanced Confidence Estimation for Language Models},
  author={squ11z1},
  year={2026},
  url={https://huggingface.co/squ11z1/Q-GPT}
}

๐Ÿ™ Acknowledgments


<div align="center">

Pro Mundi Vita

</div>