Photo by Annamária Borsos (used with permission)
Quantum Machine Learning (QML) is a fascinating fusion of quantum computing and machine learning technologies. Leveraging quantum computing’s potential in handling complex mathematical and data processing structures, QML has the potential to revolutionize industries such as drug discovery, finance, and more. This blog explores the innovative realms of quantum neural networks (QNNs) and quantum kernel techniques, showcasing their unique capabilities through practical Python examples. Mathematical concepts will not be detailed in this blog. For more information, readers are encouraged to explore the book “Machine Learning Theory and Applications: Hands-on Use Cases with Python on Classical and Quantum Machines” by the author, published by Wiley in 2024.
Quantum kernel methods introduce a quantum-enhanced approach to data processing. By mapping classical data into quantum feature space, these methods utilize the superposition and entanglement properties of quantum mechanics to perform classifications or regression tasks. Practical examples of quantum kernel estimator and quantum variational classifier demonstrate the application of these concepts. QNNs, which leverage quantum states for computation, offer a new perspective on neural network architecture. The Qiskit framework enables the implementation of both quantum kernel methods and QNNs, facilitating the exploration of quantum algorithms’ efficiency in learning and pattern recognition.
This blog aims to provide comprehensive code examples of QML for readers to explore its promising applications and the challenges it faces. Through these examples, readers can gain an understanding of the transformative potential of quantum computing in machine learning and the exciting possibilities that lie ahead.
We will utilize the open-source SDK Qiskit (https://qiskit.org) for working with quantum computers. Qiskit supports Python version 3.6 or later.
In our environment, Qiskit can be installed using pip:
pip install qiskit
Additionally, qiskit-machine-learning
can be installed using pip:
pip install qiskit-machine-learning
Documentation for Qiskit machine learning can be found on GitHub: https://github.com/Qiskit/qiskit-machine-learning/.
To run the code, simulators or real hardware can be used, although hardware is recommended to push the limits of simulators and improve research in this field. The Qiskit documentation references the Qiskit Runtime primitives, which serve as implementations of the Sampler and Estimator interfaces found in the qiskit.primitives
module. These interfaces allow for seamless interchangeability of primitive implementations with minimal code modifications. The initial release of Qiskit Runtime includes two essential primitives:
- Sampler: Generates quasi-probabilities based on input circuits.
- Estimator: Calculates expectation values derived from input circuits and observables.
For more detailed insights, information is available in the following resource: https://qiskit.org/ecosystem/ibm-runtime/tutorials/how-to-getting-started-with-sampler.html.
Exploring quantum approaches for supervised machine learning presents a novel research direction. Classical machine learning often relies on kernel methods, with the support vector machine (SVM) standing out for its application in binary classification. SVMs, used to separate data points into two groups based on a hyperplane, can also be applied to multiclass problems. Nonlinear classifications are achieved through the kernel trick, which maps inputs into a higher-dimensional feature space using a kernel function. Quantum kernel methods, blending classical and quantum strategies, open new paths in machine learning by encoding data points into inner products or amplitudes in Hilbert space through quantum feature maps.
In the first example presented, the ZZFeatureMap with linear entanglement is used to encode data, repeating the process two times and employing feature reduction with principal component analysis. Other techniques such as feature reduction, data rescaling, or feature selection can be utilized to enhance model accuracy. The breast cancer dataset is used in this example, available at: https://github.com/xaviervasques/hephaistos/blob/main/data/datasets/breastcancer.csv.
The Python script below demonstrates the integration of quantum computing techniques with traditional machine learning to classify breast cancer data. This hybrid approach combines quantum-enhanced features with classical machine learning methods to predict breast cancer diagnosis based on extracted features.
The process of quantum kernel machine learning is similar to classical data science practices. The script imports necessary libraries (Pandas, NumPy, scikit-learn) and Qiskit for quantum computing and kernel estimation, loads the data, preprocesses it, separates features (X) and target labels (y), creates a quantum feature map using the ZZFeatureMap, configures a quantum kernel with a fidelity-based approach, sets up a machine learning pipeline integrating standard scaler, PCA for dimensionality reduction, and a Support Vector Classifier (SVC) using the quantum kernel, and evaluates the model using cross-validation.
The mean cross-validation score obtained from this code execution is 0.63 when using the local simulator.