Qiskit / qiskit-tutorials

A collection of Jupyter notebooks showing how to use the Qiskit SDK
Apache License 2.0
2.3k stars 1.29k forks source link

Solver Error for QSVM legacy tutorial #1035

Closed fishgoesbloop closed 1 year ago

fishgoesbloop commented 3 years ago

Informations

What is the current behavior?

1)I'm trying to run the qsvm legacy tutorial (url) but am encountering a SolverError.

2)I've also tried out different training_size and test_size to test it out. When I use training_size=5, test_size=1 I get a different error "DQCPError: The problem is not DQCP". Which is related to issue #932 I believe.

3)When I use feature_dim =5, the code is able to run. Any idea why?

Thanks for helping and looking into this.

2020-09-10 10:17:58,005:qiskit.aqua.quantum_instance:INFO: 
Qiskit Terra version: 0.15.2
Backend: 'qasm_simulator (BasicAer)', with following setting:
{'basis_gates': ['u1', 'u2', 'u3', 'cx', 'id', 'unitary'], 'coupling_map': None}
{'pass_manager': None, 'initial_layout': None, 'seed_transpiler': 10598, 'optimization_level': None}
RunConfig(max_credits=10, seed_simulator=10598, shots=1024)
{'timeout': None}
{}
{}
Measurement mitigation: None
2020-09-10 10:18:09,911:qiskit.aqua.algorithms.classifiers.qsvm.qsvm:DEBUG: Calculating overlap:
|██████████████████████████████████████████████████| 780/780 [00:00:00:00]

---------------------------------------------------------------------------
SolverError                               Traceback (most recent call last)
<ipython-input-32-2d0ab0f254bc> in <module>
     17 quantum_instance = QuantumInstance(backend, shots=1024, seed_simulator=seed, seed_transpiler=seed)
     18 
---> 19 result = qsvm.run(quantum_instance)
     20 
     21 print("testing success ratio: ", result['testing_accuracy'])

~\Anaconda3\envs\qc_py37\lib\site-packages\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

~\Anaconda3\envs\qc_py37\lib\site-packages\qiskit\aqua\algorithms\classifiers\qsvm\qsvm.py in _run(self)
    456 
    457     def _run(self):
--> 458         return self.instance.run()
    459 
    460     @property

~\Anaconda3\envs\qc_py37\lib\site-packages\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])

~\Anaconda3\envs\qc_py37\lib\site-packages\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]

~\Anaconda3\envs\qc_py37\lib\site-packages\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

~\Anaconda3\envs\qc_py37\lib\site-packages\cvxpy\problems\problem.py in solve(self, *args, **kwargs)
    394         else:
    395             solve_func = Problem._solve
--> 396         return solve_func(self, *args, **kwargs)
    397 
    398     @classmethod

~\Anaconda3\envs\qc_py37\lib\site-packages\cvxpy\problems\problem.py in _solve(self, solver, warm_start, verbose, gp, qcp, requires_grad, enforce_dpp, **kwargs)
    746         solution = solving_chain.solve_via_data(
    747             self, data, warm_start, verbose, kwargs)
--> 748         self.unpack_results(solution, solving_chain, inverse_data)
    749         return self.value
    750 

~\Anaconda3\envs\qc_py37\lib\site-packages\cvxpy\problems\problem.py in unpack_results(self, solution, chain, inverse_data)
   1060             raise error.SolverError(
   1061                     "Solver '%s' failed. " % chain.solver.name() +
-> 1062                     "Try another solver, or solve with verbose=True for more "
   1063                     "information.")
   1064         self.unpack(solution)

SolverError: Solver 'OSQP' failed. Try another solver, or solve with verbose=True for more information.

Steps to reproduce the problem

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

import logging
from qiskit.aqua import set_qiskit_aqua_logging
set_qiskit_aqua_logging(logging.DEBUG) 

seed = 10598
feature_dim=2
sample_Total, training_input, test_input, class_labels = breast_cancer(
    training_size=20,
    test_size=10,
    n=feature_dim,
    plot_data=True
)

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

backend = BasicAer.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: ", result['testing_accuracy'])

What is the expected behavior?

No errors

Suggested solutions

nonhermitian commented 3 years ago

The legacy tutorials are so named because they have yet to be updated to the latest version of Qiskit. Perhaps @stefan-woerner or @levbishop has thoughts on when or if this will be done?

stefan-woerner commented 3 years ago

The problem is that due to sampling noise (you are using qasm_simulator) the estimated kernel matrix might not be positive semi definite. In this case the used solver cannot handle the resulting problem and fails. If you'd test the statevector_simulator you shouldn't see this problem.

But also for the qasm_simulator the problem is fixed by now in the master branch of qiskit-aqua. In the updated implementation (see PR Qiskit/qiskit-aqua#1190) we approximate the kernel matrix with the closest PSD matrix (essentially trimming negative eigenvalues to 0).

This tutorial will be adjusted and moved out of legacy_tutorials latest with the next qiskit-aqua release in a few weeks.

fishgoesbloop commented 3 years ago

Thanks, I no longer get the error after installing aqua 0.8.0. However, I realised I got vastly different accuracy scores(0.9 and 0.5), when i ran the same experiment(with same seed values) on aqua 0.7.5 and 0.8.0. One was on ibmq-vigo, the other on ibmq-valencia.

Apologies as I'm still quite new to Quantum machine learning, is this behaviour expected? Possibly due to noise factor?

stefan-woerner commented 3 years ago

It might be due to noise. You could validate your results using simulation (statevector and qasm) and maybe increase the number of shots used?

zzt0009 commented 3 years ago

@fishgoesbloop Would you please tell me how to install aqua 0.8.0? I tried this command pip install --upgrade qiskit.aqua==0.8.0 but it returned an error saying ERROR: Could not find a version that satisfies the requirement qiskit.aqua==0.8.0 (from versions: 0.2.0, 0.3.0, 0.3.1, 0.4.0, 0.4.1, 0.5.0, 0.5.1, 0.5.2, 0.5.3, 0.5.4, 0.5.5, 0.6.0, 0.6.1, 0.6.2, 0.6.3, 0.6.4, 0.6.5, 0.6.6, 0.7.0, 0.7.1, 0.7.2, 0.7.3, 0.7.4, 0.7.5) ERROR: No matching distribution found for qiskit.aqua==0.8.0

fishgoesbloop commented 3 years ago

It might be due to noise. You could validate your results using simulation (statevector and qasm) and maybe increase the number of shots used?

Thank you, I will try that.

fishgoesbloop commented 3 years ago

@fishgoesbloop Would you please tell me how to install aqua 0.8.0? I tried this command pip install --upgrade qiskit.aqua==0.8.0 but it returned an error saying ERROR: Could not find a version that satisfies the requirement qiskit.aqua==0.8.0 (from versions: 0.2.0, 0.3.0, 0.3.1, 0.4.0, 0.4.1, 0.5.0, 0.5.1, 0.5.2, 0.5.3, 0.5.4, 0.5.5, 0.6.0, 0.6.1, 0.6.2, 0.6.3, 0.6.4, 0.6.5, 0.6.6, 0.7.0, 0.7.1, 0.7.2, 0.7.3, 0.7.4, 0.7.5) ERROR: No matching distribution found for qiskit.aqua==0.8.0

I followed this documentation. https://qiskit.org/documentation/contributing_to_qiskit.html "Installing Aqua from Source"

zzt0009 commented 3 years ago

@fishgoesbloop Would you please tell me how to install aqua 0.8.0? I tried this command pip install --upgrade qiskit.aqua==0.8.0 but it returned an error saying ERROR: Could not find a version that satisfies the requirement qiskit.aqua==0.8.0 (from versions: 0.2.0, 0.3.0, 0.3.1, 0.4.0, 0.4.1, 0.5.0, 0.5.1, 0.5.2, 0.5.3, 0.5.4, 0.5.5, 0.6.0, 0.6.1, 0.6.2, 0.6.3, 0.6.4, 0.6.5, 0.6.6, 0.7.0, 0.7.1, 0.7.2, 0.7.3, 0.7.4, 0.7.5) ERROR: No matching distribution found for qiskit.aqua==0.8.0

I followed this documentation. https://qiskit.org/documentation/contributing_to_qiskit.html "Installing Aqua from Source"

Thank you so much for sharing the link! That is so helpful!

javabster commented 1 year ago

Closing as OP seems to have found a solution to their problem 😄