Skip to main content

Quantum Kernel Method

The Rigetti Quantum Kernel Method (QKM) is a new Quantum Machine Learning (QML) application specifically for Rigetti quantum computers and are designed to advance the development of applications related to classification and regression problems.

Rigetti's Quantum Kernel Method is designed to assess similarities between points in a data set, which may be valuable for usage in a classification or regression model. By assessing similarities in the exponentially larger space afforded by the quantum processing unit, the output of this method could potentially be used in anomaly detection.

Overview

​This page will describe how to run a QML application utilizing the Quantum Kernel Method

Prerequisites

​The user needs a strangeworks account and billing setup prior to any computations.

Installation

To get started, make sure you have Python 3.10 or 3.11 (installation) and are familiar with setting up and using virtual environments.

Install packages using pip: ​

pip install -U pip && pip install strangeworks-rigetti

​ Import package into python: ​

import strangeworks
from strangeworks.rigetti import get_qc

Authentication

Any issues authenticating, you may need to add a rigetti resource to your account through the Platform, https://portal.strangeworks.com

When running various computations one will need their api key and a resource id that you can get once you add a resource as stated above​

Usage

​ Before running:

  1. Set up your environment and install strangeworks-rigetti and strangeworks
  2. In the portal, Activate Strangeworks Rigetti Service to create a Resource and get the resource id and replace it with resource-id below
  3. Replace api_key below with your key from the Portal homepage
  4. Use examples below to utilize the various functionality. See Examples section below for an example script.

Authenticate with strangeworks python sdk using users API token:

import strangeworks as sw
sw.authenticate(api_key)

​ Get a quantum computing resource using the rigetti sdk extension by supplying a quantum computing backend name, the resource slug and a qvm setting

qc = get_qc(
"WavefunctionSimulator", resource_slug=resource_id, as_qvm=True
)
# as_qvm=False will produce a billable transaction using real hardware
# Can also use Rigetti backend like "Ankaa-2"

Examples

Here is an example on how to run a simple classification problem using the Quantum Kernel Method.

import os

from data import load_mtcars

import strangeworks as sw
from strangeworks.rigetti import get_qc

api_key = os.getenv("SW_API_KEY")
resource_id = os.getenv("SW_RESOURCE_ID")

# Standard machine learning training and test data
X_train, X_test, y_train, y_test = load_data()

data = {
"resource": "WavefunctionSimulator", # or a Rigetti backend like "Ankaa-2"
"num_shots": 1,
"X_train": X_train.tolist(), # expected as python list
"X_test": X_test.tolist(), # expected as python list
"y_train": y_train.tolist(), # expected as python list
"y_test": y_test.tolist(), # expected as python list
}

sw.authenticate(api_key)

# as_qvm=True uses the quantum virtual machine
# as_qvm=False uses an actual Rigetti machine
# as_qvm=False will produce a billable transaction
qc = get_qc("WavefunctionSimulator", resource_slug=resource_id, as_qvm=True)

res = qc.run(data)

print(res)

A Classification report will be printed out that looks like:

Classification Report:
precision recall f1-score support

0 1.00 0.86 0.92 7
1 0.75 1.00 0.86 3

accuracy 0.90 10
macro avg 0.88 0.93 0.89 10
weighted avg 0.93 0.90 0.90 10

​🥳 Success! You may view your job in the portal.

😅 Something went wrong? Find us in Slack!