benchmarking.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Reporting Imports
  2. import time
  3. import random
  4. import statistics
  5. # QCGPU Implementation
  6. import qcgpu
  7. def bench_qcgpu(n, depth):
  8. h = qcgpu.gate.h()
  9. x = qcgpu.gate.x()
  10. t = qcgpu.gate.t()
  11. state = qcgpu.State(n)
  12. start = time.time()
  13. for level in range(depth):
  14. for q in range(n):
  15. state.apply_gate(h, q)
  16. state.apply_gate(t, q)
  17. if q != 0:
  18. state.apply_controlled_gate(x, q, 0)
  19. return time.time() - start
  20. # Qiskit Implementation
  21. from qiskit import ClassicalRegister, QuantumRegister
  22. from qiskit import QuantumCircuit, execute
  23. def bench_qiskit(n, depth):
  24. q = QuantumRegister(n)
  25. c = ClassicalRegister(n)
  26. qc = QuantumCircuit(q, c)
  27. for level in range(depth):
  28. for i in range(n):
  29. qc.h(q[i])
  30. qc.t(q[i])
  31. if i != 0:
  32. qc.cx(q[i], q[0])
  33. qc.measure(q, c)
  34. start = time.time()
  35. job_sim = execute(qc, "local_statevector_simulator")
  36. return time.time() - start
  37. # ProjectQ Implementation
  38. from projectq import MainEngine
  39. import projectq.ops as ops
  40. from projectq.backends import Simulator
  41. from projectq.types import Qureg
  42. def bench_projectq(n, depth):
  43. eng = MainEngine(backend=Simulator(gate_fusion=True), engine_list=[])
  44. qbits = eng.allocate_qureg(n)
  45. start = time.time()
  46. for level in range(depth):
  47. for q in qbits:
  48. ops.H | q
  49. ops.T | q
  50. if q != qbits[0]:
  51. ops.CNOT | (q, qbits[0])
  52. runtime = time.time() - start
  53. for q in qbits:
  54. ops.Measure | q
  55. eng.flush()
  56. return runtime
  57. # Reporting
  58. functions = bench_qcgpu, bench_qiskit, bench_projectq
  59. times = {f.__name__: [] for f in functions}
  60. names = []
  61. means = []
  62. samples = 10
  63. for i in range(samples): # adjust accordingly so whole thing takes a few sec
  64. progress = i / samples
  65. print("\rProgress: [{0:50s}] {1:.1f}%".format('#' * int(progress * 50), progress*100), end="", flush=True)
  66. func = random.choice(functions)
  67. t = func(5,1)
  68. times[func.__name__].append(t)
  69. print('')
  70. for name, numbers in times.items():
  71. print('FUNCTION:', name, 'Used', len(numbers), 'times')
  72. print('\tMEDIAN', statistics.median(numbers))
  73. print('\tMEAN ', statistics.mean(numbers))
  74. print('\tSTDEV ', statistics.stdev(numbers))
  75. means.append(statistics.mean(numbers))
  76. names.append(name)
  77. # Graphing
  78. import numpy as np
  79. import matplotlib.pyplot as plt
  80. index = np.arange(len(names))
  81. print(index)
  82. plt.bar(index, means)
  83. plt.xlabel('Function')
  84. plt.ylabel('Time (s)')
  85. plt.xticks(index, names)
  86. plt.title('Performance')
  87. plt.show()