Skip to content

Commit

Permalink
Jen update qka (Qiskit#43)
Browse files Browse the repository at this point in the history
* renamed feature map to FeatureMap

* make initial layout optional

* updated program doc json for QKA

* fixed error in json
  • Loading branch information
jenglick authored and GitHub Enterprise committed May 5, 2021
1 parent 3b8bdcf commit 8fd6a30
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 28 deletions.
2 changes: 1 addition & 1 deletion qka/featuremaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from qiskit import QuantumCircuit, QuantumRegister


class FeatureMapACME:
class FeatureMap:
"""Mapping data with the feature map.
"""

Expand Down
10 changes: 3 additions & 7 deletions qka/kernel_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@
class KernelMatrix:
"""Build the kernel matrix from a quantum feature map."""

def __init__(self, feature_map, backend, initial_layout):
def __init__(self, feature_map, backend, initial_layout=None):
"""
Args:
feature_map (int): the feature map object
backend (Backend): the backend instance
initial layout: FINISH ME
initial layout (list or dict): initial position of virtual qubits on the physical qubits of the quantum device
"""

self._feature_map = feature_map
self._feature_map_circuit = self._feature_map.construct_circuit # the feature map circuit
self._backend = backend

if initial_layout is None:
raise ValueError('Provide an initial layout matching the problem graph.')
else:
self._initial_layout = initial_layout
self._initial_layout = initial_layout

self.results = {} # store the results object (program_data)

Expand Down
4 changes: 2 additions & 2 deletions qka/qka.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
class QKA:
"""The quantum kernel alignment algorithm."""

def __init__(self, feature_map, backend, initial_layout, verbose=True):
def __init__(self, feature_map, backend, initial_layout=None, verbose=True):
"""Constructor.
Args:
feature_map (partial obj): the quantum feature map object
backend (Backend): the backend instance
initial_layout: FINISH ME
initial_layout (list or dict): initial position of virtual qubits on the physical qubits of the quantum device
verbose (bool): print output during course of algorithm
"""

Expand Down
6 changes: 3 additions & 3 deletions qka/test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
from qiskit import Aer
from featuremaps import FeatureMapACME
from featuremaps import FeatureMap
from kernel_matrix import KernelMatrix
from qka import QKA
from sklearn import metrics
Expand Down Expand Up @@ -40,7 +40,7 @@
maxiters = 11

entangler_map=[[0,2],[3,4],[2,5],[1,4],[2,3],[4,6]]
initial_layout=[10,11,12,13,14,15,16]
initial_layout=None # [10,11,12,13,14,15,16]

x_train = dat[:2*num_train, :-1]
y_train = dat[:2*num_train, -1]
Expand Down Expand Up @@ -89,7 +89,7 @@

# Define the feature map and its initial parameters:
initial_kernel_parameters = [0.1] # np.pi/2 should yield 100% test accuracy
fm = FeatureMapACME(feature_dimension=num_features, entangler_map=entangler_map)
fm = FeatureMap(feature_dimension=num_features, entangler_map=entangler_map)
km = KernelMatrix(feature_map=fm, backend=bk, initial_layout=initial_layout)

# Train and test the initial kernel:
Expand Down
9 changes: 5 additions & 4 deletions runtime/program_doc.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
[
{ "name": "Quantum-Kernel-Alignment",
{ "name": "quantum-kernel-alignment",
"description": "Quantum kernel alignment algorithm that learns, on a given dataset, a quantum kernel maximizing the SVM classification margin.",
"id": "Quantum-Kernel-Alignment",
"id": "quantum-kernel-alignment",
"parameters": [
{"name": "feature_map", "description": "An instance of FeatureMapACME in dictionary format used to map classical data into a quantum state space.", "type": "dict", "required": true},
{"name": "feature_map", "description": "An instance of FeatureMap in dictionary format used to map classical data into a quantum state space.", "type": "dict", "required": true},
{"name": "data", "description": "NxD array of training data, where N is the number of samples and D is the feature dimension.", "type": "numpy.ndarray", "required": true},
{"name": "labels", "description": "Nx1 array of +/-1 labels of the N training samples.", "type": "numpy.ndarray", "required": true},
{"name": "initial_kernel_parameters", "description": "Initial parameters of the quantum kernel. If not specified, an array of randomly generated numbers is used.", "type": "numpy.ndarray", "required": false},
{"name": "maxiters", "description": "Number of SPSA optimization steps. Default is 10.", "type": "int", "required": false},
{"name": "C", "description": "Penalty parameter for the soft-margin support vector machine. Default is 1.", "type": "float", "required": false}
{"name": "C", "description": "Penalty parameter for the soft-margin support vector machine. Default is 1.", "type": "float", "required": false},
{"name": "initial_layout", "description": "Initial position of virtual qubits on the physical qubits of the quantum device. Default is None.", "type": "list or dict", "required": false}
],
"return_values": [
{"name": "aligned_kernel_parameters", "description": "The optimized kernel parameters found from quantum kernel alignment.", "type": "numpy.ndarray"},
Expand Down
18 changes: 7 additions & 11 deletions runtime/qka_runtime_outer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import sys


class FeatureMapACME:
class FeatureMap:
"""Mapping data with the feature map.
"""

Expand Down Expand Up @@ -102,22 +102,18 @@ def from_json(cls, feature_dimension, entangler_map=None):
class KernelMatrix:
"""Build the kernel matrix from a quantum feature map."""

def __init__(self, feature_map, backend, initial_layout):
def __init__(self, feature_map, backend, initial_layout=None):
"""
Args:
feature_map: the feature map object
backend (Backend): the backend instance
initial_layout: FINISH ME
initial_layout (list or dict): initial position of virtual qubits on the physical qubits of the quantum device
"""

self._feature_map = feature_map
self._feature_map_circuit = self._feature_map.construct_circuit
self._backend = backend

if initial_layout is None:
raise ValueError('Provide an initial layout matching the problem graph.')
else:
self._initial_layout = initial_layout
self._initial_layout = initial_layout

self.results = {}

Expand Down Expand Up @@ -205,13 +201,13 @@ def construct_kernel_matrix(self, x1_vec, x2_vec, parameters=None):
class QKA:
"""The quantum kernel alignment algorithm."""

def __init__(self, feature_map, backend, initial_layout, user_messenger=None):
def __init__(self, feature_map, backend, initial_layout=None, user_messenger=None):
"""Constructor.
Args:
feature_map (partial obj): the quantum feature map object
backend (Backend): the backend instance
initial_layout: FINISH ME
initial_layout (list or dict): initial position of virtual qubits on the physical qubits of the quantum device
user_messenger (UserMessenger): used to publish interim results.
"""

Expand Down Expand Up @@ -415,7 +411,7 @@ def main(backend, user_messenger, **kwargs):

# Reconstruct the feature map object.
feature_map = kwargs.get('feature_map')
fm = FeatureMapACME.from_json(**feature_map)
fm = FeatureMap.from_json(**feature_map)

data = kwargs.get('data')
labels = kwargs.get('labels')
Expand Down

0 comments on commit 8fd6a30

Please sign in to comment.