Source code for skqulacs.qsvm.qsvc

from typing import List

import numpy as np
from numpy.typing import NDArray
from qulacs import QuantumState
from qulacs.state import inner_product
from sklearn import svm

from skqulacs.circuit import LearningCircuit


[docs]class QSVC: """class to solve classfication problems with support vector machine with quantum kernel""" def __init__(self, circuit: LearningCircuit) -> None: """ :param circuit: circuit to generate quantum feature """ self.svc = svm.SVC(kernel="precomputed") self.circuit = circuit self.data_states: List[QuantumState] = [] self.n_qubit: int = 0
[docs] def fit(self, x: NDArray[np.float_], y: NDArray[np.int_]): """ train the machine. :param x: training inputs :param y: training labels """ self.n_qubit = len(x[0]) kar = np.zeros((len(x), len(x))) # Compute UΦx to get kernel of `x` and `y`. for i in range(len(x)): self.data_states.append(self.circuit.run(x[i])) for i in range(len(x)): for j in range(len(x)): kar[i][j] = ( abs(inner_product(self.data_states[i], self.data_states[j])) ** 2 ) self.svc.fit(kar, y)
[docs] def predict(self, xs: NDArray[np.float_]) -> NDArray[np.float_]: """ predict labels of given data :param xs: inputs to predict labels :return: List[float], predicted labels """ kar = np.zeros((len(xs), len(self.data_states))) for i in range(len(xs)): x_qc = self.circuit.run(xs[i]) for j in range(len(self.data_states)): kar[i][j] = abs(inner_product(x_qc, self.data_states[j])) ** 2 predicted: NDArray[np.float_] = self.svc.predict(kar) return predicted