2
$\begingroup$

I'm trying to calculate the expectation value of some operator for a state that comes out of a quantum circuit using Qiskit. I was initially using StatevectorSimulator, but I also wanted to experiment with QasmSimulator. I Followed this answer in trying to calculate expectation values using QasmSimulator. However I noticed that Qasmsimulator is almost $40$ times slower than StatevectorSimulator for doing the same task. Here is a minimal code for comparison:

import time 
from numpy import conjugate, pi
from qiskit import QuantumCircuit, transpile
from qiskit.providers.aer import QasmSimulator, StatevectorSimulator
from qiskit.utils import QuantumInstance
from qiskit.opflow import CircuitStateFn, PauliExpectation, CircuitSampler, StateFn
from qiskit.opflow import PauliOp, PauliSumOp
from qiskit.quantum_info import Pauli

# The details of the PauliOp and the quantum circuit I guess are irrelevant. 
# What follows is just an example of some operator (qubit_op) to calculate
# the expectation value for and a quantum circuit for enerating the statevector.

qubit_op = PauliOp(Pauli('IIZZIZZZ')) + PauliOp(Pauli('IIXXXZZX')) 
start = time.time()
qc = QuantumCircuit(8)
for i in range(4):
    for j in range(7): 
        qc.cx(j, j+1)
    qc.rz(pi/2, 7)
    for j in range(7)[::-1]: 
        qc.cx(j, j+1)
    qc.h([*range(8)])

qc = transpile(qc)  
print(f'time took to build the circuit: {time.time() - start}s')

start = time.time()
simulator = StatevectorSimulator()
result = simulator.run(qc).result()
psi = result.get_statevector()
qubit_mat = qubit_op.to_matrix()
qubit_op_ev = conjugate(psi.T) @ qubit_mat @ psi
t1 = time.time() - start
print(f'time to calculate expectation value using StatevectorSimulator: {round(t1, 4)}s')

start = time.time()
simulator = QasmSimulator()
QI = QuantumInstance(simulator, shots=1000)
psi = CircuitStateFn(qc)
ms = StateFn(qubit_op, is_measurement=True) @ psi
pe = PauliExpectation().convert(ms)
samp = CircuitSampler(QI).convert(pe).eval().real
t2 = time.time() - start
print(f'time to calculate expectation value using Qasmsimulator: {round(t2, 4)}s')

print(f'QasmSimulator is {t2/t1} times slower than StatevectorSimulator')

Running this code gives the following output:

time took to build the circuit: 0.9908s
time to calculate expectation value using StatevectorSimulator: 0.0074s
time to calculate expectation value using Qasmsimulator: 0.2886s
QasmSimulator is 38.8306 times slower than StatevectorSimulator

It's somewhat surprising to me that QasmSimulator is that much slower (I would have thought it would be faster). So I guess I have two questions:

  1. Is this the expected behavior for Qasmsimulator to be slower?
  2. If not, what is it I'm doing wrong?
$\endgroup$
1
$\begingroup$

The thing is that you're doing more than just running the circuit in both blocks and the QasmSimulator block performs more operations than the StatevectorSimulator one. I edited you're code so both simulators just run the circuit and no further processing of the data is done. It looks like this:

start = time.time()
simulator = StatevectorSimulator()
result = simulator.run(qc).result()
t1 = time.time() - start
print(f'time to calculate expectation value using StatevectorSimulator: {round(t1, 4)}s')

start = time.time()
simulator = QasmSimulator()
result = simulator.run(qc, shots=1000).result()
t2 = time.time() - start
print(f'time to calculate expectation value using Qasmsimulator: {round(t2, 4)}s')

print(f'QasmSimulator is {t2/t1} times slower than StatevectorSimulator')

Running this (and leaving the rest of your code the same), I get the following output:

time took to build the circuit: 0.009701251983642578s
time to calculate expectation value using StatevectorSimulator: 0.0033s
time to calculate expectation value using Qasmsimulator: 0.0026s
QasmSimulator is 0.7906874588183616 times slower than StatevectorSimulator

In this case, QasmSimulator is faster than StatevectorSimulator. Running this several times, you will see that sometimes the QasmSimulator is slower, but the difference is very small compared to the factor of 38 in your question.

$\endgroup$
4
  • $\begingroup$ This is a fair point. But I guess part of my question is what is the correct way of getting expectation values when using QasmSimulator that would run in similar time to StatevectorSimulator. Or is it always gonna be inherently slower when it comes to calculating expectation values? $\endgroup$ – A. Jahin Jun 28 at 17:53
  • $\begingroup$ That I'm not sure of. You could ask another question asking for the most efficient way of calculating expectation values with the QasmSimulator, but unfortunately I don't have the experience/knowledge to answer that. $\endgroup$ – epelaaez Jun 28 at 17:57
  • 1
    $\begingroup$ @A.Jahin to calculate the expectation with qasm_sim you have to run it with many shots... the accuracy of the expectation varies with respect to the number of shots... $\endgroup$ – KAJ226 Jun 28 at 18:58
  • $\begingroup$ Sure, but I'm only using 1000 shots, which hardly seems excessive. $\endgroup$ – A. Jahin Jun 28 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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