File size: 15,141 Bytes
9425aed | 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 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | /// BOB Quantum Civilization Engine - Benchmarks
/// Latency tests for each language binding call overhead
/// Measures FFI marshalling, memory allocation, and quantum ops performance
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <math.h>
#include "../include/bob_quantum.h"
#define BENCHMARK_ITERATIONS 10000
#define BENCHMARK_WARMUP 100
typedef struct {
const char *name;
uint64_t total_ns;
uint64_t min_ns;
uint64_t max_ns;
uint64_t count;
} Benchmark_Result;
/// =========================================================================
/// Timing Utilities
/// =========================================================================
static uint64_t time_now_ns(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000000UL + (uint64_t)ts.tv_nsec;
}
static void print_result(const Benchmark_Result *result) {
if (result->count == 0) return;
double avg_us = (double)result->total_ns / result->count / 1000.0;
double min_us = (double)result->min_ns / 1000.0;
double max_us = (double)result->max_ns / 1000.0;
printf("%-40s | Avg: %8.3f ΞΌs | Min: %8.3f ΞΌs | Max: %8.3f ΞΌs\n",
result->name, avg_us, min_us, max_us);
}
/// =========================================================================
/// RNG Benchmarks
/// =========================================================================
void benchmark_rng_creation(void) {
printf("\n=== RNG Creation Overhead ===\n");
Benchmark_Result result = {.name = "rng_create", .min_ns = UINT64_MAX};
// Warmup
for (int i = 0; i < BENCHMARK_WARMUP; i++) {
bob_rng_handle_t *rng = NULL;
bob_rng_create(&rng);
bob_rng_destroy(rng);
}
// Benchmark
for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
uint64_t start = time_now_ns();
bob_rng_handle_t *rng = NULL;
bob_rng_create(&rng);
bob_rng_destroy(rng);
uint64_t elapsed = time_now_ns() - start;
result.total_ns += elapsed;
result.min_ns = elapsed < result.min_ns ? elapsed : result.min_ns;
result.max_ns = elapsed > result.max_ns ? elapsed : result.max_ns;
result.count++;
}
print_result(&result);
}
void benchmark_rng_uniform(void) {
printf("\n=== RNG Uniform Generation ===\n");
bob_rng_handle_t *rng = NULL;
bob_rng_create(&rng);
bob_rng_seed(rng, 12345);
Benchmark_Result result = {.name = "rng_uniform", .min_ns = UINT64_MAX};
// Warmup
for (int i = 0; i < BENCHMARK_WARMUP; i++) {
double val;
bob_rng_uniform(rng, &val);
}
// Benchmark
for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
uint64_t start = time_now_ns();
double val;
bob_rng_uniform(rng, &val);
uint64_t elapsed = time_now_ns() - start;
result.total_ns += elapsed;
result.min_ns = elapsed < result.min_ns ? elapsed : result.min_ns;
result.max_ns = elapsed > result.max_ns ? elapsed : result.max_ns;
result.count++;
}
print_result(&result);
bob_rng_destroy(rng);
}
void benchmark_rng_normal(void) {
printf("\n=== RNG Normal Distribution ===\n");
bob_rng_handle_t *rng = NULL;
bob_rng_create(&rng);
bob_rng_seed(rng, 12345);
Benchmark_Result result = {.name = "rng_normal", .min_ns = UINT64_MAX};
// Warmup
for (int i = 0; i < BENCHMARK_WARMUP; i++) {
double val;
bob_rng_normal(rng, &val);
}
// Benchmark
for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
uint64_t start = time_now_ns();
double val;
bob_rng_normal(rng, &val);
uint64_t elapsed = time_now_ns() - start;
result.total_ns += elapsed;
result.min_ns = elapsed < result.min_ns ? elapsed : result.min_ns;
result.max_ns = elapsed > result.max_ns ? elapsed : result.max_ns;
result.count++;
}
print_result(&result);
bob_rng_destroy(rng);
}
void benchmark_rng_integer(void) {
printf("\n=== RNG Integer Generation ===\n");
bob_rng_handle_t *rng = NULL;
bob_rng_create(&rng);
bob_rng_seed(rng, 12345);
Benchmark_Result result = {.name = "rng_integer", .min_ns = UINT64_MAX};
// Warmup
for (int i = 0; i < BENCHMARK_WARMUP; i++) {
int64_t val;
bob_rng_integer(rng, 0, 100, &val);
}
// Benchmark
for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
uint64_t start = time_now_ns();
int64_t val;
bob_rng_integer(rng, 0, 100, &val);
uint64_t elapsed = time_now_ns() - start;
result.total_ns += elapsed;
result.min_ns = elapsed < result.min_ns ? elapsed : result.min_ns;
result.max_ns = elapsed > result.max_ns ? elapsed : result.max_ns;
result.count++;
}
print_result(&result);
bob_rng_destroy(rng);
}
/// =========================================================================
/// Lattice Benchmarks
/// =========================================================================
void benchmark_lattice_creation(void) {
printf("\n=== Lattice Creation (4x4x4) ===\n");
Benchmark_Result result = {.name = "lattice_create", .min_ns = UINT64_MAX};
// Warmup
for (int i = 0; i < BENCHMARK_WARMUP; i++) {
bob_lattice_handle_t *lat = NULL;
bob_lattice_create(4, 4, 4, 1.0, 12345, &lat);
bob_lattice_destroy(lat);
}
// Benchmark
for (int i = 0; i < BENCHMARK_ITERATIONS / 100; i++) {
uint64_t start = time_now_ns();
bob_lattice_handle_t *lat = NULL;
bob_lattice_create(4, 4, 4, 1.0, 12345, &lat);
bob_lattice_destroy(lat);
uint64_t elapsed = time_now_ns() - start;
result.total_ns += elapsed;
result.min_ns = elapsed < result.min_ns ? elapsed : result.min_ns;
result.max_ns = elapsed > result.max_ns ? elapsed : result.max_ns;
result.count++;
}
print_result(&result);
}
void benchmark_lattice_evolve(void) {
printf("\n=== Lattice Evolution (1 MC step) ===\n");
bob_lattice_handle_t *lat = NULL;
bob_lattice_create(4, 4, 4, 1.0, 12345, &lat);
Benchmark_Result result = {.name = "lattice_evolve", .min_ns = UINT64_MAX};
double energy;
// Warmup
for (int i = 0; i < BENCHMARK_WARMUP; i++) {
bob_lattice_evolve(lat, 1, &energy);
}
// Benchmark
for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
uint64_t start = time_now_ns();
bob_lattice_evolve(lat, 1, &energy);
uint64_t elapsed = time_now_ns() - start;
result.total_ns += elapsed;
result.min_ns = elapsed < result.min_ns ? elapsed : result.min_ns;
result.max_ns = elapsed > result.max_ns ? elapsed : result.max_ns;
result.count++;
}
print_result(&result);
bob_lattice_destroy(lat);
}
void benchmark_lattice_energy(void) {
printf("\n=== Lattice Energy Computation ===\n");
bob_lattice_handle_t *lat = NULL;
bob_lattice_create(4, 4, 4, 1.0, 12345, &lat);
Benchmark_Result result = {.name = "lattice_energy", .min_ns = UINT64_MAX};
double energy;
// Warmup
for (int i = 0; i < BENCHMARK_WARMUP; i++) {
bob_lattice_energy(lat, &energy);
}
// Benchmark
for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
uint64_t start = time_now_ns();
bob_lattice_energy(lat, &energy);
uint64_t elapsed = time_now_ns() - start;
result.total_ns += elapsed;
result.min_ns = elapsed < result.min_ns ? elapsed : result.min_ns;
result.max_ns = elapsed > result.max_ns ? elapsed : result.max_ns;
result.count++;
}
print_result(&result);
bob_lattice_destroy(lat);
}
void benchmark_lattice_entropy(void) {
printf("\n=== Lattice Entropy Computation ===\n");
bob_lattice_handle_t *lat = NULL;
bob_lattice_create(4, 4, 4, 1.0, 12345, &lat);
Benchmark_Result result = {.name = "lattice_entropy", .min_ns = UINT64_MAX};
double entropy;
// Warmup
for (int i = 0; i < BENCHMARK_WARMUP; i++) {
bob_lattice_entropy(lat, &entropy);
}
// Benchmark
for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
uint64_t start = time_now_ns();
bob_lattice_entropy(lat, &entropy);
uint64_t elapsed = time_now_ns() - start;
result.total_ns += elapsed;
result.min_ns = elapsed < result.min_ns ? elapsed : result.min_ns;
result.max_ns = elapsed > result.max_ns ? elapsed : result.max_ns;
result.count++;
}
print_result(&result);
bob_lattice_destroy(lat);
}
/// =========================================================================
/// State Benchmarks
/// =========================================================================
void benchmark_state_creation(void) {
printf("\n=== Quantum State Creation (4 qubits) ===\n");
Benchmark_Result result = {.name = "state_create", .min_ns = UINT64_MAX};
// Warmup
for (int i = 0; i < BENCHMARK_WARMUP; i++) {
bob_state_handle_t *state = NULL;
bob_state_create(4, 0, &state);
bob_state_destroy(state);
}
// Benchmark
for (int i = 0; i < BENCHMARK_ITERATIONS / 100; i++) {
uint64_t start = time_now_ns();
bob_state_handle_t *state = NULL;
bob_state_create(4, 0, &state);
bob_state_destroy(state);
uint64_t elapsed = time_now_ns() - start;
result.total_ns += elapsed;
result.min_ns = elapsed < result.min_ns ? elapsed : result.min_ns;
result.max_ns = elapsed > result.max_ns ? elapsed : result.max_ns;
result.count++;
}
print_result(&result);
}
void benchmark_state_measure(void) {
printf("\n=== Quantum State Measurement ===\n");
bob_state_handle_t *state = NULL;
bob_state_create(4, 0, &state);
Benchmark_Result result = {.name = "state_measure", .min_ns = UINT64_MAX};
int outcome;
double prob;
// Warmup
for (int i = 0; i < BENCHMARK_WARMUP; i++) {
bob_state_measure(state, 0, &outcome, &prob);
}
// Benchmark
for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
uint64_t start = time_now_ns();
bob_state_measure(state, 0, &outcome, &prob);
uint64_t elapsed = time_now_ns() - start;
result.total_ns += elapsed;
result.min_ns = elapsed < result.min_ns ? elapsed : result.min_ns;
result.max_ns = elapsed > result.max_ns ? elapsed : result.max_ns;
result.count++;
}
print_result(&result);
bob_state_destroy(state);
}
void benchmark_state_apply_gate(void) {
printf("\n=== Quantum Gate Application ===\n");
bob_state_handle_t *state = NULL;
bob_state_create(4, 0, &state);
Benchmark_Result result = {.name = "state_apply_gate", .min_ns = UINT64_MAX};
// Warmup
for (int i = 0; i < BENCHMARK_WARMUP; i++) {
bob_state_apply_gate(state, 0, 0, NULL, 0);
}
// Benchmark
for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
uint64_t start = time_now_ns();
bob_state_apply_gate(state, 0, 0, NULL, 0);
uint64_t elapsed = time_now_ns() - start;
result.total_ns += elapsed;
result.min_ns = elapsed < result.min_ns ? elapsed : result.min_ns;
result.max_ns = elapsed > result.max_ns ? elapsed : result.max_ns;
result.count++;
}
print_result(&result);
bob_state_destroy(state);
}
/// =========================================================================
/// Hamiltonian Benchmarks
/// =========================================================================
void benchmark_hamiltonian_creation(void) {
printf("\n=== Hamiltonian Creation (4 qubits) ===\n");
Benchmark_Result result = {.name = "hamiltonian_create", .min_ns = UINT64_MAX};
// Warmup
for (int i = 0; i < BENCHMARK_WARMUP; i++) {
bob_hamiltonian_handle_t *ham = NULL;
bob_hamiltonian_create(4, 0, &ham);
bob_hamiltonian_destroy(ham);
}
// Benchmark
for (int i = 0; i < BENCHMARK_ITERATIONS / 100; i++) {
uint64_t start = time_now_ns();
bob_hamiltonian_handle_t *ham = NULL;
bob_hamiltonian_create(4, 0, &ham);
bob_hamiltonian_destroy(ham);
uint64_t elapsed = time_now_ns() - start;
result.total_ns += elapsed;
result.min_ns = elapsed < result.min_ns ? elapsed : result.min_ns;
result.max_ns = elapsed > result.max_ns ? elapsed : result.max_ns;
result.count++;
}
print_result(&result);
}
void benchmark_hamiltonian_add_term(void) {
printf("\n=== Hamiltonian Add Term ===\n");
bob_hamiltonian_handle_t *ham = NULL;
bob_hamiltonian_create(4, 0, &ham);
int qubits[] = {0};
Benchmark_Result result = {.name = "hamiltonian_add_term", .min_ns = UINT64_MAX};
// Warmup
for (int i = 0; i < BENCHMARK_WARMUP; i++) {
bob_hamiltonian_add_term(ham, 1.0, 0.0, qubits, 1);
}
// Benchmark
for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
uint64_t start = time_now_ns();
bob_hamiltonian_add_term(ham, 1.0, 0.0, qubits, 1);
uint64_t elapsed = time_now_ns() - start;
result.total_ns += elapsed;
result.min_ns = elapsed < result.min_ns ? elapsed : result.min_ns;
result.max_ns = elapsed > result.max_ns ? elapsed : result.max_ns;
result.count++;
}
print_result(&result);
bob_hamiltonian_destroy(ham);
}
/// =========================================================================
/// Main Benchmark Suite
/// =========================================================================
int main(void) {
printf("\n");
printf("ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n");
printf("β BOB Quantum Civilization Engine - Performance Benchmarks β\n");
printf("β Language FFI Binding Latency Analysis β\n");
printf("ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n");
// RNG benchmarks
printf("\n>>> RNG Subsystem <<<\n");
benchmark_rng_creation();
benchmark_rng_uniform();
benchmark_rng_normal();
benchmark_rng_integer();
// Lattice benchmarks
printf("\n>>> Lattice Subsystem <<<\n");
benchmark_lattice_creation();
benchmark_lattice_evolve();
benchmark_lattice_energy();
benchmark_lattice_entropy();
// State benchmarks
printf("\n>>> Quantum State Subsystem <<<\n");
benchmark_state_creation();
benchmark_state_measure();
benchmark_state_apply_gate();
// Hamiltonian benchmarks
printf("\n>>> Hamiltonian Subsystem <<<\n");
benchmark_hamiltonian_creation();
benchmark_hamiltonian_add_term();
printf("\n=== Benchmark Complete ===\n\n");
return 0;
}
|