A hybrid classical-quantum framework for contrastive representation learning on MNIST, using variational quantum circuits as projection heads.
This project implements a Quantum Contrastive Learning framework inspired by SimCLR, combining:
- Classical encoders (ResNet-18, lightweight CNN) for feature extraction
- Quantum projection heads using variational circuits (Tensor Ring, MERA)
- NT-Xent contrastive loss for self-supervised representation learning
- Linear evaluation on frozen encoder features for classification
The framework follows a two-phase pipeline:
- Contrastive pre-training — encoder + quantum head are jointly optimized on augmented image pairs
- Linear evaluation — encoder is frozen, quantum circuit removed, linear classifier trained on frozen features
| Encoder | Output Dim | Parameters | Description |
|---|---|---|---|
| ResNet-18 | 512 | 11.4M | Standard ResNet-18 adapted for 28x28 grayscale input |
| CNN | 2^n_qubits | ~518K (4q) / ~1M (6q) | Lightweight 1D CNN with 4 conv blocks |
Two encoding strategies map classical features to quantum states:
- Amplitude encoding — direct state preparation
- Angle encoding — re-uploading with RY gates
Two circuit ansatze are supported:
| Ansatz | Qubits | Description |
|---|---|---|
| Tensor Ring | 4 (default) | Full-entanglement ring with RY-CNOT-RY blocks, configurable depth |
| MERA | 6 (fixed) | Multi-scale Entanglement Renormalization Ansatz with 9 unitary blocks per layer |
Measurement is performed via Pauli-Z expectation values.
Classical Features -> Quantum Encoding -> [Variational Layers] x D -> Pauli-Z Measurement -> Quantum Features
This project uses UV for Python package management and requires Python 3.12.
git clone https://github.com/mattiacurri/QuantumComputing-Project.git
cd QuantumComputing-Project
uv sync# Train with ResNet-18 encoder + amplitude encoding + Tensor Ring circuit
uv run src/quantum/contrastive_supervised.py \
--encoder simclr \
--quantum-encoding amplitude \
--epochs 200 \
--linear-epochs 200 \
--batch-size 64 \
--n-samples-per-class 500
# Train with CNN encoder + angle encoding + MERA circuit (6 qubits)
uv run src/quantum/contrastive_supervised.py \
--encoder cnn \
--quantum-encoding angle \
--circuit-type mera \
--num-qubits 6 \
--epochs 200 \
--linear-epochs 200 \
--batch-size 64 \
--n-samples-per-class 500
# Run all experiments from the report
bash run_experiments.sh # Linux/macOS
.\run_experiments.bat # Windows PowerShellLinux/macOS:
bash smoke_test_experiments.shWindows (PowerShell):
.\smoke_test_experiments.bat✓ Smoke tests run all 20 configurations with minimal parameters:
- Epochs: 2 (vs 200 full)
- Samples/class: 50 (vs 500 full)
- Batch size: 32 (vs 64 full)
- No WandB logging (local output only)
Linux/macOS:
bash run_experiments.shWindows (PowerShell):
.\run_experiments.batFor standalone runs, use the training script directly:
uv run src/quantum/contrastive_supervised.py --encoder simclr --quantum-encoding amplitude --epochs 200 --linear-epochs 200 --batch-size 64 --n-samples-per-class 500src/
classical/ Classical encoder architectures (ResNet-18, CNN)
quantum/ Quantum SimCLR model and training script
data/ MNIST data loading and augmentation transforms
expressivity/ Circuit expressivity analysis (Haar-random sampling)
utils/ Plotting, WandB logging, configuration
reports/ LaTeX report and circuit figures
run_experiments.bat Reproduces all 20 experiments from the report
run_experiments.sh Reproduces all 20 experiments from the report (Linux/macOS)
smoke_test_experiments.bat Runs quick smoke tests for all configurations (Windows)
smoke_test_experiments.sh Runs quick smoke tests for all configurations (Linux/macOS)
compute_circuit_expressivity.py Script for computing circuit expressivity
See the report for detailed results and analysis.
🚀