Interested in learning how to program quantum computers? Then check out our Qiskit textbook Introduction to Quantum Computing with Qiskit.
Introduction
In this tutorial we will explore how to implement a Quantum Support Vector Machine (QSVM) machine learning method on IBM’s Quantum computers using qiskit.
What is a Support Vector Machine?
A support vector machine is a supervised machine learning method that is trained using a dataset and will predict if a particular observation is in a certain class based upon what it has been trained on. It is similar to a linear classifier in that it uses a hyperplane to separate classes.
However along with the hyperplane support vectors are used. These are essentially data points that are used to maximise the margin and thus the distance between the classes.
However the Quantum Support Vector Machine is different in that it uses a feature map to map data points to a quantum circuit.
Implementation
First we will need to specify the backend device that we will send the job too.
Note: For this tutorial you will need an API token which you can get by registering here: https://quantum-computing.ibm.com/
Here we are specify the ibmq_qasm_simulator which is just a classical simulator however you can find which quantum devices are running using:
To implement the QSVM we first need to have a training dataset. In this tutorial we are using a gene expression dataset from the RNA-Seq Nexus from Smokers and Non-smokers and use the gene expression of CDKN2A as the feature.
With this we can predict if the observation is from a non-smoker or a smoker based upon the gene expression of CDKN2A.
The easiest way to implement the dataset in through numpy arrays:
Where A is the numpy array for smokers and B is the array for Non-smokers.
Next we implement a testing dataset. This will be used to test the accuracy of the QSVM model. It looks exactly the same as the training dataset.
Next we specify the number of qubits to be used. Rule of thumb is N qubits for N features. Since we are using 1 feature we are using 1 qubit.
Then we specify the feature map. This maps the data with second order expansion by entangling qubits. Since we are only using 1 feature and thus 1 qubit entanglement is not required. However for more features it will be.
Thus in the generated quantum circuit you will see that it consists of only 1 qubit and some unitary gates.
In the code above the depth just means the number of times the circuit will repeat. This is normally defaulted to 2. entanglement = ‘full’ means all qubits will be entangled with each other.
Next we create the QSVM with the following code:
Then we specify the quantum instance:
This is just a way to specify the backend as well as the number of shots we want the device to run. skip_qobj_validation=False is used to stop any warnings displaying in the output.
Next we run the QSVM to get the accuracy using the following code. This is where it is sent as a job to the quantum device.
If we print the result then we will get the accuracy based upon its performance from the testing dataset.
However if you want to use the model to predict unlabelled data then you can use the predict function as below: