top of page

AwesomeOps: Quantum Machine Learning part 4 of 4

Quick Links

AwesomeOps: Quantum Machine Learning Part 1 of 4

AwesomeOps: Quantum Machine Learning Part 2 of 4

AwesomeOps: Quantum Machine Learning Part 3 of 4

AwesomeOps: Quantum Machine Learning Part 4 of 4

Now, let's get into doing some actual Quantum Machine Learning!

In the following demo, I am going to use a simulator. I will include the code that can utilize an actual IBM quantum computer, but for the sake of speed I will use a simulator. This is not to imply that the quantum compute resources are slow, currently there are long queues to use IBM's quantum computers. For example, to run a 5-minute job, you may have to wait one hour just to execute your process. In some cases, I have seen the queue time be as high as 10 hours. IBM has kindly let the public access some of its Quantum Compute resources free of charge. This has led to many people using these free resources, thus the long queue times. Keep in mind, this is with a free IBM account. I am sure if you paid, you will have access to more resources with little to no wait time.

Now let's get into it. First thing, if you want to run this code on an actual IBM Quantum Machine, then you will need to sign up to IBM Quantum Platform: Once you sign up, you will need to grab the API Token that will be in your dashboard.

I am using python version 3.11.5 in this demo. The results of this demo may vary if you use a different version.

There will be several packages that will have to be installed. See below:

pip install scikit-learn qiskit qiskit-machine-learning qiskit-ibm-provider qiskit-aer

I will explain what the code is doing further below, but for now here is the code.

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from qiskit import Aer
from qiskit.utils import QuantumInstance
from qiskit.circuit.library import ZZFeatureMap
from qiskit_machine_learning.kernels import QuantumKernel
from qiskit_machine_learning.algorithms import QSVC
from qiskit_ibm_provider import IBMProvider

def main():
    # Load IBM Quantum Platform account using qiskit_ibm_provider
    provider = IBMProvider('IBM_API_KEY')

    random_seed = 69543
    # Load Breast Cancer dataset and preprocess
    cancer = datasets.load_breast_cancer()
    # Select first two features for simplicity
    X =[:, :2]  
    y =

    # Standardize the data
    scaler = StandardScaler()
    X = scaler.fit_transform(X)

    # Split dataset into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=random_seed, shuffle=True)

    # Choose one of the 3 options below:
    # Option 1: Below is an actual IBM Quantum device. You will need and API key set for this.
    # backend = provider.get_backend('ibm_perth')
    # Option 2: Below is a IBM simulator. You will need and API key set for this.
    # backend = provider.get_backend('ibmq_qasm_simulator')
    # Option 3: Below is a simulator that runs on your local machine
    backend = Aer.get_backend('qasm_simulator')

    # Create QuantumInstance
    quantum_instance = QuantumInstance(backend, shots=1024, seed_simulator=random_seed, seed_transpiler=random_seed)

    # Quantum Feature Map and Kernel
    feature_map = ZZFeatureMap(feature_dimension=2)
    kernel = QuantumKernel(feature_map=feature_map, quantum_instance=quantum_instance)

    # QSVC
    qsvc = QSVC(quantum_kernel=kernel), y_train)

    # Predict and score
    score = qsvc.score(X_test, y_test)
    print(f"QSVC Test score: {score}")

if __name__ == "__main__":

Let's break down what this Python code is doing, step by step. The script is designed to implement a Quantum Support Vector Classifier (QSVC) using Qiskit, a quantum computing framework. It applies this to a classic machine learning problem: classifying data from the Breast Cancer dataset.

  1. The script begins by importing necessary libraries. It includes standard machine learning libraries from sklearn and quantum computing libraries from qiskit and the qiskit_machine_learning package. There's also an import from qiskit_ibm_provider, which is used for accessing IBM's quantum computers.

  2. Initializes an IBM Quantum Experience account using an API key through IBMProvider. This allows the script to access IBM's quantum computers or simulators. Again, this is an option if you choose to run this on IBM's Quantum compute resources. For speed, I decided to run a quantum simulator locally. Please keep in mind that if you decide to use one of IBM's resources that it will take significantly longer do to the free resource popularity that IBM offers.

  3. The Breast Cancer dataset, a common dataset for classification tasks, is loaded using datasets.load_breast_cancer().

  4. Feature Selection, only the first two features of the dataset are used for simplicity. Each additional feature requires an additional quibit. Since we are using a simulator on our local machine, we are just working with 2 so that we can see the results quicker.

  5. Standardization: The selected features are standardized using StandardScaler, which is a common preprocessing step to make the data more suitable for machine learning algorithms.

  6. Splitting the Data: The selected features are standardized using StandardScaler, which is a common preprocessing step to make the data more suitable for machine learning algorithms.

  7. Quantum Backend Selection:

  8. Option 1: IBM Quantum Device, which requires an IBM Quantum Platform account and API key.

  9. Option 2: IBM Simulator, a simulator provided by IBM. This also requires and account and API key.

  10. Option 3: Local Simulator, a simulator (qasm_simulator) from Aer, which runs on the local machine. In this script, the local simulator is selected.

  11. A QuantumInstance is created with the chosen backend. It's configured with a number of shots (repetitions of each circuit evaluation) and seeds for the simulator and transpiler for consistency in results.

  12. A quantum feature map, ZZFeatureMap, is used to encode classical data into a quantum state. This particular feature map is commonly used for binary classification tasks.

  13. A QuantumKernel is created with the ZZFeatureMap and the quantum instance. This kernel is a critical part of applying quantum computing to machine learning, as it defines how data points are compared in the quantum space.

  14. The script initializes a QSVC, a quantum-enhanced version of the classical Support Vector Machine, using the previously defined quantum kernel.

  15. The QSVC model is trained (fit) using the training data.

  16. The model's performance is evaluated using the test set, and the accuracy score is printed out.

My results (your results may be different)

I received an output of 0.6228070175438597

What does that mean? The output 0.6228070175438597 is the accuracy score of the Quantum Support Vector Classifier (QSVC) model when tested on the test dataset. This means that the model predicts that a person has or does not have breast cancer 62% of the time. Keep in mind this is just training on the first two features in the dataset. Another thing to keep in mind that it is 62% accurate based on this dataset. If more data was added to this dataset, that prediction accuracy may change. Lastly, this is just from running this once. Model tuning and other parameter updates such as the number of shots and the number of features trained on can change the accuracy as well.


If you followed this 4 part series, you should have learned about quantum fundamentals, history, limitations, what the future holds for Quantum Machine learning, and how to utilize existing tools to run your first Quantum Machine Learning script! As we blend the intricacies of quantum computing with the evolving landscape of artificial intelligence, we are not just navigating a new frontier in technology; we are redefining the boundaries of human potential. Each step into this unknown is a leap towards understanding the profound mysteries of our universe, reminding us that the quest for knowledge is an endless journey, one that illuminates the path to our future as much as it honors the questions that have driven us from the beginning.

Quantum computing at Mentat

At Mentat, we are conducting research to discover ways to use quantum computers to train our AI models more efficiently. The goal is to train models in a matter of a few minutes, rather than hours or days. This allows us to train/update models that clients need in a reasonable amount of time.

11 views0 comments
bottom of page