Importing a library is the easy part of quantum simulations with Python. The harder part is keeping straight what your classical CPU is actually doing while it pretends to be a quantum system. After a decade of building backends, the place I see people stall is the stretch between a mathematical hello world and something you would put in production.
If this is new to you, my earlier Quantum Computing with Python guide covers the groundwork. This one is about quantum simulations with Python in Qiskit-Aer, which runs the experiments on your own machine instead of on a multimillion-dollar quantum rig.
Circuits and gates instead of if/else
There is no if/else here. You build quantum circuits instead: a sequence of operations, called gates, applied to qubits. The closest familiar thing is defining a neural network architecture, where you stack layers of logic until the whole thing lands in the state you want.
You need Qiskit for this. Start with a circuit holding a single qubit, then read what is inside that qubit with a Statevector.
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
# Circuit with 1 quantum bit and 0 classical bits
q = QuantumCircuit(1,0)
state = Statevector.from_instruction(q)
print(state.probabilities())
The qubit sits in state 0 with 100% probability, which is not much of a result yet. The Hadamard gate, or H-gate, is what changes that: it puts the qubit into superposition, a 50/50 mix of 0 and 1.
Why quantum simulations with Python need measurement
A circuit on its own gives you nothing readable. To pull data back into the classical world you have to collapse the superposition, and that means adding a classical bit to hold the measurement. It is also the catch: the moment you observe the qubit, you are back to a plain 0 or 1.
# Add a classical bit for measurement
q = QuantumCircuit(1,1)
q.h(0) # Apply Superposition
q.measure(qubit=0, cbit=0) # Measure state into the classical bit
Running the circuit on Qiskit-Aer
So far the code is only a mathematical description. Qiskit-Aer is what executes it. Aer is a fast simulator, and it can imitate perfect quantum hardware, which is convenient given that no such hardware exists yet.
from qiskit_aer import AerSimulator
from qiskit.visualization import plot_histogram
sim = AerSimulator()
# Run the simulation 1000 times (shots)
result = sim.run(q, shots=1000).result()
counts = result.get_counts()
print(counts)
A perfect simulation gives you a tidy 50/50 split across the 1000 shots. Tidy results are usually a sign that you have left out the environmental noise that wrecks performance on actual devices.
Adding a noise model
Real quantum computers are fragile in ways classical machines are not. Heat, vibration or a stray electromagnetic wave is enough to cause decoherence. Making quantum simulations with Python resemble that means deliberately breaking your own system with noise.
from qiskit_aer import noise
# Define a 10% error probability (depolarizing error)
n_model = noise.NoiseModel()
error = noise.depolarizing_error(param=0.10, num_qubits=1)
n_model.add_all_qubit_quantum_error(error, ['h'])
# Run again with the noise model
sim_noisy = AerSimulator(noise_model=n_model)
result_noisy = sim_noisy.run(q, shots=1000).result()
print(result_noisy.get_counts())
With a 10% depolarizing error on the H-gate, the 50/50 split stops being tidy, and that messier distribution is much closer to what hardware hands back. An algorithm that cannot survive the noise in simulation will not survive it on a real device.
If this kind of work is eating your dev hours, I can take it on. I have been dealing with WordPress and backend logic since the 4.x days.
What to take from this
Quantum computing is still at the expensive prototype stage, so simulating in Python is how you design and test a model without running up a cloud bill. Build circuits that hold up once noise is in the picture. That skill is worth more right now than keeping up with the announcements.