1

I am using IBM's quantum computing lab, and was following a tutorial made by IBM for getting started, and my code is throwing errors. I followed the tutorial exactly. Here is my code:

#-----------Cell 1:
import numpy as np

# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, transpile, Aer, IBMQ
from qiskit.tools.jupyter import *
from qiskit.visualization import *
from ibm_quantum_widgets import *
from qiskit.providers.aer import QasmSimulator

# Loading your IBM Quantum account(s)
provider = IBMQ.load_account()

#-----------Cell 2:
# Build
#------

# Create a Quantum Circuit acting on the q register
circuit = QuantumCircuit(2, 2)

# Add a H gate on qubit 0
circuit.h(0)

# Add a CX (CNOT) gate on control qubit 0 and target qubit 1
circuit.cx(0, 1)

# Map the quantum measurement to the classical bits
circuit.measure([0,1], [0,1])

# END


# Execute
#--------

# Use Aer's qasm_simulator
simulator = Aer.get_backend('qasm_simulator')

# Execute the circuit on the qasm simulator
job = execute(circuit, simulator, shots=1000)

# Grab results from the job
result = job.result()

# Return counts
counts = result.get_counts(circuit)
print("\nTotal count for 00 and 11 are:",counts)

# END


# Visualize
#----------

# Import draw_circuit, then use it to draw the circuit
from ibm_quantum_widgets import draw_circuit
draw_circuit(circuit)

# Analyze
#--------

# Plot a histogram
plot_histogram(counts)

# END

This code throws this error:

Traceback (most recent call last):
  File "/tmp/ipykernel_59/1801586149.py", line 26, in <module>
    job = execute(circuit, simulator, shots=1000)
NameError: name 'execute' is not defined

Use %tb to get the full traceback.

How do I fix this error?

Here is the tutorial I was following: https://quantum-computing.ibm.com/lab/docs/iql/first-circuit

desertnaut
  • 57,590
  • 26
  • 140
  • 166

1 Answers1

3

You did not import execute from qiskit.

Change

from qiskit import QuantumCircuit, transpile, Aer, IBMQ

to

from qiskit import QuantumCircuit, transpile, Aer, IBMQ, execute
Bob Sutor
  • 71
  • 5