The blog contains a brief explanation of Quantum Computing, its fusion with machine learning and how it ca be used to solve real life challenges.

## Introduction

Welcome to the realm where the extraordinary meets the tangible—Quantum Machine Learning (QML). In this blog journey, we embark on an exploration of the convergence between the fantastical realms of quantum physics and the pragmatic world of machine learning. In this blog, we'll demystify the fusion of quantum and classical, exploring the potential, challenges, and real-world applications of this cutting-edge technology. Whether you're a seasoned data scientist or someone curious about the future, join us on a journey where the lines between science fiction and reality blur, paving the way for a new era in machine learning.

## What is Quantum Computation?

Since the inception of our exploration into the atom, quantum physics has consistently challenged conventional logic, diverging from the familiar rules of classical physics. Quantum particles, unlike their classical counterparts, exhibit perplexing behaviors such as the ability to move both backward and forward in time, exist simultaneously in two locations, and even engage in a phenomenon akin to teleportation.

Quantum computers aim to harness these distinctive capabilities by employing quantum bits, or **qubits(quantum bits)**, in contrast to the binary system of ones and zeros used in conventional computers. Unlike classical bits, which can only be in a state of either one or zero, qubits function as both particles and waves. Leveraging the wave distribution, qubits store a significantly larger amount of data compared to conventional bits.

Let us understand this in little details:

__ __* *__Credits__*: Bit vs Qubit*

### Qubits are represented by a superposition of multiple possible states

A qubit uses the quantum mechanical phenomena of superposition to achieve a linear combination of two states. A classical binary bit can only represent a single binary value, such as 0 or 1, meaning that it can only be in one of two possible states. A qubit, however, can represent a 0, a 1, or any proportion of 0 and 1 in superposition of both states, with a certain probability of being a 0 and a certain probability of being a 1.

Qubits leverage quantum properties, particularly ** superposition** and

**, to redefine information storage and processing:**

__entanglement__Superposition gives quantum computers superior computing power

Superposition allows quantum algorithms to process information in a fraction of the time it would take even the fastest classical systems to solve certain problems.

The amount of information a qubit system can represent grows exponentially. Information that 500 qubits can easily represent would not be possible with even more than 2^500 classical bits.

It would take a classical computer millions of years to find the prime factors of a 2,048-bit number. Qubits could perform the calculation in just minutes.

__Credits__*: Illustration of a bit and qubit. (Left) A bit can take a value of '0' or '1' with 100% probability. (Middle) A qubit can be in a state of |0⟩ or |1⟩ or in a superposition state of both |0⟩ and |1⟩. Here, a qubit is illustrated in a superposition state, composed of 50% |0⟩ and 50% |1⟩. (Right) Illustration of two qubits in an entangled state. The properties of the two qubits in entangled state are linked to each other such that by looking (i.e. measuring) one of them, will reveal the other qubit, even when they are at physically large separations.*

Superposition enables quantum algorithms to utilize other quantum mechanical phenomena, such as interference and entanglement. Together, superposition, interference, and entanglement create computing power that can solve problems exponentially faster than classical computers.

**Entanglement is the phenomenon in which two or more quantum particles can become correlated in such a way that their states are intrinsically linked, regardless of the distance between them**. This correlation is stronger than any classical correlation and is described by a mathematical concept known as a quantum state

## What is Quantum Machine Learning(QML)?

Quantum machine learning (QML) represents an evolving domain that integrates quantum computing with machine learning techniques. Its objective is to leverage the capabilities of quantum computing to enhance machine learning algorithms and address intricate challenges beyond the capacity of classical computers. In the realm of QML, quantum computing is employed to execute operations on quantum data, articulated through quantum states. These quantum states serve as a medium to encode information, enabling more streamlined and resource-efficient processing and storage of data.

*Quantum machine learning* (QML) is built on two concepts: ** quantum data** and

*hybrid quantum-classical models***.**

Quantum data involves encoding classical information into quantum states, allowing for quantum parallelism and exploration of multiple possibilities simultaneously. Hybrid quantum-classical models combine the computational power of quantum processors with classical optimization algorithms to solve complex problems more efficiently.

## Quantum Machine Learning Algorithms

The prominent Quantum Machine Learning Algorithms include:

**Quantum Support Vector Machines (QSVM)**: It is like an upgraded version of classical Support Vector Machines (SVM) but for quantum computers. SVM is a common tool in regular computers for sorting things into categories or predicting values. It does this by finding the best way to draw a line or plane that separates different groups in a set of features. Now, with QSVM, we're using quantum algorithms to handle quantum data. Quantum computers have a special ability to check many solutions at once, potentially making some computations much quicker, especially when regular SVMs are struggling with big calculations.**Quantum Principal Component Analysis (QPCA)****:**It is like a smarter version of a classical method called Principal Component Analysis (PCA). PCA is used in regular computers to simplify complicated data and make it faster to analyze. It works by finding important patterns and reducing the amount of information but keeping the most crucial parts.**Quantum k-Means Clustering****:**It is an adaptation of the classical k-Means Clustering algorithm designed for quantum computers. In classical computing, k-Means is a popular clustering technique used to group data points into distinct clusters based on their similarities. Each cluster has a central point called a centroid.**Quantum Variational Circuits (QVCs)****:**They are a class of quantum circuits used in quantum computing for variational algorithms. These circuits play a crucial role in variational quantum algorithms, where the goal is to find the optimal parameters that minimize or maximize a certain objective function.**Quantum Neural Networks (QNNs):**They merge principles from quantum computing with artificial neural network architectures, presenting a novel approach to processing information. Unlike classical neural networks that use bits, QNNs leverage quantum bits or qubits to represent information. Qubits, thanks to principles like superposition and entanglement, can exist in multiple states simultaneously, offering a quantum parallelism advantage.**Quantum Reinforcement Learning (QRL)****:**It combines quantum computing and machine learning, allowing computers to learn by interacting with environments. In QRL, decisions and states are represented using quantum bits (qubits), leveraging quantum parallelism for more efficient decision-making. Quantum algorithms, like Grover's, enhance specific learning tasks. QRL excels in solving complex problems, especially with vast state spaces. However, maintaining stable quantum coherence is challenging.

## Training a Quantum Model on a Real Dataset using Python and Qiskit

Now I'll demonstrate how to train a quantum machine learning model to tackle a classification problem. I'll be using Qiskit python library specially designed to work with QML.

**Qiskit** Machine Learning introduces fundamental computational building blocks - such as Quantum Kernels and Quantum Neural Networks - used in different applications, including classification and regression. On the one hand, this design is very easy to use and allows users to rapidly prototype a first model without deep quantum computing knowledge. On the other hand, Qiskit Machine Learning is very flexible, and users can easily extend it to support cutting-edge quantum machine learning research. Read more about Qiskit __here__.

I've decided to pick a very well-known – albeit still relatively small – problem: the Iris flower dataset. This dataset even has its own Wikipedia page. Although the Iris dataset is well known to data scientists

Install all the required libraries and dependencies.

```
!pip install qiskit
!pip install pylatexenc
!pip install qiskit-optimization
!pip install qiskit-machine-learning
```

## 1. Exploratory Data Analysis

First, let us explore the Iris dataset this tutorial will use and see what it contains. For our convenience, this dataset is available in scikit-learn and can be loaded easily.

```
from sklearn.datasets import load_iris
iris_data = load_iris()
X = iris_data.data
df = pd.DataFrame(X, columns=iris_data.feature_names)
df
```

There are a few interesting observations we can find from this dataset description:

There are 150 samples (instances) in the dataset.

There are four features (attributes) in each sample.

There are three labels (classes) in the dataset.

The dataset is perfectly balanced, as there are the same number of samples (50) in each class.

We can see features are not normalized, and their value ranges are different, e.g., [4.3,7.9] and [0.1,2.5] for sepal length and petal width, respectively. So, transforming the features to the same scale may be helpful.

As stated in the table above, feature-to-class correlation in some cases is very high; this may lead us to think that our model should cope well with the dataset.

Next, create features and labels.

```
features = iris_data.data
labels = iris_data.target
```

Firstly, we’ll normalize the features. , We will apply a simple transformation to represent all features on the same scale. In our case, we squeeze all features onto the interval [0,1]. Normalization is a common technique in machine learning and often leads to better numerical stability and convergence of an algorithm.

We can use MinMaxScaler from scikit-learn to perform this. Without specifying parameters, this does exactly what is required: maps data onto [0,1].

```
from sklearn.preprocessing import MinMaxScaler
features = MinMaxScaler().fit_transform(features)
```

We plot the features pair-wise to see if there’s an observable correlation between them.

```
import seaborn as sns
df["class"] = pd.Series(iris_data.target)
sns.pairplot(df, hue="class", palette="tab10")
```

From the plots, we see that class 0 is easily separable from the other two classes, while classes 1 and 2 are sometimes intertwined, especially regarding the “sepal width” feature.

Next, split the dataset in training and test set:

```
from sklearn.model_selection import train_test_split
from qiskit_algorithms.utils import algorithm_globals
algorithm_globals.random_seed = 123
train_features, test_features, train_labels, test_labels = train_test_split(
features, labels, train_size=0.8, random_state=algorithm_globals.random_seed
)
```

Now, we’ll train a Variational Quantum Classifier (VQC). The VQC is the simplest classifier available in Qiskit Machine Learning and is a good starting point for newcomers to quantum machine learning who have a background in classical machine learning.

Before model training, let's delve into the VQC class, emphasizing its core components: the **feature map** and **ansatz**. Our data is classical, composed of bits, not qubits, __requiring encoding into qubits for an effective quantum model__. This process, termed data encoding, data embedding, or data loading, is facilitated by the feature map. Unlike classical ML, which operates solely in the classical realm, this loading into quantum states is unique to quantum machine learning.

Post-loading, the immediate step involves applying a parameterized quantum circuit, analogous to classical neural network layers. This circuit, with tunable parameters or weights, is optimized to minimize an objective function gauging the gap between predictions and labeled data. Known as a parameterized trial state, variational form, or ansatz, this quantum circuit is pivotal in quantum machine learning.

Our choice of feature map will be the ** ZZFeatureMap**. The ZZFeatureMap is one of the standard feature maps in the Qiskit circuit library. We pass num_features as feature_dimension, meaning the feature map will have num_features or 4 qubits.

We decompose the feature map into its constituent gates to give the reader a flavor of how feature maps may look.

```
from qiskit.circuit.library import ZZFeatureMap
num_features = features.shape[1]
feature_map = ZZFeatureMap(feature_dimension=num_features, reps=1)
feature_map.decompose().draw(output="mpl", fold=20)
```

If you look closely at the feature map diagram, you will notice parameters x[0], ..., x[3]. These are placeholders for our features.

Now we create and plot our ansatz. Pay attention to the repetitive structure of the ansatz circuit. We define the number of these repetitions using the reps parameter.

```
from qiskit.circuit.library import RealAmplitudes
ansatz = RealAmplitudes(num_qubits=num_features, reps=3)
ansatz.decompose().draw(output="mpl", fold=20)
```

This circuit has 16 parameters named θ[0], ..., θ[15]. These are the trainable weights of the classifier.

We then choose an optimization algorithm to use in the training process. This step is similar to what you may find in classical deep learning frameworks. To make the training process faster, we choose a gradient-free optimizer. You may explore other optimizers available in Qiskit.

```
from qiskit_algorithms.optimizers import COBYLA
optimizer = COBYLA(maxiter=100)
```

In the next step, we define where to train our classifier. We can train on a simulator or a real quantum computer. Here, we will use a simulator. We create an instance of the Sampler primitive. This is the reference implementation that is statevector based. Using qiskit runtime services you can create a sampler that is backed by a quantum computer.

```
from qiskit.primitives import Sampler
sampler = Sampler()
```

We will add a callback function called callback_graph. VQC will call this function for each evaluation of the objective function with two parameters: the current weights and the value of the objective function at those weights. Our callback will append the value of the objective function to an array so we can plot the iteration versus the objective function value. The callback will update the plot at each iteration. Note that you can do whatever you want inside a callback function, so long as it has the two-parameter signature we mentioned above.

```
from matplotlib import pyplot as plt
from IPython.display import clear_output
objective_func_vals = []
plt.rcParams["figure.figsize"] = (12, 6)
def callback_graph(weights, obj_func_eval):
clear_output(wait=True)
objective_func_vals.append(obj_func_eval)
plt.title("Objective function value against iteration")
plt.xlabel("Iteration")
plt.ylabel("Objective function value")
plt.plot(range(len(objective_func_vals)), objective_func_vals)
plt.show()
```

Now we are ready to construct the classifier and fit it.

VQC takes a feature map and an ansatz and constructs a quantum neural network automatically. In the simplest case it is enough to pass the number of qubits and a quantum instance to construct a valid classifier. You may omit the sampler parameter, in this case a Sampler instance will be created for you in the way we created it earlier. We created it manually for illustrative purposes only.

```
import time
from qiskit_machine_learning.algorithms.classifiers import VQC
vqc = VQC(
sampler=sampler,
feature_map=feature_map,
ansatz=ansatz,
optimizer=optimizer,
callback=callback_graph,
)
# clear objective value history
objective_func_vals = []
start = time.time()
vqc.fit(train_features, train_labels)
elapsed = time.time() - start
print(f"Training time: {round(elapsed)} seconds")
```

Let’s see how the quantum model performs on the real-life dataset.

```
train_score_q4 = vqc.score(train_features, train_labels)
test_score_q4 = vqc.score(test_features, test_labels)
print(f"Quantum VQC on the training dataset: {train_score_q4:.2f}")
print(f"Quantum VQC on the test dataset: {test_score_q4:.2f}")
```

As we can see, the scores are high, and the model can be used to predict labels on unseen data.

You can try out multiple use cases by visiting __Qiskit__. Quantum Machine Learning is also available on Azure under __Azure Quantum service__.

## Conclusion

In conclusion, the trajectory of quantum computing has evolved from theoretical speculation to tangible application, marking a significant paradigm shift in the technological landscape. While quantum computing itself is not a newfound concept, recent years have witnessed a surge in attention and practical advancements, enabling broader access to quantum machines.

The realm of machine learning stands out as a promising arena for quantum techniques to showcase their transformative potential. The Quantum Machine Learning model has demonstrated superior accuracy and efficiency compared to classical counterparts, signaling a glimpse into the future possibilities. Despite these advancements, it's crucial to acknowledge that quantum computers are still in the process of reaching their full potential.

As we anticipate these strides, it becomes evident that the synergy between quantum and classical computing heralds a new era, where the boundaries of what's possible in machine learning are continually expanding. The journey into the quantum frontier is only just beginning, with exciting prospects on the horizon for reshaping the future of technology.

## Comments