Skip to content

PauMayench/compositional-qsvm

Repository files navigation

Compositional Optimization of Quantum Circuits for Quantum Kernel SVMs

Authors

This project was developed by:

Overview

This project is based on the paper "Compositional Optimization of Quantum Circuits for Quantum Kernels of Support Vector Machines" by Elham Torabian and Roman V. Krems, which we partially implemented and extended.

Torabian, E., & Krems, R. V. (2022). Compositional optimization of quantum circuits for quantum kernels of support vector machines. arXiv:2203.13848. https://arxiv.org/abs/2203.13848

The paper introduces an algorithm to construct and evaluate quantum circuits layer by layer for quantum kernel methods in SVMs. At each step, all possible gate combinations for that layer are generated defining all the possible circuits. For each of them an SVM is trained and evaluated, most often using the Bayesian Information Criterion (BIC). The top K circuits are selected, and gate parameters are then optimized for the top M of those. K and M are tunable parameters. The goal is to construct the best quantum circuit that yields the best kernel for a specific dataset, even with limited training data.

Extensions implemented

  • Tested on two new datasets beyond the original paper: Hidden manifold and Primes
  • Evaluated alternative model selection metrics: Akaike Information Criterion (AIC) and Cross-Validation (CV)

Setup

Create and activate a Python virtual environment:

python3 -m venv venv
source venv/bin/activate

Install dependencies:

pip install -r requirements.txt
pip install torch --index-url https://download.pytorch.org/whl/cpu

Execution

Edit main.py to set the parameters you want, then run:

python3 main.py

Key parameters in main.py:

Parameter Description
dataset_name Dataset to use: "ad-hoc", "hidden_manifold", "perovskites", "primes"
model_name Circuit selection metric: "BIC", "AIC", "CV", "ATR"
L_max Maximum number of circuit layers
K Number of top candidate circuits kept per layer
M Number of circuits for which gate parameters are Bayesian-optimized per layer (set to 0 to skip)
N Number of Bayesian optimization steps for theta optimization
n_cores Number of CPU cores for parallelism

Workflow

  1. Run main.py — performs the compositional circuit search. For each layer, a .pkl file with the ranked candidate circuits is saved under results/<run_folder>/.
  2. Run theta_opt.py — takes the best circuit from each layer and runs Bayesian optimization to find optimal gate parameters. Saves optimized_layers.pkl in the same folder.
  3. Run accuracies_opt.py — evaluates the optimized circuits on the test set and saves accuracies_min.pkl and accuracies_avg.pkl.
  4. Run generate_plots.py — reads the accuracy files and produces all comparison plots in plots/.
main.py
  └── results/<run_folder>/
        ├── candidates_layer1.pkl
        ├── candidates_layer2.pkl
        ...
        └── candidates_layerL.pkl

theta_opt.py  (point it at the same folder)
  └── results/<run_folder>/optimized_layers.pkl

accuracies_opt.py  (point it at the same folder)
  └── results/<run_folder>/accuracies_min.pkl
      results/<run_folder>/accuracies_avg.pkl

generate_plots.py
  └── plots/*.png

Scripts

Script Description
main.py Entry point. Configure parameters here and launch the circuit search.
search.py Core compositional search (M=0): no parameter optimization during search, only structural gate selection.
search_M.py Search with parameter optimization (M>0): for each layer, the top M circuits also have their gate parameters optimized by Bayesian optimization before the next layer is selected.
theta_opt.py After the search, runs Bayesian optimization on the best circuit from each layer to find optimal parameters. Saves optimized_layers.pkl.
thetaopt_forM.py Extracts the already-optimized circuits from an M>0 search run and packages them into the same format as theta_opt.py output.
accuracies_opt.py Trains an SVM with the optimized quantum kernel for each layer and computes test-set balanced accuracies.
generate_plots.py Generates all result plots: accuracy vs layers, accuracy vs time, metric comparison, dataset comparison, and quantum vs classical SVM benchmarks.
generate_histo_plots.py Plots histograms of circuit candidate scores for each layer of a given run.
circuit_class.py Defines the quantum circuit structure and how it is built from gate sequences.
pool.py Parallel execution pool used to distribute circuit evaluations across CPU cores.
utils.py Utility functions: circuit evaluation, BIC/AIC/CV scoring, Bayesian optimization for thetas, save/load helpers.
data_preprocessing.py Loads and preprocesses each dataset into train/validation/test splits.
datasets/download_dataset.py Helper to download or generate datasets.

About

Partial replication and extension of a paper on compositional optimization of quantum kernels for QSVMs.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages