qiskit-community / qiskit-optimization

Quantum Optimization
https://qiskit-community.github.io/qiskit-optimization/
Apache License 2.0
226 stars 139 forks source link

WarmStartQAOAOptimizer fails with initial point length mismatch #210

Closed woodsp-ibm closed 2 years ago

woodsp-ibm commented 3 years ago

The issue was originally raised on Qiskit Slack #aqua channel here

The following code sample, based on what was provided in that discussion, works at reps=1 (or at least it does for me but gave the originator of the message this error ValueError: array must not contain infs or NaNs that I could not reproduce). At reps > 1 then it fails with an error like this

qiskit/algorithms/minimum_eigen_solvers/vqe.py", line 716, in _validate_initial_point f"The dimension of the initial point ({len(point)}) does not match the " ValueError: The dimension of the initial point (4) does not match the number of parameters in the circuit (3).

but was stated that it worked on prior version (I did not check).

I will note that if initial_point is set to None (a line in the sample code does that but is commented out so it will fail at present) then things work fine.

Also if you run qaoa itself it proves the initial_point is being passed in correctly in terms of at least an initial length that matches. And you then go onto re-use the same qaoa instance for the WarmStart algo, it works fine (the block with the do_qaoa that is set False at present again so it the sample will fail).

I will note I reduced SPSA max_iter down from the 500 of the original sample to 5 just for testing whether it failed or not.

import networkx as nx
import numpy as np
from qiskit import Aer
from qiskit.algorithms import QAOA
from qiskit.algorithms.optimizers import SPSA
from qiskit_optimization.algorithms import WarmStartQAOAOptimizer
from qiskit_optimization.algorithms import CobylaOptimizer
from qiskit_optimization.applications.max_cut import Maxcut
from qiskit.utils import QuantumInstance

n = 5
graph = nx.Graph()
graph.add_nodes_from(np.arange(0, n, 1))
elist = [(0, 3, 9), (0, 4, 6), (1, 2, 9), (1, 4, 10), (2, 4, 7), (3, 4, 7)]
graph.add_weighted_edges_from(elist)

max_cut_quadr_program = Maxcut(graph).to_quadratic_program()

reps = 2
init_pt = [0]*reps + [1]*reps
#init_pt = None
print(f'Using reps {reps} with initial point {init_pt}')
qaoa = QAOA(optimizer=SPSA(maxiter=5),
            quantum_instance=QuantumInstance(backend=Aer.get_backend('qasm_simulator'),shots=8000),
            reps=reps, 
            initial_point=init_pt)

do_qaoa = False
if do_qaoa:
    qubit_op, _ = max_cut_quadr_program.to_ising()
    qaoa_res = qaoa.compute_minimum_eigenvalue(qubit_op)
    print(qaoa_res)

ws_qaoa = WarmStartQAOAOptimizer(pre_solver=CobylaOptimizer(),
                                 relax_for_pre_solver=True,
                                 qaoa=qaoa)
ws_result = ws_qaoa.solve(max_cut_quadr_program)
print(ws_result)
adekusar-drl commented 3 years ago

I confirm the code works with Qiskit 0.27, but does not with 0.28. The problem is introduced here: https://github.com/Qiskit/qiskit-terra/pull/6396. Now the number of parameters in QAOAAnsatz computed incorrectly and the beta parameter has only one value for all reps. @Cryoris please confirm.

t-imamichi commented 3 years ago

I tried the script in https://github.com/Qiskit/qiskit-optimization/issues/210#issue-951099960 with qiskit-optimization main and https://github.com/Qiskit/qiskit-terra/pull/6828. It works as follows.

Using reps 2 with initial point [0, 0, 1, 1]
optimal function value: 35.0
optimal value: [0. 1. 0. 1. 0.]
status: SUCCESS
lasys commented 3 years ago

@t-imamichi could you please also check, if all beta values are set correctly? i'm not sure if that the parameter beta here: WarmStartQAOAFactory is considered in the PR you mentioned. Thanks!

t-imamichi commented 3 years ago

I executed the same code https://github.com/Qiskit/qiskit-optimization/issues/210#issuecomment-892663105 with the main branch and it generated the same output. I don't know what are the expected values for beta, but, I see the dimension matches and the code runs without any error.

t-imamichi commented 2 years ago

Because https://github.com/Qiskit/qiskit-terra/pull/6828 is merged, I think this issue is fixed. This is the output in my environment with the latest terra and opt.

Using reps 2 with initial point [0, 0, 1, 1]
/Users/ima/tasks/1_2021/qiskit/terra/qiskit/opflow/primitive_ops/pauli_sum_op.py:362: DeprecationWarning: The SparsePauliOp.table method is deprecated as of Qiskit Terra 0.19.0 and will be removed no sooner than 3 months after the releasedate. Use SparsePauliOp.paulis method instead.
  tables = self.primitive.table
optimal function value: 35.0
optimal value: [0. 1. 0. 1. 0.]
status: SUCCESS