I am trying to run the following code:
import numpy as np
from qiskit import *
from qiskit_ibm_runtime import Options
from qiskit_ibm_runtime import QiskitRuntimeService, Estimator
from qiskit.algorithms.optimizers import NFT
from qiskit.circuit.library import TwoLocal
from qiskit.quantum_info import SparsePauliOp
from qiskit.algorithms.minimum_eigensolvers import VQE
from qiskit.opflow import X, Y, Z, I
from qiskit.opflow import PauliSumOp
service = QiskitRuntimeService(token="YOUR-TOKEN",channel="ibm_quantum")
options = Options()
options.optimization_level=3
options.execution.shots=100
#backend = service.backend("ibm_lagos")
backend = service.backend("ibmq_qasm_simulator")
estimator = Estimator(session=backend,options=options)
hamiltonian_0= SparsePauliOp(['IIII'] )
hamiltonian_1=PauliSumOp(SparsePauliOp(['IIII', 'IIIZ', 'IIZI', 'IIZZ', 'IZII', 'IZIZ',
'IZZI', 'IZZZ', 'ZIII', 'ZIIZ', 'ZIZI', 'ZIZZ', 'ZZII', 'ZZIZ', 'ZZZI', 'ZZZZ'],
coeffs=[ 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,
1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,
1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j]), coeff=1.0)
dim=hamiltonian_0.num_qubits
ansatz = TwoLocal(dim, rotation_blocks=["ry"],entanglement='reverse_linear',entanglement_blocks="cx", reps=1)
optimizer = NFT(maxiter=100)
vqe = VQE(estimator=estimator, ansatz=ansatz, optimizer=optimizer )
result = vqe.compute_minimum_eigenvalue(hamiltonian_0)
In this code I want to compare the time it takes to compute the eigenvalue for the two Hamiltonians.
If I understand correctly, the VQE algorithm calculates a circuit for each of the PauliStrings, then sums the different expectation values and finally changes the parameters and repeats the whole process. So for the hamiltonian_0
$(H_0=I\otimes I\otimes I \otimes I )$ the VQE shall make $1\times 100$ calculations and for hamiltonian_1
that has 16 Paulistrings $(H_1=\sum_{i=0}^{15} P_i )$ , the VQE shall make $16\times 100$ calculations.
Whenever I run it , the optimizer_time
of the VQEResult is the same for both hamiltonian_0
and hamiltonian_1
, roughly $1000~s$ .I would expect (maybe naively) , for the hamiltonian_0
to be 16 times faster than hamiltonian_1
.
I have also made several tests with similar, up to 8 qubit Hamiltonians using the primitives, but with qasm_simulator
. I get the same optimizer_time
for Hamiltonians with $1$ term and with $2^8$ terms.
Does anyone know why this happens? Is it because the estimator primitive has a parallelization procedure?