How the number of function evaluations is calculated in Qiskit when running VQE #32
Closed
radumarg
started this conversation in
General Feedback
Replies: 2 comments
-
My assumption seems to be correct. More details here: https://quantumcomputing.stackexchange.com/questions/31996/how-the-number-of-function-evaluations-is-calculated-in-qiskit-when-running-vqe/31997#31997 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want to understand how the number of function evaluation is calculated by Qiskit when running VQE algorithms. Here is some code I used in order to test this:
from qiskit.primitives import Estimator
from qiskit.providers.aer import QasmSimulator, AerSimulator
from qiskit.algorithms.optimizers import SLSQP
from qiskit.utils import QuantumInstance
from qiskit_nature.second_q.algorithms import NumPyMinimumEigensolverFactory
from qiskit_nature.second_q.algorithms.initial_points import HFInitialPoint
from qiskit_nature.second_q.algorithms.ground_state_solvers import GroundStateEigensolver, VQEUCCFactory
from qiskit_nature.second_q.formats.molecule_info import MoleculeInfo
from qiskit_nature.second_q.mappers import QubitConverter
from qiskit_nature.second_q.drivers import PySCFDriver
from qiskit_nature.second_q.transformers import FreezeCoreTransformer
from qiskit_nature.second_q.circuit.library.ansatzes import UCC
objective_function_tolerance = 1e-6
slsqp = SLSQP(maxiter=10000, tol=objective_function_tolerance)
numpy_solver = NumPyMinimumEigensolverFactory()
quantum_instance = QuantumInstance(AerSimulator(method='statevector', device="CPU"))
molecule = MoleculeInfo(["Li", "H"], [(0.0, 0.0, 0.0), (0.0, 0.0, 1.595)])
driver = PySCFDriver.from_molecule(molecule, basis="sto3g")
electronic_structure_problem = driver.run()
transformer = FreezeCoreTransformer()
electronic_structure_problem = transformer.transform(electronic_structure_problem)
num_particles = electronic_structure_problem.num_particles
num_spatial_orbitals = electronic_structure_problem.num_spatial_orbitals
uccsd = UCC(num_spatial_orbitals, num_particles, excitations='sd')
def callback(eval_count, parameters, mean, std): print(eval_count)
vqe_factory = VQEUCCFactory(Estimator(), uccsd, slsqp, initial_point = HFInitialPoint())
vqe_factory.minimum_eigensolver.callback = callback
converter = QubitConverter(ParityMapper(), two_qubit_reduction=True, z2symmetry_reduction=None)
gse = GroundStateEigensolver(converter, vqe_factory)
result = gse.solve(electronic_structure_problem)
print("function evaluations: ", result.raw_result.cost_function_evals)
From inspecting the Qiskit code, it seems to me that the number of function evaluations is incremented once after each expectation value of the Hamiltonian is measured or calculated in case of running a simulation on a classical computer. This might appear quite reasonable, except that I was somehow expecting/hoping that the number of function evaluation is incremented after each Pauli string (or group of Pauli strings in case those are grouped in commuting sets) is evaluated during a VQE calculation. Could someone please confirm if my conclusion is correct or not?
Beta Was this translation helpful? Give feedback.
All reactions