Skip to main content

Module circuit_simulation

Module circuit_simulation 

Source
Expand description

Self-contained dense state-vector simulator for QML circuits.

The quantum-ML algorithms in this crate (VQE, QAOA, QNNs, gradient estimators, trainers) operate on ParameterizedQuantumCircuit values and need real measurement statistics / expectation values to drive their optimisation loops. Historically the execute_circuit_helper methods in those modules returned a hard-coded 50/50 split of |0…0⟩ and |1…1⟩ counts, which silently fed fabricated data into every gradient and training computation.

This module provides a small, exact, in-crate state-vector engine so those helpers can produce genuine results. It is intentionally self-contained: quantrs2-device must not depend on quantrs2-sim (that crate is a sibling consumer, and adding it here would create a cross-dependency), so we implement the few gates that QuantumGate can express directly.

For paths that genuinely require execution on remote hardware (with credentials / network access), callers should return an honest DeviceError instead of using this local simulator.

Functions§

expected_hamming_weight
Compute the exact expectation value of the total-spin (number-of-ones) observable Σ_q (1 - Z_q)/2 = Σ_q n_q, i.e. the expected Hamming weight of a measurement outcome, directly from the state vector.
outcome_probabilities
Compute the exact probability of each computational-basis outcome.
simulate_and_sample
Simulate circuit and sample shots measurement outcomes from the exact output distribution, returning a CircuitResult with real counts.
simulate_statevector
Simulate a ParameterizedQuantumCircuit from the all-zero state and return the resulting amplitude vector of length 2^num_qubits.