Skip to content

NandithKumar/Quantum-Classical-NLP-Classifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

βš›οΈ Hybrid Quantum-Classical NLP Classifier

A production-grade hybrid quantum-classical machine learning system for text classification. Combines classical NLP preprocessing with a Variational Quantum Circuit (VQC) layer using PennyLane and PyTorch β€” trained end-to-end via gradient descent through quantum backpropagation.

Python PennyLane PyTorch Quantum Streamlit


🎯 Real Training Outputs

Training History

Training History

Quantum vs Classical Baseline Comparison

Quantum vs Classical

Confusion Matrix

Confusion Matrix

Variational Quantum Circuit Diagram

Circuit Diagram

PCA Feature Importance for Quantum Encoding

Feature Importance

Model Architecture

Architecture


πŸ“Œ What is this?

This project implements a Hybrid Quantum-Classical Neural Network for NLP β€” one of the most active research areas in quantum machine learning. The key idea:

  • Classical computers handle text preprocessing (TF-IDF, PCA) and output classification
  • Quantum circuit (VQC) performs non-linear feature transformation in high-dimensional Hilbert space β€” something exponentially expensive to simulate classically
  • The entire pipeline is trained end-to-end using quantum backpropagation (parameter-shift rule)

πŸ—οΈ Architecture

Raw Text Input
      β”‚
      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  CLASSICAL PRE-PROCESSING                                    β”‚
β”‚  TF-IDF Vectorizer (bigrams) β†’ PCA β†’ MinMaxScaler [0, Ο€]   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚  n_qubits features
                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  CLASSICAL PRE-NET                                           β”‚
│  Linear(n→n) → BatchNorm → Tanh                             │
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚  angles ∈ [0, Ο€]
                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  QUANTUM VQC LAYER  βš›οΈ                                      β”‚
β”‚                                                             β”‚
β”‚  |q0⟩ ──RY(x0)──RZ(ΞΈ)──RY(Ο†)──●─────────────── ⟨Z0⟩      β”‚
β”‚  |q1⟩ ──RY(x1)──RZ(ΞΈ)──RY(Ο†)β”€β”€βŠ•β”€β”€β—β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ ⟨Z1⟩      β”‚
β”‚  |q2⟩ ──RY(x2)──RZ(ΞΈ)──RY(Ο†)β”€β”€β”€β”€β”€βŠ•β”€β”€β—β”€β”€β”€β”€β”€β”€β”€β”€β”€ ⟨Z2⟩      β”‚
β”‚  |q3⟩ ──RY(x3)──RZ(ΞΈ)──RY(Ο†)β”€β”€β”€β”€β”€β”€β”€β”€βŠ•β”€β”€β”€β”€β”€β”€β”€β”€β”€ ⟨Z3⟩      β”‚
β”‚                                                             β”‚
β”‚  Repeated n_layers times with trainable ΞΈ, Ο† parameters    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚  n_qubits expectation values
                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  CLASSICAL POST-NET                                          β”‚
│  Linear(n→32) → BatchNorm → ReLU → Dropout                  │
β”‚  Linear(32β†’n_classes) β†’ Softmax                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚
                         β–Ό
                  Class Prediction

βš›οΈ How the Quantum Circuit Works

1. Angle Encoding

Each classical feature $x_i$ is encoded as a rotation angle on qubit $i$:

RY(x_i)|0⟩ = cos(x_i/2)|0⟩ + sin(x_i/2)|1⟩

This maps the classical input into quantum state (Hilbert space).

2. Variational Layers

Each layer applies parameterized rotations:

RZ(ΞΈ_i) RY(Ο†_i) on qubit i    ← trainable parameters

Followed by CNOT entanglement gates creating quantum correlations between qubits.

3. Entanglement (CNOT Ring)

CNOT(0β†’1), CNOT(1β†’2), CNOT(2β†’3), CNOT(3β†’0)

Creates correlations between all qubits β€” capturing relationships between input features.

4. Measurement

⟨Z_i⟩ = ⟨ψ|Z_i|ψ⟩  ∈ [-1, +1]

Pauli-Z expectation value collapses quantum state back to classical values.

5. Training (Backpropagation through Quantum Circuit)

Parameters are optimized using quantum backpropagation via PennyLane's diff_method="backprop", computing exact gradients through the quantum circuit.


✨ Features

Feature Details
βš›οΈ Quantum Layer PennyLane VQC with angle encoding + entanglement
πŸ”€ Hybrid Design Classical pre/post nets + quantum middle layer
πŸ“ NLP Pipeline TF-IDF (bigrams) + PCA dimensionality reduction
πŸŽ“ End-to-End Training Quantum backpropagation via parameter-shift rule
πŸ“Š Comparison Quantum vs Classical baseline training
🌐 Streamlit App Interactive demo with circuit visualization
πŸ§ͺ Unit Tests Full test suite for all modules
πŸ“ˆ Visualizations 6 real output plots from actual training run

πŸ“ Project Structure

Quantum-Classical-NLP/
β”œβ”€β”€ app.py                          # Streamlit interactive demo
β”œβ”€β”€ train.py                        # Main training script
β”œβ”€β”€ predict.py                      # Inference on new text
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ quantum_circuit.py          # VQC definition (PennyLane QNodes)
β”‚   β”œβ”€β”€ classical_preprocessor.py  # TF-IDF + PCA + dataset utilities
β”‚   β”œβ”€β”€ hybrid_model.py             # HybridQuantumClassifier + ClassicalBaseline
β”‚   β”œβ”€β”€ trainer.py                  # Training loop + evaluation + early stopping
β”‚   └── visualizer.py              # All matplotlib plots (dark theme)
β”œβ”€β”€ tests/
β”‚   └── test_quantum_nlp.py        # Unit tests (preprocessing + model + training)
β”œβ”€β”€ outputs/
β”‚   β”œβ”€β”€ training_history.png        # Real training curves
β”‚   β”œβ”€β”€ quantum_vs_classical.png   # Model comparison plot
β”‚   β”œβ”€β”€ confusion_matrix.png        # Test set confusion matrix
β”‚   β”œβ”€β”€ circuit_diagram.png         # VQC architecture diagram
β”‚   β”œβ”€β”€ feature_importance.png      # PCA variance explained
β”‚   └── model_architecture.png     # Full hybrid architecture
β”œβ”€β”€ notebooks/                      # Jupyter exploration notebooks
β”œβ”€β”€ data/                           # Custom datasets (CSV format)
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ .env.example
β”œβ”€β”€ .gitignore
└── README.md

βš™οΈ Setup & Installation

1. Clone the repo

git clone https://github.com/NandithKumar/Quantum-Classical-NLP-Classifier.git
cd Quantum-Classical-NLP-Classifier

2. Create virtual environment

python -m venv venv
source venv/bin/activate     # Linux/Mac
venv\Scripts\activate        # Windows

3. Install dependencies

pip install -r requirements.txt

No API keys required β€” runs fully locally using quantum simulation!


πŸš€ Usage

Train the model

python train.py

Run interactive Streamlit demo

streamlit run app.py

Predict on custom text

python predict.py --text "This product is absolutely amazing!"

Run unit tests

python -m pytest tests/ -v

πŸ”¬ Quantum Advantage

Aspect Classical Model Quantum-Classical Model
Feature transformation Linear / polynomial Hilbert space (exponential)
Entanglement Not possible CNOT gates create correlations
Parameter efficiency More params needed Fewer quantum params, same expressivity
Training Standard backprop Quantum backpropagation
Hardware CPU / GPU Quantum simulator (β†’ real QPU)

πŸ› οΈ Tech Stack

Component Technology
Quantum Computing PennyLane 0.36+
Deep Learning PyTorch 2.0+
NLP Features Scikit-learn (TF-IDF, PCA)
Visualization Matplotlib (dark theme)
Interactive Demo Streamlit
Language Python 3.8+

🌐 Real-World Applications

  • Sentiment Analysis β€” product reviews, social media, customer feedback
  • Document Classification β€” spam detection, topic labeling
  • Intent Detection β€” chatbots, virtual assistants
  • Fraud Text Detection β€” financial NLP

πŸ‘€ Author

Paladugu Nandith Kumar


πŸ“š References

  • Schuld, M., & Petruccione, F. (2021). Machine Learning with Quantum Computers. Springer.
  • Bergholm, V., et al. (2022). PennyLane: Automatic differentiation of hybrid quantum-classical computations. arXiv:1811.04968
  • Cerezo, M., et al. (2021). Variational quantum algorithms. Nature Reviews Physics, 3(9), 625-644.

πŸ“„ License

MIT License

About

Hybrid Quantum-Classical NLP text classifier using PennyLane VQC + PyTorch with real training outputs

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages