Quantum Machine Learning: Quantum-Enhanced Support Vector Machines (QSVM) untuk Permasalahan Klasifikasi

Spread the love

SVM adalah salah satu algoritma yang sangat terkenal. Baca = https://en.wikipedia.org/wiki/Support_vector_machine

Metode ini masuk dalam kategori supervised learning, yang terdiri dari fase pelatihan (di mana kernel dihitung dan support vector diperoleh) dan fase uji atau klasifikasi (di mana data baru yang tidak berlabel diklasifikasikan menurut solusi yang ditemukan dalam fase pelatihan).

Quantum Machine Learning (Quantum-Enhanced Machine Learning) adalah topik penelitian baru, yang berarti algoritma Machine Learning untuk analisis data klasik yang dijalankan pada komputer kuantum. Selengkapnya baca = https://en.wikipedia.org/wiki/Quantum_machine_learning

 

1. persiapan, buat jupyter notebook baru:

# butuh install cvxopt sebelum mencoba program ini:
# conda install -c conda-forge cvxopt

import matplotlib.pyplot as plt
import numpy as np
from qiskit import BasicAer
from qiskit.ml.datasets import *
from qiskit.aqua.utils import split_dataset_to_data_and_labels, map_label_to_class_name
from qiskit.aqua import QuantumInstance
from qiskit.aqua.algorithms import QSVM
from qiskit.aqua.components.feature_maps import SecondOrderExpansion
from qiskit.circuit.library import ZZFeatureMap

# setup aqua logging
import logging
from qiskit.aqua import set_qiskit_aqua_logging
set_qiskit_aqua_logging(logging.DEBUG) # choose INFO, DEBUG to see the log

from qiskit import IBMQ
provider = IBMQ.load_account()

2. Pertama-tama kita siapkan dataset untuk training, testing and final prediction.
Setelah dataset siap kita inisialisasi input yang diperlukan untuk algoritma:

NB: Qiskit sudah menyediakan beberapa dataset seperti: ad hoc dataset, sample ad hoc data, breast cancer dataset, digits dataset, gaussian dataset, iris dataset, dan wine dataset.
Selengkapnya baca = https://qiskit.org/documentation/apidoc/qiskit.ml.datasets.html

feature_dim=2 # we support feature_dim 2 or 3
sample_Total, training_input, test_input, class_labels = ad_hoc_data(
training_size=20,
test_size=10,
n=feature_dim,
gap=0.3,
plot_data=True
)
# ditampilkan scatter plot data training

extra_test_data = sample_ad_hoc_data(sample_Total, 10, n=feature_dim)
datapoints, class_to_label = split_dataset_to_data_and_labels(extra_test_data)
print(class_to_label)

3. Penjelasan isi variabel diatas =

### sample_Total =
[[1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], …]

### training_input =
{‘A’: array([[4.64955713, 4.58672527],
[1.88495559, 5.15221195],
[1.00530965, 1.94778745],
[4.46106157, 5.02654825],
[2.95309709, 0. ],
[4.71238898, 4.52389342],
[0.06283185, 3.51858377],
[2.82743339, 0.25132741],
[4.71238898, 4.52389342],
[3.76991118, 1.75929189],
[4.90088454, 5.2150438 ],
[4.77522083, 4.52389342],
[1.25663706, 5.0893801 ],
[3.95840674, 6.03185789],
[2.0106193 , 3.70707933],
[0.81681409, 5.65486678],
[1.75929189, 4.08407045],
[4.0212386 , 2.136283 ],
[0.31415927, 3.39292007],
[3.45575192, 5.71769863]]),
‘B’: array([[5.84336234, 2.26194671],
[5.27787566, 5.34070751],
[0.56548668, 4.0212386 ],
[0. , 3.76991118],
[4.27256601, 3.14159265],
[4.1469023 , 1.00530965],
[1.31946891, 4.39822972],
[3.70707933, 4.08407045],
[0.37699112, 1.38230077],
[5.65486678, 1.25663706],
[3.76991118, 0.69115038],
[5.0893801 , 5.59203492],
[3.76991118, 5.40353936],
[1.75929189, 5.78053048],
[4.39822972, 5.52920307],
[0.81681409, 5.96902604],
[5.52920307, 2.38761042],
[4.71238898, 3.26725636],
[4.96371639, 3.01592895],
[5.40353936, 4.39822972]])}

### test_input =
{‘A’: array([[0.56548668, 5.90619419],
[2.0106193 , 1.13097336],
[5.02654825, 1.94778745],
[0.12566371, 2.89026524],
[3.70707933, 1.75929189],
[0. , 5.84336234],
[1.69646003, 3.95840674],
[3.83274304, 2.32477856],
[4.77522083, 1.69646003],
[4.77522083, 4.39822972]]),
‘B’: array([[1.0681415 , 0.87964594],
[4.39822972, 3.01592895],
[1.38230077, 3.01592895],
[0.43982297, 4.90088454],
[1.19380521, 0.25132741],
[3.76991118, 2.63893783],
[2.70176968, 5.2150438 ],
[0.62831853, 3.01592895],
[4.0212386 , 0.9424778 ],
[2.32477856, 4.71238898]])}

### class_labels =
[‘A’, ‘B’]

### extra_test_data =
{‘A’: array([[1.00530965, 4.90088454],
[2.38761042, 4.1469023 ],
[5.0893801 , 5.0893801 ],
[6.09468975, 3.83274304],
[4.83805269, 5.34070751],
[5.2150438 , 0.37699112],
[4.96371639, 1.69646003],
[3.95840674, 1.88495559],
[0.37699112, 3.33008821],
[0.56548668, 2.136283 ]]),
‘B’: array([[1.69646003, 3.33008821],
[4.0212386 , 4.90088454],
[2.63893783, 2.136283 ],
[5.59203492, 4.27256601],
[2.95309709, 2.26194671],
[4.0212386 , 3.95840674],
[4.20973416, 1.31946891],
[2.45044227, 0.43982297],
[5.34070751, 3.14159265],
[2.51327412, 3.45575192]])}

### datapoints =
[array([[1.00530965, 4.90088454],
[2.38761042, 4.1469023 ],
[5.0893801 , 5.0893801 ],
[6.09468975, 3.83274304],
[4.83805269, 5.34070751],
[5.2150438 , 0.37699112],
[4.96371639, 1.69646003],
[3.95840674, 1.88495559],
[0.37699112, 3.33008821],
[0.56548668, 2.136283 ],
[1.69646003, 3.33008821],
[4.0212386 , 4.90088454],
[2.63893783, 2.136283 ],
[5.59203492, 4.27256601],
[2.95309709, 2.26194671],
[4.0212386 , 3.95840674],
[4.20973416, 1.31946891],
[2.45044227, 0.43982297],
[5.34070751, 3.14159265],
[2.51327412, 3.45575192]]),
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])]

### class_to_label =
{‘A’: 0, ‘B’: 1}

4. Dengan semuanya diatur, kita sekarang dapat menjalankan algoritma.
Untuk pengujian, hasilnya mencakup detail dan rasio keberhasilan.
Untuk prediksi, hasilnya termasuk label yang diprediksi.

NB: Qiskit Aqua sudah menyediakan beberapa algoritma ML ( qiskit.aqua.algorithms)=

1) Classifiers (Algoritma untuk klasifikasi data):
– QSVM: Quantum SVM algorithm.
– VQC: The Variational Quantum Classifier algorithm.
– SklearnSVM: The Sklearn SVM algorithm (classical).

2) Distribution Learners:
– QGAN: The Quantum Generative Adversarial Network algorithm.
dll

Selengkapnya, baca = https://qiskit.org/documentation/apidoc/qiskit.aqua.algorithms.html

Lebih detail tentang QSVM, baca = https://qiskit.org/documentation/stubs/qiskit.aqua.algorithms.QSVM.html

seed = 10598

feature_map = ZZFeatureMap(feature_dimension=feature_dim, entanglement=’linear’)
qsvm = QSVM(feature_map, training_input, test_input, datapoints[0])
# Parameter feature_map = Feature map module, used to transform data
# Parameter training_dataset = Training dataset.
# Parameter test_dataset = Testing dataset.
# Parameter datapoints = Prediction dataset.

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

result = qsvm.run(quantum_instance)
# QSVM.run([quantum_instance]) = Execute the algorithm with selected backend.

print(“testing success ratio: {}”.format(result[‘testing_accuracy’]))
print(“preduction of datapoints:”)
print(“ground truth: {}”.format(map_label_to_class_name(datapoints[1], qsvm.label_to_class)))
print(“prediction: {}”.format(result[‘predicted_classes’]))

5. Penjelasan mengenai isi variable results diatas =

Akurasi = 100% (testing success ratio: 1.0)

Karena hasil prediksi QSVM sesuai dengan ground truth / kunci jawabannya =
preduction of datapoints:
ground truth: [‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’]
prediction: [‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’]

result =
{
‘kernel_matrix_training’: array([[1.00000000e+00, 2.79296875e-01, 4.97070312e-01, …,
3.69140625e-01, 9.76562500e-04, 5.15625000e-01],
[2.79296875e-01, 1.00000000e+00, 3.90625000e-03, …,
9.37500000e-02, 1.30859375e-01, 3.43750000e-01],
[4.97070312e-01, 3.90625000e-03, 1.00000000e+00, …,
2.07031250e-01, 4.88281250e-02, 1.37695312e-01],
…,
[3.69140625e-01, 9.37500000e-02, 2.07031250e-01, …,
1.00000000e+00, 1.24023438e-01, 5.77148438e-01],
[9.76562500e-04, 1.30859375e-01, 4.88281250e-02, …,
1.24023438e-01, 1.00000000e+00, 2.14843750e-02],
[5.15625000e-01, 3.43750000e-01, 1.37695312e-01, …,
5.77148438e-01, 2.14843750e-02, 1.00000000e+00]]),

‘svm’: {
‘alphas’: array([2.95109901, 0.35275075, 2.25687796, 2.50482981, 0.21687916,
0.44354806, 0.471835 , 0.96616252, 5.10496645, 0.90039991,
0.76381619, 1.4688486 ]),

‘bias’: array([0.07975191]),

‘support_vectors’: array([[2.26194671, 1.00530965],
[4.52389342, 3.70707933],
[3.45575192, 1.0681415 ],
[1.0681415 , 5.52920307],
[5.15221195, 1.25663706],
[5.0893801 , 3.51858377],
[4.71238898, 4.1469023 ],
[3.70707933, 6.22035345],
[3.89557489, 1.19380521],
[3.64424748, 5.15221195],
[1.82212374, 5.40353936],
[2.0106193 , 5.0893801 ]]),

‘yin’: array([-1., -1., -1., -1., -1., -1., -1., 1., 1., 1., 1., 1.])
},

‘kernel_matrix_testing’: array([[0.51367188, 0.29980469, 0.68554688, 0.75683594, 0.13183594,
0.03320312, 0.66308594, 0.15429688, 0.67285156, 0.04785156,
0.22949219, 0.17871094],… ]),

‘testing_accuracy’: 1.0,

‘test_success_ratio’: 1.0,

‘predicted_labels’: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),

‘predicted_classes’: [‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’, ‘B’]
}

6. tampilkan kernel matrix:

print(“kernel matrix during the training:”)
kernel_matrix = result[‘kernel_matrix_training’]
img = plt.imshow(np.asmatrix(kernel_matrix),interpolation=’nearest’,origin=’upper’,cmap=’bone_r’)
plt.show()

7. berikutnya, coba ganti dataset dengan Breast Cancer dataset (breast_cancer), kemudian amati hasilnya


sumber = https://quantum-computing.ibm.com/jupyter/tutorial/advanced/aqua/machine_learning/qsvm_classification.ipynb

Github = https://github.com/keamanansiber/qiskit/tree/master/3QuantumMachineLearning

Tinggalkan Balasan

This site uses Akismet to reduce spam. Learn how your comment data is processed.