A minimal n-qubit statevector quantum circuit simulator
Project description
min_qsim
A minimalistic n-qubit statevector quantum circuit simulator, built from scratch in NumPy for educational purposes. No external quantum computing libraries — every gate, state, and measurement routine is implemented directly on top of the raw statevector.
Installation
git clone https://github.com/yourhandle/min_qsim.git
cd min_qsim
pip install -e .
Requires Python >= 3.9 and NumPy >= 1.24.
Quick example
import min_qsim as q
# Bell state: |00> -> H(q0) -> CNOT(q0, q1)
state = q.init_state(2)
state = q.apply_gate(state, q.H, 0, 2)
state = q.apply_cnot(state, 0, 1, 2)
print(q.get_probabilities(state, 2))
# {'00': 0.5, '01': 0.0, '10': 0.0, '11': 0.5}
print(q.sampling(state, 2, shots=1000))
# {'00': 502, '01': 0, '10': 0, '11': 498}
Status
Completed
State initialization (state.py)
init_state(n)— initializes an n-qubit register to |00...0>custom_state(vector)— validates and loads an arbitrary user-provided statevector (checks power-of-2 length and normalization)
Gates (gates.py)
- Constant single-qubit gates:
I,X,Y,Z,H,S,T - Constant multi-qubit gates (as raw matrices):
CNOT,CZ,SWAP,CCX - Parameterised rotation gates:
rx(theta),ry(theta),rz(theta) apply_gate(state, gate, qubit_index, n)— applies any single-qubit gate to a target qubit via full tensor-product expansion, with unitarity/shape/index validationapply_cnot(state, control, target, n)— bitmask-based CNOT application (no full matrix construction)apply_cz(state, qubit_a, qubit_b, n)— bitmask-based CZ applicationapply_swap(state, qubit_a, qubit_b, n)— bitmask-based SWAP applicationapply_toffoli(state, qubit_a, qubit_b, target, n)— bitmask-based CCX application
Measurement (measurement.py)
get_probabilities(state, n, basis_state=None)— returns the probability of every basis state, or a single basis state's probabilitysampling(state, n, shots)— repeatedly samples the distribution to build up measurement statistics without collapsing state between shotsfull_measurement(state, n)— performs a single full projective measurement, returning the collapsed state and the observed basis stringpartial_measurement(state, n, qubit_to_measure)— measures a subset of qubits, returning the correctly renormalized post-measurement statevector over the full register
Tests (tests/)
- Basic sanity check for
custom_state+partial_measurement(test_gates.py)
Not yet implemented
Circuitclass (circuit.py) — a builder API for composing gates into a named circuit and running it end-to-end, instead of manually threading the statevector throughapply_*calls- Debug utilities (
debug.py) —show_statevector(pretty-print amplitudes/probabilities) andcheck_normalized(standalone normalization check) reset_qubit— force a qubit back to |0> after measurement- Broader test coverage — gates, measurement, and edge cases beyond the single existing script
- Packaging cleanup —
LICENSEfile is a placeholder/typo'd asLISENCE,pyproject.tomlauthor fields still say "Your Name"/"you@example.com", and the GitHub URLs are placeholders
Design notes
- Gate application comes in two flavors:
apply_gatebuilds the full 2^n x 2^n operator vianp.kron(simple, general, but O(4^n) memory) — used only for single-qubit gates. Multi-qubit gates (apply_cnot,apply_cz,apply_swap,apply_toffoli) instead operate directly on state-vector indices using bitmasks, avoiding full matrix construction. - All qubit indexing is big-endian (qubit 0 is the most significant bit in the basis string).
samplingdraws from the probability distribution without collapsing the state;full_measurementandpartial_measurementreturn properly collapsed and renormalized statevectors.
License
See LICENSE
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file min_qsim-0.1.0.tar.gz.
File metadata
- Download URL: min_qsim-0.1.0.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba2d478bd7aceb698713260202885f7ffda7e44be1cc3a6a9288f5cca7ed1a48
|
|
| MD5 |
35a89610081b3a42e1c0090331560867
|
|
| BLAKE2b-256 |
578cef5716162ff82d5c21eee3a350fe45633672e6162c3b61e711d0a0cb0c30
|
File details
Details for the file min_qsim-0.1.0-py3-none-any.whl.
File metadata
- Download URL: min_qsim-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f3cd6418cf687853d6fb0284b69bf075357fd70f2661ee431ad0c2a20e4f2c1
|
|
| MD5 |
42cfb2ca0cc9163688c1f3262728217f
|
|
| BLAKE2b-256 |
0e94b0d3837bd2eb190bd2a4aa35b9fb3e13a10e301b409cf0ee7b0fb34a2820
|