qiskit-community / qiskit-aqua

Quantum Algorithms & Applications (**DEPRECATED** since April 2021 - see readme for more info)
https://qiskit.org/aqua
Apache License 2.0
571 stars 377 forks source link

qsvm.run raises DQCPError #1106

Closed t-imamichi closed 4 years ago

t-imamichi commented 4 years ago

Information

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 https://github.com/Qiskit/qiskit-tutorials/issues/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

t-imamichi commented 4 years ago

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