<jupyter_start><jupyter_text>Chapter 8 Code: quantum machine learning<jupyter_code># Implementing MLP using sklearn

import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

np.random.seed(42)

iris = load_digits()
X = iris.data
Y = iris.target

x_train, x_test, y_train, y_test = train_test_split(X,Y, test_size=0.2, random_state=42)

clf = MLPClassifier(alpha=1, max_iter=1000)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
print('Accuracy is:', accuracy_score(y_test, y_pred))
print('\nClassification Report is:\n', classification_report(y_test, y_pred))
print('\nConfusion Matrix is:\n', confusion_matrix(y_test, y_pred))
#implementing SVM using sklearn

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

np.random.seed(42)

iris = load_digits()
X = iris.data
Y = iris.target

x_train, x_test, y_train, y_test = train_test_split(X,Y, test_size=0.2, random_state=42)

clf = SVC(kernel='rbf', gamma = 0.0001, C=1e1)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
print('Accuracy is:', accuracy_score(y_test, y_pred))
print('\nClassification Report is:\n', classification_report(y_test, y_pred))
print('\nConfusion Matrix is:\n', confusion_matrix(y_test, y_pred))
#!pip install qiskit

#QSVM code 

#from qsvm_datasets import *

#Chapter 8 - quantum support vector machine

from qiskit import BasicAer
from qiskit.aqua import QuantumInstance, aqua_globals
from qiskit.aqua.algorithms import VQC, QSVM
from qiskit.aqua.components.multiclass_extensions import *
from qiskit.aqua.components.optimizers import COBYLA
from qiskit.aqua.components.feature_maps import RawFeatureVector
#from qiskit.aqua.algorithms.many_sample.qsvm._qsvm_estimator import _QSVM_Estimator
from qiskit.circuit.library import ZZFeatureMap, ZFeatureMap, PauliFeatureMap
from qiskit.ml.datasets import breast_cancer
from qiskit.circuit.library import TwoLocal

seed = 42
aqua_globals.random_seed = seed

feature_dim = 4  # dimension of each data point
_, training_input, test_input, _ = breast_cancer(training_size=12,
                                        test_size=4,
                                        n=feature_dim)
feature_map = ZZFeatureMap(feature_dimension=feature_dim, reps=2, entanglement='linear')
#feature_map = RawFeatureVector(feature_dimension=feature_dim)
qsvm = QSVM(feature_map, training_input, test_input)

backend = BasicAer.get_backend('qasm_simulator')
quantum_instance = QuantumInstance(backend, shots=1024, seed_simulator=seed, seed_transpiler=seed)

result = qsvm.run(quantum_instance)


print('Testing accuracy: {:0.2f}'.format(result['testing_accuracy']))
#!pip install qiskit
#Chapter 8 - variational quantum classifier 

from qiskit import BasicAer
from qiskit.aqua import QuantumInstance, aqua_globals
from qiskit.aqua.algorithms import VQC
from qiskit.aqua.components.optimizers import COBYLA
from qiskit.circuit.library import ZZFeatureMap, ZFeatureMap, PauliFeatureMap
from qiskit.aqua.components.feature_maps import RawFeatureVector
from qiskit.ml.datasets import breast_cancer
from qiskit.circuit.library import TwoLocal

seed = 42
aqua_globals.random_seed = seed


feature_dim = 4  # dimension of each data point
_, training_input, test_input, _ = breast_cancer(training_size=12,
                                        test_size=4,
                                        n=feature_dim)

#feature_map = ZZFeatureMap(feature_dimension=feature_dim, reps=2, entanglement='linear')
feature_map = RawFeatureVector(feature_dimension=feature_dim)
vqc = VQC(COBYLA(maxiter=1000),
          feature_map,
          TwoLocal(feature_map.num_qubits, ['ry', 'rz'], 'cz', reps=3),
          training_input,
          test_input)

backend = BasicAer.get_backend('qasm_simulator')
quantum_instance = QuantumInstance(backend, shots=1024, seed_simulator=seed, seed_transpiler=seed)

result = vqc.run(quantum_instance)

print('Testing accuracy: {:0.2f}'.format(result['testing_accuracy']))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08919540229885058, 'fraction_numerical': 0.013333333333333334, 'mean_word_length': 5.4174041297935105, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '204818', 'license': 'CC0-1.0', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1418, 'n_words': 320, 'path': 'ch-8.ipynb', 'repo_name': 'makhamisasenekane/Hands-on-QIP', 'size': '4350'}