Skip to content

Qiskit “cannot import name ‘execute’” Error — Explained and Fixed

🚨 The Problem

When running older Qiskit code with a newer version of the library, you may encounter the following error:

ImportError: cannot import name 'execute' from 'qiskit'

This happens because recent versions of Qiskit (v1.0 and later) have removed the execute() function from the main package. It was part of the older API (used before 2024) and has now been replaced by a more modular execution interface.

In short:
➡️ Old code expects execute() to exist.
➡️ New Qiskit doesn’t include it anymore.


🧠 Why It Happens

Qiskit underwent a major redesign to make its API simpler and more modular.
The goal was to separate simulation and execution tools from the core circuit framework.
As a result:

  • execute() is no longer imported directly from qiskit.

  • The Aer simulator now uses a class-based run() method.

  • Users are encouraged to import simulation and backend tools explicitly from qiskit_aer or other submodules.


🧩 Example of the Old Code

If your script looks like this:

from qiskit import QuantumCircuit, Aer, execute

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()

backend = Aer.get_backend('qasm_simulator')
result = execute(qc, backend).result()
print(result.get_counts())

…it will no longer work with new Qiskit releases.


✅ The Modern Fix

Use the new AerSimulator interface provided by the qiskit-aer module:

from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator

# Create circuit
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()

# Simulate
simulator = AerSimulator()
result = simulator.run(qc).result()

# Output result
counts = result.get_counts()
print(counts)

This version:

  • Works with Qiskit 1.0+

  • Uses the modern execution API

  • Produces the same results as before


⚙️ Alternative: Downgrade (Not Recommended)

If you prefer to keep old syntax for compatibility reasons, you can downgrade Qiskit:

pip uninstall qiskit
pip install qiskit==0.43.3

However, this approach is not future-proof, since older Qiskit versions will eventually lose support and updates.


🧭 Summary

Aspect Old Qiskit New Qiskit
Execution method execute() run()
Module import from qiskit import execute from qiskit_aer import AerSimulator
Version range ≤ 0.43.x ≥ 1.0
Recommended? ❌ Deprecated ✅ Supported

💡 Final Advice

If you are learning or starting new projects:

  • Use Qiskit ≥ 1.0

  • Always read the latest documentation

  • Replace execute() with run() methods from modern simulators

This small change ensures your quantum programs remain compatible, maintainable, and future-ready.

Leave a Reply

Discover more from Sowft | Transforming Ideas into Digital Success

Subscribe now to keep reading and get access to the full archive.

Continue reading