Skip to content

RsGoksel/quantum-encoding-nlp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Quantum Encoding Selection for NLP Sentiment Analysis

DOI License: MIT Paper License: CC BY 4.0 Python 3.13

A systematic comparison of four variational quantum circuit (VQC) data-encoding strategies β€” Angle, Dense Angle, IQP, and Data Re-uploading β€” for binary sentiment classification, with multi-seed validation on IMDb / SST-2 and real-hardware verification on IBM Quantum (ibm_fez, 156 qubits).

πŸ“„ Preprint: GΓΌndΓΌz, K. G. (2026). Evaluating Quantum Data Encoding Strategies and Gradient Trainability for NLP Sentiment Analysis. ResearchGate. https://doi.org/10.13140/RG.2.2.32211.13607

Local draft: docs/paper_draft.md Β· LaTeX source: docs/paper.tex


Highlights

  • Dataset-agnostic encoding ranking. Angle > Dense > IQP > Re-uploading β€” preserved across IMDb (5 seeds) and SST-2.
  • Gradient variance is informative but not deterministic. High variance helps optimization but does not guarantee accuracy (Re-uploading: variance 9.10 β†’ accuracy 60%); low variance does not preclude strong performance (Dense Angle: 0.86 β†’ 67.6%).
  • Sigmoid scaling is a +17 pp design lever. Aligning PCA outputs (~N(0,1)) to the encoder domain [0, Ο€] is a first-order concern, not a hyperparameter footnote.
  • Hardware validation with T1 bias control. A class-balanced 10-sample test on ibm_fez reveals 100% simulator–QPU prediction agreement with symmetric per-class fidelity β€” no T1 amplitude-damping confound at the scales tested.
  • 3,400Γ— faster training via PyTorch-native statevector simulation with autograd, vs. Qiskit's parameter-shift rule (14 ms vs. 1,501 ms per backward pass).

Pipeline

Text  ──DistilBERT (frozen)──► R^768 ──PCA──► R^8 ──σ(x)·π──► [0,Ο€]^8
                                                                  β”‚
                                            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                            β–Ό
                              Parameterized Quantum Circuit (8 qubits, reps=1)
                                            β”‚
                                            β–Ό
                              ⟨Z⟩ per qubit  ──Linear──►  logits ∈ R^2

Headline Results

Encoding comparison β€” IMDb (5 seeds) + SST-2 (seed 42)

Encoding Qubits Params IMDb (mean Β± std) SST-2 Grad Var
Angle 8 50 70.1 Β± 1.1 % 76.4 % 9.757
Dense Angle 4 26 67.6 Β± 0.5 % 73.0 % 0.855
IQP 8 50 62.7 Β± 0.6 % 66.5 % 0.899
Re-uploading* 8 50 60.2 Β± 0.9 % 61.1 % 9.098
Linear baseline β€” 18 72.2 % β€” β€”

* Constrained variant (no entangling layer after re-upload β€” see docs/paper_draft.md Β§5.2).

IBM Quantum hardware β€” ibm_fez (Eagle r3, 156 qubits), balanced 5+5 test set

Encoding Sim Acc QPU Acc Sim–QPU Agreement L0 Agree L1 Agree Transpiled Depth
Angle 80 % 80 % 10/10 (100 %) 5/5 5/5 19
Dense 80 % 80 % 10/10 (100 %) 5/5 5/5 18
IQP 80 % 80 % 10/10 (100 %) 5/5 5/5 101
Reupload 80 % 80 % 10/10 (100 %) 5/5 5/5 21

Symmetric per-class agreement rules out T1 amplitude-damping bias.

Repository Layout

quantum-attention/
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ preprocess.py             # DistilBERT embedding + PCA fitting
β”‚   β”œβ”€β”€ imdb_embeddings.pt        # cached 8-dim PCA features (IMDb)
β”‚   └── sst2_embeddings.pt        # cached 8-dim PCA features (SST-2)
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ classical_baseline.py     # Linear(8,2)
β”‚   β”œβ”€β”€ classical_attention.py    # Q/K/V baseline
β”‚   └── quantum_encodings.py      # Angle / Dense / IQP / Re-uploading + ansatz
β”œβ”€β”€ training/
β”‚   β”œβ”€β”€ config.py                 # TrainingConfig dataclass
β”‚   └── train.py                  # unified training loop (all model types)
β”œβ”€β”€ experiments/
β”‚   β”œβ”€β”€ run_multiseed.py          # batch runner (skip-if-exists)
β”‚   β”œβ”€β”€ barren_plateau.py         # gradient-variance analysis (100 random inits)
β”‚   β”œβ”€β”€ ibm_encoding_inference.py # QPU inference: 4 encodings Γ— 10 samples
β”‚   β”œβ”€β”€ ibm_balanced_qpu.py       # T1-controlled balanced (5+5) QPU run
β”‚   └── paper_analysis.py         # reproduces all paper figures
β”œβ”€β”€ results/
β”‚   β”œβ”€β”€ logs/                     # per-experiment JSON history files
β”‚   └── plots/                    # paper figures (fig1 … fig5)
β”œβ”€β”€ checkpoints/                  # trained .pt files (one per (model, dataset, seed))
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ paper_draft.md            # full paper draft
β”‚   β”œβ”€β”€ paper.tex                 # LaTeX source
β”‚   └── plans/                    # design + implementation plans
β”œβ”€β”€ CLAUDE.md                     # project context for LLM sessions
β”œβ”€β”€ system_map.md                 # detailed system map
β”œβ”€β”€ developments.md               # chronological dev log
└── ibm_quantum_almanac.md        # IBM Quantum API notes & gotchas

Setup

git clone https://github.com/RsGoksel/quantum-encoding-nlp.git
cd quantum-encoding-nlp
python -m venv .venv && source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install -r requirements.txt

Tested on Python 3.13, PyTorch 2.6 (CUDA 12.4), Qiskit 2.3, qiskit-ibm-runtime 0.45.

Optional: IBM Quantum access

Only needed to reproduce hardware experiments β€” simulator-only training works without it.

from qiskit_ibm_runtime import QiskitRuntimeService
QiskitRuntimeService.save_account(
    channel="ibm_quantum_platform",   # NOT 'ibm_quantum' β€” Qiskit β‰₯ 2.x
    token="YOUR_IBM_QUANTUM_TOKEN",   # https://quantum.ibm.com/account
    overwrite=True,
)

Reproducing Paper Results

The cached embeddings (data/*.pt) are committed, so step (1) is optional.

# (1) optional β€” re-embed text with DistilBERT and refit PCA
python data/preprocess.py

# (2) train all (encoding Γ— seed) combinations, skip-if-exists
python experiments/run_multiseed.py --dataset imdb --seeds 42 123 456 789 2024
python experiments/run_multiseed.py --dataset sst2 --seeds 42

# (3) gradient-variance analysis (qubit + depth scaling)
python experiments/barren_plateau.py

# (4) IBM Quantum hardware inference (requires IBM account)
python experiments/ibm_balanced_qpu.py        # primary balanced 5+5 run
python experiments/ibm_encoding_inference.py  # initial 4Γ—10 (all label 0)

# (5) regenerate every paper figure + summary table
python experiments/paper_analysis.py

Windows users: prefix Python invocations with PYTHONIOENCODING=utf-8 to avoid Unicode console errors.

Train one encoding manually

python training/train.py \
  --model enc_angle \
  --dataset imdb \
  --reps 1 \
  --lr_quantum 0.05 \
  --subset \
  --seed 42 \
  --suffix _seed42

--model accepts: baseline, attention, quantum, enc_{angle,dense,iqp,reupload}, with optional _rand suffix for random initialization.

Implementation Notes

  • Quantum simulation. PyTorch-native statevector (256 complex amplitudes for 8 qubits) on CPU, with autograd. ~3,400Γ— faster than the parameter-shift rule for our circuits.
  • Variational ansatz (shared by all encodings): Ry Β· Rz β†’ CZ (linear) β†’ Ry Β· Rz, reps=1, identity initialization (ΞΈ = 0) per [Mele et al., 2024].
  • Critical bug we fixed. Initial Qiskit circuits used EfficientSU2 with the default CX entanglement, while training used CZ. We replaced them with hand-built CZ circuits and cross-validated against PyTorch to 6 decimal places (see docs/paper_draft.md Appendix A).
  • Sigmoid scaling. PCA features ~ N(0,1) are mapped via Οƒ(x)Β·Ο€ ∈ [0,Ο€] to cover the full Bloch arc. Without it, accuracy collapses to ~52 % (majority class).

Limitations

We make no claim of quantum advantage. The 8-qubit circuits are classically simulable, the linear baseline outperforms all VQCs, and PCA retains only ~36 % of the DistilBERT variance. The contribution is methodological: a controlled comparison + gradient-trainability diagnostic + hardware fidelity protocol for selecting an encoding within the quantum design space. See Β§5.4 of the paper for the full limitations list.

Citation

@misc{gunduz2026quantumencoding,
  author    = {G\"und\"uz, Kadir G\"oksel},
  title     = {Evaluating Quantum Data Encoding Strategies and Gradient Trainability
               for {NLP} Sentiment Analysis},
  year      = {2026},
  month     = apr,
  publisher = {ResearchGate},
  note      = {Preprint},
  doi       = {10.13140/RG.2.2.32211.13607},
  url       = {https://doi.org/10.13140/RG.2.2.32211.13607},
}

License

Released under the MIT License.

Acknowledgements

IBM Quantum Open Plan (10 min/month) for free QPU access; HuggingFace for DistilBERT and the IMDb / SST-2 dataset cards; the Qiskit and PyTorch communities.

About

Systematic comparison of four VQC data-encoding strategies (Angle, Dense Angle, IQP, Data Re-uploading) for NLP sentiment analysis, with multi-seed validation on IMDb/SST-2 and IBM Quantum hardware verification.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages