# Quantum SVMs \[Help Needed for implementation 🙏\]

**URL:** <https://discuss.pennylane.ai/t/quantum-svms-help-needed-for-implementation/4593>\
**Category:** PennyLane Help\
**Created:** [May 12, 2024, 11:45pm UTC](https://discuss.pennylane.ai/t/quantum-svms-help-needed-for-implementation/4593 "2024-05-12T23:45:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Priyansh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/priyansh/32/2209_2.png) [@Priyansh](https://discuss.pennylane.ai/u/Priyansh)\
**Post date:** [May 12, 2024, 11:45pm UTC](https://discuss.pennylane.ai/t/quantum-svms-help-needed-for-implementation/4593/1 "2024-05-12T23:45:53Z")

</div>

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** 🥲 🙏🏻 for implementation of the other two types (QV-SVM & QVK-SVM)

I am also attaching circuit images for reference.

**1)Quantum Kernel SVM**

 ![Screenshot 2024-05-09 233055](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/3/313e51b8741fbbf6bac40bacc3d16e565fef36af.png)

**2)Quantum Variational SVM**

 ![Screenshot 2024-05-09 234137](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/c/c4c0ed4255b9321824faba773595dd6d02dab836.png)

**3)Quantum Variational Kernel SVM**

 ![Screenshot 2024-05-09 234310](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/4/4a499dbd17f34adc3e31852ee0bb36ed9719c7c9.png)

Here’s the code for the QKSVM

```auto
n_qubits= 8

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

@qml.qnode(dev)
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

```auto
0.9615384615384616

```

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

```auto
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
Author: 
Author-email: 
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](https://arxiv.org/abs/2305.06063)

---

<div class="post-metadata">

**Author:** ![Guillermo\_Alonso](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/guillermo_alonso/32/816_2.png) [@Guillermo\_Alonso](https://discuss.pennylane.ai/u/Guillermo_Alonso)\
**Post date:** [May 14, 2024, 2:16pm UTC](https://discuss.pennylane.ai/t/quantum-svms-help-needed-for-implementation/4593/2 "2024-05-14T14:16:48Z")

</div>

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

First of all, you must be familiar with variational circuits, for which, you can orient yourself from this [demo](https://pennylane.ai/qml/demos/tutorial_variational_classifier/).

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! 💪

---

<div class="post-metadata">

**Author:** ![Priyansh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/priyansh/32/2209_2.png) [@Priyansh](https://discuss.pennylane.ai/u/Priyansh)\
**Post date:** [May 15, 2024, 9:03pm UTC](https://discuss.pennylane.ai/t/quantum-svms-help-needed-for-implementation/4593/3 "2024-05-15T21:03:37Z")

</div>

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.
