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

Bug in nakanishi_fujii_todo #1600

Closed riuriuriuriu closed 3 years ago

riuriuriuriu commented 3 years ago

Information

What is the current behavior?

nakanishi_fujii_todo() optimizer fail to return OptimizeResult except in the case of args = ().

Steps to reproduce the problem

I tried to run the following code


from qiskit.aqua.components.optimizers.nft import nakanishi_fujii_todo
import numpy as np

def cost_function(param, a):
    return a[0] * np.sin(param) + a[1] * np.cos(param) + a[2]

a = np.random.rand(3)
nakanishi_fujii_todo(
    cost_function,
    x0 = [0.1],
    args = (a,),
    maxiter = 3
)

and then I got the following error


TypeError                                 Traceback (most recent call last)
 in 
      3 
      4 a = np.random.rand(3)
----> 5 nakanishi_fujii_todo(
      6     cost_function,
      7     x0 = [0.1],

~/venv/lib/python3.9/site-packages/qiskit/aqua/components/optimizers/nft.py in nakanishi_fujii_todo(fun, x0, args, maxiter, maxfev, reset_interval, eps, callback, **_)
    177                 break
    178 
--> 179     return OptimizeResult(fun=fun(np.copy(x0)), x=x0, nit=niter, nfev=funcalls, success=(niter > 1))

TypeError: cost_function() missing 1 required positional argument: 'a'

What is the expected behavior?

nakanishi_fujii_todo() should return OptimizeResult without errors.

Suggested solutions

OptimizeResult(fun=fun(np.copy(x0)), x=x0, nit=niter, nfev=funcalls, success=(niter > 1)) should be OptimizeResult(fun=fun(np.copy(x0), *args), x=x0, nit=niter, nfev=funcalls, success=(niter > 1))

ikkoham commented 3 years ago

Thank you. Aqua was deprecated from the previous release, so the fix in Terra is enough. https://github.com/Qiskit/qiskit-terra/issues/6525

woodsp-ibm commented 3 years ago

As per above - I don't think this is critical bug that needs fixing, which is the policy now Aqua has been deprecated. We are encouraging people to move off Aqua and use the code from where it now been relocated, so if anyone comes across this then it has been fixed already in the relocated code in Terra thanks to your reported issue there.