I have implemented QSVM on breast cancer dataset using link: "https://qiskit.org/documentation/stable/0.24/tutorials/machine_learning/01_qsvm_classification.html" but unable to compute other metrics such as sensitivity, specificity and F1-score. Only able to compute accuracy.

How can I compute other metrics in QSVM?

  • 1
    $\begingroup$ The QSVM code is part of Qiskit Aqua which was deprecated and is no longer supported. The QSVM became (moved/refactored) QSVC as part of Qiskit Machine Learning. See qiskit.org/documentation/machine-learning/tutorials/… for a tutorial. This directly extends sklearn SVC, (or you can use the kernel directly as a callable to SVC) As sklearn provides functions for the metrics you are looking for that may be path you can try. $\endgroup$
    – Steve Wood
    Commented May 20, 2022 at 16:09
  • $\begingroup$ You can also refer to this tutorial for QSVM, github.com/qiskit-community/… $\endgroup$
    – sohamb172
    Commented May 22, 2022 at 20:10

1 Answer 1


Other metrics, such as accuracy, recall, f1, and confusion matrix, can be calculated in the same way as in classical machine learning. sklearn already contains built-in functions. So, for example, if you want to view the precision, recall, and F1-score, you may use the classification report function from sklearn.metrics:

classification_report(test_labels, qsvc.predict(test_features))


qsvc is the name of your quantum kernel

In some cases, kernel matrices are used instead of the test_data in that case you can directly put the test_matrix in the predict function to evaluate your model.

  • $\begingroup$ Thank you. But can you explain this taking an example. It would be of help to me $\endgroup$
    – user14924
    Commented May 24, 2022 at 15:12
  • $\begingroup$ The code that I have written is an extension of the tutorial in steve's comment. Please try it out yourself $\endgroup$
    – sohamb172
    Commented May 24, 2022 at 21:48
  • $\begingroup$ Thank you. It worked for QSVM. Can the same code work for VQC as well using vqc.predict. When i try it gives value error: Inconsistent number of samples. $\endgroup$
    – user14924
    Commented May 25, 2022 at 11:03
  • $\begingroup$ This error is coming out because your training and test data do not have the same length. Try reshaping your data with NumPy. Another suggestion I have for you is to first learn the fundamentals of classical machine learning before diving into quantum machine learning algorithms. You must comprehend the fundamentals of the traditional algorithm. Otherwise, coding these algorithms would be extremely difficult for you. $\endgroup$
    – sohamb172
    Commented May 25, 2022 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.