Skip to content

About

Tucker/CP compression of a CIFAR-10 CNN and MPS simulation of quantum circuits with Qiskit

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

Tensor-Networks

Tensor decompositions applied to two problems: compressing a CNN trained on CIFAR-10, and simulating quantum circuits with Matrix Product States (MPS) through Qiskit.

Made for the Quantum Computing course at Universidad Pontificia Comillas ICAI (November 2025).

What it does

  • Compresses a three-layer CNN by replacing its convolutions with Tucker-factorised convolutions and its fully connected layers with low-rank factorised ones, then fine-tunes the result.
  • Builds quantum states (GHZ, W, random circuits) as MPS with a small tensor-network library written from scratch (Tensor, SVD truncation, contractions, entanglement entropy).
  • Runs the same circuits on Qiskit Aer with the statevector and matrix_product_state methods and times both.

How it works

CNN compression (src/cnn/cnn_compression.py):

  • SimpleCNN has three 3x3 convolutions (64, 128, 256 channels), one 512-unit fully connected layer and a 10-class output. It has 2,473,610 parameters.
  • TuckerConv2d.from_conv2d runs a Tucker decomposition (TensorLy) of each convolution weight, keeping half of the input and output channels (rank_ratio=0.5) and the full 3x3 kernel. The layer becomes a 1x1 convolution, a 3x3 convolution on the reduced channels and a 1x1 convolution back.
  • CPFullyConnected.from_linear runs a CP decomposition (parafac) of the weight matrix and stores it as two linear layers joined by a rank-r bottleneck (64 for the first layer, 32 for the second).
  • The demo trains the network for 3 epochs with Adam (learning rate 0.001, batch size 128), decomposes it, and fine-tunes for 1 epoch.

MPS simulation (src/tensor_core/, src/quantum/):

  • MPS.from_state_vector splits a state vector into site tensors with successive SVDs and an optional bond dimension cap (chi_max).
  • create_ghz_mps and create_w_mps build those states directly with small bond dimension.
  • qiskit_simulation.py builds the GHZ, W and random circuits in Qiskit and benchmarks Aer's two simulation methods on them.
  • gates.py has a small dense circuit simulator with the usual gates, used for Bell states and concurrence.

Results

CNN, from the metadata stored in models/baseline.pth and models/compressed.pth (CIFAR-10 test set, trained on the author's laptop GPU, an NVIDIA GTX 1650 Max-Q):

Model Parameters Test accuracy Inference time (batch of 128)
Baseline 2,473,610 71.91% 8.11 ms
Compressed 458,285 63.34% 7.41 ms

That is a 5.4x reduction in parameters, 8.57 points of accuracy lost, and inference about 1.09x faster. Both models were trained for only 3 epochs, so the accuracies are low in absolute terms.

The quantum benchmarks print their timings when you run them. No timing numbers are stored in the repository, so none are quoted here.

How to run

git clone https://github.com/Drakit0/Tensor-Networks
cd Tensor-Networks
pip install -r requirements.txt
python demos/demo.py

demo.py asks for a choice: 1 for the CNN compression, 2 for the quantum simulation, 3 for both. CIFAR-10 is downloaded to data/ on the first CNN run. A CUDA GPU is used if present, otherwise the CPU.

The visualisation scripts write figures to generated_images/:

python demos/visual_cnn_demo.py
python demos/visual_quantum_demo.py

Example of the MPS code on its own:

from src.tensor_core.mps import create_ghz_mps

mps = create_ghz_mps(20)
print(mps.bond_dims)

Structure

src/cnn/cnn_compression.py        Tucker and CP layers, training, benchmark
src/tensor_core/mps.py            MPS class, GHZ and W states
src/tensor_core/tensor_ops.py     Tensor class, contraction, SVD truncation
src/quantum/gates.py              gates and a dense circuit simulator
src/quantum/qiskit_simulation.py  Qiskit circuits and benchmarks
demos/                            interactive demo and plotting scripts
models/                           trained baseline and compressed weights
original_paper/                   reference paper (PDF)

Author

Pablo Tuñón Laguna, Universidad Pontificia Comillas ICAI.

License

MIT, see LICENSE.

About

Tucker/CP compression of a CIFAR-10 CNN and MPS simulation of quantum circuits with Qiskit

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages