Algoritma Supervised Learning Variational Quantum Classifier (VQC)

Spread the love

Melanjutkan artikel sebelumnya tentang algoritma Quantum-Enhanced Support Vector Machines (QSVM) yang menggunakan pendekatan berbasis kernel, kali ini kita coba pakai pendekatan variational method.

Lebih lanjut tentang VQC baca di = https://arxiv.org/pdf/1804.11326.pdf

1. buat new jupyter notebook:

from qiskit.ml.datasets import ad_hoc_data
from qiskit import BasicAer
from qiskit.aqua import QuantumInstance
from qiskit.aqua.algorithms import VQC
from qiskit.aqua.components.optimizers import SPSA
from qiskit.circuit.library import TwoLocal, ZZFeatureMap
from qiskit.aqua.utils import split_dataset_to_data_and_labels, map_label_to_class_name

2. Siapkan dataset, untuk training, testing dan prediction:

feature_dim = 2 # dimension of each data point
training_dataset_size = 20
testing_dataset_size = 10
random_seed = 10598
shots = 1024
sample_Total, training_input, test_input, class_labels = ad_hoc_data(
training_size=training_dataset_size,
test_size=testing_dataset_size,
n=feature_dim, gap=0.3, plot_data=True
)
datapoints, class_to_label = split_dataset_to_data_and_labels(test_input)
print(class_to_label)

3. Kita buat instance vqc, dan komponen-komponen nya=
– optimizer instance (class SPSA).
– feature map instance (class ZZFeatureMap).
– varitional form instance (class TwoLocal).
– vqc instance (class VQC).

backend = BasicAer.get_backend(‘qasm_simulator’)
optimizer = SPSA(max_trials=100, c0=4.0, skip_calibration=True)
optimizer.set_options(save_steps=1)
feature_map = ZZFeatureMap(feature_dimension=feature_dim, reps=2)
var_form = TwoLocal(feature_dim, [‘ry’, ‘rz’], ‘cz’, reps=3)
vqc = VQC(optimizer, feature_map, var_form, training_input, test_input)
quantum_instance = QuantumInstance(backend, shots=shots, seed_simulator=random_seed, seed_transpiler=random_seed)

4. RUN

result = vqc.run(quantum_instance)
print(“testing success ratio: “, result[‘testing_accuracy’])

5. Kita coba gunakan model VQC yang sudah dilatih, untuk melakukan prediksi label dari input data yang baru:

predicted_probs, predicted_labels = vqc.predict(datapoints[0])
predicted_classes = map_label_to_class_name(predicted_labels, vqc.label_to_class)
print(“prediction: {}”.format(predicted_labels))

6. Untuk mencoba menggunakan dataset breast_cancer, kita ubah sedikit program pada nomer 2, dari ad_hoc_data menjadi breast_cancer

Sumber = https://github.com/qiskit-community/qiskit-community-tutorials/blob/master/machine_learning/vqc.ipynb

Kode program di GitHub = https://github.com/keamanansiber/qiskit/blob/master/3QuantumMachineLearning/VQC_VariationalQuantumClassifier.ipynb

Tinggalkan Balasan

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