In this demonstration you will:
Load and preprocess the Yeh concrete dataset
Implement classical PCA using scikit-learn
Implement QPCA using a Variational Quantum Eigensolver (VQE) in PennyLane
Compare PCA and QPCA in terms of:
runtime
explained variance
reconstruction mean squared error (MSE)
Step 1: Install and import libraries
Create a new Colab notebook and run this cell first.
This cell makes your runs reproducible and loads the dataset from Kaggle.
This prepares the features for PCA and QPCA.
Used by the quantum part and to compare PCA/QPCA.
This creates the quantum circuit and qnodes for QPCA.
This finds the top eigenvector(s) of the covariance matrix.
This ties everything together and prints/plots the comparison.
After running the full experiment, the following quantitative results were obtained:
PCA
PCA time = 0.0220 s
2D variance = 0.7016
Reconstruction MSE = 0.288285
QPCA
QPCA time = 0.4398 s
2D variance = 0.7014
Reconstruction MSE = 0.289066
Bar chart 1: Explained Variance (Train)
The first bar chart shows the total variance captured by the first two components for PCA and QPCA.
PCA explained variance ≈ 0.7016
QPCA explained variance ≈ 0.7014
The bars are almost the same height, which tells us that both methods capture essentially the same amount of variance in the 2-dimensional subspace.
Bar chart 2: Reconstruction Error (Test)
The second bar chart shows reconstruction mean squared error on the test set.
PCA reconstruction MSE ≈ 0.288285
QPCA reconstruction MSE ≈ 0.289066
Again, the difference is extremely small, so QPCA reconstructs the data about as well as PCA when using two components.
Accuracy comparison
PCA and QPCA produce almost identical 2D variance values (0.7016 vs 0.7014).
Their reconstruction errors are also nearly identical (0.288285 vs 0.289066).
This means that, for this dataset and 2 components, QPCA successfully approximates the same principal subspace that classical PCA finds.
Runtime comparison
PCA time ≈ 0.0220 s
QPCA time ≈ 0.4398 s
On current classical hardware, PCA is much faster because it uses highly optimized linear algebra routines, while QPCA must simulate quantum circuits and run an iterative optimizer.
What this shows about QPCA
QPCA is not yet faster than PCA for small, classical problems.
However, it demonstrates that a quantum-inspired method can reproduce PCA’s behavior:
same dominant directions of variance
similar reconstruction quality
As quantum hardware improves and datasets grow in size and dimension, QPCA could become more attractive for very large covariance structures that are hard to handle classically.