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
|
use crate::common::{print_section, states_equal, BenchmarkResult, CircuitCases};
use psi::{QuantumCircuit, Runtime, RuntimeConfig};
use std::f64::consts::PI;
use std::time::Instant;
pub fn run_all(results: &mut Vec<BenchmarkResult>) {
println!("═══════════════════════════════════════════════════════════════");
println!(" KERNEL BATCHING TESTS");
println!("═══════════════════════════════════════════════════════════════\n");
test_kernel_fusion(results);
test_batched_vs_basic(results);
test_batched_large_circuits(results);
test_structure_aware(results);
test_composable_runtime(results);
}
pub fn test_kernel_fusion(results: &mut Vec<BenchmarkResult>) {
print_section("Kernel Fusion Test");
let builder = || {
let mut circuit = QuantumCircuit::new(2);
circuit.h(0).t(0).s(0).x(0).h(1).z(1);
circuit
};
let circuit = builder();
let batch = Runtime::build_kernel_batch(2, circuit.operations());
let original_count = batch.len();
println!("Original kernels: {}", original_count);
for (i, k) in batch.kernels().iter().enumerate() {
println!(" {}: {} on {:?}", i, k.name, k.targets);
}
let mut optimized_batch = Runtime::build_kernel_batch(2, circuit.operations());
optimized_batch.optimize();
let optimized_count = optimized_batch.len();
println!("\nOptimized kernels: {}", optimized_count);
for (i, k) in optimized_batch.kernels().iter().enumerate() {
println!(" {}: {} on {:?}", i, k.name, k.targets);
}
let reduction = ((original_count - optimized_count) as f64 / original_count as f64) * 100.0;
println!(
"\nKernel reduction: {} → {} ({:.0}% fewer)",
original_count, optimized_count, reduction
);
let mut basic = builder();
let start = Instant::now();
basic.compute_with(Runtime::BasicRT);
let basic_time = start.elapsed();
let mut batched = builder();
let start = Instant::now();
batched.compute_with(Runtime::BatchedRT);
let batched_time = start.elapsed();
let match_result = states_equal(basic.state(), batched.state());
println!("Results match: {}\n", if match_result { "✓" } else { "✗" });
results.push(BenchmarkResult {
name: format!("Fusion ({}→{} kernels)", original_count, optimized_count),
basic_time,
mt_time: batched_time,
results_match: match_result,
});
let fusion_heavy = || {
let mut circuit = QuantumCircuit::new(1);
circuit.h(0).t(0).s(0).x(0).y(0).z(0).h(0).t(0);
circuit
};
let circuit2 = fusion_heavy();
let batch2 = Runtime::build_kernel_batch(1, circuit2.operations());
let orig2 = batch2.len();
let mut opt_batch2 = Runtime::build_kernel_batch(1, circuit2.operations());
opt_batch2.optimize();
let opt2 = opt_batch2.len();
let mut basic2 = fusion_heavy();
let start = Instant::now();
basic2.compute_with(Runtime::BasicRT);
let basic_time2 = start.elapsed();
let mut batched2 = fusion_heavy();
let start = Instant::now();
batched2.compute_with(Runtime::BatchedRT);
let batched_time2 = start.elapsed();
let match2 = states_equal(basic2.state(), batched2.state());
results.push(BenchmarkResult {
name: format!("Heavy fusion ({}→{} kernels)", orig2, opt2),
basic_time: basic_time2,
mt_time: batched_time2,
results_match: match2,
});
}
pub fn test_batched_vs_basic(results: &mut Vec<BenchmarkResult>) {
print_section("Batched vs Basic Runtime Comparison");
let test_cases: CircuitCases = vec![
(
"Bell State",
Box::new(|| {
let mut c = QuantumCircuit::new(2);
c.h(0).cnot(0, 1);
c
}),
),
(
"GHZ State",
Box::new(|| {
let mut c = QuantumCircuit::new(3);
c.h(0).cnot(0, 1).cnot(0, 2);
c
}),
),
(
"Rotation Chain",
Box::new(|| {
let mut c = QuantumCircuit::new(3);
c.rx(0, PI / 4.0)
.ry(0, PI / 4.0)
.rz(0, PI / 4.0)
.rx(1, PI / 3.0)
.ry(1, PI / 3.0);
c
}),
),
(
"Mixed Gates",
Box::new(|| {
let mut c = QuantumCircuit::new(4);
c.h(0).h(1).h(2).h(3).cnot(0, 1).cnot(2, 3).cz(1, 2);
c
}),
),
];
for (name, builder) in test_cases {
let mut basic = builder();
let start = Instant::now();
basic.compute_with(Runtime::BasicRT);
let basic_time = start.elapsed();
let mut batched = builder();
let start = Instant::now();
batched.compute_with(Runtime::BatchedRT);
let batched_time = start.elapsed();
let match_result = states_equal(basic.state(), batched.state());
println!(
"{}: Basic={:.2}μs, Batched={:.2}μs, Match={}",
name,
basic_time.as_secs_f64() * 1_000_000.0,
batched_time.as_secs_f64() * 1_000_000.0,
if match_result { "✓" } else { "✗" }
);
results.push(BenchmarkResult {
name: format!("Batched: {}", name),
basic_time,
mt_time: batched_time,
results_match: match_result,
});
}
println!();
}
pub fn test_batched_large_circuits(results: &mut Vec<BenchmarkResult>) {
print_section("Batched Runtime on Large Circuits");
let sizes = [8, 10, 12];
for &n in &sizes {
let builder = || {
let mut circuit = QuantumCircuit::new(n);
for i in 0..n {
circuit.h(i);
}
for i in 0..(n - 1) {
circuit.cnot(i, i + 1);
}
circuit
};
let mut basic_mt = builder();
let start = Instant::now();
basic_mt.compute_with(Runtime::BasicRTMT);
let basic_mt_time = start.elapsed();
let mut batched_mt = builder();
let start = Instant::now();
batched_mt.compute_with(Runtime::BatchedRTMT);
let batched_mt_time = start.elapsed();
let match_result = states_equal(basic_mt.state(), batched_mt.state());
println!(
"{}-qubit: BasicRTMT={:.3}ms, BatchedRTMT={:.3}ms, Match={}",
n,
basic_mt_time.as_secs_f64() * 1000.0,
batched_mt_time.as_secs_f64() * 1000.0,
if match_result { "✓" } else { "✗" }
);
results.push(BenchmarkResult {
name: format!("{}-qubit batched", n),
basic_time: basic_mt_time,
mt_time: batched_mt_time,
results_match: match_result,
});
}
println!();
}
pub fn test_structure_aware(results: &mut Vec<BenchmarkResult>) {
print_section("Structure-Aware Kernel Optimisation");
let commute_test = || {
let mut c = QuantumCircuit::new(3);
c.t(0).h(1).t(0).h(2).s(0).t(1).rz(0, PI / 4.0);
c
};
let circuit = commute_test();
let mut batch = Runtime::build_structure_aware_batch(3, circuit.operations());
let original = batch.len();
println!("Original operations: {}", original);
for (i, k) in batch.kernels().iter().enumerate() {
println!(" {}: {} on {:?} ({:?})", i, k.name, k.targets, k.gate_type);
}
batch.optimise();
let optimised = batch.len();
println!("\nAfter optimisation: {}", optimised);
for (i, k) in batch.kernels().iter().enumerate() {
println!(" {}: {} on {:?}", i, k.name, k.targets);
}
println!("\nExecution layers: {}", batch.num_layers());
for (i, layer) in batch.layers().iter().enumerate() {
let names: Vec<_> = layer.kernels.iter().map(|k| k.name.as_str()).collect();
println!(" Layer {}: {:?}", i, names);
}
let stats = batch.stats();
println!("\nStats: {}", stats);
let mut basic = commute_test();
let start = Instant::now();
basic.compute_with(Runtime::BasicRT);
let basic_time = start.elapsed();
let mut sa = commute_test();
let start = Instant::now();
sa.compute_with(Runtime::StructureAwareRT);
let sa_time = start.elapsed();
let match_result = states_equal(basic.state(), sa.state());
println!(
"\nBasic={:.2}μs, StructureAware={:.2}μs, Match={}",
basic_time.as_secs_f64() * 1_000_000.0,
sa_time.as_secs_f64() * 1_000_000.0,
if match_result { "✓" } else { "✗" }
);
results.push(BenchmarkResult {
name: format!("SA: Commuting ({}→{})", original, optimised),
basic_time,
mt_time: sa_time,
results_match: match_result,
});
println!();
print_section("Structure-Aware vs Other Runtimes");
let test_cases: CircuitCases = vec![
(
"Diagonal-heavy (5q)",
Box::new(|| {
let mut c = QuantumCircuit::new(5);
for q in 0..5 {
c.t(q).s(q).rz(q, PI / 4.0).t(q);
}
c
}),
),
(
"Interleaved (4q)",
Box::new(|| {
let mut c = QuantumCircuit::new(4);
c.h(0).h(1).h(2).h(3);
c.t(0).t(1).t(2).t(3);
c.cnot(0, 1).cnot(2, 3);
c.s(0).s(1).s(2).s(3);
c
}),
),
(
"Deep rotation (3q)",
Box::new(|| {
let mut c = QuantumCircuit::new(3);
for _ in 0..5 {
for q in 0..3 {
c.rx(q, PI / 8.0).ry(q, PI / 8.0).rz(q, PI / 8.0);
}
}
c
}),
),
];
for (name, builder) in test_cases {
let mut batched = builder();
let start = Instant::now();
batched.compute_with(Runtime::BatchedRT);
let batched_time = start.elapsed();
let mut sa = builder();
let start = Instant::now();
sa.compute_with(Runtime::StructureAwareRT);
let sa_time = start.elapsed();
let match_result = states_equal(batched.state(), sa.state());
let speedup = batched_time.as_secs_f64() / sa_time.as_secs_f64();
println!(
"{}: Batched={:.2}μs, SA={:.2}μs, Speedup={:.2}x, Match={}",
name,
batched_time.as_secs_f64() * 1_000_000.0,
sa_time.as_secs_f64() * 1_000_000.0,
speedup,
if match_result { "✓" } else { "✗" }
);
results.push(BenchmarkResult {
name: format!("SA: {}", name),
basic_time: batched_time,
mt_time: sa_time,
results_match: match_result,
});
}
println!();
}
pub fn test_composable_runtime(results: &mut Vec<BenchmarkResult>) {
print_section("Composable Runtime Configurations");
let builder = || {
let mut c = QuantumCircuit::new(6);
for q in 0..6 {
c.h(q).t(q).s(q);
}
for q in 0..5 {
c.cnot(q, q + 1);
}
for q in 0..6 {
c.rx(q, PI / 4.0).rz(q, PI / 4.0);
}
c
};
let configs: Vec<(&str, RuntimeConfig)> = vec![
("Basic", RuntimeConfig::new()),
("Batched", RuntimeConfig::new().batched()),
("SIMD", RuntimeConfig::new().simd()),
("Batched+SIMD", RuntimeConfig::new().batched().simd()),
("SA+SIMD", RuntimeConfig::new().structure_aware().simd()),
(
"SA+SIMD+Parallel",
RuntimeConfig::new().structure_aware().simd().parallel(),
),
("Optimal", Runtime::optimal()),
];
let mut reference = builder();
reference.compute_with(Runtime::BasicRT);
let ref_state = reference.state().clone();
println!("Testing 6-qubit circuit with different runtime configurations:\n");
for (name, config) in &configs {
let mut circuit = builder();
let start = Instant::now();
circuit.compute_with_config(*config);
let time = start.elapsed();
let match_result = states_equal(&ref_state, circuit.state());
println!(
"{:20} : {:.2}μs, Match={}",
name,
time.as_secs_f64() * 1_000_000.0,
if match_result { "✓" } else { "✗" }
);
results.push(BenchmarkResult {
name: format!("Config: {}", name),
basic_time: time,
mt_time: time,
results_match: match_result,
});
}
println!("\nConfiguration Display Examples:");
println!(" {}", RuntimeConfig::new());
println!(" {}", RuntimeConfig::new().batched().simd());
println!(
" {}",
RuntimeConfig::new().structure_aware().simd().parallel()
);
println!(" {}", Runtime::optimal());
println!();
}
|