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$

2 Answers 2

2
$\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$
2
$\begingroup$

Once you have the .qasm, you can also upload it to the Classiq platform https://platform.classiq.io/circuit, and then run it on a simulator.

Here's how to do this step by step:

Step 1: Create and save the .qasm file:

from qiskit import *
circuit = QuantumCircuit(2,2)
circuit.h(0)
circuit.cx(0,1) 
circuit.measure([0,1], [0,1]) 
circuit.qasm(filename = "my_bell_state.qasm")

Step 2: Upload the .qasm file to https://platform.classiq.io/circuit

Upload the .qasm file to the IDE

Then, press the "execute" button on the upper right hand side Bell state circuit

Step 3: Run the circuit on the simulator:

Choose a simulator on the left hand menu. You can choose a "regular" (shots) simulator or a state vector simulator. In this case you are looking for a shots simulator. enter image description here

Step 4: Run the circuit on the simulator:

Press run, and you can get the histogram as well as download the results. enter image description here

$\endgroup$

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