Skip to content

Quantum API

typescript
import {
  QuantumPropertyManager,
  QuantumRecorder,
  ensureLoaded,
  startBackgroundLoad,
  isReady,
  getModule,
  getQuantumForge,
  getVersion,
  getMaxDimension,
  getMaxQudits,
  getWasmMemoryBytes,
  setWasmBasePath,
  registerServiceWorker,
} from "quantum-forge/quantum";

import type {
  QuantumOperation,
  SerializedPredicate,
  PredicateSpec,
  QuantumRecorderHook,
} from "quantum-forge/quantum";

Module Loading

ensureLoaded(): Promise<void>

Loads the WASM module. Must complete before any quantum operations. Caches the result — subsequent calls resolve immediately.

startBackgroundLoad(logger?: LoggerInterface): void

Begins loading in the background via requestIdleCallback. Call early for faster perceived startup.

isReady(): boolean

Non-blocking check. Returns true if the module has finished loading.

setWasmBasePath(path: string): void

Set the URL path where WASM files are served. Default: "/quantum-forge". Must be called before ensureLoaded().

registerServiceWorker(swPath?: string): Promise<ServiceWorkerRegistration | null>

Register the offline caching service worker. Default path: "/quantum-forge-sw.js".

Module Queries

getModule(): QFModule

Returns the loaded WASM module for gate calls and queries.

getVersion(): string

WASM module version string.

getMaxDimension(): number

Maximum qudit dimension supported by this build.

getMaxQudits(): number

Maximum number of qudits supported by this build.

getWasmMemoryBytes(): number | null

Current WASM heap usage in bytes.

QuantumPropertyManager

Base class for quantum property lifecycle management. Extend or compose this in your game.

Constructor

typescript
new QuantumPropertyManager(options?: {
  dimension?: number;  // default: 2
  logger?: LoggerInterface;
})

Public API

MethodReturnsDescription
acquireProperty()QFPropertyGet a property at |0⟩ (pool first, then fresh)
releaseProperty(prop, value)voidReset to |0⟩ and return to pool
setProperty(id, prop)voidMap a string ID to a property handle
getProperty(id)QFProperty | undefinedGet handle by ID
hasProperty(id)booleanCheck if ID exists
deleteProperty(id)voidRemove ID mapping (does not release)
removeProperty(id)voidMeasure + release + delete in one call
getModule()QFModuleAccess the WASM module for all gate and query calls
dimensionnumberThe configured dimension (readonly)
clear()voidClear all properties and pool
sizenumberNumber of registered properties
poolSizenumberNumber of pooled (recycled) properties
setRecorder(recorder)voidAttach a QuantumRecorder for opt-in operation logging
getRecorder()QuantumRecorderHook | undefinedGet attached recorder

WASM Gate Functions

All gates are called via getModule(). Names are snake_case.

Single-Property Gates

FunctionSignatureDescription
cyclem.cycle(prop, fraction?, predicates?)Cyclic permutation (NOT for dim=2)
shiftm.shift(prop, fraction?, predicates?)Shift gate / Pauli X
hadamardm.hadamard(prop, fraction?, predicates?)Hadamard gate
inverse_hadamardm.inverse_hadamard(prop, predicates?)Adjoint Hadamard
clockm.clock(prop, fraction, predicates?)Clock gate / Z-rotation
ym.y(prop, fraction?, predicates?)Pauli Y (dim=2 only, throws otherwise)
resetm.reset(prop, value)Reset property to |0⟩ from known value

Two-Property Gates

FunctionSignatureDescription
i_swapm.i_swap(p1, p2, fraction, predicates?)iSwap entanglement gate
swapm.swap(p1, p2, predicates?)Swap two properties

Phase

FunctionSignatureDescription
phase_rotatem.phase_rotate(predicates, angle)Phase rotation (predicates required)

Measurement

FunctionReturnsDescription
m.measure_properties(props)number[]Measure and collapse
m.forced_measure_properties(props, values)number[]Force specific outcomes (replay)
m.measure_predicate(predicates)numberProjective predicate measurement (0 or 1)

Read-Only Queries

typescript
m.probabilities(props: QFProperty[]): Array<{
  probability: number;
  qudit_values: number[];
}>

m.reduced_density_matrix(props: QFProperty[]): Array<{
  row_values: number[];
  col_values: number[];
  value: { real: number; imag: number };
}>

QuantumRecorder

Opt-in recording and replay of quantum operations. Attach to a manager via setRecorder().

Constructor

typescript
new QuantumRecorder(manager: QuantumPropertyManager)

Recording API

MethodReturnsDescription
startRecording()voidBegin recording; resets log
stopRecording()QuantumOperation[]Stop and return log
isRecording()booleanCheck recording status
getOperationLog()QuantumOperation[]Get log copy (even while recording)
replayLog(ops)voidClear manager state and replay from log
recordOp(op)voidRecord a gate operation manually
getIndex(prop)number | undefinedGet recorded index for a property
buildWasmPredicates(specs)QFPredicate[]Build WASM predicates from specs
serializePredicates(specs)SerializedPredicate[] | undefinedSerialize for log

See Recording & Replay for usage guide.

PredicateSpec

typescript
interface PredicateSpec {
  property: QFProperty;
  value: number;
  isEqual: boolean;  // true = "is this value", false = "is NOT this value"
}

QuantumOperation

Union type for all recorded operations. See Recording & Replay for the full type definition.

Powered by Quantum Forge