Skip to content

Instantly share code, notes, and snippets.

@allen-munsch
Created February 25, 2025 22:35
Show Gist options
  • Save allen-munsch/a5ee3d29a038288bff7ce468a0dd97b8 to your computer and use it in GitHub Desktop.
Save allen-munsch/a5ee3d29a038288bff7ce468a0dd97b8 to your computer and use it in GitHub Desktop.
quantum support vector machine

Step 1: Install QuASK

First, ensure you have QuASK installed. You can install it via pip:

pip install quask

Step 2: Prepare Your Dataset

You need to preprocess your dataset. QuASK typically works with NumPy arrays or similar data structures.

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# Example dataset (replace with your actual data)
# X should be a 2D array with shape (n_samples, 19)
# y should be a 1D array with shape (n_samples,)
X = np.random.rand(100, 19)  # 100 samples, 19 features
y = np.random.randint(0, 2, 100)  # Binary classification labels (0 or 1)

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Standardize the features (important for SVM)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

Step 3: Set Up QuASK for QSVM

QuASK provides tools for quantum machine learning, including QSVM. You’ll need to define the quantum feature map and kernel.

from quask.core import KernelFactory
from quask.core import QSVM

# Define the quantum feature map
# QuASK provides built-in feature maps, such as PauliFeatureMap or custom ones
feature_map = KernelFactory.create_feature_map("PauliFeatureMap", num_features=19, reps=2)

# Define the quantum kernel
quantum_kernel = KernelFactory.create_kernel("QuantumKernel", feature_map=feature_map)

# Initialize the QSVM
qsvm = QSVM(kernel=quantum_kernel)

Step 4: Train the QSVM

Train the QSVM using your training data.

# Train the QSVM
qsvm.fit(X_train, y_train)

Step 5: Evaluate the Model

After training, evaluate the model on the test set.

from sklearn.metrics import accuracy_score

# Predict on the test set
y_pred = qsvm.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")

Step 6: (Optional) Use a Quantum Simulator or Hardware

If you want to run the QSVM on a quantum simulator or real hardware, you can configure the backend in QuASK.

from quask.backends import BackendFactory

# Set up a quantum backend (simulator or hardware)
backend = BackendFactory.create_backend("qasm_simulator")  # Example: QASM simulator

# Update the quantum kernel with the backend
quantum_kernel.set_backend(backend)

# Retrain the QSVM with the backend
qsvm.fit(X_train, y_train)

# Evaluate again
y_pred = qsvm.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy with backend: {accuracy:.2f}")

Summary of Steps

  1. Install QuASK: Use pip to install the QuASK library.
  2. Prepare Dataset: Preprocess your 19-feature dataset and split it into training and testing sets.
  3. Set Up QuASK: Define the quantum feature map and kernel using QuASK's KernelFactory.
  4. Train QSVM: Train the QSVM using the training data.
  5. Evaluate Model: Test the model on the test set and calculate accuracy.
  6. Optional Backend: Configure a quantum backend (simulator or hardware) if needed.

Notes

  • QuASK is designed to be modular, so you can customize the feature map, kernel, and backend as needed.
  • If your dataset is large, consider dimensionality reduction techniques (e.g., PCA) to reduce the number of features before training.
  • Quantum simulations can be computationally expensive, so start with small datasets and gradually scale up.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment