# Recording the time taken by a VQE to find ground state energy

I want to record the time that a VQE solver takes to find the ground state. At the moment I am using:

%matplotlib inline
# Importing standard Qiskit libraries and configuring account
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.compiler import transpile, assemble
from qiskit.tools.jupyter import *
from qiskit.visualization import *
import numpy as np
import matplotlib.pyplot as plt
import time

provider = IBMQ.get_provider('ibm-q')
qcomp = provider.get_backend('ibmq_valencia')

#Imports for algorithm
from qiskit.chemistry.drivers import PySCFDriver, UnitsType, Molecule
from qiskit.chemistry.transformations import FermionicTransformation, FermionicQubitMappingType
from qiskit.aqua.algorithms import NumPyMinimumEigensolver
from qiskit import BasicAer
from qiskit.aqua import QuantumInstance
from qiskit.chemistry.algorithms.ground_state_solvers.minimum_eigensolver_factories import VQEUCCSDFactory
from qiskit.chemistry.algorithms.ground_state_solvers import GroundStateEigensolver

#Define molecule
molecule = Molecule(geometry=[['H', [0., 0., 0.]],
['H', [0., 0., 0.735]]],
charge=0, multiplicity=1)

driver = PySCFDriver(molecule = molecule, unit=UnitsType.ANGSTROM, basis='sto3g')

#Map fermionic hamiltonian to qubit hamiltonian
transformation = FermionicTransformation(qubit_mapping=FermionicQubitMappingType.PARITY,
two_qubit_reduction=True,
freeze_core=True)

#Initialize the VQE solver
vqe_solver = VQEUCCSDFactory(QuantumInstance(backend = qcomp))

#Ground state algorithm
calculation_VQE = GroundStateEigensolver(transformation, vqe_solver)

#Calculate results
start = time.time()
VQE_result = calculation_VQE.solve(driver)
end = time.time()
time_difference = (end - start)
print(VQE_result.total_energies)
print(time_difference)


However, after running this a few times on different IBM quantum machines, I am quite sure that the times I am recording are including the time spent waiting in queue to run the computations (but not completely certain on this). Is there any way to record the time taken by the VQE to compute the ground state energy without including queue time.

You are correct that your current strategy will include the time spent in queue and other overhead processes.

If you are looking to only get the time spent evaluating, then you can use Aqua's logging feature. If you turn on Aqua's logging, then it will show you INFO logs during the VQE's run. Within these logs are times taken for each evaluation and parameter binding. Although not ideal, you can add all of these times up and come up with a rough estimate of the total time taken for the entire VQE run.

You can enable Aqua's logging by running this snippet of code with your imports:

import logging
from qiskit.aqua import set_qiskit_aqua_logging
set_qiskit_aqua_logging(logging.DEBUG)  # choose INFO, DEBUG to see the log


An example of what one of the iteration's logs would look like:

2020-11-20 10:30:48,980:qiskit.aqua.algorithms.minimum_eigen_solvers.vqe:INFO: Energy evaluation returned [-1.17232169] - 74336.84897 (ms), eval count: 1

• I inserted that snippet of code into my imports and the times being returned in the logs are definitely including queue time (I've cross-referenced the log times with the queue time + running time for each job displayed by IBM QE). Log example: 2020-11-20 20:44:59,519:qiskit.aqua.algorithms.minimum_eigen_solvers.vqe:INFO: Energy evaluation returned [-1.76474899] - 4983694.64421 (ms), eval count: 1 Any ideas why this is the case? – LouisL Nov 21 '20 at 14:37