Difference in state vector simulator and qasm simulator

Difference in qasm and state vector simulatiuon

Difference in qasm simulation and state vector simulatiuon

I have come across two kinds of simulation while I am leanring Qiskit and later on I encounter people asking what are their differences. So I make this notebook for my self-record and share to other what is the difference in visualization. To start with Qiskit, we first impoort package we want for manipulation.

To give a brief introduction. Qasm Simulator is to mimic an actual device. It executes the quantum circuit and returns a count which is the values of the classical registers. You can think of register as class of bit or qubit. The simulator only provide the measurement outcome.

In [1]:
from qiskit import QuantumCircuit, execute, Aer, BasicAer, IBMQ, QuantumRegister, ClassicalRegister
from qiskit.tools.jupyter import *
from qiskit.visualization import *
from qiskit.tools.visualization import plot_bloch_multivector
from qiskit.visualization import plot_state_hinton
get_ipython().run_line_magic('matplotlib', 'inline')

Then we create quantum circuit qc with two qubits and two classical bits in it, they are respectively called qr and cr.

In [2]:
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
qc = QuantumCircuit(qr, cr)

Before the measreuement

In the circuit, I put the Hadamard gate actin on the 0th qubit, CNOT gate on the oth qubit as control bit and 1st qubit as the target bit. After that, I print the circuit diagram using matplot lib as output. This will create our standard entangled bell state.

IMG_4229E3997B22-1.jpeg

In [3]:
qc.h(qr[0]) #H gate applied on qubit 0
qc.cx(qr[0], qr[1])
qc.draw(output='mpl')
Out[3]:

After that, let's use the QASM simulator first.

QASM Simulator

We define the qasm simulator backend in Aer backend and put it to backend. We execute qc by backend and get the result, we call it job. Finally, we plot the histogram.

In [4]:
backend = Aer.get_backend('qasm_simulator')
job = execute(qc, backend=backend).result()
plot_histogram(job.get_counts(qc))
Out[4]:

We try to examine the measurement outcome for each individual shot for 10 shots and see that all are in |00> state.

In [5]:
# Execute and get memory
result = execute(qc, backend, shots=10, memory=True).result()
memory = result.get_memory(qc)
print(memory)
['00', '00', '00', '00', '00', '00', '00', '00', '00', '00']

We totally perform 1024 shots, all the results are |00>.

In [6]:
print(job.get_counts(qc))
{'00': 1024}

In the above picture, we do not have any measurement after applying the qasm simulator. So, by observation, it is just giving you all counts on the default value of the classical register, which is 00. After that, let's try statevector simulator.

State Vector Simulator

We follow the similar procedure as above. We define the qasm simulator, but with different name as the output. it prints the state in superposition of entangled state.

In [7]:
#simulator = Aer.get_backend('statevector_simulator')

backend = Aer.get_backend('statevector_simulator')

job = execute(qc, backend)
result = job.result()
outputstate = result.get_statevector(qc, decimals=3)
print(outputstate)
[0.707+0.j 0.   +0.j 0.   +0.j 0.707+0.j]
In [8]:
# Execute and get memory
result = execute(qc, backend, shots=10, memory=True).result()
memory = result.get_memory(qc)
print(memory)
['00']

The above printed output corresponds to the real and imaginary component of the state |00>, |01>, |10>, |11>. As expected from our above working, we find that we only have |00> and |11> as our output. Now we want to visualize the sate by calling plot_state_city in our outputstate.

In [9]:
from qiskit.visualization import plot_state_city
plot_state_city(outputstate)
Out[9]:

State vector simulator returns the state of your qubits, so if you run it before measurement, it will return the qubit state in superposition; if you run it after measurement, it will return the projected qubit. In other words, you get the result on the quantum register. So, you will only have one state as shown below. The presentation of Qasm and State vector simulation provide different results before and afer measuement. Plotting the state vector is the standard view for quantum states where the real and imaginary (imag) parts of the state matrix are plotted like a city.

After Measurement

In this part, we perform the simulation after the measurement and observe the differences. So we now we measure on the quantum circuit qc and put the value of quantum register qr to the classical register cr. Then we print the circuit diagram. We see the symobl like a clock transfer the infomation from quantum register to the classical register.

In [10]:
qc.measure(qr, cr)
qc.draw(output='mpl')
Out[10]:

QASM Simulator

In this part, we just follow the similar procedure as above. We plot the result that is different from that before the measurement.

In [11]:
backend = BasicAer.get_backend('qasm_simulator')
job = execute(qc, backend=backend).result()
plot_histogram(job.get_counts(qc))
Out[11]:

Within this 10 shots, we have 5 |00> and 5 |11>. Both have probability of 1/2 to get the state.

In [12]:
# Execute and get memory
result = execute(qc, backend, shots=10, memory=True).result()
memory = result.get_memory(qc)
print(memory)
['00', '00', '00', '00', '00', '11', '00', '00', '11', '00']

We totally perform 1024 shots, we have 529 |00> and 495 |11>.

In [13]:
print(job.get_counts(qc))
{'11': 506, '00': 518}

State Vector Simulator

In this simulator, however, we only see one city in the output.

In [14]:
#simulator = Aer.get_backend('statevector_simulator')

backend = Aer.get_backend('statevector_simulator')

job = execute(qc, backend)
result = job.result()
outputstate = result.get_statevector(qc, decimals=3)
print(outputstate)
[0.+0.j 0.+0.j 0.+0.j 1.+0.j]

After the measurement, we only have one state, which is |00>.

In [15]:
print(result.get_counts(qc))
{'11': 1}
In [16]:
from qiskit.visualization import plot_state_city
plot_state_city(outputstate)
Out[16]:

If we excute the simulation one more time, we may get |11>. SO for the Bell-state circuit, the final statevecor will be either |00> or |11>.

In [17]:
# Execute and get memory
result = execute(qc, backend, shots=10, memory=True).result()
memory = result.get_memory(qc)
print(memory)
['11']

To sum up, for the statevector simulator, we will have qubit state in superposition before measurement and we will have projected qubits after the measurement, i.e., the resullt of the quantum register.

For the qasm simulator, we will have the counts on the calssical register, so we no measurement on the circuit so it will just return the default value of the classical register. After the measurement, we will have the result of the classical register of individually accumulated counts.

We see the result of qasm simulator and statevector simulator reverse before and after the measurement. So when you implement the simulator, be aware of when should you do it by knowing what kind of result you want.