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:
- Is this the expected behavior for
Qasmsimulator
to be slower? - If not, what is it I'm doing wrong?