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

qsvm.run raises DQCPError #1106

Closed
t-imamichi opened this issue Jul 9, 2020 · 1 comment · Fixed by #1190
Closed

qsvm.run raises DQCPError #1106

t-imamichi opened this issue Jul 9, 2020 · 1 comment · Fixed by #1190
Labels
type: bug Something isn't working

Comments

@t-imamichi
Copy link
Contributor

t-imamichi commented Jul 9, 2020

Information

  • Qiskit Aqua version: master (d45f689)
  • Python version: Python 3.7.8
  • Operating system: Ubuntu 20.04

What is the current behavior?

When I execute a notebook https://github.com/Qiskit/qiskit-tutorials/blob/master/legacy_tutorials/aqua/machine_learning/qsvm_classification.ipynb, qsvm.run raises DQCPError

#1083 might not be enough to fix this issue.
It is mentioned in a tutorial issue Qiskit/qiskit-tutorials#932

Steps to reproduce the problem

Run https://github.com/Qiskit/qiskit-tutorials/blob/master/legacy_tutorials/aqua/machine_learning/qsvm_classification.ipynb

Notebook cell

seed = 10598

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

backend = Aer.get_backend('qasm_simulator')
quantum_instance = QuantumInstance(backend, shots=1024, seed_simulator=seed, seed_transpiler=seed)

result = qsvm.run(quantum_instance)

print("testing success ratio: {}".format(result['testing_accuracy']))
print("preduction of datapoints:")
print("ground truth: {}".format(map_label_to_class_name(datapoints[1], qsvm.label_to_class)))
print("prediction:   {}".format(result['predicted_classes']))
---------------------------------------------------------------------------
DQCPError                                 Traceback (most recent call last)
 in 
      7 quantum_instance = QuantumInstance(backend, shots=1024, seed_simulator=seed, seed_transpiler=seed)
      8 
----> 9 result = qsvm.run(quantum_instance)
     10 
     11 print("testing success ratio: {}".format(result['testing_accuracy']))

~/github/qiskit/aqua/qiskit/aqua/algorithms/quantum_algorithm.py in run(self, quantum_instance, **kwargs)
     68                 self.quantum_instance = quantum_instance
     69 
---> 70         return self._run()
     71 
     72     @abstractmethod

~/github/qiskit/aqua/qiskit/aqua/algorithms/classifiers/qsvm/qsvm.py in _run(self)
    456 
    457     def _run(self):
--> 458         return self.instance.run()
    459 
    460     @property

~/github/qiskit/aqua/qiskit/aqua/algorithms/classifiers/qsvm/_qsvm_binary.py in run(self)
    134     def run(self):
    135         """Put the train, test, predict together."""
--> 136         self.train(self._qalgo.training_dataset[0], self._qalgo.training_dataset[1])
    137         if self._qalgo.test_dataset is not None:
    138             self.test(self._qalgo.test_dataset[0], self._qalgo.test_dataset[1])

~/github/qiskit/aqua/qiskit/aqua/algorithms/classifiers/qsvm/_qsvm_binary.py in train(self, data, labels)
     81         labels = labels * 2 - 1  # map label from 0 --> -1 and 1 --> 1
     82         labels = labels.astype(np.float)
---> 83         [alpha, b, support] = optimize_svm(kernel_matrix, labels, scaling=scaling)
     84         support_index = np.where(support)
     85         alphas = alpha[support_index]

~/github/qiskit/aqua/qiskit/aqua/utils/qp_solver.py in optimize_svm(kernel_matrix, y, scaling, maxiter, show_progress, max_iters)
     88         [G@x <= h,
     89          A@x == b])
---> 90     prob.solve(verbose=show_progress, qcp=True)
     91     result = np.asarray(x.value).reshape((n, 1))
     92     alpha = result * scaling

~/envs/qopt/lib/python3.7/site-packages/cvxpy/problems/problem.py in solve(self, *args, **kwargs)
    393         else:
    394             solve_func = Problem._solve
--> 395         return solve_func(self, *args, **kwargs)
    396 
    397     @classmethod

~/envs/qopt/lib/python3.7/site-packages/cvxpy/problems/problem.py in _solve(self, solver, warm_start, verbose, gp, qcp, requires_grad, enforce_dpp, **kwargs)
    731             if qcp and not self.is_dcp():
    732                 if not self.is_dqcp():
--> 733                     raise error.DQCPError("The problem is not DQCP.")
    734                 reductions = [dqcp2dcp.Dqcp2Dcp()]
    735                 if type(self.objective) == Maximize:

DQCPError: The problem is not DQCP.

What is the expected behavior?

No error

Suggested solutions

@Cryoris Cryoris added the type: bug Something isn't working label Jul 9, 2020
@t-imamichi
Copy link
Contributor Author

t-imamichi commented Jul 9, 2020

It may be because matrix P is not positive semidefinite due to sampling error of qasm_simulator. The code works fine with statevector_simulator, but raises the error with qasm_simulator.
https://github.com/Qiskit/qiskit-aqua/blob/d45f689cc7b8cc4509ceeb43fd7da78baa01a49a/qiskit/aqua/utils/qp_solver.py#L87

We maybe need to correct P to be positive semidefinite.
E.g., https://stackoverflow.com/questions/10939213/how-can-i-calculate-the-nearest-positive-semi-definite-matrix

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants