Quantum SVMs [Help Needed for implementation πŸ™]

Hello! I am doing some experiments related to Quantum SVMs of 3 types:
I have implemented the first one QK-SVM on my own (with help of some resources). I would greatly appreciate some help :smiling_face_with_tear: :pray:t2: for implementation of the other two types (QV-SVM & QVK-SVM)

I am also attaching circuit images for reference.

1)Quantum Kernel SVM

2)Quantum Variational SVM

3)Quantum Variational Kernel SVM

Here’s the code for the QKSVM

n_qubits= 8

dev = qml.device("lightning.qubit",wires = n_qubits)

def qksvm_kernel_cirq(a,b):
  qml.AngleEmbedding(a,wires = range(n_qubits))
  qml.adjoint(qml.AngleEmbedding(b,wires = range(n_qubits)))
  return qml.probs(wires = range(n_qubits))

def quantum_kernel_pca(A,B):
  return np.array([[qksvm_kernel_cirq(a,b)[0] for b in B] for a in A])

svm = SVC(kernel = quantum_kernel_pca).fit(xs_tr, y_tr)

from sklearn.metrics import accuracy_score
print(accuracy_score(svm.predict(xs_test), y_test))

This is the accuracy


And, finally, make sure to include the versions of your packages. Specifically, show us the output of qml.about().

Name: PennyLane
Version: 0.36.0
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
License: Apache License 2.0
Location: /usr/local/lib/python3.10/dist-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info:           Linux-6.1.58+-x86_64-with-glibc2.35
Python version:          3.10.12
Numpy version:           1.25.2
Scipy version:           1.11.4
Installed devices:
- lightning.qubit (PennyLane_Lightning-0.36.0)
- default.clifford (PennyLane-0.36.0)
- default.gaussian (PennyLane-0.36.0)
- default.mixed (PennyLane-0.36.0)
- default.qubit (PennyLane-0.36.0)
- default.qubit.autograd (PennyLane-0.36.0)
- default.qubit.jax (PennyLane-0.36.0)
- default.qubit.legacy (PennyLane-0.36.0)
- default.qubit.tf (PennyLane-0.36.0)
- default.qubit.torch (PennyLane-0.36.0)
- default.qutrit (PennyLane-0.36.0)
- default.qutrit.mixed (PennyLane-0.36.0)
- null.qubit (PennyLane-0.36.0)

Reference paper

Hi, thanks for the question! It is not an easy change what you comment. Let me try to give you some guidance anyway :slight_smile:

First of all, you must be familiar with variational circuits, for which, you can orient yourself from this demo.

Secondary , you should add some variational parameters in qksvm_kernel_cirq that you will train.
The idea of variational SVM is to perform a two-stage training. You will have to define a loss function that is the accuracy of the model you get. That is, within that function, you have to do the SVM training itself.

In your code, accuracy_score can be treated as the new error function that depends of the parameters. It may seem confusing but you will see that it makes sense! :muscle:


Thanks for your guidance, I really appreciate it.

Sure, I will perform some experiments to strengthen my concepts.
I will share my learnings and try to perform the implementation myself.

I’ll get back soon.
Thanks again @Guillermo_Alonso.