Quantum computing for software engineers
Compute a Bell state in plain Python and understand amplitudes, measurement and bit ordering.
Published by DME with AI assistance. Examples are educational and identify their assumptions and limitations. Editorial approach and corrections.
A quantum program describes a state preparation, a sequence of operations and a measurement. For a software engineer, the first useful step is learning how amplitudes produce outcome probabilities. You can do that with a tiny classical simulator before accessing quantum hardware. The example below computes a Bell state and checks its probabilities with Python's standard library.
Represent a two-qubit state
Use four amplitudes in the order |00⟩, |01⟩, |10⟩, |11⟩. In this article the left bit is the first qubit. A normalized state has squared amplitude magnitudes summing to one. The starting state |00⟩ is represented by [1, 0, 0, 0]. Complex amplitudes matter because later operations can make contributions reinforce or cancel each other.
Measurement does not reveal all amplitudes from one run. It returns an outcome sampled from their squared magnitudes. To estimate a distribution, repeat the preparation and measurement. A simulator that prints every amplitude is exposing its internal classical representation; a physical device does not provide the same direct inspection.
Create a Bell state
from math import sqrt
def hadamard_first(state):
a, b, c, d = state
scale = sqrt(2)
return [(a+c)/scale, (b+d)/scale,
(a-c)/scale, (b-d)/scale]
def cnot_first_controls_second(state):
a, b, c, d = state
return [a, b, d, c]
state = cnot_first_controls_second(
hadamard_first([1+0j, 0j, 0j, 0j]))
probabilities = [abs(amplitude)**2 for amplitude in state]
assert abs(sum(probabilities) - 1) < 1e-12
assert abs(probabilities[0] - 0.5) < 1e-12
assert abs(probabilities[3] - 0.5) < 1e-12
assert probabilities[1] == probabilities[2] == 0
print(dict(zip(["00", "01", "10", "11"], probabilities)))
The Hadamard operation on the first qubit creates (|00⟩ + |10⟩)/√2. The controlled-NOT flips the second bit when the first bit is one, giving (|00⟩ + |11⟩)/√2. The ideal measured outcomes are 00 and 11, each with probability one half. Floating-point output may differ slightly from exactly 0.5.
Understand what the result proves
The state cannot be written as a product of two independent single-qubit states. That is entanglement. Measuring both qubits in this basis produces correlated results, but correlations alone are not a complete experimental verification of entanglement: a classical mixture can also produce matching bits. Hardware verification needs suitable measurements in additional bases and careful statistical interpretation.
This four-amplitude computation is educational. It does not demonstrate quantum speedup. A general state-vector simulator requires a number of amplitudes that grows as 2 to the power of the number of qubits. Doubling the represented state for each extra qubit quickly increases memory requirements; small circuits are therefore much easier to simulate than large general circuits.
Make bit ordering explicit
Frameworks may label and display bits differently from this example. Before porting the code, prepare a simple basis state, measure it and confirm which printed position corresponds to which qubit. An ordering mistake can leave a program apparently working while interpreting the wrong register or control wire. Record the convention next to your circuit and tests.
Move to hardware with a clear question
A reasonable first hardware experiment asks how measured counts differ from ideal probabilities. Record circuit compilation, backend, shots and date. Finite samples create variation even on an ideal device, while hardware noise can introduce additional outcomes. Compare repeat runs and keep ideal simulation, sampled simulation and hardware measurements separate.
Do not interpret a nonzero 01 count as proof that the algorithm is fundamentally wrong; investigate sampling, noise, compilation and readout behavior. Equally, do not erase those counts without explaining the mitigation method. Read Assessing quantum advantage to turn a demonstration into a fair comparison, and watch DME's introductory video for the wider context.
Technical reference
IBM Qiskit bit-ordering documentation explains its conventions. Our small simulator uses the explicitly stated left-bit-first convention and has no Qiskit dependency.