aboutsummaryrefslogtreecommitdiff
path: root/tester/src/main.rs
blob: 660dc2bd83030d637b3fcb08ec1944e6f768c083 (plain)
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
use libpsi_core::*;
use libpsi_visualizer::*;
use std::time::{Duration, Instant};

struct BenchmarkResult {
    name: String,
    basic_time: Duration,
    mt_time: Duration,
    results_match: bool,
}

fn benchmark_circuit<F>(name: &str, circuit_builder: F) -> BenchmarkResult
where
    F: Fn() -> QuantumCircuit,
{
    let mut circuit_st = circuit_builder();
    let mut circuit_mt = circuit_builder();

    let start_st = Instant::now();
    circuit_st.compute_with(Runtime::BasicRT);
    let basic_time = start_st.elapsed();

    let start_mt = Instant::now();
    circuit_mt.compute_with(Runtime::BasicRTMT);
    let mt_time = start_mt.elapsed();

    let state_st = circuit_st.state();
    let state_mt = circuit_mt.state();

    let results_match = states_equal(state_st, state_mt);

    BenchmarkResult {
        name: name.to_string(),
        basic_time,
        mt_time,
        results_match,
    }
}

fn states_equal(a: &QuantumState, b: &QuantumState) -> bool {
    use crate::maths::vector::Vector;
    if a.size() != b.size() {
        return false;
    }
    for i in 0..a.size() {
        let amp_a = a.get(i);
        let amp_b = b.get(i);
        let diff_real = (amp_a.real - amp_b.real).abs();
        let diff_imag = (amp_a.imaginary - amp_b.imaginary).abs();
        if diff_real > 1e-10 || diff_imag > 1e-10 {
            return false;
        }
    }
    true
}

fn format_duration(d: Duration) -> String {
    if d.as_secs() > 0 {
        format!("{:.3}s", d.as_secs_f64())
    } else if d.as_millis() > 0 {
        format!("{:.3}ms", d.as_secs_f64() * 1000.0)
    } else {
        format!("{:.3}us", d.as_secs_f64() * 1_000_000.0)
    }
}

fn print_benchmark_table(results: &[BenchmarkResult]) {
    // Column widths (content only, not including borders)
    const C1: usize = 30; // Circuit name
    const C2: usize = 12; // BasicRT
    const C3: usize = 12; // BasicRTMT
    const C4: usize = 10; // Speedup
    const C5: usize = 5; // Match

    let top = format!(
        "╔{}═{}═{}═{}═{}╗",
        "═".repeat(C1 + 2),
        "═".repeat(C2 + 2),
        "═".repeat(C3 + 2),
        "═".repeat(C4 + 2),
        "═".repeat(C5 + 2)
    );
    let title = format!(
        "╠{}╤{}╤{}╤{}╤{}╣",
        "═".repeat(C1 + 2),
        "═".repeat(C2 + 2),
        "═".repeat(C3 + 2),
        "═".repeat(C4 + 2),
        "═".repeat(C5 + 2)
    );
    let header = format!(
        "╠{}╪{}╪{}╪{}╪{}╣",
        "═".repeat(C1 + 2),
        "═".repeat(C2 + 2),
        "═".repeat(C3 + 2),
        "═".repeat(C4 + 2),
        "═".repeat(C5 + 2)
    );
    let bottom = format!(
        "╚{}╧{}╧{}╧{}╧{}╝",
        "═".repeat(C1 + 2),
        "═".repeat(C2 + 2),
        "═".repeat(C3 + 2),
        "═".repeat(C4 + 2),
        "═".repeat(C5 + 2)
    );

    let total_width = C1 + C2 + C3 + C4 + C5 + 14;

    println!("\n{}", top);
    println!(
        "║{:^width$}║",
        "RUNTIME BENCHMARK RESULTS",
        width = total_width
    );
    println!("{}", title);
    println!(
        "║ {:<C1$} │ {:^C2$} │ {:^C3$} │ {:^C4$} │ {:^C5$} ║",
        "Circuit",
        "BasicRT",
        "BasicRTMT",
        "Speedup",
        "Match",
        C1 = C1,
        C2 = C2,
        C3 = C3,
        C4 = C4,
        C5 = C5
    );
    println!("{}", header);

    for r in results {
        let speedup = r.basic_time.as_secs_f64() / r.mt_time.as_secs_f64();
        let speedup_str = format!("{:.2}x", speedup);
        let match_str = if r.results_match { "✓" } else { "✗" };

        println!(
            "║ {:<C1$} │ {:>C2$} │ {:>C3$} │ {:>C4$} │ {:^C5$} ║",
            r.name,
            format_duration(r.basic_time),
            format_duration(r.mt_time),
            speedup_str,
            match_str,
            C1 = C1,
            C2 = C2,
            C3 = C3,
            C4 = C4,
            C5 = C5
        );
    }

    println!("{}", bottom);
}

fn main() {
    println!("═══════════════════════════════════════════════════════════════");
    println!("                    PSI Quantum Simulator");
    println!("═══════════════════════════════════════════════════════════════\n");

    let mut results: Vec<BenchmarkResult> = Vec::new();

    println!("┌─────────────────────────────────────────────────────────────┐");
    println!("│ Bell State with Measurement                                 │");
    println!("└─────────────────────────────────────────────────────────────┘\n");

    let bell_builder = || {
        let mut circuit = QuantumCircuit::with_classical(2, 2);
        circuit.h(0).cnot(0, 1).measure(0, 0).measure(1, 1);
        circuit
    };

    let bell = bell_builder();
    println!("Horizontal:\n{}", HorizontalRenderer::new(&bell));
    println!("Vertical:\n{}", VerticalRenderer::new(&bell));

    results.push(benchmark_circuit("Bell State (2 qubits)", bell_builder));
    let mut bell_display = bell_builder();
    bell_display.compute();
    println!("{}\n", bell_display);

    println!("┌─────────────────────────────────────────────────────────────┐");
    println!("│ GHZ State                                                   │");
    println!("└─────────────────────────────────────────────────────────────┘\n");

    let ghz_builder = || {
        let mut circuit = QuantumCircuit::new(3);
        circuit.h(0).cnot(0, 1).cnot(0, 2);
        circuit
    };

    let ghz = ghz_builder();
    println!("Horizontal:\n{}", HorizontalRenderer::new(&ghz));
    println!("Vertical:\n{}", VerticalRenderer::new(&ghz));

    results.push(benchmark_circuit("GHZ State (3 qubits)", ghz_builder));
    let mut ghz_display = ghz_builder();
    ghz_display.compute();
    println!("{}\n", ghz_display);

    println!("┌─────────────────────────────────────────────────────────────┐");
    println!("│ SWAP via 3 CNOTs                                            │");
    println!("└─────────────────────────────────────────────────────────────┘\n");

    let swap_builder = || {
        let mut circuit = QuantumCircuit::new(2);
        circuit.x(0).cnot(0, 1).cnot(1, 0).cnot(0, 1);
        circuit
    };

    let swap_circuit = swap_builder();
    println!("Horizontal:\n{}", HorizontalRenderer::new(&swap_circuit));
    println!("Vertical:\n{}", VerticalRenderer::new(&swap_circuit));

    results.push(benchmark_circuit("SWAP via CNOTs (2 qubits)", swap_builder));
    let mut swap_display = swap_builder();
    swap_display.compute();
    println!("{}\n", swap_display);

    println!("┌─────────────────────────────────────────────────────────────┐");
    println!("│ Toffoli Gate                                                │");
    println!("└─────────────────────────────────────────────────────────────┘\n");

    let toffoli_builder = || {
        let mut circuit = QuantumCircuit::new(3);
        circuit.x(0).x(1).toffoli(0, 1, 2);
        circuit
    };

    let toffoli = toffoli_builder();
    println!("Horizontal:\n{}", HorizontalRenderer::new(&toffoli));
    println!("Vertical:\n{}", VerticalRenderer::new(&toffoli));

    results.push(benchmark_circuit("Toffoli (3 qubits)", toffoli_builder));
    let mut toffoli_display = toffoli_builder();
    toffoli_display.compute();
    println!("{}\n", toffoli_display);

    println!("┌─────────────────────────────────────────────────────────────┐");
    println!("│ Full Circuit with Measurements                              │");
    println!("└─────────────────────────────────────────────────────────────┘\n");

    let full_builder = || {
        let mut circuit = QuantumCircuit::with_classical(3, 3);
        circuit.h(0).h(1).h(2).measure_all();
        circuit
    };

    let full = full_builder();
    println!("Horizontal:\n{}", HorizontalRenderer::new(&full));
    println!("Vertical:\n{}", VerticalRenderer::new(&full));

    results.push(benchmark_circuit(
        "3-qubit Hadamard + Measure",
        full_builder,
    ));
    let mut full_display = full_builder();
    full_display.compute();
    println!("{}\n", full_display);

    println!("┌─────────────────────────────────────────────────────────────┐");
    println!("│ Complex Circuit                                             │");
    println!("└─────────────────────────────────────────────────────────────┘\n");

    let complex_builder = || {
        let mut circuit = QuantumCircuit::with_classical(4, 2);
        circuit
            .h(0)
            .h(1)
            .cnot(0, 2)
            .cnot(1, 3)
            .cz(2, 3)
            .swap(0, 1)
            .measure(0, 0)
            .measure(1, 1);
        circuit
    };

    let complex = complex_builder();
    println!("Horizontal:\n{}", HorizontalRenderer::new(&complex));
    println!("Vertical:\n{}", VerticalRenderer::new(&complex));

    results.push(benchmark_circuit("Complex (4 qubits)", complex_builder));
    let mut complex_display = complex_builder();
    complex_display.compute();
    println!("{}\n", complex_display);

    println!("┌─────────────────────────────────────────────────────────────┐");
    println!("│ Custom Gate: Bell Pair Creator                              │");
    println!("└─────────────────────────────────────────────────────────────┘\n");

    let bell_gate = CustomGateBuilder::new("BELL", 2).h(0).cnot(0, 1).build();
    let bell_gate_clone = bell_gate.clone();

    let custom_bell_builder = move || {
        let mut circuit = QuantumCircuit::new(4);
        circuit
            .apply_custom(bell_gate_clone.clone(), &[0, 1])
            .apply_custom(bell_gate_clone.clone(), &[2, 3]);
        circuit
    };

    let custom_bell = {
        let mut circuit = QuantumCircuit::new(4);
        circuit
            .apply_custom(bell_gate.clone(), &[0, 1])
            .apply_custom(bell_gate.clone(), &[2, 3]);
        circuit
    };
    println!("Horizontal:\n{}", HorizontalRenderer::new(&custom_bell));
    println!("Vertical:\n{}", VerticalRenderer::new(&custom_bell));

    results.push(benchmark_circuit(
        "Custom BELL (4 qubits)",
        custom_bell_builder,
    ));
    let mut custom_bell_display = {
        let mut circuit = QuantumCircuit::new(4);
        circuit
            .apply_custom(bell_gate.clone(), &[0, 1])
            .apply_custom(bell_gate.clone(), &[2, 3]);
        circuit
    };
    custom_bell_display.compute();
    println!("{}\n", custom_bell_display);

    println!("┌─────────────────────────────────────────────────────────────┐");
    println!("│ Custom Gate: Swap via CNOTs                                 │");
    println!("└─────────────────────────────────────────────────────────────┘\n");

    let swap_gate = CustomGateBuilder::new("MYSWAP", 2)
        .cnot(0, 1)
        .cnot(1, 0)
        .cnot(0, 1)
        .build();
    let swap_gate_clone = swap_gate.clone();

    let custom_swap_builder = move || {
        let mut circuit = QuantumCircuit::new(2);
        circuit.x(0).apply_custom(swap_gate_clone.clone(), &[0, 1]);
        circuit
    };

    let custom_swap = {
        let mut circuit = QuantumCircuit::new(2);
        circuit.x(0).apply_custom(swap_gate.clone(), &[0, 1]);
        circuit
    };
    println!("Horizontal:\n{}", HorizontalRenderer::new(&custom_swap));
    println!("Vertical:\n{}", VerticalRenderer::new(&custom_swap));

    results.push(benchmark_circuit(
        "Custom SWAP (2 qubits)",
        custom_swap_builder,
    ));
    let mut custom_swap_display = {
        let mut circuit = QuantumCircuit::new(2);
        circuit.x(0).apply_custom(swap_gate.clone(), &[0, 1]);
        circuit
    };
    custom_swap_display.compute();
    println!("{}\n", custom_swap_display);

    println!("┌─────────────────────────────────────────────────────────────┐");
    println!("│ Custom Gate: Matrix-defined √X gate                         │");
    println!("└─────────────────────────────────────────────────────────────┘\n");

    let sqrt_x_matrix = matrix!(
        [complex!(0.5, 0.5), complex!(0.5, -0.5)];
        [complex!(0.5, -0.5), complex!(0.5, 0.5)]
    );
    let sqrt_x = CustomGate::from_matrix("√X", sqrt_x_matrix);
    let sqrt_x_clone = sqrt_x.clone();

    let sqrt_x_builder = move || {
        let mut circuit = QuantumCircuit::new(1);
        circuit
            .apply_custom(sqrt_x_clone.clone(), &[0])
            .apply_custom(sqrt_x_clone.clone(), &[0]);
        circuit
    };

    let sqrt_x_circuit = {
        let mut circuit = QuantumCircuit::new(1);
        circuit
            .apply_custom(sqrt_x.clone(), &[0])
            .apply_custom(sqrt_x.clone(), &[0]);
        circuit
    };
    println!("Horizontal:\n{}", HorizontalRenderer::new(&sqrt_x_circuit));
    println!("Vertical:\n{}", VerticalRenderer::new(&sqrt_x_circuit));

    results.push(benchmark_circuit("√X gate (1 qubit)", sqrt_x_builder));
    let mut sqrt_x_display = {
        let mut circuit = QuantumCircuit::new(1);
        circuit
            .apply_custom(sqrt_x.clone(), &[0])
            .apply_custom(sqrt_x.clone(), &[0]);
        circuit
    };
    sqrt_x_display.compute();
    println!("{}", sqrt_x_display);
    println!("(Two √X gates should equal X, so |0⟩ becomes |1⟩)\n");

    println!("┌─────────────────────────────────────────────────────────────┐");
    println!("│ Larger Circuits (for benchmark comparison)                  │");
    println!("└─────────────────────────────────────────────────────────────┘\n");

    let large_8_builder = || {
        let mut circuit = QuantumCircuit::new(8);
        for i in 0..8 {
            circuit.h(i);
        }
        for i in 0..7 {
            circuit.cnot(i, i + 1);
        }
        circuit
    };
    println!("8-qubit entangled:");
    println!("{}", HorizontalRenderer::new(&large_8_builder()));
    results.push(benchmark_circuit("8-qubit entangled", large_8_builder));

    let large_10_builder = || {
        let mut circuit = QuantumCircuit::new(10);
        for i in 0..10 {
            circuit.h(i);
        }
        for i in 0..9 {
            circuit.cnot(i, i + 1);
        }
        circuit.cz(0, 9);
        circuit
    };
    println!("10-qubit entangled:");
    println!("{}", HorizontalRenderer::new(&large_10_builder()));
    results.push(benchmark_circuit("10-qubit entangled", large_10_builder));

    let large_12_builder = || {
        let mut circuit = QuantumCircuit::new(12);
        for i in 0..12 {
            circuit.h(i);
        }
        for i in 0..11 {
            circuit.cnot(i, i + 1);
        }
        circuit.cz(0, 11);
        circuit.swap(5, 6);
        circuit
    };
    println!("12-qubit entangled:");
    println!("{}", HorizontalRenderer::new(&large_12_builder()));
    results.push(benchmark_circuit("12-qubit entangled", large_12_builder));

    let large_14_builder = || {
        let mut circuit = QuantumCircuit::new(14);
        for i in 0..14 {
            circuit.h(i);
        }
        for i in 0..13 {
            circuit.cnot(i, i + 1);
        }
        circuit
    };
    println!("14-qubit entangled:");
    println!("{}", HorizontalRenderer::new(&large_14_builder()));
    results.push(benchmark_circuit("14-qubit entangled", large_14_builder));

    print_benchmark_table(&results);

    let all_match = results.iter().all(|r| r.results_match);
    println!("\n");
    if all_match {
        println!("✓ All circuits produced identical results with both runtimes!");
    } else {
        println!("✗ WARNING: Some circuits produced different results!");
    }

    let total_basic: Duration = results.iter().map(|r| r.basic_time).sum();
    let total_mt: Duration = results.iter().map(|r| r.mt_time).sum();
    let overall_speedup = total_basic.as_secs_f64() / total_mt.as_secs_f64();

    println!(
        "\nTotal time - BasicRT: {} | BasicRTMT: {} | Overall speedup: {:.2}x",
        format_duration(total_basic),
        format_duration(total_mt),
        overall_speedup
    );
}