1. Home
  2. Quantum Computing
  3. Qiskit Tutorial: Your First Quantum Circuit in Python
Quantum Computing

Qiskit Tutorial: Your First Quantum Circuit in Python

Published on November 6, 2026 · by Qubira 11 min read
Qiskit Tutorial: Your First Quantum Circuit in Python

You don’t need a lab, a physics degree or an account anywhere to run a quantum program. If you can install a Python package, you can build a quantum circuit with Qiskit, run it a thousand times and watch two qubits behave in a way no pair of coins ever could. This guide takes you through the whole thing: setup, the circuit, the results and the usual stumbles. The code here is a small example we ran ourselves on a laptop simulator before putting it in the article, and we flag the places where versions of the library tend to differ.

10 min
What you'll buildA two-qubit Bell state, the "hello world" of quantum computing: five lines of Python, one simulator run, and a histogram that shows only 00 and 11. It is also the smallest working demonstration of entanglement.

Why Qiskit, and what you need

Qiskit is an open-source Python toolkit for writing quantum programs, started by IBM and now used well beyond it. You describe a circuit (qubits, then gates applied to them, then measurements), and Qiskit can run it on a simulator or send it to real quantum hardware. Other toolkits exist, among them Google’s Cirq and Xanadu’s PennyLane, and each has fans. We start people on Qiskit because it has the largest collection of tutorials, the most forum answers when you get stuck, and a direct road to real hardware through IBM Quantum’s free tier, which we covered in our guide to trying a quantum computer for free.

What you need is modest: a reasonably recent Python 3 and a terminal. If you have never touched Python, an afternoon with a beginner tutorial will pay off first. And if the word qubit still feels slippery, spend ten minutes on what a qubit is, since the whole point of this exercise is to see those ideas in action.

Create a virtual environment so the install stays tidy, then install two packages:

python -m venv quantum-env
source quantum-env/bin/activate      # on Windows: quantum-env\Scripts\activate
pip install qiskit qiskit-aer

The first package, qiskit, is the core library for building circuits. The second, qiskit-aer, is a fast simulator that runs on your CPU. Splitting them is a design choice that trips people up: older tutorials import the simulator from qiskit directly, and that no longer works. If you copy code from a three-year-old blog post and hit an import error, this is almost always why.

The circuit, one line at a time

Create a file called bell.py and start with the imports and the circuit itself:

from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator

qc = QuantumCircuit(2)   # two qubits, both start in state 0
qc.h(0)                  # Hadamard gate on qubit 0
qc.cx(0, 1)              # CNOT: control = qubit 0, target = qubit 1
qc.measure_all()         # measure every qubit at the end

print(qc.draw("text"))

Run it with python bell.py and you get a little text diagram of what you just wrote:

        ┌───┐      ░ ┌─┐   
   q_0: ┤ H ├──■───░─┤M├───
        └───┘┌─┴─┐ ░ └╥┘┌─┐
   q_1: ─────┤ X ├─░──╫─┤M├
             └───┘ ░  ║ └╥┘
meas: 2/══════════════╩══╩═
                      0  1 

Read it left to right like sheet music. Each horizontal line is a qubit, and time flows along it. Both qubits begin in the state 0. The box labelled H is the Hadamard gate, which takes qubit 0 and puts it into an even superposition: if you measured it now, you’d get 0 or 1 with equal probability. The dot and the circled X form the CNOT gate, short for controlled-NOT. It flips the second qubit if, and only if, the first one is 1. The dashed barrier (the column of ░ symbols) is just a visual separator added by measure_all(), and the two M boxes are the measurements, writing results into two classical bits.

Here is the part worth slowing down for. After the Hadamard, qubit 0 is “both” in the loose popular sense. The CNOT then acts on that superposition, and because the control is in a mix of 0 and 1, the target ends up in a mix as well, but a correlated one. The state of the pair can’t be described as “qubit 0 is in this state and qubit 1 is in that state” any more; there’s only a joint state, which says: either both are 0, or both are 1, each with probability one half. That’s an entangled pair, the same Bell state we described in quantum entanglement explained simply.

Running it on the simulator

Add these lines at the bottom of the file:

sim = AerSimulator()
compiled = transpile(qc, sim)
result = sim.run(compiled, shots=1000).result()
print(result.get_counts())

The transpile step rewrites your circuit into the gates a given backend understands natively. For a simulator it hardly changes anything, but on real hardware it matters a great deal, so we include it from day one to build the habit. shots=1000 tells the backend to run the circuit a thousand times from scratch, since a single run only gives one random-looking answer.

On our run the output was:

{'00': 495, '11': 505}

Yours will differ by a few counts, and it should. Measurement is probabilistic, so you’ll see something like 487 and 513 one time and 502 and 498 the next, just as a thousand fair coin flips never land on exactly 500 heads. What should never appear on an ideal simulator is 01 or 10. Take a moment with that. Each qubit on its own looks like a perfectly fair coin, yet the two always agree. No pair of ordinary coins behaves like that unless someone is cheating, and here nobody is.

One detail that confuses newcomers: Qiskit prints bit strings with qubit 0 on the right. In this symmetrical example you can’t tell, but as soon as you build asymmetrical circuits the string '01' means qubit 1 gave 0 and qubit 0 gave 1. It’s a convention, it takes a few days to feel natural, and it’s the cause of more “my circuit is wrong” forum posts than any real bug.

Part of the codeWhat it doesCommon mistake
QuantumCircuit(2)Creates two qubits, both starting at 0Forgetting that classical bits are separate from qubits
qc.h(0)Puts qubit 0 in superpositionExpecting it to “store” 0 and 1 you can read later
qc.cx(0, 1)Links qubit 1 to qubit 0Swapping control and target
qc.measure_all()Reads out all qubitsMeasuring before the entangling gate
shots=1000Repeats the run 1,000 timesUsing one shot and trusting the answer

Seeing the histogram

Numbers are fine, but a picture lands better. Qiskit has a plotting helper that needs matplotlib:

pip install matplotlib
from qiskit.visualization import plot_histogram
counts = result.get_counts()
fig = plot_histogram(counts)
fig.savefig("bell.png")

You’ll get two tall bars at 00 and 11 and an empty space where 01 and 10 would be. That empty space is the whole point of the exercise. If you’re working inside a Jupyter notebook, you can leave off the save line and the figure shows up under the cell.

💡 Tip. Try removing the qc.cx(0, 1) line and run again. Now the qubits are independent and you'll see all four outcomes (00, 01, 10, 11) at roughly 25% each. Adding that single gate is what removes two of them. Nothing teaches entanglement faster than breaking it on purpose.

Three experiments worth doing next

Once the Bell state works, small variations teach more than another article does. Here are the ones we’d suggest, in the order we’d try them.

First, change the number of shots to 10, then 100, then 100,000, and watch the counts settle toward half and half. This is a feel for statistics you’ll need later, because everything on real hardware is a distribution rather than a single answer.

Second, extend the circuit to three qubits by adding qc.cx(1, 2) after the first CNOT and creating the circuit with QuantumCircuit(3). You’ll get only 000 and 111, a GHZ state. It is the same idea stretched across more qubits, and it shows how entanglement spreads along a chain of gates.

Third, swap qc.h(0) for qc.x(0), which simply flips qubit 0 to 1 with certainty. The output becomes 11 every time, and you’ve just seen the difference between a classical operation and a quantum one. A deterministic gate gives one answer; a Hadamard gives a coin toss, and the CNOT carries that coin toss onto the second qubit.

After those three, the natural next topics are the other single-qubit gates (Z, S, T and the rotations), phase, and then small algorithms such as Deutsch-Jozsa and Grover’s search. A structured course will carry you through them much faster than ad-hoc searching, and our list of free quantum computing courses for beginners shows which ones lean on Qiskit. If you prefer paper, the quantum programming books review compares the titles we think are worth the shelf space.

Difficulty of this first circuit
Easy
Setup friction (version mismatches)
Moderate
Insight per minute spent
High
Closeness to real hardware results
Ideal only

These bars are our editorial impression of the exercise, not measurements. The last one is the honest caveat: an ideal simulator is noise-free, and real devices are not.

From laptop to real hardware

When you’re ready to leave the simulator, IBM Quantum offers free access to real processors with usage limits that have changed over the years, so check their current terms rather than trusting any article, ours included. The workflow is the same: you save an API token from your account, connect through Qiskit’s IBM runtime package, pick a backend, and send the circuit. It sits in a queue, sometimes for minutes, sometimes longer, then returns counts.

Expect the results to disappoint you in a useful way. Instead of a clean 50/50 between 00 and 11, you’ll see a few percent of 01 and 10. That’s noise: gates aren’t perfect, qubits lose their state over time, and the readout itself makes mistakes. It’s a miniature version of the problem quantum error correction is trying to solve at scale, and it explains why a headline qubit count tells you far less than how well the qubits behave. Running a Bell state on both a simulator and a real chip, and putting the two histograms side by side, is probably the most instructive afternoon a newcomer can spend. For the bigger picture of what these machines are actually good for, start from our pillar guide, quantum computing explained.

Common stumbles, and what usually fixes them

ImportError on the simulator almost always means you’re following an old tutorial or you installed qiskit without qiskit-aer. Install the second package and import from qiskit_aer. An error saying a method doesn’t exist after you copied code usually means the library moved on; look at the official documentation for the equivalent in your installed version, and run pip show qiskit so you know which one that is.

If the counts look wrong, check whether you measured before entangling, and remember the right-to-left bit ordering. If nothing prints at all, make sure you called .result().get_counts() and printed it, because run returns a job object, not numbers. And if your machine is old, don’t worry: two qubits barely register, though simulating thirty or so qubits will start to hurt, since memory use doubles with every qubit you add. That doubling is, incidentally, a hands-on way to feel why quantum computers are interesting in the first place.

  • Install qiskit and qiskit-aer in a virtual environment; the simulator lives in its own package.
  • A Hadamard plus a CNOT gives you a Bell state: results of 00 and 11 only, about half each.
  • Measurement is probabilistic, so run many shots and read the distribution, not a single result.
  • Qiskit prints bit strings with qubit 0 on the right, a classic source of confusion.
  • Real hardware adds noise, which is the best free lesson in why quantum computing is hard.

You’ve now done something that sounds intimidating and turned out to be a short Python file. From here it’s mostly a matter of curiosity: change a gate, predict the result before you run it, and see whether you were right. Being wrong a few times, and understanding why, is the fastest way we know to start thinking like the people who build these machines.

Want to know what each gate in this circuit really does? Read our guide to quantum logic gates: Hadamard, CNOT and Pauli explained.

FAQ

Do I need a quantum computer to run Qiskit code?
No. Qiskit ships with simulators, and the Aer simulator runs on your own laptop. A two-qubit circuit like the one in this guide finishes in a fraction of a second. Real hardware is optional, and you can add it later through IBM Quantum's free access.
Which Python version and packages do I need?
A recent Python 3 and two packages: qiskit and qiskit-aer. Install them inside a virtual environment so they don't clash with other projects. If an example fails after a Qiskit update, check the official documentation, because the library has changed its interfaces between major versions.
Why do I get 00 and 11 but never 01 or 10?
Because the two qubits are entangled. The Hadamard gate puts the first qubit in superposition, and the CNOT gate ties the second one to it. Each qubit alone looks like a fair coin, but the pair always agrees. On an ideal simulator the split is close to 50/50 with nothing in between.
Why aren't my counts exactly 500 and 500?
Measurement is probabilistic. With 1,000 shots you will see numbers like 487 and 513, just as 1,000 coin flips rarely give exactly 500 heads. Increase the shots and the proportions get closer to one half each.
What is a shot?
One complete run of the circuit from start to measurement. Because each run gives a single random-looking outcome, you repeat the circuit many times and look at the distribution of results.
What changes when I run this on real hardware?
You will see small amounts of 01 and 10, caused by noise and imperfect gates. Real chips also need the circuit rewritten for their native gates and qubit layout, which Qiskit's transpiler does for you. Seeing the noise is part of the learning.
Do I need to know the maths of quantum mechanics first?
Not to get started. If you know what a qubit is, you can build and run circuits and learn the linear algebra as questions come up. Most people find it easier to learn the maths after they have watched a few circuits behave.