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. start = time.time()
  28. for level in range(depth):
  29. for i in range(n):
  30. qc.h(q[i])
  31. qc.t(q[i])
  32. if i != 0:
  33. qc.cx(q[i], q[0])
  34. job_sim = execute(qc, "local_statevector_simulator")
  35. job_sim.result()
  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. for q in qbits:
  53. ops.Measure | q
  54. return time.time() - start
  55. # Reporting
  56. functions = bench_qcgpu, bench_qiskit#, bench_projectq
  57. # functions = bench_qiskit,
  58. times = {f.__name__: [] for f in functions}
  59. names = []
  60. means = []
  61. samples = 100
  62. for i in range(samples): # adjust accordingly so whole thing takes a few sec
  63. progress = i / samples
  64. print("\rProgress: [{0:50s}] {1:.1f}%".format('#' * int(progress * 50), progress*100), end="", flush=True)
  65. func = random.choice(functions)
  66. t = func(20,10)
  67. times[func.__name__].append(t)
  68. print('')
  69. for name, numbers in times.items():
  70. print('FUNCTION:', name, 'Used', len(numbers), 'times')
  71. print('\tMEDIAN', statistics.median(numbers))
  72. print('\tMEAN ', statistics.mean(numbers))
  73. print('\tSTDEV ', statistics.stdev(numbers))
  74. means.append(statistics.mean(numbers))
  75. names.append(name)
  76. # Graphing
  77. import numpy as np
  78. import matplotlib.pyplot as plt
  79. index = np.arange(len(names))
  80. print(index)
  81. plt.bar(index, means)
  82. plt.xlabel('Function')
  83. plt.ylabel('Time (s)')
  84. plt.xticks(index, names)
  85. plt.title('Performance')
  86. plt.show()