Your First Quantum Circuit
Track: Quantum Programming · Difficulty: Beginner · Est: 13 min
Your First Quantum Circuit
Overview
This page introduces the programming concept of a complete experiment loop:
- build a circuit
- measure it
- run it on a simulator
- interpret results
It answers: How do I create and run a one-qubit circuit in code?
Conceptual Mapping
From theory:
- Start in
- Apply a gate (like )
- Measure to get a classical outcome
- Repeat many times to estimate probabilities
In Qiskit-shaped code:
- a
QuantumCircuitstores operations in order qc.h(0)adds a gate on qubit 0qc.measure(0, 0)maps qubit 0 to classical bit 0- a simulator backend executes the circuit and returns counts
Code Walkthrough
Minimal, readable code (one file) to run on a simulator:
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
qc = QuantumCircuit(1, 1)
qc.h(0)
qc.measure(0, 0)
sim = AerSimulator()
result = sim.run(qc, shots=1000).result()
counts = result.get_counts()
print(counts)Line by line:
QuantumCircuit(1, 1)creates one qubit and one classical bit.h(0)applies a Hadamard: it prepares a superposition.measure(0, 0)says: store the measurement of qubit 0 into classical bit 0.AerSimulator()chooses a simulator backend.run(..., shots=1000)repeats the experiment many times.get_counts()returns a dictionary like{ '0': ..., '1': ... }.
Interpreting results:
- You should see both
0and1appear. - The exact split varies run to run.
- More shots gives a more stable estimate of probabilities.
What Happens Under the Hood
Conceptually, the simulator is doing what you did by hand in Foundations:
- represent the state internally
- apply a gate update for
H - sample measurement outcomes according to the state’s probabilities
- repeat sampling for many shots
The key point: even on a simulator, measurement results are produced by sampling. So you’ll still see randomness (because that’s part of the model).
Turtle Tip
A quantum circuit is an experiment description. Shots are repeated trials of the same experiment. If your results look “random,” that can be correct behavior.
Common Pitfalls
- Forgetting to add measurement and then wondering why there are no classical results.
- Expecting exactly equal counts; sampling noise is normal.
- Using too few shots and over-interpreting the output.
- Mixing up qubit indices and classical bit indices.
Quick Check
- What does
shotsmean in the simulator run? - Why do you usually see both
0and1after applyingHthen measuring? - What does
qc.measure(0, 0)express conceptually?
What’s Next
Next we’ll slow down and look at circuits, gates, and measurements as programming objects. You’ll learn how operation order and measurement mapping show up explicitly in code.
