Quantum Computing First Program
Writing a first quantum program turns theory into action. The goal here is simple: build a circuit that creates a perfect random coin flip, run it many times, and read the counts. The whole program fits in under ten lines.
What You Need
- Python installed on your computer
- The Qiskit and Qiskit Aer libraries
- A text editor or notebook
The Plan
A coin flip needs a qubit that lands on 0 or 1 with equal chance. The Hadamard gate creates that blend. The measurement picks one side.
|0> ---[ H ]---[ Measure ]---> 0 or 1 Start: certain 0 After H: 50 percent 0, 50 percent 1 After measure: one definite result
The Code
from qiskit import QuantumCircuit from qiskit_aer import AerSimulator qc = QuantumCircuit(1, 1) qc.h(0) qc.measure(0, 0) sim = AerSimulator() job = sim.run(qc, shots=1000) counts = job.result().get_counts() print(counts)
Step-by-Step Explanation
Step 1: Import the Tools
The first two lines load the circuit builder and the simulator.
Step 2: Create the Circuit
QuantumCircuit(1, 1) makes one qubit and one classical bit. The classical bit stores the measured answer.
Step 3: Add the Gate
qc.h(0) applies the Hadamard gate to qubit 0. The qubit now holds an equal blend.
Step 4: Add the Measurement
qc.measure(0, 0) reads qubit 0 and writes the result into classical bit 0.
Step 5: Run Many Shots
The simulator repeats the circuit 1000 times. Each shot gives one result.
Step 6: Read the Counts
The program prints a dictionary of outcomes.
Sample Output
{'0': 507, '1': 493}
Your numbers will differ slightly on every run. The totals stay near 500 each, which matches the 50-50 prediction. Perfect equality would look suspicious, since real randomness always wobbles a little.
Visual Check With a Chart
Outcome 0 | ##################### 507 Outcome 1 | ################### 493
Upgrade: Make It a Bell Pair
Add a second qubit and a CX gate to see entanglement.
qc = QuantumCircuit(2, 2) qc.h(0) qc.cx(0, 1) qc.measure([0, 1], [0, 1])
| Outcome | Expected Share |
|---|---|
| 00 | About 50 percent |
| 11 | About 50 percent |
| 01 | Zero |
| 10 | Zero |
The two qubits always agree. Neither qubit has a fixed answer before measurement, yet the pair never disagrees.
Troubleshooting
- An import error means a library is missing. Run the pip install command again.
- Strange outcome order comes from Qiskit's right-to-left bit labels.
- A count of 1000 for a single outcome means the Hadamard gate is missing.
Layman Example: Rolling a Magic Coin
Picture a coin that stays in the air spinning until you catch it. You catch it 1000 times and write down heads or tails each time. Your tally ends near 500 each. The program does exactly that with a real quantum rule.
Key Takeaways
A first program proves that quantum computing works on an ordinary laptop. The coin flip shows superposition, the shot count shows probability, and the Bell pair shows entanglement. Change one detail at a time and watch the output. Try three qubits, add an X gate, or reduce the shot count to ten. Small experiments reveal how each gate shapes the result. Save your working files, because later topics build on this same pattern of create, run, and read.
Experiment Ideas
Experiments turn a working program into real understanding. Replace the Hadamard gate with an X gate and watch every shot return 1. Remove the gate entirely and watch every shot return 0. Add two Hadamard gates in a row and confirm the interference result from the earlier topic. Raise the shots to 10,000 and notice how the counts settle closer to an even split. Each change takes seconds to test and teaches a rule that stays in memory.
Practice Questions
- Which gate creates the equal blend?
- What does the shots value control?
- Which outcomes never appear in the Bell pair?
The answers are the Hadamard gate, the number of repeated runs, and 01 and 10.
