qft-wasm 1.0.0

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.
package/README.md ADDED
@@ -0,0 +1,299 @@
1
+ # qft-wasm - Quantum File Type WebAssembly SDK
2
+
3
+ Production-grade WebAssembly bindings for the Quantum File Type (.qft) format.
4
+
5
+ ## Installation
6
+
7
+ ### npm
8
+
9
+ ```bash
10
+ npm install qft-wasm
11
+ ```
12
+
13
+ ### From Source
14
+
15
+ ```bash
16
+ # Requires wasm-pack
17
+ cargo install wasm-pack
18
+ cd crates/qft-wasm
19
+ wasm-pack build --target web --release
20
+ ```
21
+
22
+ ## Quick Start
23
+
24
+ ### Browser (ES Modules)
25
+
26
+ ```javascript
27
+ import init, { QftFile, createBellState, version } from 'qft-wasm';
28
+
29
+ async function main() {
30
+ await init();
31
+
32
+ console.log(`QFT WASM v${version()}`);
33
+
34
+ // Create a 4-qubit state
35
+ const file = new QftFile(4);
36
+ console.log(`Qubits: ${file.numQubits}`);
37
+ console.log(`Dimension: ${file.dimension}`);
38
+
39
+ // Set Bell-like state |0000⟩ + |1111⟩
40
+ const real = new Float64Array(16);
41
+ const imag = new Float64Array(16);
42
+ real[0] = 1 / Math.sqrt(2);
43
+ real[15] = 1 / Math.sqrt(2);
44
+ file.setAmplitudes(real, imag);
45
+
46
+ // Serialize to bytes
47
+ const bytes = file.toBytes();
48
+ console.log(`Serialized: ${bytes.length} bytes`);
49
+
50
+ // Download as file
51
+ downloadQft(bytes, 'state.qft');
52
+ }
53
+
54
+ function downloadQft(bytes, filename) {
55
+ const blob = new Blob([bytes], { type: 'application/octet-stream' });
56
+ const url = URL.createObjectURL(blob);
57
+ const a = document.createElement('a');
58
+ a.href = url;
59
+ a.download = filename;
60
+ a.click();
61
+ URL.revokeObjectURL(url);
62
+ }
63
+
64
+ main();
65
+ ```
66
+
67
+ ### Node.js
68
+
69
+ ```javascript
70
+ const { QftFile, version } = require('qft-wasm');
71
+
72
+ // Create state
73
+ const file = new QftFile(4);
74
+ console.log(`Version: ${version()}`);
75
+
76
+ // Export to JSON
77
+ const json = file.toJson();
78
+ console.log(json);
79
+
80
+ // Save to file
81
+ const fs = require('fs');
82
+ fs.writeFileSync('state.qft', Buffer.from(file.toBytes()));
83
+ ```
84
+
85
+ ### TypeScript
86
+
87
+ ```typescript
88
+ import init, { QftFile, Encoding, StreamingConverter } from 'qft-wasm';
89
+
90
+ async function processLargeFile(file: File): Promise<void> {
91
+ await init();
92
+
93
+ const converter = new StreamingConverter(
94
+ file.size,
95
+ 1024 * 1024, // 1MB chunks
96
+ Encoding.Amplitude
97
+ );
98
+
99
+ const reader = file.stream().getReader();
100
+
101
+ while (!converter.isComplete) {
102
+ const { done, value } = await reader.read();
103
+ if (done) break;
104
+
105
+ const chunk = new Uint8Array(value);
106
+ const state = converter.processChunk(chunk);
107
+ console.log(`Progress: ${(converter.progress * 100).toFixed(1)}%`);
108
+
109
+ // Process state...
110
+ }
111
+ }
112
+ ```
113
+
114
+ ## API Reference
115
+
116
+ ### QftFile Class
117
+
118
+ ```typescript
119
+ class QftFile {
120
+ constructor(numQubits: number);
121
+
122
+ // Properties
123
+ readonly numQubits: number;
124
+ readonly dimension: number;
125
+ bondDimension: number;
126
+ golayEnabled: boolean;
127
+
128
+ // Amplitude access
129
+ getAmplitudesReal(): Float64Array;
130
+ getAmplitudesImag(): Float64Array;
131
+ setAmplitudes(real: Float64Array, imag: Float64Array): void;
132
+ getAmplitude(index: number): { real: number; imag: number };
133
+ setAmplitude(index: number, real: number, imag: number): void;
134
+
135
+ // Validation
136
+ isNormalized(tolerance?: number): boolean;
137
+ normalize(): void;
138
+ normSquared(): number;
139
+ fidelity(other: QftFile): number;
140
+
141
+ // Metadata
142
+ setMetadata(key: string, value: string): void;
143
+ getMetadata(key: string): string | undefined;
144
+ getMetadataObject(): Record<string, string>;
145
+
146
+ // Serialization
147
+ toBytes(): Uint8Array;
148
+ static fromBytes(data: Uint8Array): QftFile;
149
+ toJson(): string;
150
+ static fromJson(json: string): QftFile;
151
+ }
152
+ ```
153
+
154
+ ### StreamingConverter Class
155
+
156
+ ```typescript
157
+ class StreamingConverter {
158
+ constructor(
159
+ totalSize: number,
160
+ chunkSize?: number,
161
+ encoding?: Encoding
162
+ );
163
+
164
+ processChunk(data: Uint8Array): QftFile;
165
+
166
+ readonly progress: number;
167
+ readonly isComplete: boolean;
168
+ readonly chunkSize: number;
169
+ }
170
+ ```
171
+
172
+ ### Encoding Enum
173
+
174
+ ```typescript
175
+ enum Encoding {
176
+ Amplitude = 0,
177
+ BasisState = 1,
178
+ Qram = 2,
179
+ Angle = 3,
180
+ Block = 4,
181
+ }
182
+ ```
183
+
184
+ ### Utility Functions
185
+
186
+ ```typescript
187
+ function version(): string;
188
+ function verify(data: Uint8Array): boolean;
189
+ function fidelityFromBytes(data1: Uint8Array, data2: Uint8Array): number;
190
+ function createBellState(): QftFile;
191
+ function createGhzState(numQubits: number): QftFile;
192
+ function createUniformState(numQubits: number): QftFile;
193
+ ```
194
+
195
+ ## Examples
196
+
197
+ ### Create Bell State
198
+
199
+ ```javascript
200
+ import { createBellState } from 'qft-wasm';
201
+
202
+ const bell = createBellState();
203
+ console.log(bell.getAmplitudesReal());
204
+ // Float64Array [0.707..., 0, 0, 0.707...]
205
+ ```
206
+
207
+ ### Calculate Fidelity
208
+
209
+ ```javascript
210
+ import { QftFile } from 'qft-wasm';
211
+
212
+ const state1 = new QftFile(2);
213
+ const state2 = new QftFile(2);
214
+
215
+ // Both initialized to |00⟩
216
+ const fidelity = state1.fidelity(state2);
217
+ console.log(`Fidelity: ${fidelity}`); // 1.0
218
+ ```
219
+
220
+ ### File Upload Handler
221
+
222
+ ```javascript
223
+ async function handleFileUpload(event) {
224
+ const file = event.target.files[0];
225
+ const buffer = await file.arrayBuffer();
226
+ const bytes = new Uint8Array(buffer);
227
+
228
+ try {
229
+ const qft = QftFile.fromBytes(bytes);
230
+ console.log(`Loaded ${qft.numQubits}-qubit state`);
231
+ console.log(`Normalized: ${qft.isNormalized()}`);
232
+ } catch (e) {
233
+ console.error('Invalid QFT file:', e.message);
234
+ }
235
+ }
236
+ ```
237
+
238
+ ### React Component
239
+
240
+ ```tsx
241
+ import React, { useState, useEffect } from 'react';
242
+ import init, { QftFile, createBellState } from 'qft-wasm';
243
+
244
+ export function QuantumStateViewer() {
245
+ const [state, setState] = useState<QftFile | null>(null);
246
+ const [ready, setReady] = useState(false);
247
+
248
+ useEffect(() => {
249
+ init().then(() => setReady(true));
250
+ }, []);
251
+
252
+ const createBell = () => {
253
+ const bell = createBellState();
254
+ setState(bell);
255
+ };
256
+
257
+ if (!ready) return <div>Loading WASM...</div>;
258
+
259
+ return (
260
+ <div>
261
+ <button onClick={createBell}>Create Bell State</button>
262
+ {state && (
263
+ <div>
264
+ <p>Qubits: {state.numQubits}</p>
265
+ <p>Dimension: {state.dimension}</p>
266
+ <p>Normalized: {state.isNormalized() ? 'Yes' : 'No'}</p>
267
+ </div>
268
+ )}
269
+ </div>
270
+ );
271
+ }
272
+ ```
273
+
274
+ ## Bundle Size
275
+
276
+ | Build | Size (gzip) |
277
+ |-------|-------------|
278
+ | Web | ~45 KB |
279
+ | Node.js | ~50 KB |
280
+
281
+ ## Browser Support
282
+
283
+ - Chrome 89+
284
+ - Firefox 89+
285
+ - Safari 15+
286
+ - Edge 89+
287
+
288
+ ## Performance
289
+
290
+ | Operation | 10 qubits | 20 qubits |
291
+ |-----------|-----------|-----------|
292
+ | Create | 0.1 ms | 2 ms |
293
+ | Serialize | 0.2 ms | 20 ms |
294
+ | Deserialize | 0.1 ms | 15 ms |
295
+ | Fidelity | 0.1 ms | 10 ms |
296
+
297
+ ## License
298
+
299
+ MIT OR Apache-2.0
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "qft-wasm",
3
+ "type": "module",
4
+ "collaborators": [
5
+ "MACROHARD Quantum OS Team"
6
+ ],
7
+ "description": "Production-grade WebAssembly bindings for Quantum File Type (.qft) format",
8
+ "version": "1.0.0",
9
+ "license": "MIT OR Apache-2.0",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/WeaveITMeta/Quantum-Operating-System"
13
+ },
14
+ "files": [
15
+ "qft_wasm_bg.wasm",
16
+ "qft_wasm.js",
17
+ "qft_wasm.d.ts"
18
+ ],
19
+ "main": "qft_wasm.js",
20
+ "types": "qft_wasm.d.ts",
21
+ "sideEffects": [
22
+ "./snippets/*"
23
+ ],
24
+ "keywords": [
25
+ "quantum",
26
+ "wasm",
27
+ "webassembly",
28
+ "qft",
29
+ "mps"
30
+ ]
31
+ }
package/qft_wasm.d.ts ADDED
@@ -0,0 +1,288 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * Encoding strategies for file conversion.
6
+ */
7
+ export enum Encoding {
8
+ Amplitude = 0,
9
+ BasisState = 1,
10
+ Qram = 2,
11
+ Angle = 3,
12
+ Block = 4,
13
+ }
14
+
15
+ /**
16
+ * A Quantum File Type (.qft) file handle for JavaScript/TypeScript.
17
+ *
18
+ * @example
19
+ * ```javascript
20
+ * import { QftFile } from 'qft-wasm';
21
+ *
22
+ * // Create a 4-qubit state
23
+ * const file = new QftFile(4);
24
+ * console.log(file.numQubits); // 4
25
+ * console.log(file.dimension); // 16
26
+ *
27
+ * // Set Bell-like state
28
+ * const real = new Float64Array(16);
29
+ * const imag = new Float64Array(16);
30
+ * real[0] = 1 / Math.sqrt(2);
31
+ * real[15] = 1 / Math.sqrt(2);
32
+ * file.setAmplitudes(real, imag);
33
+ *
34
+ * // Save to bytes
35
+ * const bytes = file.toBytes();
36
+ * ```
37
+ */
38
+ export class QftFile {
39
+ free(): void;
40
+ [Symbol.dispose](): void;
41
+ /**
42
+ * Calculate fidelity with another state: |⟨self|other⟩|².
43
+ *
44
+ * @param other - Another QftFile
45
+ * @returns Fidelity between 0 and 1
46
+ */
47
+ fidelity(other: QftFile): number;
48
+ /**
49
+ * Create from Uint8Array.
50
+ */
51
+ static fromBytes(data: Uint8Array): QftFile;
52
+ /**
53
+ * Create from JSON string.
54
+ */
55
+ static fromJson(json: string): QftFile;
56
+ /**
57
+ * Get a single amplitude by basis state index.
58
+ *
59
+ * @param index - Basis state index (0 to 2^n - 1)
60
+ * @returns Object with real and imag properties
61
+ */
62
+ getAmplitude(index: number): any;
63
+ /**
64
+ * Get the imaginary parts of amplitudes as Float64Array.
65
+ */
66
+ getAmplitudesImag(): Float64Array;
67
+ /**
68
+ * Get the real parts of amplitudes as Float64Array.
69
+ */
70
+ getAmplitudesReal(): Float64Array;
71
+ /**
72
+ * Get metadata value by key.
73
+ */
74
+ getMetadata(key: string): string | undefined;
75
+ /**
76
+ * Get all metadata as a JavaScript object.
77
+ */
78
+ getMetadataObject(): any;
79
+ /**
80
+ * Check if the state is normalized.
81
+ *
82
+ * @param tolerance - Tolerance for check (default: 1e-10)
83
+ * @returns true if normalized within tolerance
84
+ */
85
+ isNormalized(tolerance?: number | null): boolean;
86
+ /**
87
+ * Create a new QFT file with the specified number of qubits.
88
+ *
89
+ * @param numQubits - Number of qubits (1-30)
90
+ * @returns A new QftFile initialized to |0...0⟩
91
+ * @throws Error if numQubits is not in range 1-30
92
+ */
93
+ constructor(num_qubits: number);
94
+ /**
95
+ * Calculate the norm squared.
96
+ */
97
+ normSquared(): number;
98
+ /**
99
+ * Normalize the state vector in place.
100
+ */
101
+ normalize(): void;
102
+ /**
103
+ * Set a single amplitude by basis state index.
104
+ *
105
+ * @param index - Basis state index (0 to 2^n - 1)
106
+ * @param real - Real part of amplitude
107
+ * @param imag - Imaginary part of amplitude
108
+ */
109
+ setAmplitude(index: number, real: number, imag: number): void;
110
+ /**
111
+ * Set amplitudes from Float64Arrays.
112
+ *
113
+ * @param real - Real parts of amplitudes
114
+ * @param imag - Imaginary parts of amplitudes
115
+ */
116
+ setAmplitudes(real: Float64Array, imag: Float64Array): void;
117
+ /**
118
+ * Set metadata key-value pair.
119
+ */
120
+ setMetadata(key: string, value: string): void;
121
+ /**
122
+ * Serialize to Uint8Array.
123
+ */
124
+ toBytes(): Uint8Array;
125
+ /**
126
+ * Export to JSON string.
127
+ */
128
+ toJson(): string;
129
+ /**
130
+ * Bond dimension for MPS compression.
131
+ */
132
+ bondDimension: number;
133
+ /**
134
+ * Dimension of the state vector (2^numQubits).
135
+ */
136
+ readonly dimension: number;
137
+ /**
138
+ * Whether Golay error correction is enabled.
139
+ */
140
+ golayEnabled: boolean;
141
+ /**
142
+ * Number of qubits in the quantum state.
143
+ */
144
+ readonly numQubits: number;
145
+ }
146
+
147
+ /**
148
+ * Streaming converter for large files.
149
+ */
150
+ export class StreamingConverter {
151
+ free(): void;
152
+ [Symbol.dispose](): void;
153
+ /**
154
+ * Create a new streaming converter.
155
+ *
156
+ * @param totalSize - Total size of input data in bytes
157
+ * @param chunkSize - Size of each chunk (default: 1MB)
158
+ * @param encoding - Encoding strategy (default: Amplitude)
159
+ */
160
+ constructor(total_size: number, chunk_size?: number | null, encoding?: Encoding | null);
161
+ /**
162
+ * Process a chunk of data.
163
+ *
164
+ * @param data - Uint8Array chunk
165
+ * @returns QftFile for this chunk
166
+ */
167
+ processChunk(data: Uint8Array): QftFile;
168
+ /**
169
+ * Get recommended chunk size.
170
+ */
171
+ readonly chunkSize: number;
172
+ /**
173
+ * Check if conversion is complete.
174
+ */
175
+ readonly isComplete: boolean;
176
+ /**
177
+ * Get conversion progress (0.0 to 1.0).
178
+ */
179
+ readonly progress: number;
180
+ }
181
+
182
+ /**
183
+ * Create a Bell state for testing.
184
+ */
185
+ export function createBellState(): QftFile;
186
+
187
+ /**
188
+ * Create a GHZ state for testing.
189
+ */
190
+ export function createGhzState(num_qubits: number): QftFile;
191
+
192
+ /**
193
+ * Create a uniform superposition state.
194
+ */
195
+ export function createUniformState(num_qubits: number): QftFile;
196
+
197
+ /**
198
+ * Calculate fidelity between two states from bytes.
199
+ */
200
+ export function fidelityFromBytes(data1: Uint8Array, data2: Uint8Array): number;
201
+
202
+ /**
203
+ * Initialize the WASM module with better error messages
204
+ */
205
+ export function init(): void;
206
+
207
+ /**
208
+ * Verify a QFT file's integrity from bytes.
209
+ */
210
+ export function verify(data: Uint8Array): boolean;
211
+
212
+ /**
213
+ * Get library version.
214
+ */
215
+ export function version(): string;
216
+
217
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
218
+
219
+ export interface InitOutput {
220
+ readonly memory: WebAssembly.Memory;
221
+ readonly __wbg_qftfile_free: (a: number, b: number) => void;
222
+ readonly __wbg_streamingconverter_free: (a: number, b: number) => void;
223
+ readonly createBellState: () => [number, number, number];
224
+ readonly createGhzState: (a: number) => [number, number, number];
225
+ readonly createUniformState: (a: number) => [number, number, number];
226
+ readonly fidelityFromBytes: (a: any, b: any) => [number, number, number];
227
+ readonly qftfile_bondDimension: (a: number) => number;
228
+ readonly qftfile_dimension: (a: number) => number;
229
+ readonly qftfile_fidelity: (a: number, b: number) => [number, number, number];
230
+ readonly qftfile_fromBytes: (a: any) => [number, number, number];
231
+ readonly qftfile_fromJson: (a: number, b: number) => [number, number, number];
232
+ readonly qftfile_getAmplitude: (a: number, b: number) => [number, number, number];
233
+ readonly qftfile_getAmplitudesImag: (a: number) => any;
234
+ readonly qftfile_getAmplitudesReal: (a: number) => any;
235
+ readonly qftfile_getMetadata: (a: number, b: number, c: number) => [number, number];
236
+ readonly qftfile_getMetadataObject: (a: number) => [number, number, number];
237
+ readonly qftfile_golayEnabled: (a: number) => number;
238
+ readonly qftfile_isNormalized: (a: number, b: number, c: number) => number;
239
+ readonly qftfile_new: (a: number) => [number, number, number];
240
+ readonly qftfile_normSquared: (a: number) => number;
241
+ readonly qftfile_normalize: (a: number) => [number, number];
242
+ readonly qftfile_numQubits: (a: number) => number;
243
+ readonly qftfile_setAmplitude: (a: number, b: number, c: number, d: number) => [number, number];
244
+ readonly qftfile_setAmplitudes: (a: number, b: any, c: any) => [number, number];
245
+ readonly qftfile_setMetadata: (a: number, b: number, c: number, d: number, e: number) => void;
246
+ readonly qftfile_set_bondDimension: (a: number, b: number) => [number, number];
247
+ readonly qftfile_set_golayEnabled: (a: number, b: number) => void;
248
+ readonly qftfile_toBytes: (a: number) => any;
249
+ readonly qftfile_toJson: (a: number) => [number, number, number, number];
250
+ readonly streamingconverter_chunkSize: (a: number) => number;
251
+ readonly streamingconverter_isComplete: (a: number) => number;
252
+ readonly streamingconverter_new: (a: number, b: number, c: number) => number;
253
+ readonly streamingconverter_processChunk: (a: number, b: any) => [number, number, number];
254
+ readonly streamingconverter_progress: (a: number) => number;
255
+ readonly verify: (a: any) => number;
256
+ readonly version: () => [number, number];
257
+ readonly init: () => void;
258
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
259
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
260
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
261
+ readonly __wbindgen_exn_store: (a: number) => void;
262
+ readonly __externref_table_alloc: () => number;
263
+ readonly __wbindgen_externrefs: WebAssembly.Table;
264
+ readonly __externref_table_dealloc: (a: number) => void;
265
+ readonly __wbindgen_start: () => void;
266
+ }
267
+
268
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
269
+
270
+ /**
271
+ * Instantiates the given `module`, which can either be bytes or
272
+ * a precompiled `WebAssembly.Module`.
273
+ *
274
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
275
+ *
276
+ * @returns {InitOutput}
277
+ */
278
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
279
+
280
+ /**
281
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
282
+ * for everything else, calls `WebAssembly.instantiate` directly.
283
+ *
284
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
285
+ *
286
+ * @returns {Promise<InitOutput>}
287
+ */
288
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;