Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Error Model and Public API

Error model

Fallible public APIs return Result<T, PrismError>. Error variants:

VariantCategoryDescription
ParseParsingOpenQASM parse error with line number
UnsupportedConstructParsingValid OpenQASM not supported by PRISM-Q
UndefinedRegisterParsingReference to undeclared register
InvalidQubitValidationQubit index exceeds register size
InvalidClassicalBitValidationClassical bit index exceeds register
GateArityValidationWrong number of qubits for gate
InvalidParameterValidationInvalid gate parameter (NaN, etc.)
ExportUnsupportedExportInstruction with no OpenQASM 3.0 spelling
BackendUnsupportedRuntimeBackend can't perform requested operation
IncompatibleBackendRuntimeBackend incompatible with circuit
StepLimitRuntimeA dynamic program shot ran more blocks than its step bound; names the loop
ResourceLimitRuntimeAn allocation over a memory cap; the boxed ResourceLimit carries the operation, the ResourceKind unit, required and limit, and the environment variable that overrides the cap

Note

Invalid input data (QASM text, incompatible backend) returns PrismError. API misuse (out-of-range indices, wrong-variant accessors) panics, and each such method documents the condition under # Panics. debug_assert! is used for internal invariants only.

Public API surface

Top-level re-exports from src/lib.rs. The full generated documentation is on docs.rs.

AreaItems
Simulationsimulate, Simulate, Unseeded, Seeded, run_on, run_on_state, run_qasm, PauliObservable, ObservableExpectation, ObservableVariance, ExpectationResult, OverlapResult, bitstring
Route inspectionQuery, Plan, PlanFallback, PlannedExactness, RejectReason, RouteRule, MemoryEstimate, MemoryEstimateKind
LimitsLimits, LimitKnob, LimitSource, default_limits, set_default_limits
Dynamic programsDynamicProgram, DynamicProgramBuilder, simulate_program, SimulateProgram
GradientsExpectationGradient, returned by Simulate::expectation_gradient, Simulate::expectation_gradient_shift and the PreparedCircuit methods of the same names, all taking the Hamiltonian as a PauliObservable
ParametersParameters, ParamLink, PreparedCircuit
Compiled samplingcompile_measurements, compile_forward, compile_detector_sampler, compile_noisy, run_shots_compiled, run_shots_noisy, noisy_marginals_analytical, density_matrix_expectation_values, CompiledSampler, CompiledDetectorSampler, DetectorSampleBatch; with gpu: run_shots_compiled_with_gpu, DevicePackedShots
AccumulatorsShotAccumulator, HistogramAccumulator, MarginalsAccumulator, PauliExpectationAccumulator, CorrelatorAccumulator, NullAccumulator, PackedShots, ShotLayout, ParityStats
NoiseNoiseModel, NoiseBuilder, NoiseChannel, NoiseEvent, GateFilter, ReadoutError, DriftDistribution, NoisyCompiledSampler, DeviceCalibration, QubitCalibration, GateCalibration, GateTimes, ErrorChainComplex
Native QECparse_qec_program, compile_qec_program_rows, run_qec_program, run_qec_program_reference, run_qec_program_with_strategy, run_qec_program_spd_rerouted, QecProgram, QecOp, QecOptions, QecSampleResult, QecBasis, QecPauli, QecRecordRef, QecNoise, QecCorrelatedBranch, QecCircuitNoise, QecDetectorReference, QecMeasurementRow, QecCompiledRows, QecCompiledSampler, QecObservableEstimate, QecObservableReroute, QecTStrategy
QEC decodingDetectorErrorModel, ErrorMechanism, UnionFindDecoder, MatchingDecoder, BpOsdDecoder, BpOsdOptions, BpMethod, OsdMethod
Clifford+Trun_stabilizer_rank, run_stabilizer_rank_approx, stabilizer_overlap_sq, stabilizer_inner_product, StabRankResult, run_spp, run_spp_observable, run_spd, run_spd_with, run_spd_observable, run_spd_observable_budgeted, run_spd_observable_light_cone, inverse_light_cone, PauliAxis, PauliTerm, SppResult, SppObservableResult, SpdResult, SpdObservableResult, SpdTruncation
Circuits and gatesCircuit, CircuitBuilder, TrotterOrder, Instruction, ClassicalCondition, SaveSpec, SaveRecord, SavedValue, SvgOptions, TextOptions, Gate, GeneratorKind, McuData, UnitaryData
Results and provenanceBackendKind, RunOutcome, CountsResult, MarginalsResult, ShotsResult, ReducedDensityMatrix, EntropyResult, Probabilities, FactoredBlock, RunMetadata, BondReport, Engine, Exactness, Placement, ResolvedBackend
ErrorsPrismError, ResourceLimit, ResourceKind, Result
BackendsStatevectorBackend, StabilizerBackend, SparseBackend, MpsBackend, ProductStateBackend, TensorNetworkBackend, FactoredBackend, FactoredStabilizerBackend, DensityMatrixBackend; with distributed: DistributedStatevectorBackend, DistributedContext, RankComm, SerialComm; with distributed-mpi: MpiComm
Threadingwith parallel: ThreadPool, a Rayon pool the caller supplies, which simulation runs inside instead of sizing the process-wide one; see Threading, SIMD, and Memory Layout

Not re-exported at the root but part of the documented surface: the Backend trait and BasisSamples at prism_q::backend, run_batch at prism_q::sim, parse_dynamic and the IR types under prism_q::circuit::openqasm and prism_q::circuit::dynamic, and the accumulator chunk size helpers (default_chunk_size, optimal_chunk_size) at prism_q::sim::compiled. Backend is sealed: only the crate implements it, so a new method is additive. Items behind the bench-internal feature (bench_variant, the sweep profile helpers, QecProfiledSampler) are benchmark tooling, not public API.

The complete item list is checked in as public-api.txt, generated by cargo public-api with --features "parallel gpu distributed". CI regenerates it and fails on a difference, so every surface change shows in review as a diff to that file.

Save points

Circuit::add_save appends an Instruction::Save recording a SaveSpec, and Simulate::run returns one SaveRecord per point in RunOutcome::saves, carrying a SavedValue. See Circuit IR for what a save does to fusion and which routes decline one.

Batches

prism_q::sim::run_batch runs a list of circuits, holding one backend across those of the same width that draw no randomness. Results match running each circuit alone with the same seed. Under parallel, batches of circuits up to 16 qubits split across cores, one circuit per core with each run single threaded. Wider batches run one circuit at a time on the kernels' own threads.

State diagnostics

  • Simulate::reduced_density_matrix returns a ReducedDensityMatrix (row major, side 2^k, qubits[0] the lowest bit of the row index) with purity for Tr(rho^2).
  • Simulate::entanglement_entropy returns an EntropyResult: the von Neumann entropy in nats and the descending Schmidt spectrum, None where the backend holds the entropy without the spectrum.
  • Simulate::overlap takes a second seeded builder over a circuit of the same width and returns an OverlapResult: the squared inner product and the provenance of both runs.

All three require a unitary circuit, and under BackendKind::Auto a route that cannot answer falls back to the statevector. Backends tabulates which backends answer each.

Prepared circuits

PreparedCircuit answers the Simulate terminals on a bound circuit, each with a _many form over a list of bindings. Parameters, Binding and Gradients lists the terminals and how the batches split.

Growth of the public enums

Public enums are #[non_exhaustive] unless named below. A new gate, backend, engine label, error variant, noise channel, truncation policy, dialect, result request or result value is an additive release, so a match from outside the crate keeps a wildcard arm. RunMetadata is #[non_exhaustive] too: a new field is additive, and outside code reads it rather than building it.

Instruction stays exhaustive. It is the circuit IR, and a new instruction kind changes what every consumer handles, so adding one is a breaking change.

Enums mirroring a closed mathematical set stay exhaustive as well, since a wildcard arm the caller can never reach buys nothing: PauliAxis and QecBasis are X, Y and Z, GeneratorKind and DiagEntry are gate-algebra shapes, and QftTextbookStep is a fixed decomposition.