QuantumSim
- 4 Devlogs
- 4 Total hours
Quantum simulator in python. now no longer just a matrix engine.
Quantum simulator in python. now no longer just a matrix engine.
I rebuilt (1st attempt was with matplotlib) the rendering layer around a hardware-accelerated Pygame canvas. Now the frontend intercepts mouse clicks, updates matrix values, and redraws the probability bars smoothly at 60 FPS.
Linking my simulator backend to the visual grid immediately broke macroscopic entanglement. The backend processed binary indices left-to-right, but standard notation and my GUI histogram read states right-to-left. Because of this index mismatch, clicking the top wire (Qubit 0) in the GUI was mathematically altering the bottom wire (Qubit 2) in the engine, corrupting Bell State results.Instead of patching it on the frontend with messy string reversals, I went into the math engine and inverted the Kronecker product expansion direction to process wires top-down natively:
Don’t know if the devlog will process this .md snippet…
$$U_{\text{full}} = U_{\text{gate}} \otimes I \otimes \dots \otimes I$$
With the backend math matching the visual grid layout 1:1, dropping a Hadamard and a CNOT on Qubit 0 instantly maps to the correct indices, splitting the system probability cleanly into a |000⟩ and |110⟩ state.
Added Phase Gates (Z, S, T): I integrated phase manipulation matrices using Python’s complex number notation (1j). This allows my simulator to rotate qubits along the Z-axis of the Bloch sphere, shifting the phase coefficients without altering immediate measurement probabilities.
Executed the Deutsch-Jozsa Algorithm: I verified the simulator’s computational accuracy by running a true quantum algorithm. Using a 3-qubit register and a balanced oracle constructed from my CNOT logic, the simulator successfully utilized phase kickback to identify the function as balanced in a single evaluation pass.
Implemented Non-Destructive Observables: I created get_expectation_value(), a method that calculates the theoretical average outcome of a specific qubit (0.0 to 1.0). It reads the state probabilities using Born’s rule without triggering a wavefunction collapse, keeping the superposition intact for debugging.
Built a High-Level Circuit Wrapper: I abstracted the raw matrix math behind a QuantumCircuit class API (similar to Qiskit). It queues gate instructions and automates running the simulator across multiple execution “shots.” A 1,000-shot test on an entangled system yielded a statistically sound 50/50 distribution (001 and 111), proving full framework stability.
I built a functional N-qubit quantum computer simulator from scratch in Python by breaking the project down into four main challenges:
State Vector Representation: Instead of using classical bits, I represented the quantum register as a 1D NumPy array of size 2^N using np.complex128 to track complex probability amplitudes. I initialized the system to the ground state (|00...0>) by setting index 0 to 1.0 and filling the rest with zeros.
Single-Qubit Gates: Single-qubit operations (like Pauli-X and Hadamard) are 2x2 matrices, which can't be directly multiplied by a larger multi-qubit state vector. I solved this by using recursive Kronecker tensor products (np.kron) to weave the gate matrix with Identity matrices, building a massive system-wide operator matrix to update the state via np.dot.
Multi-Qubit CNOT Gate: To avoid the messy matrix math of scaling a CNOT gate when the control and target qubits aren't adjacent, I bypassed matrix multiplication entirely. I wrote a loop that converts the vector's decimal indices into binary strings, checks the control bit, flips the target bit if the control is '1', and maps the amplitude over to its new index in a buffer array.
Measurement & Collapse: To read classical data out of the superposition, I calculated the exact probabilities of each state using the absolute square of the amplitudes (np.abs(state) 2) and simulated the random choice using np.random.choice. Once an index was selected, I cleared the state vector and forced it to collapse completely onto that single winning outcome.
I tested the complete system by generating an entangled Bell State, and it successfully outputs synchronized configurations (00 or 11) every time.
Representing qubits in arrays, working with kroneker function to apply X gate (NOT gates in quantum computing) to specific qubit in a multi qubit system.
State Vector:
[0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j]
Correctly flipped qubit 0