import matplotlib.pyplot as plt
from qiskit import Aer
from qiskit.algorithms.state_fidelities import ComputeUncompute
from qiskit.circuit.library import ZZFeatureMap
from qiskit.primitives import Sampler
from qiskit.utils import algorithm_globals, QuantumInstance
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler

from qiskit_machine_learning.algorithms import QSVC
from qiskit_machine_learning.kernels import QuantumKernel, FidelityQuantumKernel

algorithm_globals.random_seed = 123456
features, labels = make_classification(
    n_samples=20, 
    n_features=2, 
    n_redundant=0, 
    random_state=algorithm_globals.random_seed
)

features = MinMaxScaler().fit_transform(features)

plt.scatter(features[:, 0], features[:, 1], c=labels)

train_features, test_feature, train_labels, test_labels = train_test_split(
    features, labels, 
    train_size=15, 
    random_state=algorithm_globals.random_seed
)

qi_sv = QuantumInstance(
    Aer.get_backend("statevector_simulator"),
    seed_simulator=algorithm_globals.random_seed,
    seed_transpiler=algorithm_globals.random_seed,
)

qk_old = QuantumKernel(feature_map=ZZFeatureMap(2), quantum_instance=qi_sv)
qsvc = QSVC(quantum_kernel=qk_old)

qsvc.fit(train_features, train_labels)

qsvc.score(test_feature, test_labels)

# these are optional
sampler = Sampler()
fidelity = ComputeUncompute(sampler)

qk_new = FidelityQuantumKernel(feature_map=ZZFeatureMap(2), fidelity=fidelity)

qsvc = QSVC(quantum_kernel=qk_new)

qsvc.fit(train_features, train_labels)

qsvc.score(test_feature, test_labels)

import qiskit.tools.jupyter

%qiskit_copyright