Skip to main content

Module simulator

Module simulator 

Source
Expand description

Lightweight exact state-vector simulator for QML circuits.

QML primitives (variational classifiers, reinforcement-learning value/policy circuits, NLP models) build sequences of GateOp objects and need their real output statistics (measurement probabilities, Pauli-Z expectations) to compute losses and parameter-shift gradients. This module provides a small, dependency-free, exact simulator that applies arbitrary one- and two-qubit gates (via their dense matrix()) to a full 2^n amplitude vector.

§Qubit convention

Qubit q corresponds to bit q of the basis-state index (little-endian), i.e. amplitude index i has qubit q in state (i >> q) & 1. This matches the convention used by the per-gate applicators in crate::qml::advanced_algorithms and the gate matrix() definitions in crate::gate::functions.

Functions§

apply_gate
Apply a single one- or two-qubit gate to state in place.
expectation_z
Expectation value of the Pauli-Z operator on qubit target, ⟨Z⟩ = P(|0⟩) − P(|1⟩) ∈ [−1, 1].
probabilities
Full probability distribution over all 2^n computational basis states.
probability_one
Probability of measuring qubit target in state |1⟩.
simulate
Apply a sequence of gates to a fresh |0…0⟩ state and return the final amplitude vector.
zero_state
Allocate the |0…0⟩ computational-basis state for num_qubits qubits.