The simplest quantum binary classifier

How the simplest quantum binary classifier looks like? While searching in internet for the smallest quantum classifier I came across the article from Maria Schuld “Building the world’s smallest quantum classifier” (1) and, based on the circuit proposed there, I thought an even smaller classifier could be built. Here I show a simple variational circuit that can be applied to a 2-feature dataset for binary classification. Though the example is questionable for practical purposes, it gives interesting insights on how to apply quantum computing to machine learning.

The circuit is composed of two parts: the embedding, which transforms the classical data into quantum states, and the ansatz, which is parametrized, and the parameters are optimized using a particle swarm optimization algorithm (2). The circuit and the findings follow.

First, we need a dataset! Instead of searching for real datasets which must be cleaned, normalized, etc… I’ve created one. The dataset contains 60 samples, each sample with two features – feat1 and feat2 – and one binary label with values 0 or 1. 

The dataset is randomly generated, and for this exercise, specially engineered to facilitate the visualization of the sample, once the embedding is created.

The next figure shows how the samples in the dataset are partitioned, considering the two features. As can be observed, feat1 nicely separates the two classes – blue (label 0) and red (label 1).


With a dataset having two features, the features can be embedded within the angles of the state vector on a single qubit. The circuit for the embedding is given by the figure below:

The parameters e00 and e01 encodes respectively feat1 and feat2. The usage of a single qubit allows me to represent the state vectors generated by the embedding in a single Bloch sphere. The result of applying the embedding above to the initial dataset is shown in figure below.
 
As can be observed in the Bloch sphere, the dataset has been naturally separated during the embedding. All the blue states are on the upper half of the sphere whereas the red ones are in the lower one.

By inspecting the sphere one can easily conclude that a classifier which separates the states across the plane XY will successfully classify correctly all the points in the dataset. Now, lets utilize the quantum computer and the variational algorithm and see if it reaches the same conclusion!
 
The circuit for the ansatz is given by the figure below.
Note there are two parameters – p00, p01 – which must be adjusted so that error, when classifying the dataset, is minimized. Pretending we don’t know the answer, let’s leave the algorithm to adjust them. For the optimization algorithm, instead of gradient descent, I use the particle swarm optimization algorithm.

The final quantum circuit (embedding + ansatz) is given by the circuit below.

After running the swarm optimization using 20 particles and 50 iterations, the best parameters found for p00 and p01 are respectively 0.28762104 for p00 and 0.05244946 for p01. These values are close to the expected XY plane but are not exactly representing the XY plane. Consequently, by utilizing these parameters, the circuit misclassifies 5 points. The misclassified states are shown in the Bloch sphere below.

Another interesting observation is how the state space is classified by the newly created ansatz. This is given by the Bloch sphere below. The light red area (south hemisphere of the sphere) represents the state where the ansatz classifies as 1, whereas the blue (north hemisphere) are the states classified as 0. The dark blue and red points represent the expected states given by the original dataset.


 

Observe the region near the state |+y> in the figures above (in the sphere, the points are close to the center. The same points are magnified in the figure below the sphere). There are 4 red states in the light blue region very close to the border, indicating the points that are misclassified around the state |+y>. The other misclassifications are on the other side of the sphere and not seen in the figure.

This exercise demonstrates how a simple variational circuit, composed of one single qubit and 4 gates, can learn and classify a two-feature dataset. By employing a particle swarm optimization algorithm to adjust the rotational angles of the ansatz, the circuit successfully learns and classifies the dataset. However, a significant drawback of the circuit is the fact that it can only separate classes using a plane, limiting its practical applicability. Nonetheless, this exercise provides a clear illustration of the feature encoding through angle encoding and the implementation of machine learning algorithms using quantum computing.

If you are interested in the code used in this blog, refer to the following notebook: Simple Binary Classifier

 

References:

(1) building-the-worlds-smallest-quantum-classifier

(2) Quantum Machine Learning and Optimisation in Finance, Jacquier and Kondratyev, 2022

Comments

Popular posts from this blog

IBM Qiskit Exam: my four weeks preparation

A View on the Deutsch Algorithm