quantum-forge 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,370 @@
1
+ import { LoggerInterface } from './logging.js';
2
+
3
+ declare class Predicate {
4
+ private cppInstance;
5
+ constructor(cppInstance: any);
6
+ value(): number;
7
+ is_equal(): boolean;
8
+ getCppInstance(): any;
9
+ }
10
+ declare class QuantumProperty {
11
+ private cppInstance;
12
+ constructor(dimension: number);
13
+ index(): number;
14
+ dimension(): number;
15
+ is(value: number): Predicate;
16
+ is_not(value: number): Predicate;
17
+ getCppInstance(): any;
18
+ }
19
+ declare function cycle(prop: QuantumProperty, fraction?: number, predicates?: Predicate[]): void;
20
+ declare function shift(prop: QuantumProperty, fraction?: number, predicates?: Predicate[]): void;
21
+ declare function clock(prop: QuantumProperty, fraction?: number, predicates?: Predicate[]): void;
22
+ declare function hadamard(prop: QuantumProperty, fraction?: number, predicates?: Predicate[]): void;
23
+ declare function inverse_hadamard(prop: QuantumProperty, predicates?: Predicate[]): void;
24
+ declare function swap(prop1: QuantumProperty, prop2: QuantumProperty, predicates?: Predicate[]): void;
25
+ declare function i_swap(prop1: QuantumProperty, prop2: QuantumProperty, fraction: number, predicates?: Predicate[]): void;
26
+ declare function x(prop: QuantumProperty, fraction?: number, predicates?: Predicate[]): void;
27
+ declare function z(prop: QuantumProperty, fraction?: number, predicates?: Predicate[]): void;
28
+ declare function y(prop: QuantumProperty, fraction?: number, predicates?: Predicate[]): void;
29
+ declare function reset(prop: QuantumProperty, currentValue: number): void;
30
+ declare function phase_rotate(predicates: Predicate[], angle: number): void;
31
+ declare function measure_properties(props: QuantumProperty[]): number[];
32
+ declare function forced_measure_properties(props: QuantumProperty[], forcedValues: number[]): number[];
33
+ declare function measure_predicate(predicates: Predicate[]): number;
34
+ declare function probabilities(props: QuantumProperty[]): Array<{
35
+ probability: number;
36
+ qudit_values: number[];
37
+ }>;
38
+ declare function reduced_density_matrix(props: QuantumProperty[]): Array<{
39
+ row_values: number[];
40
+ col_values: number[];
41
+ value: {
42
+ real: number;
43
+ imag: number;
44
+ };
45
+ }>;
46
+ declare class QuantumForge {
47
+ /**
48
+ * Initialize QuantumForge
49
+ * Automatically detects and loads the appropriate WASM module format
50
+ */
51
+ static initialize(): Promise<void>;
52
+ /**
53
+ * Check if QuantumForge is initialized
54
+ */
55
+ static isInitialized(): boolean;
56
+ static createQuantumProperty(dimension: number): QuantumProperty;
57
+ static getVersion(): string;
58
+ static getMaxDimension(): number;
59
+ static getMaxQudits(): number;
60
+ static isValidDimension(dimension: number): boolean;
61
+ }
62
+
63
+ type __quantum_forge_api_mjs_Predicate = Predicate;
64
+ declare const __quantum_forge_api_mjs_Predicate: typeof Predicate;
65
+ type __quantum_forge_api_mjs_QuantumForge = QuantumForge;
66
+ declare const __quantum_forge_api_mjs_QuantumForge: typeof QuantumForge;
67
+ type __quantum_forge_api_mjs_QuantumProperty = QuantumProperty;
68
+ declare const __quantum_forge_api_mjs_QuantumProperty: typeof QuantumProperty;
69
+ declare const __quantum_forge_api_mjs_clock: typeof clock;
70
+ declare const __quantum_forge_api_mjs_cycle: typeof cycle;
71
+ declare const __quantum_forge_api_mjs_forced_measure_properties: typeof forced_measure_properties;
72
+ declare const __quantum_forge_api_mjs_hadamard: typeof hadamard;
73
+ declare const __quantum_forge_api_mjs_i_swap: typeof i_swap;
74
+ declare const __quantum_forge_api_mjs_inverse_hadamard: typeof inverse_hadamard;
75
+ declare const __quantum_forge_api_mjs_measure_predicate: typeof measure_predicate;
76
+ declare const __quantum_forge_api_mjs_measure_properties: typeof measure_properties;
77
+ declare const __quantum_forge_api_mjs_phase_rotate: typeof phase_rotate;
78
+ declare const __quantum_forge_api_mjs_probabilities: typeof probabilities;
79
+ declare const __quantum_forge_api_mjs_reduced_density_matrix: typeof reduced_density_matrix;
80
+ declare const __quantum_forge_api_mjs_reset: typeof reset;
81
+ declare const __quantum_forge_api_mjs_shift: typeof shift;
82
+ declare const __quantum_forge_api_mjs_swap: typeof swap;
83
+ declare const __quantum_forge_api_mjs_x: typeof x;
84
+ declare const __quantum_forge_api_mjs_y: typeof y;
85
+ declare const __quantum_forge_api_mjs_z: typeof z;
86
+ declare namespace __quantum_forge_api_mjs {
87
+ export { __quantum_forge_api_mjs_Predicate as Predicate, __quantum_forge_api_mjs_QuantumForge as QuantumForge, __quantum_forge_api_mjs_QuantumProperty as QuantumProperty, __quantum_forge_api_mjs_clock as clock, __quantum_forge_api_mjs_cycle as cycle, __quantum_forge_api_mjs_forced_measure_properties as forced_measure_properties, __quantum_forge_api_mjs_hadamard as hadamard, __quantum_forge_api_mjs_i_swap as i_swap, __quantum_forge_api_mjs_inverse_hadamard as inverse_hadamard, __quantum_forge_api_mjs_measure_predicate as measure_predicate, __quantum_forge_api_mjs_measure_properties as measure_properties, __quantum_forge_api_mjs_phase_rotate as phase_rotate, __quantum_forge_api_mjs_probabilities as probabilities, __quantum_forge_api_mjs_reduced_density_matrix as reduced_density_matrix, __quantum_forge_api_mjs_reset as reset, __quantum_forge_api_mjs_shift as shift, __quantum_forge_api_mjs_swap as swap, __quantum_forge_api_mjs_x as x, __quantum_forge_api_mjs_y as y, __quantum_forge_api_mjs_z as z };
88
+ }
89
+
90
+ /**
91
+ * Set the base URL path where Quantum Forge WASM files are served.
92
+ * Default is "/quantum-forge" which matches the Vite plugin's serve path.
93
+ * Consumers using the Vite plugin don't need to call this.
94
+ */
95
+ declare function setWasmBasePath(path: string): void;
96
+ /**
97
+ * Select a named WASM build variant (e.g. "d7n10").
98
+ * Sugar for `setWasmBasePath("/quantum-forge-{name}")`.
99
+ *
100
+ * Must be called before `ensureLoaded()`. If the module is already loaded,
101
+ * a warning is logged and the call is ignored.
102
+ */
103
+ declare function useQuantumForgeBuild(name: string): void;
104
+ /**
105
+ * Start loading the WASM module in the background.
106
+ * Call this after the page has rendered (e.g., after DOMContentLoaded or initial paint).
107
+ */
108
+ declare function startBackgroundLoad(loggerRef?: LoggerInterface): void;
109
+ /**
110
+ * Ensure Quantum Forge is loaded and initialized.
111
+ * Returns a promise that resolves when the module is ready.
112
+ * Can be called multiple times - will return the same promise.
113
+ */
114
+ declare function ensureLoaded(): Promise<void>;
115
+ /**
116
+ * Check if Quantum Forge is ready to use (non-blocking).
117
+ */
118
+ declare function isReady(): boolean;
119
+ /**
120
+ * Get the loaded module. Throws if not loaded.
121
+ * For synchronous access after ensuring it's loaded.
122
+ */
123
+ declare function getModule(): typeof __quantum_forge_api_mjs;
124
+ /**
125
+ * Get the QuantumForge class from the loaded module.
126
+ */
127
+ declare function getQuantumForge(): typeof QuantumForge;
128
+ /**
129
+ * Convenience re-exports for common operations.
130
+ * These will throw if module not loaded.
131
+ */
132
+ declare function getVersion(): string;
133
+ declare function getMaxDimension(): number;
134
+ declare function getMaxQudits(): number;
135
+ /**
136
+ * Get the WASM memory bytes (for analytics).
137
+ */
138
+ declare function getWasmMemoryBytes(): number | null;
139
+ /**
140
+ * Get the required attribution text for display in your application.
141
+ * Include this in a user-visible location (credits screen, about page, etc.).
142
+ */
143
+ declare function getAttribution(): string;
144
+ /**
145
+ * Register the Quantum Forge service worker for offline WASM caching.
146
+ * Call once from your game controller after page load.
147
+ * The SW caches WASM artifacts on first fetch so subsequent loads work offline.
148
+ *
149
+ * @param swPath - Path to the service worker file. Default: "/quantum-forge-sw.js"
150
+ */
151
+ declare function registerServiceWorker(swPath?: string): Promise<ServiceWorkerRegistration | null>;
152
+
153
+ /**
154
+ * QuantumPropertyManager — manages quantum property lifecycles.
155
+ *
156
+ * Handles the common pattern of acquiring, pooling, and releasing WASM
157
+ * QuantumProperty handles. Games either extend this class or compose it
158
+ * to add game-specific quantum operations via getModule().
159
+ *
160
+ * Property pooling is critical: measured/removed properties are recycled
161
+ * to avoid growing the tensor product and hitting qudit limits.
162
+ *
163
+ * For opt-in operation recording, attach a QuantumRecorder via setRecorder().
164
+ */
165
+
166
+ interface PredicateSpec$1 {
167
+ property: QuantumProperty;
168
+ value: number;
169
+ isEqual: boolean;
170
+ }
171
+ interface QuantumRecorderHook {
172
+ onAcquire?(prop: QuantumProperty): void;
173
+ onRelease?(prop: QuantumProperty, value: number): void;
174
+ onSetProperty?(id: string, prop: QuantumProperty): void;
175
+ onDeleteProperty?(id: string): void;
176
+ }
177
+ declare class QuantumPropertyManager {
178
+ readonly dimension: number;
179
+ private properties;
180
+ private pool;
181
+ protected logger?: LoggerInterface;
182
+ private _recorder?;
183
+ constructor(options?: {
184
+ dimension?: number;
185
+ logger?: LoggerInterface;
186
+ });
187
+ /** Attach an optional recorder for operation logging. */
188
+ setRecorder(recorder: QuantumRecorderHook | undefined): void;
189
+ /** Get the currently attached recorder, if any. */
190
+ getRecorder(): QuantumRecorderHook | undefined;
191
+ /**
192
+ * Get a property at |0⟩ — reuses a pooled one if available,
193
+ * otherwise creates a fresh standalone property.
194
+ */
195
+ acquireProperty(): QuantumProperty;
196
+ /**
197
+ * Return a property to the pool after resetting it to |0⟩.
198
+ * Uses the `reset` primitive which applies non-fractional cycles —
199
+ * correct for all dimensions (no superposition created).
200
+ */
201
+ releaseProperty(prop: QuantumProperty, measuredValue: number): void;
202
+ setProperty(id: string, prop: QuantumProperty): void;
203
+ getProperty(id: string): QuantumProperty | undefined;
204
+ deleteProperty(id: string): void;
205
+ hasProperty(id: string): boolean;
206
+ /**
207
+ * Remove a property by ID: measure it, pool the handle, delete the mapping.
208
+ */
209
+ removeProperty(id: string): void;
210
+ /** Clear all properties, pool, and recorder. */
211
+ clear(): void;
212
+ get size(): number;
213
+ get poolSize(): number;
214
+ getModule(): ReturnType<typeof getModule>;
215
+ /** @internal — used by QuantumRecorder.replayLog() to restore pool state. */
216
+ _setPool(pool: QuantumProperty[]): void;
217
+ /** @internal — used by QuantumRecorder to enumerate live handles. */
218
+ _getProperties(): Map<string, QuantumProperty>;
219
+ /** @internal — used by QuantumRecorder to enumerate pool handles. */
220
+ _getPool(): QuantumProperty[];
221
+ }
222
+
223
+ /**
224
+ * QuantumOperationLog — serializable record of quantum state mutations.
225
+ *
226
+ * Each entry captures a single WASM-level operation. Property handles are
227
+ * mapped to sequential integer indices so the log is independent of runtime
228
+ * handle values. On replay, `forced_measure_properties` reproduces the
229
+ * exact measurement outcomes, yielding identical quantum state.
230
+ */
231
+ interface SerializedPredicate {
232
+ propertyIndex: number;
233
+ value: number;
234
+ isEqual: boolean;
235
+ }
236
+ type QuantumOperation = {
237
+ op: "acquire";
238
+ index: number;
239
+ } | {
240
+ op: "release";
241
+ index: number;
242
+ value: number;
243
+ } | {
244
+ op: "assign";
245
+ index: number;
246
+ id: string;
247
+ } | {
248
+ op: "unassign";
249
+ id: string;
250
+ } | {
251
+ op: "cycle";
252
+ index: number;
253
+ fraction: number;
254
+ predicates?: SerializedPredicate[];
255
+ } | {
256
+ op: "i_swap";
257
+ index1: number;
258
+ index2: number;
259
+ fraction: number;
260
+ predicates?: SerializedPredicate[];
261
+ } | {
262
+ op: "clock";
263
+ index: number;
264
+ fraction: number;
265
+ predicates?: SerializedPredicate[];
266
+ } | {
267
+ op: "hadamard";
268
+ index: number;
269
+ fraction: number;
270
+ predicates?: SerializedPredicate[];
271
+ } | {
272
+ op: "measure";
273
+ indices: number[];
274
+ outcomes: number[];
275
+ } | {
276
+ op: "shift";
277
+ index: number;
278
+ fraction: number;
279
+ predicates?: SerializedPredicate[];
280
+ } | {
281
+ op: "y";
282
+ index: number;
283
+ fraction: number;
284
+ predicates?: SerializedPredicate[];
285
+ } | {
286
+ op: "inverse_hadamard";
287
+ index: number;
288
+ predicates?: SerializedPredicate[];
289
+ } | {
290
+ op: "swap";
291
+ index1: number;
292
+ index2: number;
293
+ predicates?: SerializedPredicate[];
294
+ } | {
295
+ op: "phase_rotate";
296
+ predicates: SerializedPredicate[];
297
+ angle: number;
298
+ } | {
299
+ op: "measure_predicate";
300
+ predicates: SerializedPredicate[];
301
+ outcome: number;
302
+ } | {
303
+ op: "reset";
304
+ index: number;
305
+ value: number;
306
+ };
307
+
308
+ /**
309
+ * QuantumRecorder — opt-in recording and replay of quantum operations.
310
+ *
311
+ * Attach to a QuantumPropertyManager via `manager.setRecorder(recorder)`.
312
+ * When recording is active, lifecycle hooks log every state-mutating
313
+ * operation. The log can be replayed via replayLog() to recreate
314
+ * identical quantum state — measurements are forced to their recorded
315
+ * outcomes using forced_measure_properties.
316
+ *
317
+ * For gate recording, call wrapGate() around each WASM gate call.
318
+ */
319
+
320
+ interface PredicateSpec {
321
+ property: QuantumProperty;
322
+ value: number;
323
+ isEqual: boolean;
324
+ }
325
+ declare class QuantumRecorder implements QuantumRecorderHook {
326
+ private _recording;
327
+ private _log;
328
+ private _handleToIndex;
329
+ private _nextIndex;
330
+ private readonly _manager;
331
+ constructor(manager: QuantumPropertyManager);
332
+ onAcquire(prop: QuantumProperty): void;
333
+ onRelease(prop: QuantumProperty, value: number): void;
334
+ onSetProperty(id: string, prop: QuantumProperty): void;
335
+ onDeleteProperty(id: string): void;
336
+ /**
337
+ * Build WASM predicate objects from PredicateSpec array.
338
+ */
339
+ buildWasmPredicates(specs: PredicateSpec[]): Predicate[];
340
+ /**
341
+ * Serialize predicates for the operation log.
342
+ */
343
+ serializePredicates(specs: PredicateSpec[]): SerializedPredicate[] | undefined;
344
+ /**
345
+ * Record a gate operation. Call this when recording is active
346
+ * and you want to log a gate call for replay.
347
+ */
348
+ recordOp(op: QuantumOperation): void;
349
+ /**
350
+ * Get the recorded index for a property handle.
351
+ */
352
+ getIndex(prop: QuantumProperty): number | undefined;
353
+ /** Begin recording quantum operations. Resets any existing log. */
354
+ startRecording(): void;
355
+ /** Stop recording and return the captured log. */
356
+ stopRecording(): QuantumOperation[];
357
+ /** Whether recording is currently active. */
358
+ isRecording(): boolean;
359
+ /** Get a copy of the current operation log (even while recording). */
360
+ getOperationLog(): QuantumOperation[];
361
+ /**
362
+ * Replay an operation log to recreate quantum state from scratch.
363
+ * Clears all existing state on the manager first. Measurements are
364
+ * forced to their recorded outcomes via forced_measure_properties.
365
+ */
366
+ replayLog(operations: QuantumOperation[]): void;
367
+ private _replayPredicates;
368
+ }
369
+
370
+ export { type PredicateSpec$1 as PredicateSpec, type QuantumOperation, QuantumPropertyManager, QuantumRecorder, type QuantumRecorderHook, type SerializedPredicate, ensureLoaded, getAttribution, getMaxDimension, getMaxQudits, getModule, getQuantumForge, getVersion, getWasmMemoryBytes, isReady, registerServiceWorker, setWasmBasePath, startBackgroundLoad, useQuantumForgeBuild };