Quantum Support Vector Machines
Building a custom support vector machine for testing on datasets.
Bridging Classical and Quantum Machine Learning: Building a Quantum Support Vector Machine
Quantum computing is gradually reshaping the landscape of data science and machine learning. In this blog post, I share my journey from implementing classical support vector classifiers (SVCs) to developing a quantum support vector machine (QSVM) capable of handling diverse datasets. In doing so, I combine established theoretical frameworks with hands-on experimentation—offering insights and context into the burgeoning field of quantum machine learning.
Setting the Stage: From Classical to Quantum
Traditional machine learning methods, such as SVCs, have proven effective in tackling a variety of classification tasks. However, as data complexity increases and high-dimensional problems emerge, classical approaches may struggle to capture intricate patterns. Quantum computing offers new avenues through phenomena such as superposition, entanglement, and quantum interference—features that, in theory, allow a quantum machine learning algorithm to explore vast solution spaces more efficiently than classical algorithms.
Building a QSVM involves two major steps:
- Data Encoding: Mapping classical data to quantum states.
- Quantum Kernel Evaluation: Leveraging quantum circuits to compute similarities between encoded data points.
My project started with establishing a robust classical baseline before venturing into the quantum domain.
The Classical SVC Experiments
Datasets and Approach
I experimented with three different datasets, each presenting unique challenges and characteristics:
- Truffle Dataset: This dataset involved binary classification with a 25%/75% training/testing split, following the methodology outlined in Peters et al. (2021). A grid search was performed over parameters such as C (regularization) and various kernels (linear, RBF, polynomial, and sigmoid).
- Pizza Dataset: This smaller dataset focused on brand detection. The simplicity of the dataset allowed classical SVCs to achieve perfect accuracy using a linear kernel with minimal regularization.
- Wine Dataset: With more complex, noisy data, the wine dataset proved challenging. Although high accuracy was reached with a linear kernel, the slight decrease in performance underscored the limitations faced by classical methods in certain contexts.
Results and Insights
The classical experiments yielded excellent performance, especially for the Truffle and Pizza datasets, with accuracy scores approaching or reaching 1.0. The rigorous evaluation through confusion matrices and detailed classification reports provided clear evidence of the model’s strengths and limitations across different data distributions.
Transitioning to Quantum: Developing the QSVM
Quantum Data Encoding Techniques
The first challenge in building a QSVM is transforming classical data into quantum states. I investigated several encoding methods, each with specific advantages and constraints:
Basis Encoding: Directly maps classical binary data to computational basis states ( 0⟩ and 1⟩). It is highly robust against noise but has limited capacity since it doesn’t fully exploit the quantum state space. - Angle Encoding: Utilizes the Bloch sphere representation where data values determine rotation angles. This method captures richer information but demands precise control over gate operations.
- Amplitude Encoding: Encodes entire data vectors into the amplitudes of a quantum state. Although this method scales well with data size, it requires more qubits and is susceptible to decoherence.
- Fock and Coherent-State Encoding: These advanced techniques are mainly used in quantum optics and require high-quality photon sources and detectors. They provide interesting insights when experimenting with continuous-variable quantum communication.
Each encoding method offers a trade-off between expressiveness and robustness. In my project, angle encoding emerged as a practical choice, allowing for a flexible and intuitive mapping from classical features to quantum states.
Building the Quantum Circuit
Using libraries such as QuTiP and Pennylane, I constructed a quantum circuit designed to encode classical information into quantum states. The circuit consisted of a series of rotation gates (RX, RY, and RZ) which were parameterized by the normalized data vector. Here’s a snippet of the encoding function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
n_qubits = 3
def normalize_data(vector):
norm_vector = np.pi * (2 * (vector - vector.min()) / (vector.max() - vector.min()) - 1) / 2
return norm_vector
def encode_data(vector):
norm_vector = normalize_data(vector)
circuit = QubitCircuit(n_qubits)
for i in range(3):
circuit.add_gate("RX", targets=[i], arg_value=norm_vector[4 * i])
circuit.add_gate("RY", targets=[i], arg_value=norm_vector[4 * i + 1])
circuit.add_gate("RX", targets=[i], arg_value=norm_vector[4 * i + 2])
circuit.add_gate("RX", targets=[i], arg_value=norm_vector[4 * i + 3])
return circuit