Skip to main content

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 validation
  • apply_cnot(state, control, target, n) — bitmask-based CNOT application (no full matrix construction)
  • apply_cz(state, qubit_a, qubit_b, n) — bitmask-based CZ application
  • apply_swap(state, qubit_a, qubit_b, n) — bitmask-based SWAP application
  • apply_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 probability
  • sampling(state, n, shots) — repeatedly samples the distribution to build up measurement statistics without collapsing state between shots
  • full_measurement(state, n) — performs a single full projective measurement, returning the collapsed state and the observed basis string
  • partial_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

  • Circuit class (circuit.py) — a builder API for composing gates into a named circuit and running it end-to-end, instead of manually threading the statevector through apply_* calls
  • Debug utilities (debug.py) — show_statevector (pretty-print amplitudes/probabilities) and check_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 — LICENSE file is a placeholder/typo'd as LISENCE, pyproject.toml author fields still say "Your Name"/"you@example.com", and the GitHub URLs are placeholders

Design notes

  • Gate application comes in two flavors: apply_gate builds the full 2^n x 2^n operator via np.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).
  • sampling draws from the probability distribution without collapsing the state; full_measurement and partial_measurement return properly collapsed and renormalized statevectors.

License

See LICENSE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

min_qsim-0.1.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

min_qsim-0.1.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

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

Hashes for min_qsim-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ba2d478bd7aceb698713260202885f7ffda7e44be1cc3a6a9288f5cca7ed1a48
MD5 35a89610081b3a42e1c0090331560867
BLAKE2b-256 578cef5716162ff82d5c21eee3a350fe45633672e6162c3b61e711d0a0cb0c30

See more details on using hashes here.

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

Hashes for min_qsim-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f3cd6418cf687853d6fb0284b69bf075357fd70f2661ee431ad0c2a20e4f2c1
MD5 42cfb2ca0cc9163688c1f3262728217f
BLAKE2b-256 0e94b0d3837bd2eb190bd2a4aa35b9fb3e13a10e301b409cf0ee7b0fb34a2820

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page