Setup
You need Python 3.9 or newer. Create a virtual environment and install Qiskit and its high-performance simulator:
python -m venv .venv && source .venv/bin/activate
pip install qiskit qiskit-aer
python -c "import qiskit; print(qiskit.__version__)"The examples below were run with Qiskit 2.6 and Aer 0.18. The core API (QuantumCircuit, Statevector, AerSimulator) has been stable across 1.x and 2.x, but if something fails, check the version first.
Gates Are Matrices
A gate transforms the state vector by matrix multiplication. Four gates cover most introductory work:
| Gate | Effect | Think of it as |
|---|---|---|
X | Swaps |0⟩ and |1⟩ | The NOT gate |
Z | Leaves |0⟩, multiplies |1⟩ by −1 | A pure phase flip (invisible to a direct measurement) |
H (Hadamard) | Turns |0⟩ into (|0⟩+|1⟩)/√2 | Creates an equal superposition; applying it again undoes it |
CX / CNOT | Flips the target qubit if the control is |1⟩ | A conditional NOT; the gate that creates entanglement |
All quantum gates are reversible (unitary): no information is lost until you measure. That is a real constraint compared with classical logic, where AND and OR throw information away.
Your First Circuit: Superposition and Interference
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
qc = QuantumCircuit(1)
qc.h(0)
print(Statevector.from_instruction(qc))
# Statevector([0.70710678+0.j, 0.70710678+0.j], dims=(2,))
hh = QuantumCircuit(1); hh.h(0); hh.h(0)
clean = lambda d: {str(k): round(float(v), 3) for k, v in d.items() if v > 1e-9}
print(clean(Statevector.from_instruction(hh).probabilities_dict())) # {'0': 1.0}
hzh = QuantumCircuit(1); hzh.h(0); hzh.z(0); hzh.h(0)
print(clean(Statevector.from_instruction(hzh).probabilities_dict())) # {'1': 1.0}Statevector.from_instruction computes the exact state without measuring, which is only possible on a simulator and is ideal for learning. The last two lines show interference: H then H returns to |0⟩, while H, Z, H lands on |1⟩.
Entanglement: The Bell Pair
Put qubit 0 in superposition with H, then use it as the control of a CNOT on qubit 1. The result is the Bell state (|00⟩ + |11⟩)/√2. Neither qubit has a definite value on its own, yet the two results always agree.
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
bell = QuantumCircuit(2, 2) # 2 qubits, 2 classical bits for results
bell.h(0)
bell.cx(0, 1)
bell.measure([0, 1], [0, 1])
print(bell.draw(output="text"))
sim = AerSimulator()
result = sim.run(transpile(bell, sim), shots=1000, seed_simulator=7).result()
print(result.get_counts()) # {'11': 511, '00': 489}You only ever see 00 or 11, each about half the time, and never 01 or 10. The two measurement results are perfectly correlated even though each one alone is random. Entanglement is not a way to send messages (the individual results are random, so nothing is communicated), but the correlations are stronger than any classical shared-coin explanation allows, and many algorithms depend on them.
The Bit-Ordering Trap
Qiskit prints bitstrings with qubit 0 on the right. A result of '110' means qubit 2 = 1, qubit 1 = 1, qubit 0 = 0. This is little-endian, matching how integers are written, and it is the opposite of how circuit diagrams list qubits (qubit 0 on top). If you prepare qubit 0 with an X gate on a 3-qubit circuit and see '001', nothing is wrong. Always check ordering before you decide an algorithm is buggy.
Shots, Seeds and Reproducibility
- Shots are repetitions of the whole circuit. Counts are statistical, so 1000 shots of a fair coin gives something like 489/511, not exactly 500/500.
- seed_simulator makes simulator runs repeatable, which you want in tests. Real hardware has no such option.
- Estimating a probability to precision ε takes roughly 1/ε² shots. Distinguishing 0.50 from 0.52 needs thousands of shots.
From Simulator to Real Hardware
Running on a real device uses the same circuit, with extra steps that the simulator hides:
- Transpilation: your gates are rewritten into the small native gate set of the chip and mapped onto its physical qubit layout, which adds extra operations.
- Queueing: cloud devices are shared; jobs wait in a queue. Providers such as IBM Quantum, Amazon Braket and Azure Quantum offer access, and some have free tiers whose limits change, so check the current terms.
- Noise: every gate and measurement has a non-zero error rate. A Bell pair on real hardware also returns a few
01and10results. Deep circuits accumulate error, which is the central engineering problem covered in the hardware article.
Practice Exercises
- Build a 3-qubit GHZ state: H on qubit 0, then CNOT from 0 to 1 and from 0 to 2. Predict the counts before running.
- Apply X to qubit 0 only on a 3-qubit circuit and confirm you see
'001'. - Replace the CNOT in the Bell circuit with a CZ gate (
bell.cz(0, 1)). Why do you now see 00 and 01? (Hint: CZ only changes a phase, so qubit 1 never leaves |0⟩, and qubit 0 is the rightmost bit of each result.)
Frequently Asked Questions
What is the difference between a gate and a measurement?
A gate is a reversible matrix operation that transforms the quantum state without revealing it. Measurement is irreversible: it samples one outcome and collapses the state.
Why does Qiskit show bitstrings in reverse order?
Qiskit uses little-endian ordering, so qubit 0 is the rightmost bit of a result string. Circuit diagrams list qubit 0 at the top, which makes the two views look reversed.
Does entanglement let me send information faster than light?
No. Each qubit's measurement result is random on its own, so no message is transmitted. Only comparing the two results afterwards reveals the correlation, and that comparison needs ordinary communication.
Do I need a real quantum computer to learn Qiskit?
No. The Aer simulator runs the same circuits on your laptop with exact or sampled results. Real devices add noise and queue time but are not required to learn gates and algorithms.
Why do real devices return results that a simulator never does?
Physical gates and measurements have error rates, so a perfect Bell circuit on hardware also yields a small fraction of 01 and 10 outcomes. Simulators are noise-free unless you add a noise model.