Statevector Simulator is a simulator provided by Qiskit that calculates and returns the statevector for Qubits in the Quantum Circuit. It is useful when the states of various Qubits in the Quantum Circuit need to be visualized. In this chapter of the Qiskit tutorial, you will learn about Statevector Simulator and how you can use the Statevector Simulator from the BasicAer
module of qiskit.
Statevector Simulator calculates and returns the statevector of Qubits. Statevector Simulator is useful when studying and understanding the states of Qubits in a Quantum Circuit which are collapsed upon measuring the Qubits.
Selecting the Statevector Simulator
The Statevector Simulator from BasicAer
module can be selected using the get_backend()
function.
from qiskit import BasicAer
backend = BasicAer.get_backend('statevector_simulator')
Creating a Quantum Circuit
Statevector simulator is useful only when the state of Qubits in the Quantum Circuit need to be visualized and the Quantum Circuit does not contain any measurement instruction.
from qiskit import QuantumCircuit
qc = QuantumCircuit(2)
qc.h(0)
qc.h(1)
Note– No measurement instruction is a part of the Quantum Circuit. Since the only job of bits is to store the results of Qubits once they are measured, no bits are required in the Quantum Circuit.
The Quantum Circuit specified above puts both the Qubits in the state of superposition. The state ψ
of the Quantum Circuit can be written as-
ψ = 1/sqrt(4) [ |00> + |01> + |10> + |11> ]
Note– sqrt(4)
denotes square-root of 4, i.e, 2.
Simulating Quantum Circuit through Statevector Simulator
We will stimulate the Quantum Circuit we just created.
Creating a Job
First we execute the Quantum Circuit we created on the Statevector Simulator backend. This creates a job to execute the Quantum Circuit which is stored as an object in the variable job
.
from qiskit.execute_function import execute
job = execute(qc, backend)
Getting Results
job
object contains information related to the job that was executed on the Statevector Simulator. The result of the simulation can be obtained by calling the result()
method on job
. The result is stored as an object in the variable result
.
result = job.result()
The state of Qubits can be obtained from the result
variable by calling the get_statevector()
methods on result
.
state_vector = result.get_statevector()
# Print the statevector
print(state_vector)
The output of this will be-
[0.5+0.j 0.5+0.j 0.5+0.j 0.5+0.j]
Notice that the result from the Statevector Simulator are as expected. The coefficients of |00>
, |01>
, |10>
, and|11>
in the statevector were all supposed to be equal to 1/2, which happens to be the case.
Plotting the Results
The results from the Statevector Simulator can be plotted by using a Bloch multi-vector. Learn More about plotting bloch multi-vector using Qiskit
The results from the Statevector Simulator can also be plotted by using a Q Sphere. Learn More about plotting Q Sphere using Qiskit
You will learn about Qiskit Visualization Tools in later chapters.