Open Christophe-pere opened 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
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
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.
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
And, surprise... I got the error
AttributeError: 'QNSPSA' object has no attribute 'optimize'
becauseoptimize
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.