qiskit-community / qiskit-algorithms

A library of quantum algorithms for Qiskit.
https://qiskit-community.github.io/qiskit-algorithms/
Apache License 2.0
111 stars 54 forks source link

QNSPSA function optimize not implemented #163

Open Christophe-pere opened 6 months ago

Christophe-pere commented 6 months ago

Environment

What is happening?

Hi,

I want to use the optimizer QNSPSA. I used the documentation on the github page. I used the sample code in the description of the class

            import numpy as np
            from qiskit_algorithms.optimizers import QNSPSA
            from qiskit.circuit.library import PauliTwoDesign
            from qiskit.primitives import Estimator, Sampler
            from qiskit.quantum_info import Pauli

            # problem setup
            ansatz = PauliTwoDesign(2, reps=1, seed=2)
            observable = Pauli("ZZ")
            initial_point = np.random.random(ansatz.num_parameters)

            # loss function
            estimator = Estimator()

            def loss(x):
                result = estimator.run([ansatz], [observable], [x]).result()
                return np.real(result.values[0])

            # fidelity for estimation of the geometric tensor
            sampler = Sampler()
            fidelity = QNSPSA.get_fidelity(ansatz, sampler)

            # run QN-SPSA
            qnspsa = QNSPSA(fidelity, maxiter=300)
            result = qnspsa.optimize(ansatz.num_parameters, loss, initial_point=initial_point)

And, surprise... I got the error AttributeError: 'QNSPSA' object has no attribute 'optimize' because optimize is not implemented...

How can we reproduce the issue?

Simply run the code provided in the class description.

What should happen?

Optimization process.

Any suggestions?

Complete the class with the optimize function or update the documentation and class description in order to have a good reference to use the optimizer.

woodsp-ibm commented 6 months ago

The optimize function was replaced some time ago by minimize and it seems that the sample has not been updated - so it needs that documentation updated. Meanwhile just use minimize instead if you want to use the optimizer - this is the function you would use on any of the optimizers here

Christophe-pere commented 6 months ago

Hi,

Yes, but the minimize() doesn't work similarly.

Here is the correct code:

import numpy as np
from qiskit_algorithms.optimizers import QNSPSA
from qiskit.circuit.library import PauliTwoDesign
from qiskit.primitives import Estimator, Sampler
from qiskit.quantum_info import Pauli

# problem setup
ansatz = PauliTwoDesign(2, reps=1, seed=2)
observable = Pauli("ZZ")
initial_point = np.random.random(ansatz.num_parameters)

# loss function
estimator = Estimator()

def loss(x):
    result = estimator.run([ansatz], [observable], [x]).result()
    return np.real(result.values[0])

# fidelity for estimation of the geometric tensor
sampler = Sampler()
fidelity = QNSPSA.get_fidelity(ansatz, sampler=sampler)

# run QN-SPSA
qnspsa = QNSPSA(fidelity, maxiter=300)
result = qnspsa.minimize(loss, x0=initial_point)

We can check the result:

print(result.x)
>> array([2.20052599, 0.97016695, 0.78032163, 2.34066807])
print(result.fun)
>> -0.8660253352863846 
woodsp-ibm commented 6 months ago

Yes, the parameters and return more match scipy now - you cannot simply change the name there, you also need to change the parameters as you have shown.