Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QNSPSA function optimize not implemented #163

Open
Christophe-pere opened this issue Mar 7, 2024 · 3 comments
Open

QNSPSA function optimize not implemented #163

Christophe-pere opened this issue Mar 7, 2024 · 3 comments
Labels
bug Something isn't working documentation Improvements or additions to documentation good first issue Good for newcomers

Comments

@Christophe-pere
Copy link

Environment

  • Qiskit Algorithms version: 0.3.0
  • Python version: 3.10.9
  • Operating system: Linux

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.

@Christophe-pere Christophe-pere added the bug Something isn't working label Mar 7, 2024
@woodsp-ibm
Copy link
Member

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

@woodsp-ibm woodsp-ibm added documentation Improvements or additions to documentation good first issue Good for newcomers labels Mar 8, 2024
@Christophe-pere
Copy link
Author

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants