Quantum circuit simulator
I wrote a quantum circuit simulator in plain NumPy and checked it against IBM's Qiskit, which it matches to within 1e-9 on every standard gate and 30 random circuits.
- Year
- 2025
- Built with
- Python, React, quantum computing
- Links
- Source code
Overview
The simulator runs quantum circuits on an ordinary computer. I wrote it in Python with NumPy and no quantum library, so every gate, measurement and noise model is my own code. A FastAPI backend runs the simulations. A React front end lets you build a circuit by dragging gates onto it, and shows each qubit on a 3D Bloch sphere, the standard picture of a single qubit's state.
What I'm most confident about is how it was checked. A test suite runs every standard gate and 30 random circuits through both my simulator and Qiskit, IBM's reference library, and the two must agree to within 1e-9.
On top of the engine, the backend runs Grover's search, the quantum Fourier transform, phase estimation, three error correcting codes and two hybrid algorithms. VQE estimates the lowest energy of the H₂ molecule, and QAOA solves graph problems such as MaxCut. Both tune a circuit with a classical optimizer.

How it works
The state of n qubits is a list of 2^n complex numbers, one for each possible outcome. When a circuit includes noise, the simulator switches to a density matrix, a 2^n by 2^n matrix that can also describe a probabilistic mix of states.
Gates are applied as tensor contractions. Instead of building one huge matrix for the whole system, the code reshapes the state into a tensor with one axis per qubit and multiplies the gate into only the axes it touches. A gate then costs O(2^n) instead of O(4^n). The density matrix code reuses the same routine on its rows and columns.
When every measurement comes at the end, the simulator runs the gates once and draws all the shots from the final distribution. A WebSocket lets the front end step through a circuit one gate at a time.
Design decisions
- State vectors first. They are exact and work for any gate. Tensor networks and stabilizer methods scale further, but only for particular kinds of circuits.
- Density matrices only when needed. Noise requires them, but they square the memory cost, so noiseless circuits never pay for them.
- One qubit ordering, enforced in one place. An earlier version hid an ordering mismatch behind a table that reordered qubits depending on the gate's name. I removed the table and made the gate routine enforce a single convention.
- Gates return a new state.
apply_gatenever modifies its input, which makes step by step snapshots simple and rules out bugs from shared state.
Problems I ran into
- Speed. The first version built the full 2^n by 2^n matrix for every gate, filling it entry by entry in a Python loop for gates on several qubits. Rewriting gate application as a tensor contraction took one 10-qubit layer from about 19 seconds to a few milliseconds.
- Grover's search failed above 3 qubits. Its multi-controlled Z gate was a simplified chain of Toffoli gates that was wrong for larger circuits. I replaced it with a recursive decomposition that needs no extra qubits.
- Measurements in the X and Y bases were silently treated as measurements in Z. The simulator now rotates into the Z basis, measures, and rotates back.
- The entanglement map gave the same number to every pair of qubits that shared a first qubit. It now computes the concurrence of each pair, a standard measure of how entangled two qubits are.
Results
- Agreement with Qiskit.
tests/test_qiskit_validation.pyapplies each standard gate in every qubit position and runs 30 random circuits of 2 to 5 qubits with a depth of 20. Each final state must reach a fidelity above 0.999999999 with Qiskit'sStatevector, where fidelity measures how close two quantum states are. The suite runs in CI on Python 3.10, 3.11 and 3.12. - Speed. One layer of gates on 10 qubits went from about 19 seconds to a few milliseconds after the rewrite.
- Grover's search. The tests require a success probability above 0.9 on 2 to 5 qubits, within 1e-8 of the value the formula predicts.
What I'd do next
The simulator stays practical up to about 14 qubits and refuses more than 20. The server also accepts requests only from local development addresses, so putting it online would need its own configuration.