Quantum Support Vector Machines ( QSVM ) using Qiskit.

10 min readSep 13, 2024

Quantum Machine Learning (QML) is an interdisciplinary field at the intersection of quantum computing and machine learning. It leverages quantum computing principles to enhance or transform how machine learning tasks are approached, with the potential to achieve significant speedups and efficiencies for certain problems.

Core Concepts

  1. Quantum Computing Basics: Quantum computing harnesses the principles of quantum mechanics, such as superposition and entanglement, to process information in ways that classical computers cannot. Quantum bits (qubits) can exist in multiple states simultaneously, unlike classical bits which are either 0 or 1. This allows quantum computers to explore many possible solutions to a problem at once.

2. Quantum Speedup: Quantum algorithms can potentially solve specific computational problems more efficiently than classical algorithms. For example, Shor’s algorithm can factor large integers exponentially faster than the best-known classical algorithms, and Grover’s algorithm can search an unsorted database quadratically faster.

3. Machine Learning on Quantum Computers: QML aims to utilize quantum computing to improve machine learning algorithms in various ways:

  • Quantum Data: Quantum computers can process data that is inherently quantum in nature, which classical computers cannot handle directly.
  • Quantum Speedup: Certain machine learning tasks, like optimization and data fitting, might benefit from quantum speedup, potentially making algorithms run faster or handle larger datasets more efficiently.
  • Quantum-enhanced Models: Quantum versions of classical models (e.g., quantum neural networks) can be developed to exploit quantum properties, potentially offering superior performance or new capabilities.

4. Quantum Algorithms for Machine Learning: Several quantum algorithms are being explored for machine learning tasks:

  • Quantum Principal Component Analysis (PCA): Aims to perform dimensionality reduction more efficiently by exploiting quantum superposition to process data in fewer operations.
  • Quantum Support Vector Machines (SVMs): Seeks to improve classification tasks by leveraging quantum computing for faster kernel evaluations.
  • Quantum Neural Networks (QNNs): Designed to exploit quantum circuits to perform computations that mimic neural networks, potentially offering advantages in learning complex patterns.

5. Challenges and Limitations: While QML holds promise, it faces several challenges:

  • Quantum Hardware: Current quantum computers are in the Noisy Intermediate-Scale Quantum (NISQ) era, meaning they have limited qubits and are prone to errors. This limits the complexity of problems they can address effectively.
  • Algorithm Development: Many quantum algorithms are still theoretical or in early experimental stages, and translating classical machine learning problems into quantum terms can be non-trivial.
  • Integration with Classical Systems: Effective QML requires hybrid approaches that combine quantum and classical computing, which can be complex and requires efficient interfacing between the two.

As quantum hardware advances, QML is expected to become more practical. Researchers are exploring hybrid models where classical and quantum systems work together to leverage the strengths of both. Potential breakthroughs in QML could revolutionize fields such as data analysis, optimization, and pattern recognition, leading to new discoveries and innovations.

IBM Qiskit

IBM Qiskit is an open-source quantum computing framework developed by IBM to facilitate quantum programming and research. At its core, Qiskit allows users to design quantum algorithms, run them on real quantum computers, and analyze their results through a high-level Python library.

The Qiskit Python library provides a comprehensive toolkit for quantum computing. It comprises several components, each serving a specific role. The qiskit.circuit module enables the construction of quantum circuits, allowing users to create and manipulate quantum gates and operations. This modular approach lets researchers and developers design complex quantum algorithms in a structured manner.

Here is the link for IBM Qiskit Tutorials:

The qiskit.aer module offers high-performance simulation capabilities. It allows users to simulate quantum circuits on classical computers, providing insights into how quantum algorithms would perform on real quantum hardware. This is particularly useful for testing and debugging quantum algorithms before deploying them on actual quantum processors.

For running quantum circuits on real quantum hardware, Qiskit provides the qiskit.ibm module. This module interfaces with IBM Quantum’s cloud-based quantum processors, enabling users to submit jobs to quantum computers and retrieve results. Through this, researchers can access IBM's extensive quantum hardware and contribute to advancing the field.

Qiskit also includes tools for quantum chemistry, optimization, and machine learning through its qiskit-nature, qiskit-optimization, and qiskit-machine-learning extensions. These components expand the library’s applicability beyond fundamental quantum computing to practical applications in various scientific and engineering disciplines.

Quantum Support Vector Machine (QSVM)

A Quantum Support Vector Machine (QSVM) represents an advanced intersection of quantum computing and classical machine learning techniques, designed to address the challenges associated with traditional support vector machines (SVMs). To understand QSVM, it’s essential first to grasp the foundational concepts of both SVMs and quantum computing.

Support Vector Machines are a popular method in classical machine learning for classification tasks. They work by finding the hyperplane in a high-dimensional space that best separates different classes of data. The goal is to maximize the margin between the closest points of the classes (support vectors) and the hyperplane, thus ensuring the best possible separation. This method is effective in various scenarios, particularly when dealing with linearly separable data. However, in practice, SVMs can face challenges when working with complex datasets that involve non-linear relationships, high-dimensional spaces, or very large datasets.

Quantum computing introduces a new paradigm by leveraging the principles of quantum mechanics — superposition and entanglement — to perform computations. In quantum computing, information is processed using quantum bits, or qubits, which can represent multiple states simultaneously, unlike classical bits that are either 0 or 1. This capability allows quantum computers to handle certain types of problems more efficiently than classical computers.

Quantum Support Vector Machine ( QSVM )

The QSVM builds upon these quantum principles to enhance the capabilities of traditional SVMs. At its core, QSVM aims to harness the power of quantum computing to address the limitations of classical SVMs, particularly in handling large datasets and complex feature spaces.

One of the primary advantages of QSVM is its ability to perform high-dimensional computations more efficiently. In classical SVMs, the computational cost grows significantly with the dimensionality of the data. QSVM takes advantage of quantum algorithms to encode data into quantum states, where quantum parallelism can be utilized to perform computations on large feature spaces more rapidly. This encoding process involves transforming classical data into quantum states, which allows quantum computers to process and analyze these states simultaneously through quantum operations.

Quantum algorithms used in QSVM include the quantum kernel method and quantum optimization algorithms. The quantum kernel method is particularly noteworthy. In a classical SVM, the kernel trick is used to map data into a higher-dimensional space to make it linearly separable. Quantum computers can efficiently compute quantum kernels, which can potentially lead to a more effective mapping of data into high-dimensional spaces than classical methods. By using quantum kernels, QSVM can discover complex patterns and relationships in data that might be difficult for classical SVMs to identify.

Architecture of the Quantum Support Vector Classifier

Another critical aspect of QSVM is its potential for faster optimization. Classical SVMs rely on optimization techniques to find the best hyperplane, which can be computationally expensive, especially for large datasets. Quantum optimization algorithms, such as the Quantum Approximate Optimization Algorithm (QAOA), can be applied to solve these optimization problems more efficiently. QAOA and similar quantum algorithms can explore the solution space more effectively, potentially leading to faster convergence to the optimal solution.

However, despite the theoretical advantages of QSVM, practical implementation faces several challenges. Quantum computers are still in their early stages of development, and building large-scale, fault-tolerant quantum systems remains a significant challenge. Current quantum computers have limited qubit counts and are susceptible to errors, which can affect the reliability and accuracy of QSVM implementations. Additionally, designing quantum algorithms that are both efficient and practical for real-world problems requires careful consideration and development.

The implementation of QSVM also involves the development of quantum circuits and algorithms tailored to specific classification tasks. Researchers and engineers must design quantum circuits that can effectively encode data and perform the necessary quantum operations. This process requires a deep understanding of both quantum mechanics and machine learning principles, as well as expertise in quantum programming languages and tools.

Despite these challenges, the potential benefits of QSVM make it an exciting area of research. As quantum computing technology continues to advance, QSVM could offer significant improvements in classification tasks, especially for complex and high-dimensional datasets. Researchers are actively exploring various approaches to overcome current limitations and to develop practical QSVM solutions that can be applied to real-world problems.

Quantum Support Vector Machines — Code implementation using Qiskit :

  1. Installing Dependencies
!pip install scikit-learn==1.2.1
!pip install pennylane==0.26
!pip install qiskit==0.39.2
!pip install qiskit_machine_learning==0.5.0

This cell installs the necessary Python libraries:

  • scikit-learn for machine learning algorithms.
  • pennylane for quantum machine learning and quantum computing.
  • qiskit for quantum computing with IBM's quantum hardware.
  • qiskit_machine_learning for quantum machine learning algorithms.

2. Setting Up Data and Preprocessing

import numpy as np
seed = 1234
np.random.seed(seed)

from sklearn.datasets import load_wine
x, y = load_wine(return_X_y=True)

x = x[:59+71]
y = y[:59+71]

from sklearn.model_selection import train_test_split
x_tr, x_test, y_tr, y_test = train_test_split(x, y, train_size=0.9)

from sklearn.preprocessing import MaxAbsScaler
scaler = MaxAbsScaler()
x_tr = scaler.fit_transform(x_tr)

x_test = scaler.transform(x_test)
x_test = np.clip(x_test, 0, 1)
  • Data Loading and Splitting: Loads the wine dataset and splits it into training and test sets, using 90% of the data for training and 10% for testing.
  • Scaling: Uses MaxAbsScaler to scale the data to the range [0, 1], which is suitable for quantum computing tasks.

3. Quantum Kernel and Support Vector Machine

import pennylane as qml

nqubits = 4
dev = qml.device("lightning.qubit", wires=nqubits)

@qml.qnode(dev)
def kernel_circ(a, b):
qml.AmplitudeEmbedding(
a, wires=range(nqubits), pad_with=0, normalize=True)
qml.adjoint(qml.AmplitudeEmbedding(
b, wires=range(nqubits), pad_with=0, normalize=True))
return qml.probs(wires=range(nqubits))

from sklearn.svm import SVC
def qkernel(A, B):
return np.array([[kernel_circ(a, b)[0] for b in B] for a in A])

svm = SVC(kernel=qkernel).fit(x_tr, y_tr)

from sklearn.metrics import accuracy_score
print(accuracy_score(svm.predict(x_test), y_test))
  • Quantum Circuit: Defines a quantum kernel circuit using Pennylane. This circuit uses amplitude embedding and measures the probabilities.
  • Quantum Kernel Function: Implements the quantum kernel function, which computes the kernel matrix for the Support Vector Machine (SVM) using the quantum circuit.
  • SVM Training and Evaluation: Trains an SVM classifier using the quantum kernel and evaluates its performance on the test set.

4. PCA for Dimensionality Reduction

from sklearn.decomposition import PCA
pca = PCA(n_components=8)
xs_tr = pca.fit_transform(x_tr)
xs_test = pca.transform(x_test)

nqubits = 8
dev = qml.device("lightning.qubit", wires=nqubits)

@qml.qnode(dev)
def kernel_circ(a, b):
qml.AngleEmbedding(a, wires=range(nqubits))
qml.adjoint(qml.AngleEmbedding(b, wires=range(nqubits)))
return qml.probs(wires=range(nqubits))

svm = SVC(kernel=qkernel).fit(xs_tr, y_tr)

print(accuracy_score(svm.predict(xs_test), y_test))
  • PCA Transformation: Reduces the dimensionality of the data to 8 components using PCA.
  • Quantum Circuit: Redefines the quantum kernel circuit with angle embedding instead of amplitude embedding.
  • SVM Training and Evaluation: Trains an SVM classifier with the new quantum kernel and evaluates its performance.

5. ZZFeatureMap and SVM with Quantum Kernel

from itertools import combinations

def ZZFeatureMap(nqubits, data):
nload = min(len(data), nqubits)
for i in range(nload):
qml.Hadamard(i)
qml.RZ(2.0 * data[i], wires=i)
for pair in list(combinations(range(nload), 2)):
q0 = pair[0]
q1 = pair[1]
qml.CZ(wires=[q0, q1])
qml.RZ(2.0 * (np.pi - data[q0]) * (np.pi - data[q1]), wires=q1)
qml.CZ(wires=[q0, q1])

nqubits = 4
dev = qml.device("lightning.qubit", wires=nqubits)

@qml.qnode(dev)
def kernel_circ(a, b):
ZZFeatureMap(nqubits, a)
qml.adjoint(ZZFeatureMap)(nqubits, b)
return qml.probs(wires=range(nqubits))

svm = SVC(kernel=qkernel).fit(xs_tr, y_tr)
print(accuracy_score(svm.predict(xs_test), y_test))
  • ZZFeatureMap Function: Defines a feature map for the quantum kernel based on the ZZ interactions and RZ rotations.
  • Quantum Circuit: Uses the ZZFeatureMap function in the quantum circuit.
  • SVM Training and Evaluation: Trains an SVM classifier with this new quantum kernel and evaluates its performance.

6. Using Qiskit for Quantum SVM

from qiskit import *
from qiskit.circuit import Parameter
parameter = Parameter("x")
qc = QuantumCircuit(1)
qc.rx(parameter, 0)

from qiskit.circuit import ParameterVector
parameter = ParameterVector("x", length=2)
qc = QuantumCircuit(2)
qc.rx(parameter[0], 0)
qc.rx(parameter[1], 1)

from qiskit.circuit.library import ZZFeatureMap

zzfm = ZZFeatureMap(8)

from qiskit_machine_learning.kernels import QuantumKernel
from qiskit.providers.aer import AerSimulator

qkernel = QuantumKernel(feature_map=zzfm, quantum_instance=AerSimulator())

from qiskit_machine_learning.algorithms import QSVC

qsvm = QSVC(quantum_kernel=qkernel)
qsvm.fit(xs_tr, y_tr)

print(accuracy_score(qsvm.predict(xs_test), y_test))
  • Quantum Circuit with Parameters: Defines quantum circuits with parameterized gates using Qiskit.
  • ZZFeatureMap and QuantumKernel: Creates a ZZFeatureMap and uses it to create a quantum kernel.
  • QSVC: Trains a Quantum SVM classifier using the quantum kernel and evaluates its performance.

7. Using IBM Quantum Devices

ibm_token = "1234"
IBMQ.save_account(ibm_token)

provider = IBMQ.load_account()

from qiskit.providers.ibmq import *

dev_list = provider.backends(
filters=lambda x: x.configuration().n_qubits >= 7,
simulator=False)
dev = least_busy(dev_list)

from sklearn.decomposition import PCA

pca = PCA(n_components=7)
xss_tr = pca.fit_transform(x_tr)
xss_test = pca.transform(x_test)

zzfm = ZZFeatureMap(7)
qkernel = QuantumKernel(feature_map=zzfm, quantum_instance=dev)
qsvm = QSVC(quantum_kernel=qkernel)
qsvm.fit(xss_tr, y_tr)

Note: In the following cell, you need to replace “1234” with your actual IBM token.

  • IBM Quantum Account: Saves and loads the IBM Quantum account using an authentication token.
  • Device Selection: Finds the least busy IBM quantum device with at least 7 qubits.
  • PCA and Quantum SVM: Reduces the dimensionality to 7 components, creates a ZZFeatureMap, and trains a QSVC using the selected IBM quantum device.

Here is the link for Quantum Machine Learning Tutorials:

Here is a Joke on QSVM:

Why did the Quantum Support Vector Machine break up with its classical counterpart?

Because it needed more space for superposition and entanglement, and the classical SVM just couldn’t handle the quantum entanglements!

--

--

Devmallya Karar
Devmallya Karar

Written by Devmallya Karar

Lead AI Scientist at HCLTech and entangled with Generative AI, Quantum ML, Deep Learning and Machine Learning. These are the O2 of my life.

No responses yet