1
$\begingroup$

When running Qiskit vqe.compute_minumum_eigenvalue() a dictionary of various values are returned.

Within this dictionary are two parameters called cost_function_evals and optimizer_evals. The first always contains some number whereas optimizer_evals = None.

My question is, are these two the same?

In the source code I see no place where the value optimizer_evals is actually updated, but cost_function_evals is. Here is the source code: https://qiskit.org/documentation/_modules/qiskit/algorithms/minimum_eigen_solvers/vqe.html

$\endgroup$

1 Answer 1

1
$\begingroup$

I think you are right that these two attributes are supposed to the same. Looking at the git history, optimizer_evals was used prior to this PR: https://github.com/Qiskit/qiskit-terra/pull/6418 but the usage was removed during the refactoring in that PR.

Prior to #6418, cost_function_evals was set by _eval_count which in turn was set by optimizer_evals.

L460 self._ret.cost_function_evals = self._eval_count
L443 if vqresult.optimizer_evals is not None and self._eval_count >= vqresult.optimizer_evals:
L444    self._eval_count = vqresult.optimizer_evals

in https://github.com/Qiskit/qiskit-terra/blob/5ec42789171b001780239d9e03e5dba4e077aa40/qiskit/algorithms/minimum_eigen_solvers/vqe.py

opt_params, opt_val, num_optimizer_evals = optimizer.optimize(
    nparms,
    cost_fn,
    variable_bounds=bounds,
    initial_point=initial_point,
    gradient_function=gradient_fn,
)
eval_time = time.time() - start


result = VariationalResult()
result.optimizer_evals = num_optimizer_evals

in https://github.com/Qiskit/qiskit-terra/blob/5ec42789171b001780239d9e03e5dba4e077aa40/qiskit/algorithms/variational_algorithm.py#L229-L239

After #6418, cost_functions_evals are directly set by optimizer.nfev and therefore the intermediate optimizer_evals are no longer used.

opt_result = self.optimizer.minimize(
    fun=energy_evaluation, x0=initial_point, jac=gradient, bounds=bounds
)


eval_time = time() - start_time


result = VQEResult()
result.optimal_point = opt_result.x
result.optimal_parameters = dict(zip(self.ansatz.parameters, opt_result.x))
result.optimal_value = opt_result.fun
result.cost_function_evals = opt_result.nfev

https://github.com/Qiskit/qiskit-terra/blob/dd7f9390cf076f9545fa6086de486f2c06764ea5/qiskit/algorithms/minimum_eigen_solvers/vqe.py#L546-L556

$\endgroup$
2
  • $\begingroup$ Thank you very much, Junye. $\endgroup$ Dec 16, 2022 at 11:24
  • $\begingroup$ You are welcome :) $\endgroup$ Dec 16, 2022 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.