!pip install --upgrade pyscf

from qiskit_nature.units import DistanceUnit
from qiskit_nature.second_q.drivers import PySCFDriver

driver = PySCFDriver(
    atom="H 0 0 0; H 0 0 0.735",
    basis="sto3g",
    charge=0,
    spin=0,
    unit=DistanceUnit.ANGSTROM,
)

es_problem = driver.run()





#!pip install qiskit_algorithms

from qiskit_algorithms.optimizers import SPSA # simultaneous perturbation stochastic approximation

from qiskit.circuit.library import EfficientSU2 # ansatz para o circuito variacional

ansatz = EfficientSU2(2)

ansatz.decompose().draw('mpl')

from qiskit.primitives import Estimator

estimator = Estimator()

callback = lambda nfev, params, fval, step, acc: print(f'Evaluation {nfev}: {fval}')

optimizer = SPSA(callback=callback, maxiter=1000)

cost_func = lambda params: estimator.run(ansatz, H2_op, parameter_values=params).result().values[0]

result = optimizer.minimize(cost_func, x0=np.zeros(ansatz.num_parameters))