1
$\begingroup$

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))

enter image description herep.png

$\endgroup$

1 Answer 1

1
$\begingroup$

Although the function runs correctly, some functionalities of qiskit have been deprecated, which might've been causing this error. Do this instead for your code:

Basic Imports

Make sure you have the updated qiskit, and install it

pip install qiskit pip install qiskit-aer

from qiskit import *
from qiskit_aer import Aer
from qiskt.primitives import BackendSampler
from qiskit.visualization import plot_distribution

Making the Quantum Circuit

circuit = QuantumCircuit(2,2)
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 the Quantum Circuit

simulator = Aer.get_backend('qasm_simulator')
sampler = BackendSampler(backend=simulator)
result = sampler.run(circuit).result().quasi_dists[0]

Plotting the result

plot_distribution(result.binary_probabilities(),figsize(15,5))

This is the result you'll get enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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