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
|
use crate::common::{print_section, states_equal, BenchmarkResult, CircuitCases};
use psi::{get_simd_info, QuantumCircuit, Runtime};
use std::f64::consts::PI;
use std::time::Instant;
pub fn run_all(results: &mut Vec<BenchmarkResult>) {
println!("═══════════════════════════════════════════════════════════════");
println!(" SIMD ACCELERATION TESTS");
println!("═══════════════════════════════════════════════════════════════\n");
println!("Detected: {}\n", get_simd_info());
test_simd_correctness(results);
test_simd_vs_batched(results);
test_simd_large_circuits(results);
}
pub fn test_simd_correctness(results: &mut Vec<BenchmarkResult>) {
print_section("SIMD Correctness Verification");
let test_cases: CircuitCases = vec![
(
"Bell State",
Box::new(|| {
let mut c = QuantumCircuit::new(2);
c.h(0).cnot(0, 1);
c
}),
),
(
"GHZ-3",
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 Single-Qubit",
Box::new(|| {
let mut c = QuantumCircuit::new(4);
c.h(0).t(0).s(0).x(0).h(1).y(1).z(1).h(2).t(2).h(3).s(3);
c
}),
),
];
for (name, builder) in test_cases {
let mut basic = builder();
basic.compute_with(Runtime::BasicRT);
let mut simd = builder();
simd.compute_with(Runtime::SimdRT);
let match_result = states_equal(basic.state(), simd.state());
println!(
"{}: {}",
name,
if match_result {
"✓ Match"
} else {
"✗ MISMATCH"
}
);
results.push(BenchmarkResult {
name: format!("SIMD verify: {}", name),
basic_time: std::time::Duration::from_micros(0),
mt_time: std::time::Duration::from_micros(0),
results_match: match_result,
});
}
println!();
}
pub fn test_simd_vs_batched(results: &mut Vec<BenchmarkResult>) {
print_section("SIMD vs Batched Runtime Comparison");
let test_cases: CircuitCases = vec![
(
"Single-Qubit Heavy (6q)",
Box::new(|| {
let mut c = QuantumCircuit::new(6);
for q in 0..6 {
c.h(q).t(q).s(q).x(q).y(q).z(q);
}
c
}),
),
(
"Rotation Circuit (5q)",
Box::new(|| {
let mut c = QuantumCircuit::new(5);
for q in 0..5 {
c.rx(q, PI / 4.0).ry(q, PI / 3.0).rz(q, PI / 6.0);
}
c
}),
),
(
"Deep Single-Qubit (4q)",
Box::new(|| {
let mut c = QuantumCircuit::new(4);
for _ in 0..10 {
for q in 0..4 {
c.h(q).t(q);
}
}
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 simd = builder();
let start = Instant::now();
simd.compute_with(Runtime::SimdRT);
let simd_time = start.elapsed();
let match_result = states_equal(batched.state(), simd.state());
let speedup = batched_time.as_secs_f64() / simd_time.as_secs_f64();
println!(
"{}: Batched={:.2}μs, SIMD={:.2}μs, Speedup={:.2}x, Match={}",
name,
batched_time.as_secs_f64() * 1_000_000.0,
simd_time.as_secs_f64() * 1_000_000.0,
speedup,
if match_result { "✓" } else { "✗" }
);
results.push(BenchmarkResult {
name: format!("SIMD: {}", name),
basic_time: batched_time,
mt_time: simd_time,
results_match: match_result,
});
}
println!();
}
pub fn test_simd_large_circuits(results: &mut Vec<BenchmarkResult>) {
print_section("SIMD on Large Circuits (Multi-threaded)");
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);
}
for i in 0..n {
circuit.t(i).s(i);
}
circuit
};
let mut batched_mt = builder();
let start = Instant::now();
batched_mt.compute_with(Runtime::BatchedRTMT);
let batched_time = start.elapsed();
let mut simd_mt = builder();
let start = Instant::now();
simd_mt.compute_with(Runtime::SimdRTMT);
let simd_time = start.elapsed();
let match_result = states_equal(batched_mt.state(), simd_mt.state());
let speedup = batched_time.as_secs_f64() / simd_time.as_secs_f64();
println!(
"{}-qubit: BatchedMT={:.3}ms, SIMD_MT={:.3}ms, Speedup={:.2}x, Match={}",
n,
batched_time.as_secs_f64() * 1000.0,
simd_time.as_secs_f64() * 1000.0,
speedup,
if match_result { "✓" } else { "✗" }
);
results.push(BenchmarkResult {
name: format!("{}-qubit SIMD", n),
basic_time: batched_time,
mt_time: simd_time,
results_match: match_result,
});
}
println!();
}
|