Classifying Breast Cancer with Quantum Machine Learning using IBM Qiskit

8 min readJan 10, 2023

Part 1. Quantum Kernel Method

Image source: Quantum.gov

Breast cancer is a common and often aggressive form of cancer that affects millions of people worldwide. Early diagnosis is key to successful treatment, and machine-learning techniques have been widely used to help classify breast tumors as benign or malignant. This blog post will explore how quantum machine learning can classify breast cancer using the Wisconsin breast cancer dataset. In the first article of this two-part series, we will introduce the quantum kernel method, and in the second, we will show how to implement a variational quantum circuit for the same task. We will use the IBM Qiskit library to implement these techniques on a quantum simulator and a quantum computer.

Introduction

Quantum machine learning is a subfield of machine learning that uses quantum computers to perform machine learning tasks. It can provide significant improvements in the speed and accuracy of machine learning algorithms, particularly for tasks that are difficult or impossible to achieve with classical computers.

One of the main reasons that quantum machine learning is a commonly asked topic is that quantum computers have the potential to perform certain types of calculations much faster than classical computers. In particular, quantum computers can perform certain linear algebra operations, such as matrix inversion and eigenvalue decomposition, exponentially faster than classical computers. This makes them well-suited to tasks such as data classification and clustering, which rely heavily on linear algebra operations.

Quantum computers use quantum bits or qubits. A bit is the most basic unit of classical information, and it can represent either a 0 or a 1. A bit is usually implemented using an electrical or optical signal, and it is used to store and transmit information in classical computers.

A qubit is the most basic unit of quantum information, and it can represent a 0, a 1, or any linear combination of these states. A qubit is usually implemented using a quantum system, such as an atom, a nucleus, or a photon, and it is used to store and transmit information in quantum computers.

One key difference between bits and qubits is that qubits can exist in a superposition of states, which means that they can represent multiple states simultaneously. Entanglement is another fundamental property of quantum systems that describes the correlations that exist between different quantum states. When two or more quantum systems are entangled, their states are correlated in such a way that measuring one system affects the other systems. These two properties allow quantum computers to perform certain types of computation much faster than classical computers, and it is one of the main reasons why quantum computers have the potential to revolutionize computing.

There are many types of quantum machine learning. However, this post will concentrate on the quantum kernel method. This method uses quantum computers to compute the kernel function in kernel-based machine learning algorithms, such as support vector machines (SVMs). The kernel function is a measure of similarity between pairs of data points. It maps the data into a higher-dimensional space where a linear boundary can separate it. Using a quantum computer to compute the kernel function makes it possible to perform this mapping much faster than with a classical computer, which can improve the performance of the machine-learning algorithm.

In the next few steps, we will walk through the python program for this project.

  1. We import basic python libraries:

2. We load the Wisconsin Breast Cancer Dataset:

3. We convert the sklearn dataset to the Pandas dataframe:

4. We assign independent variables and dependent (target) variable:

It is worth noting that the Wisconsin breast cancer dataset has many features (over 30), making it difficult to process and analyze on a quantum computer with a limited number of qubits. Therefore, to make the task more manageable, we will first use principal component analysis (PCA) to reduce the dimensionality of the dataset to just four variables.

5. We standardize features by removing the mean and scaling to unit variance, which is needed for PCA:

6. We reduce feature dimensionality to four with PCA:

7. We normalize the data with MinMaxScaler, which is needed for some Quantum algorithms:

8. We split the dataset into training and testing:

9. We fit the classical classifier SVC (Support Vector Classification) with four different classical kernels, so we will be able to compare classical kernels with quantum kernel results:

10. We encode our data as qubits by creating the quantum circuit with four qubits (one qubit for each feature):

Our feature map circuit

The quantum circuit is composed of qubits and quantum gates. Quantum gates, just like classical computer gates, change the state of the qubit. The Block sphere best represents the state of the qubit.

The Bloch sphere is a sphere with a radius of 1, with the qubit’s state represented as a point on the surface of the sphere. The north pole of the sphere represents the |0> state, and the south pole represents the |1> state.

The state of a qubit can be represented as a linear combination of the |0> and |1> states:

|ψ> = α|0> + β|1>

where α and β are complex numbers known as the amplitudes of the |0> and |1> states, respectively.

The state of a qubit can be visualized as a point on the Bloch sphere, with the position of the point determined by the amplitudes α and β. For example, if the qubit is in the |0> state, the point representing its state will be at the north pole of the sphere. If the qubit is in the |1> state, the point representing its state will be at the south pole. If the qubit is in a superposition state, the point representing its state will be somewhere else on the surface of the sphere.

The Bloch sphere is a useful tool for visualizing and understanding the state of a qubit, and it is widely used in quantum computing and quantum information theory.

Bloch sphere (wikipedia.com)

The three types of quantum gates seen on our circuit are (1) H-gate (Hadamard gate) is a type of gate used to put a qubit into a superposition state. If the qubit is in state |0>, the H gate will transform it to the state (|0> + |1>)/√2. If the qubit is in state |1>, the H gate will transform it to the state (|0> — |1>)/√2, (2) P gate (phase gate) is parametrized, that is, it needs a number (ϕ) to tell it exactly what to do. The P-gate performs a rotation of ϕ around the Z-axis direction, (3) The CNOT gate is a two-qubit gate that flips the state of the second qubit (the target qubit) if the state of the first qubit (the control qubit) is |1>. If the control qubit is in state |0>, the CNOT gate does nothing. The CNOT gate is a fundamental gate used to entangle two qubits and create quantum correlations between them.

Let’s get back to our code.

11. We create a backend for a quantum instance, either simulator or quantum computer:

Using QASM simulator as a backend

12. To use the actual IBM quantum computer as a backend, we must set up the IBM Quantum Experience account. After that, we can copy the authentication token we need for the initial connection. The info will be saved on our computer the first time we run this code.

13. Now we can load our IBM credentials and set up the IBM quantum computer as a backend for our instance:

14. We create a quantum kernel:

There are two ways we can use a quantum kernel with SVC. One is by providing the kernel as a callable function, and the second is by precomputing the kernel matrix.

15. Providing the kernel as a callable function method:

Output with the QASM simulator as a backend
Output with the IBM quantum computer as a backend

16. Precomputing the kernel matrix method:

The same results were obtained with both the QASM simulator and the IBM quantum computer.

Conclusion

Using the quantum kernel on the IBM quantum computer, we obtained the same results on the testing dataset (score 0.9) as using the rbf and linear kernels, but better results than using the poly kernel (score 0.80) and the sigmoid kernel (score 0.60).

In this series’s second part, we will use the variational quantum circuit for the Wisconsin Breast Cancer Dataset classification.

I hope you enjoyed reading this article.

All the best,
Andrew

--

--

Andrew A Borkowski
Andrew A Borkowski

Written by Andrew A Borkowski

Pathologist and Deep Learning Enthusiast

Responses (1)