qiskit-community / qiskit-aqua

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

Optimization Stableset Error #124

Closed Epulsar closed 5 years ago

Epulsar commented 5 years ago

Information

What is the current behavior?

Value Error raised when attempting to run the stableset optimization script using ExactEigensolver or VQE as the solver algorithm. A TypeError is raised when QAOA is used as the solver algorithm. Speifically, this used the stableset.py script to construct the operator for the Ising problem (all of this is outlined here https://github.com/Qiskit/aqua-tutorials/blob/master/optimization/stableset.ipynb).

Steps to reproduce the problem

Run the following code in Python3 from qiskit_aqua import Operator, run_algorithm, get_algorithm_instance from qiskit_aqua.input import get_input_instance from qiskit_aqua.algorithms.classical.cplex import stableset import numpy as np import networkx as nx

G = nx.gnp_random_graph(10, 0.567658261253) w = nx.to_numpy_matrix(G) qubitOp, offset = stableset.get_stableset_qubitops(w) algo_input = get_input_instance('EnergyInput') algo_input.qubit_op = qubitOp

if 1==1: algorithm_cfg = { 'name': 'ExactEigensolver', }

params = {
    'problem': {'name': 'ising'},
    'algorithm': algorithm_cfg
}
result = run_algorithm(params,algo_input)

x = stableset.sample_most_likely(result['eigvecs'][0])
print('energy:', result['energy'])
print('stable set objective:', result['energy'] + offset)
print('solution:', stableset.get_graph_solution(x))
print('solution objective and feasibility:', stableset.stableset_value(x, w))

if 1==1: algorithm_cfg = { 'name': 'VQE', 'operator_mode': 'matrix' }

optimizer_cfg = {
    'name': 'L_BFGS_B',
    'maxfun': 2000
}

var_form_cfg = {
    'name': 'RYRZ',
    'depth': 3,
    'entanglement': 'linear'
}

params = {
    'problem': {'name': 'ising'},
    'algorithm': algorithm_cfg,
    'optimizer': optimizer_cfg,
    'variational_form': var_form_cfg,
    'backend': {'name': 'local_statevector_simulator'}
}

result = run_algorithm(params,algo_input)

x = stableset.sample_most_likely(result['eigvecs'][0])
print('energy:', result['energy'])
print('time:', result['eval_time'])
print('stable set objective:', result['energy'] + offset)
print('solution:', stableset.get_graph_solution(x))
print('solution objective and feasibility:', stableset.stableset_value(x, w))

What is the expected behavior?

This Value Error (note this is error is specific to when ExactEigensolver is used, because ExactEigensolver is run first. The same error does occur if VQE is used in place of ExactEigensolver)

/root/anaconda3/lib/python3.6/site-packages/sklearn/cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20. "This module will be removed in 0.20.", DeprecationWarning) Traceback (most recent call last): File "mis_test.py", line 22, in result = run_algorithm(params,algo_input) File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/algomethods.py", line 83, in run_algorithm algorithm.init_params(algo_params, algo_input) File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/exacteigensolver/exacteigensolver.py", line 73, in init_params self.init_args(algo_input.qubit_op, k, algo_input.aux_ops) File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/exacteigensolver/exacteigensolver.py", line 88, in init_args self._operator.convert('paulis', 'matrix') File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/operator.py", line 824, in convert self._paulis_to_matrix() File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/operator.py", line 939, in _paulis_to_matrix self._to_dia_matrix(mode='paulis') File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/operator.py", line 310, in _to_dia_matrix dia_matrix += coeff * pauli.to_spmatrix().diagonal() File "/root/anaconda3/lib/python3.6/site-packages/numpy/matrixlib/defmatrix.py", line 309, in mul return N.dot(self, asmatrix(other)) ValueError: shapes (1,10) and (1,1024) not aligned: 10 (dim 1) != 1 (dim 0)

QAOA solve: another error encountered In place of ExactEigensolver or VQE for algorithm_cfg, if we put in QAOA instead, we get a different error than the one observed for the other two algorithms. /root/anaconda3/lib/python3.6/site-packages/sklearn/cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20. "This module will be removed in 0.20.", DeprecationWarning) capi_return is NULL Call-back cb_calcfc_incobylauserroutines failed. Traceback (most recent call last): File "mis_test.py", line 54, in result = run_algorithm(params,algo_input) File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/algomethods.py", line 84, in run_algorithm value = algorithm.run() File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/vqe/vqe.py", line 236, in run self._solve() File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/vqe/vqe.py", line 188, in _solve opt_params, opt_val = self.find_minimum_eigenvalue() File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/vqe/vqe.py", line 307, in find_minimum_eigenvalue variable_bounds=bounds, initial_point=initial_point) File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/utils/optimizers/cobyla.py", line 80, in optimize res = minimize(objective_function, initial_point, tol=self._tol, method="COBYLA", options=self._options) File "/root/anaconda3/lib/python3.6/site-packages/scipy/optimize/_minimize.py", line 608, in minimize return _minimize_cobyla(fun, x0, args, constraints, *options) File "/root/anaconda3/lib/python3.6/site-packages/scipy/optimize/cobyla.py", line 252, in _minimize_cobyla dinfo=info) File "/root/anaconda3/lib/python3.6/site-packages/scipy/optimize/cobyla.py", line 242, in calcfc f = fun(x, args) File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/vqe/vqe.py", line 254, in _energy_evaluation input_circuit = self._var_form.construct_circuit(parameters) File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/qaoa/varform.py", line 60, in construct_circuit circuit += self._cost_operator.evolve(None, gamma, 'circuit', 1, quantum_registers=q) File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/operator.py", line 1369, in evolve slice_pauli_list, evo_time, num_time_slices, quantum_registers File "/root/anaconda3/lib/python3.6/site-packages/qiskit_aqua/operator.py", line 1185, in construct_evolution_circuit qc.u1(lam, state_registers[top_XYZ_pauli_indices[pauli_idx]]) File "/root/anaconda3/lib/python3.6/site-packages/qiskit/extensions/standard/u1.py", line 54, in u1 return self._attach(U1Gate(theta, q, self)) File "/root/anaconda3/lib/python3.6/site-packages/qiskit/extensions/standard/u1.py", line 26, in init super().init("u1", [theta], [qubit], circ) File "/root/anaconda3/lib/python3.6/site-packages/qiskit/_gate.py", line 35, in init super().init(name, param, args, circuit) File "/root/anaconda3/lib/python3.6/site-packages/qiskit/_instruction.py", line 37, in init self.param.append(Number(single_param)) File "/root/anaconda3/lib/python3.6/site-packages/sympy/core/numbers.py", line 547, in new raise TypeError(msg % type(obj).name__) TypeError: expected str|int|long|float|Decimal|Number object but got 'matrix'

Suggested solutions

I would suggest implementing an error handling for the ValueError here, but I am not certain of how to solve the this specific problem. For the Qaoa algorithm error, I am not certain what why it is giving a TypeError in this specific case.

chunfuchen commented 5 years ago

Hi, the issue should come from the potential issue fixed by c92b7774dca906f6b7b802e43cb67158f5252252. (The created Operator is not valid)

do you mind checking that does QAOA encounter this issue after this commit?

Thanks.

Epulsar commented 5 years ago

Hi, the issue should come from the potential issue fixed by c92b777. (The created Operator is not valid)

do you mind checking that does QAOA encounter this issue after this commit?

Thanks.

I tried it with QAOA and VQE and it gave the same error message as before. Also, I tried different minimizer functions form scipy, such as TNC, and got the same error message as before.

woodsp-ibm commented 5 years ago

What version of qiskit are you using? With the latest Aqua master, that it seems you are using, the latest qiskit master is reqd. With the above fix, using the logic you provided, I can run ExactEigensolver and VQE to completion. QAOA also runs, but at size 10 I encounter a performance problem, it runs at smaller graph sizes ok and I do not see any of the issues you reported above.

Epulsar commented 5 years ago

I tried this on a different computer, using only qiskit and qiskit_aqua files directly from the Github repository, and it did work for ExactEigensolver, VQE, and QAOA. Although I had to use QAOA.Variational in place of QAOA. And given that I didn't optimize the number of steps, the results weren't accurate, but I didn't encounter the error. Thanks! As an aside, what is the difference between QAOA.Variational and QAOA?

woodsp-ibm commented 5 years ago

We renamed and re-organized algorithms since the last official release. What we then called QAOA became QAOA.Variational as it uses a variational approach but its the same algorithm. As things are now working for you I will close this issue.