benchmark.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # This script is to run the benchmarks.
  2. import numpy as np
  3. import sys
  4. import csv
  5. import time
  6. import qcgpu
  7. import os.path
  8. results_file = 'benchmark_results.csv'
  9. def bench_qcgpu(n, depth):
  10. state = qcgpu.State(n)
  11. h = qcgpu.gate.h()
  12. x = qcgpu.gate.x()
  13. sqrt_x = qcgpu.gate.sqrt_x()
  14. start = time.time()
  15. for level in range(depth):
  16. for q in range(n):
  17. state.apply_gate(h, q)
  18. state.apply_gate(sqrt_x, q)
  19. if q != 0:
  20. state.apply_controlled_gate(x, q, 0)
  21. runtime = time.time() - start
  22. return {'name': 'qcgpu', 'num_qubits': n, 'depth': depth, 'time': runtime}
  23. def create_csv(filename):
  24. file_exists = os.path.isfile(filename)
  25. csvfile = open(filename, 'a')
  26. headers = ['name', 'num_qubits', 'depth', 'time']
  27. writer = csv.DictWriter(csvfile, delimiter=',', lineterminator='\n',fieldnames=headers)
  28. if not file_exists:
  29. writer.writeheader() # file doesn't exist yet, write a header
  30. # writer.writerow({'TimeStamp': dic['ts'], 'light': dic['light'], 'Proximity': dic['prox']}
  31. return writer
  32. def write_csv(writer, data):
  33. print("Qubits: " + str(data['num_qubits']) + ", Depth: " + str(data['depth']) + ", Time: " + str(data['depth']))
  34. writer.writerow(data)
  35. writer = create_csv(results_file)
  36. for i in range(2, 25):
  37. for d in [5,10,15,20]:
  38. data = bench_qcgpu(i, d)
  39. write_csv(writer, data)