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.
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)
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
Each classical feature
RY(x_i)|0β© = cos(x_i/2)|0β© + sin(x_i/2)|1β©
This maps the classical input into quantum state (Hilbert space).
Each layer applies parameterized rotations:
RZ(ΞΈ_i) RY(Ο_i) on qubit i β trainable parameters
Followed by CNOT entanglement gates creating quantum correlations between qubits.
CNOT(0β1), CNOT(1β2), CNOT(2β3), CNOT(3β0)
Creates correlations between all qubits β capturing relationships between input features.
β¨Z_iβ© = β¨Ο|Z_i|Οβ© β [-1, +1]
Pauli-Z expectation value collapses quantum state back to classical values.
Parameters are optimized using quantum backpropagation via PennyLane's diff_method="backprop", computing exact gradients through the quantum circuit.
| 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 |
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
git clone https://github.com/NandithKumar/Quantum-Classical-NLP-Classifier.git
cd Quantum-Classical-NLP-Classifierpython -m venv venv
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windowspip install -r requirements.txtNo API keys required β runs fully locally using quantum simulation!
python train.pystreamlit run app.pypython predict.py --text "This product is absolutely amazing!"python -m pytest tests/ -v| 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) |
| 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+ |
- Sentiment Analysis β product reviews, social media, customer feedback
- Document Classification β spam detection, topic labeling
- Intent Detection β chatbots, virtual assistants
- Fraud Text Detection β financial NLP
Paladugu Nandith Kumar
- π B.Tech CSE (AI & ML) β RGMCET, Kadapa
- πΌ LinkedIn
- π Portfolio
- π§ nandith1411@gmail.com
- 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.
MIT License





