I am using this code, as a test of my qiskit, but I am having errors on using the simulator
Building the circuit
We begin by importing Qiskit.
from qiskit import *
In the video, we initialized two qubits into a QuantumRegister and two classical bits into a ClassicalRegister by writing
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
Then we built a QuantumCircuit composed of the two classical and quantum bits by saying
circuit = QuantumCircuit(qr, cr)
The above three lines of code can be simplified into one line by writing:
circuit = QuantumCircuit(2,2)
the first argument is the number of quantum bits, and the second is the number of classical bits. This is the recommended way of creating circuits.
Once you create your circuit, you can draw it at any point by writing
circuit.draw(output='mpl')
For a plot based on matplotlib, use instead
%matplotlib inline
circuit.draw(output='mpl')
You only need to run the %matplotlib inline command once in your notebook.
Next, let's add the gates.
# The quantum circuit has two qubits. they are indexed as qubits 0 and 1
circuit.h(0)
circuit.cx(0,1) # order is control, target
circuit.measure([0,1], [0,1]) # qubits [0,1] are measured and results are stored in classical bits [0,1] in order
circuit.draw(output='mpl')
Running on a simulator
We begin by setting the simulator to qasm_simulator
from the Aer element of Qiskit.
simulator = Aer.get_backend('qasm_simulator')
Then, we execute the circuit on the simulator and store the results in a variable called result.
result = execute(circuit, backend=simulator).result()
At this point, we are ready to plot the results of the simulator execution. To do this, first import the visualization tools from Qiskit using
from qiskit.visualization import plot_histogram
# Then, we plot the results using
plot_histogram(result.get_counts(circuit))