quantrs2-core 0.1.3

Core types and traits for the QuantRS2 quantum computing framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// WebGPU shaders for specialized quantum gate operations
// Cross-platform high-performance implementation for holonomic, post-quantum crypto, and quantum ML gates

// Workgroup size constants
const WORKGROUP_SIZE: u32 = 256u;
const MAX_QUBITS: u32 = 16u;
const TENSOR_SIZE: u32 = 16u;

// Complex number structure
struct Complex {
    real: f32,
    imag: f32,
}

// Helper functions for complex arithmetic
fn complex_add(a: Complex, b: Complex) -> Complex {
    return Complex(a.real + b.real, a.imag + b.imag);
}

fn complex_mul(a: Complex, b: Complex) -> Complex {
    return Complex(
        a.real * b.real - a.imag * b.imag,
        a.real * b.imag + a.imag * b.real
    );
}

fn complex_conj(a: Complex) -> Complex {
    return Complex(a.real, -a.imag);
}

fn complex_abs_squared(a: Complex) -> f32 {
    return a.real * a.real + a.imag * a.imag;
}

fn complex_normalize(a: Complex) -> Complex {
    let norm = sqrt(complex_abs_squared(a));
    if (norm > 1e-10) {
        return Complex(a.real / norm, a.imag / norm);
    } else {
        return Complex(0.0, 0.0);
    }
}

// ============= Holonomic Gate Shader =============

// Buffer bindings for holonomic gate
@group(0) @binding(0) var<storage, read_write> state: array<Complex>;
@group(0) @binding(1) var<storage, read> holonomy_matrix: array<Complex>;
@group(0) @binding(2) var<storage, read> target_qubits: array<u32>;
@group(0) @binding(3) var<uniform> params: HolonomicParams;

struct HolonomicParams {
    state_size: u32,
    num_qubits: u32,
    matrix_size: u32,
    work_per_thread: u32,
}

@compute @workgroup_size(WORKGROUP_SIZE)
fn holonomic_gate_main(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let thread_id = global_id.x;
    let total_work_items = params.state_size / params.work_per_thread;
    
    if (thread_id >= total_work_items) {
        return;
    }
    
    // Calculate qubit mask for target qubits
    var qubit_mask: u32 = 0u;
    for (var i: u32 = 0u; i < params.num_qubits; i++) {
        qubit_mask |= (1u << target_qubits[i]);
    }
    
    // Calculate base state index for this thread
    var base_idx: u32 = 0u;
    var temp_id = thread_id;
    
    for (var i: u32 = 0u; i < 32u; i++) {
        if ((qubit_mask & (1u << i)) == 0u) {
            if ((temp_id & 1u) != 0u) {
                base_idx |= (1u << i);
            }
            temp_id >>= 1u;
        }
    }
    
    // Local storage for state amplitudes
    var temp_state: array<Complex, 32>;
    
    // Load relevant state amplitudes
    for (var i: u32 = 0u; i < params.work_per_thread && i < 32u; i++) {
        let state_idx = base_idx | (i << firstTrailingBit(qubit_mask));
        temp_state[i] = state[state_idx];
    }
    
    // Apply holonomic transformation using matrix multiplication
    for (var i: u32 = 0u; i < params.work_per_thread && i < 32u; i++) {
        var result = Complex(0.0, 0.0);
        
        for (var j: u32 = 0u; j < params.work_per_thread && j < 32u; j++) {
            let matrix_idx = i * params.work_per_thread + j;
            result = complex_add(result, complex_mul(holonomy_matrix[matrix_idx], temp_state[j]));
        }
        
        let state_idx = base_idx | (i << firstTrailingBit(qubit_mask));
        state[state_idx] = result;
    }
}

// ============= Post-Quantum Hash Gate Shader =============

@group(0) @binding(0) var<storage, read_write> hash_state: array<Complex>;
@group(0) @binding(1) var<storage, read> hash_circuit: array<Complex>;
@group(0) @binding(2) var<uniform> hash_params: PostQuantumParams;

struct PostQuantumParams {
    state_size: u32,
    rate: u32,
    capacity: u32,
    num_rounds: u32,
}

// Shared memory for sponge construction
var<workgroup> sponge_state: array<Complex, WORKGROUP_SIZE>;
var<workgroup> round_matrix: array<Complex, WORKGROUP_SIZE>;

@compute @workgroup_size(WORKGROUP_SIZE)
fn post_quantum_hash_main(@builtin(global_invocation_id) global_id: vec3<u32>,
                          @builtin(local_invocation_id) local_id: vec3<u32>) {
    let thread_id = global_id.x;
    let local_thread_id = local_id.x;
    
    if (thread_id >= hash_params.state_size) {
        return;
    }
    
    // Load state into shared memory for rate portion
    if (local_thread_id < hash_params.rate && local_thread_id < WORKGROUP_SIZE) {
        sponge_state[local_thread_id] = hash_state[thread_id];
    }
    
    workgroupBarrier();
    
    // Apply quantum sponge rounds
    for (var round: u32 = 0u; round < hash_params.num_rounds; round++) {
        // Absorption phase - XOR input with rate portion
        if (local_thread_id < hash_params.rate) {
            let circuit_idx = round * hash_params.rate + local_thread_id;
            sponge_state[local_thread_id] = complex_add(
                sponge_state[local_thread_id], 
                hash_circuit[circuit_idx]
            );
        }
        
        workgroupBarrier();
        
        // Permutation phase - apply quantum permutation
        var temp = Complex(0.0, 0.0);
        if (local_thread_id < hash_params.rate) {
            for (var i: u32 = 0u; i < hash_params.rate; i++) {
                let perm_idx = round * hash_params.rate * hash_params.rate + 
                              local_thread_id * hash_params.rate + i;
                temp = complex_add(temp, complex_mul(
                    hash_circuit[perm_idx], 
                    sponge_state[i]
                ));
            }
            sponge_state[local_thread_id] = temp;
        }
        
        workgroupBarrier();
    }
    
    // Write back to global memory
    if (local_thread_id < hash_params.rate && thread_id < hash_params.state_size) {
        hash_state[thread_id] = sponge_state[local_thread_id];
    }
}

// ============= Quantum ML Attention Shader =============

@group(0) @binding(0) var<storage, read_write> attention_state: array<Complex>;
@group(0) @binding(1) var<storage, read> query_params: array<Complex>;
@group(0) @binding(2) var<storage, read> key_params: array<Complex>;
@group(0) @binding(3) var<storage, read> value_params: array<Complex>;
@group(0) @binding(4) var<uniform> attention_params: AttentionParams;

struct AttentionParams {
    state_size: u32,
    num_heads: u32,
    head_dim: u32,
    seq_length: u32,
}

// Shared memory for attention computation
var<workgroup> attention_scores: array<Complex, WORKGROUP_SIZE>;
var<workgroup> query_cache: array<Complex, WORKGROUP_SIZE>;
var<workgroup> key_cache: array<Complex, WORKGROUP_SIZE>;
var<workgroup> value_cache: array<Complex, WORKGROUP_SIZE>;

@compute @workgroup_size(WORKGROUP_SIZE)
fn quantum_ml_attention_main(@builtin(global_invocation_id) global_id: vec3<u32>,
                             @builtin(local_invocation_id) local_id: vec3<u32>,
                             @builtin(workgroup_id) workgroup_id: vec3<u32>) {
    let thread_id = local_id.x;
    let head_id = workgroup_id.x % attention_params.num_heads;
    let seq_id = workgroup_id.x / attention_params.num_heads;
    
    if (seq_id >= attention_params.seq_length) {
        return;
    }
    
    let head_offset = head_id * attention_params.head_dim;
    let seq_offset = seq_id * attention_params.head_dim;
    
    // Load query, key, value for this head into shared memory
    if (thread_id < attention_params.head_dim) {
        query_cache[thread_id] = query_params[head_offset + thread_id];
        key_cache[thread_id] = key_params[head_offset + thread_id];
        value_cache[thread_id] = value_params[head_offset + thread_id];
    }
    
    workgroupBarrier();
    
    // Compute attention scores using quantum interference
    if (thread_id < attention_params.head_dim) {
        let query_val = query_cache[thread_id];
        var attention_sum = Complex(0.0, 0.0);
        
        // Compute attention weights through quantum inner product
        for (var k: u32 = 0u; k < attention_params.head_dim; k++) {
            let key_val = key_cache[k];
            let score = complex_mul(complex_conj(query_val), key_val);
            attention_sum = complex_add(attention_sum, score);
        }
        
        // Apply quantum normalization (softmax-like)
        attention_scores[thread_id] = complex_normalize(attention_sum);
    }
    
    workgroupBarrier();
    
    // Apply attention to values
    if (thread_id < attention_params.head_dim) {
        var output = Complex(0.0, 0.0);
        
        for (var v: u32 = 0u; v < attention_params.head_dim; v++) {
            output = complex_add(output, complex_mul(attention_scores[v], value_cache[v]));
        }
        
        // Update state with attention output
        let state_idx = seq_id * attention_params.num_heads * attention_params.head_dim + 
                       head_offset + thread_id;
        if (state_idx < attention_params.state_size) {
            attention_state[state_idx] = output;
        }
    }
}

// ============= Fused Rotation Sequence Shader =============

@group(0) @binding(0) var<storage, read_write> rotation_state: array<Complex>;
@group(0) @binding(1) var<storage, read> rotation_angles: array<f32>;
@group(0) @binding(2) var<storage, read> rotation_axes: array<u32>; // 0=X, 1=Y, 2=Z
@group(0) @binding(3) var<storage, read> rotation_targets: array<u32>;
@group(0) @binding(4) var<uniform> rotation_params: RotationParams;

struct RotationParams {
    state_size: u32,
    num_rotations: u32,
    num_qubits: u32,
}

@compute @workgroup_size(WORKGROUP_SIZE)
fn fused_rotation_main(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let thread_id = global_id.x;
    
    if (thread_id >= rotation_params.state_size) {
        return;
    }
    
    var current_amplitude = rotation_state[thread_id];
    
    // Apply each rotation in the sequence
    for (var rot: u32 = 0u; rot < rotation_params.num_rotations; rot++) {
        let angle = rotation_angles[rot];
        let axis = rotation_axes[rot];
        let target_qubit = rotation_targets[rot];
        
        let qubit_state = (thread_id >> target_qubit) & 1u;
        let paired_idx = thread_id ^ (1u << target_qubit);
        
        let cos_half = cos(angle / 2.0);
        let sin_half = sin(angle / 2.0);
        
        var paired_amplitude = Complex(0.0, 0.0);
        if (paired_idx < rotation_params.state_size) {
            paired_amplitude = rotation_state[paired_idx];
        }
        
        var new_amplitude: Complex;
        
        switch (axis) {
            case 0u: { // X rotation
                if (qubit_state == 0u) {
                    new_amplitude = complex_add(
                        complex_mul(Complex(cos_half, 0.0), current_amplitude),
                        complex_mul(Complex(0.0, -sin_half), paired_amplitude)
                    );
                } else {
                    new_amplitude = complex_add(
                        complex_mul(Complex(cos_half, 0.0), current_amplitude),
                        complex_mul(Complex(0.0, -sin_half), paired_amplitude)
                    );
                }
            }
            case 1u: { // Y rotation
                if (qubit_state == 0u) {
                    new_amplitude = complex_add(
                        complex_mul(Complex(cos_half, 0.0), current_amplitude),
                        complex_mul(Complex(-sin_half, 0.0), paired_amplitude)
                    );
                } else {
                    new_amplitude = complex_add(
                        complex_mul(Complex(sin_half, 0.0), paired_amplitude),
                        complex_mul(Complex(cos_half, 0.0), current_amplitude)
                    );
                }
            }
            case 2u: { // Z rotation
                if (qubit_state == 0u) {
                    new_amplitude = complex_mul(Complex(cos_half, -sin_half), current_amplitude);
                } else {
                    new_amplitude = complex_mul(Complex(cos_half, sin_half), current_amplitude);
                }
            }
            default: {
                new_amplitude = current_amplitude;
            }
        }
        
        current_amplitude = new_amplitude;
    }
    
    // Write final result
    rotation_state[thread_id] = current_amplitude;
}

// ============= Tensor Network Contraction Shader =============

@group(0) @binding(0) var<storage, read_write> tensor_data: array<Complex>;
@group(0) @binding(1) var<storage, read> contraction_indices: array<u32>;
@group(0) @binding(2) var<uniform> tensor_params: TensorParams;

struct TensorParams {
    tensor_size: u32,
    contraction_pairs: u32,
    bond_dimension: u32,
}

// Shared memory for tensor operations
var<workgroup> tensor_cache: array<Complex, WORKGROUP_SIZE>;

@compute @workgroup_size(WORKGROUP_SIZE)
fn tensor_contraction_main(@builtin(global_invocation_id) global_id: vec3<u32>,
                          @builtin(local_invocation_id) local_id: vec3<u32>) {
    let thread_id = global_id.x;
    let local_thread_id = local_id.x;
    
    if (thread_id >= tensor_params.tensor_size) {
        return;
    }
    
    // Load tensor data into shared memory
    if (local_thread_id < WORKGROUP_SIZE && thread_id < tensor_params.tensor_size) {
        tensor_cache[local_thread_id] = tensor_data[thread_id];
    }
    
    workgroupBarrier();
    
    // Perform tensor contraction
    var result = Complex(0.0, 0.0);
    
    for (var pair: u32 = 0u; pair < tensor_params.contraction_pairs; pair++) {
        let idx1 = contraction_indices[pair * 2u];
        let idx2 = contraction_indices[pair * 2u + 1u];
        
        if (idx1 < WORKGROUP_SIZE && idx2 < WORKGROUP_SIZE) {
            result = complex_add(result, complex_mul(tensor_cache[idx1], tensor_cache[idx2]));
        }
    }
    
    workgroupBarrier();
    
    // Write result back to global memory
    tensor_data[thread_id] = result;
}

// ============= Gate Fusion Optimization Shader =============

@group(0) @binding(0) var<storage, read_write> fusion_state: array<Complex>;
@group(0) @binding(1) var<storage, read> fusion_matrices: array<Complex>;
@group(0) @binding(2) var<storage, read> fusion_targets: array<u32>;
@group(0) @binding(3) var<uniform> fusion_params: FusionParams;

struct FusionParams {
    state_size: u32,
    num_fused_gates: u32,
    matrix_stride: u32,
}

@compute @workgroup_size(WORKGROUP_SIZE)
fn gate_fusion_main(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let thread_id = global_id.x;
    
    if (thread_id >= fusion_params.state_size) {
        return;
    }
    
    var current_amplitude = fusion_state[thread_id];
    
    // Apply fused gate sequence
    for (var gate: u32 = 0u; gate < fusion_params.num_fused_gates; gate++) {
        let target_qubit = fusion_targets[gate];
        let matrix_offset = gate * fusion_params.matrix_stride;
        
        let qubit_state = (thread_id >> target_qubit) & 1u;
        let paired_idx = thread_id ^ (1u << target_qubit);
        
        var paired_amplitude = Complex(0.0, 0.0);
        if (paired_idx < fusion_params.state_size) {
            paired_amplitude = fusion_state[paired_idx];
        }
        
        // Apply 2x2 gate matrix
        let m00 = fusion_matrices[matrix_offset + 0u];
        let m01 = fusion_matrices[matrix_offset + 1u];
        let m10 = fusion_matrices[matrix_offset + 2u];
        let m11 = fusion_matrices[matrix_offset + 3u];
        
        var new_amplitude: Complex;
        if (qubit_state == 0u) {
            new_amplitude = complex_add(
                complex_mul(m00, current_amplitude),
                complex_mul(m01, paired_amplitude)
            );
        } else {
            new_amplitude = complex_add(
                complex_mul(m10, paired_amplitude),
                complex_mul(m11, current_amplitude)
            );
        }
        
        current_amplitude = new_amplitude;
    }
    
    // Write final result
    fusion_state[thread_id] = current_amplitude;
}