Skip to content
This repository was archived by the owner on Dec 7, 2021. It is now read-only.

QSVM error with state vector simulator and breast cancer dataset #1402

Closed
vbelis opened this issue Nov 3, 2020 · 4 comments
Closed

QSVM error with state vector simulator and breast cancer dataset #1402

vbelis opened this issue Nov 3, 2020 · 4 comments

Comments

@vbelis
Copy link

vbelis commented Nov 3, 2020

Information

  • Qiskit Aqua version: Using the IBM Quantum experience platform
  • Python version: 3.7
  • Operating system: Using the IBM Quantum experience platform

What is the current behavior?

qsvm.run(quantum_instance, verbose=True) gives an error when used for the breast cancer dataset.

The error comes from the cvxpy optimizer used in the qsvm. This issue is similar to the one previously raised (see below) with the qasm simulator due to sampling errors making the kernel matrix non positive semi-definite (fixed here: #1190). Nevertheless, this is not expected to happen with the statevector simulator where no errors are introduced.

Similar reports (with Qasm simulator):
Qiskit/qiskit-tutorials#1035
#1106

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-dd591a5335ab> in <module>
     21 quantum_instance = QuantumInstance(backend)
     22 
---> 23 result = qsvm.run(quantum_instance, verbose=True)
     24 print("testing success ratio: ", result['testing_accuracy'])

/opt/conda/lib/python3.7/site-packages/qiskit/aqua/algorithms/quantum_algorithm.py in run(self, quantum_instance, **kwargs)
     69                 self.quantum_instance = quantum_instance
     70 
---> 71         return self._run()
     72 
     73     @abstractmethod

/opt/conda/lib/python3.7/site-packages/qiskit/aqua/algorithms/classifiers/qsvm/qsvm.py in _run(self)
    467 
    468     def _run(self):
--> 469         return self.instance.run()
    470 
    471     @property

/opt/conda/lib/python3.7/site-packages/qiskit/aqua/algorithms/classifiers/qsvm/_qsvm_binary.py in run(self)
    132     def run(self):
    133         """Put the train, test, predict together."""
--> 134         self.train(self._qalgo.training_dataset[0], self._qalgo.training_dataset[1])
    135         if self._qalgo.test_dataset is not None:
    136             self.test(self._qalgo.test_dataset[0], self._qalgo.test_dataset[1])

/opt/conda/lib/python3.7/site-packages/qiskit/aqua/algorithms/classifiers/qsvm/_qsvm_binary.py in train(self, data, labels)
     79         labels = labels * 2 - 1  # map label from 0 --> -1 and 1 --> 1
     80         labels = labels.astype(np.float)
---> 81         [alpha, b, support] = optimize_svm(kernel_matrix, labels, scaling=scaling)
     82         support_index = np.where(support)
     83         alphas = alpha[support_index]

/opt/conda/lib/python3.7/site-packages/qiskit/aqua/utils/qp_solver.py in optimize_svm(kernel_matrix, y, scaling, maxiter, show_progress, max_iters)
     91          A@x == b])
     92     prob.solve(verbose=show_progress, qcp=True)
---> 93     result = np.asarray(x.value).reshape((n, 1))
     94     alpha = result * scaling
     95     avg_y = np.sum(y)

ValueError: cannot reshape array of size 1 into shape (90,1)

Steps to reproduce the problem

Running the following code:

import matplotlib.pyplot as plt
import numpy as np
from qiskit import Aer
from qiskit.ml.datasets import *
from qiskit.circuit.library import ZZFeatureMap
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

feature_dim = 2

sample_Total, training_input, test_input, class_labels = breast_cancer(
    training_size=45,
    test_size=10,
    n=2,
    plot_data=True
)

feature_map = ZZFeatureMap(feature_dimension=feature_dim, reps=2, entanglement='linear')
qsvm = QSVM(feature_map, training_input, test_input)

backend=Aer.get_backend('statevector_simulator')
quantum_instance = QuantumInstance(backend)

result = qsvm.run(quantum_instance, verbose=True)
print("testing success ratio: ", result['testing_accuracy'])

### What is the expected behavior?
No error.


### Suggested solutions


@EliasCombarro
Copy link

I am having similar issues. It might be linked with a problem of non-separability in the data, since this also happens with the classical SVM as reported by another user here: #1401

Is it possible that the classical solver used to obtain the support vectors and alphas is in "hard margin" mode or something?

@woodsp-ibm
Copy link
Member

I am not sure about #1401 but this issue seems like it should be fixed in the current master via #1378 which addressed the issue as described above. Try running off an install from master here

@vbelis
Copy link
Author

vbelis commented Nov 4, 2020

Thank you for the prompt answer. It seems that adding lambda2 hyperparameter to make the SVM a soft margin one, has fixed my issue of convergence. It would be really nice as you mention in #1378 if this parameter could be tweaked from the QSVM class :)

@woodsp-ibm
Copy link
Member

woodsp-ibm commented Nov 4, 2020

Adjusting the value, when you use QSVM, is already there - see lambda2 param https://github.com/Qiskit/qiskit-aqua/blob/36ffb11a08669f04ff6f7d44741c76eec8afeb2c/qiskit/aqua/algorithms/classifiers/qsvm/qsvm.py#L78-L95

As this seems to have fixed your issue - and also you have the means to adjust it, which is already there in line with your suggestion, I will go ahead and close this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants