Przeglądaj źródła

Adding more benchmarking tools

Adam Kelly 6 lat temu
rodzic
commit
aee970f646
5 zmienionych plików z 166 dodań i 34 usunięć
  1. 97 34
      notebooks/Testing.ipynb
  2. 24 0
      notebooks/ipython_cell_input.py
  3. 40 0
      other_testing.py
  4. 2 0
      qcgpu/backend.py
  5. 3 0
      qcgpu/state.py

+ 97 - 34
notebooks/Testing.ipynb

@@ -2,16 +2,22 @@
  "cells": [
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": 32,
    "metadata": {},
    "outputs": [],
    "source": [
+    "%load_ext line_profiler\n",
     "%matplotlib inline\n",
     "\n",
     "import matplotlib.pyplot as plt\n",
     "from IPython.display import set_matplotlib_formats\n",
     "import numpy as np\n",
     "import qcgpu\n",
+    "from projectq import MainEngine\n",
+    "import projectq.ops as ops\n",
+    "from projectq.backends import Simulator\n",
+    "import sys\n",
+    "import time\n",
     "\n",
     "set_matplotlib_formats('png', 'pdf')\n",
     "plt.style.use('ggplot')"
@@ -19,11 +25,11 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
+   "execution_count": 27,
    "metadata": {},
    "outputs": [],
    "source": [
-    "def plot_state(state):\n",
+    "def plot_qcgpu_state(state):\n",
     "    labels = map(\n",
     "        lambda i: np.binary_repr(i, state.num_qubits), \n",
     "        range(0, 2**state.num_qubits)\n",
@@ -42,49 +48,106 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 9,
+   "execution_count": 28,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def bench_qcgpu(n, depth):\n",
+    "    state = qcgpu.State(n)\n",
+    "\n",
+    "    h = qcgpu.gate.h()\n",
+    "    x = qcgpu.gate.x()\n",
+    "    sqrt_x = qcgpu.gate.sqrt_x()\n",
+    "\n",
+    "    print('started')\n",
+    "    start = time.time()\n",
+    "\n",
+    "    for level in range(depth):\n",
+    "        for q in range(n):\n",
+    "    \n",
+    "            state.apply_gate(h, q)\n",
+    "            state.apply_gate(sqrt_x, q)\n",
+    "\n",
+    "            if q != 0:\n",
+    "                state.apply_controlled_gate(x, q, 0)\n",
+    "        \n",
+    "    runtime = time.time() - start\n",
+    "    print('ended: ', runtime)\n",
+    "    return runtime\n",
+    "    \n",
+    "def bench_projectq(n, depth):\n",
+    "    eng = MainEngine(backend=Simulator(gate_fusion=True), engine_list=[])\n",
+    "    qbits = eng.allocate_qureg(n)\n",
+    "\n",
+    "    print('started')\n",
+    "    start = time.time()\n",
+    "\n",
+    "    for level in range(depth):\n",
+    "        for q in qbits:\n",
+    "            ops.H | q\n",
+    "            ops.SqrtX | q\n",
+    "            if q != qbits[0]:\n",
+    "                ops.CNOT | (q, qbits[0])\n",
+    "\n",
+    "    runtime = time.time() - start\n",
+    "    print('ended: ', runtime)\n",
+    "\n",
+    "    for q in qbits:\n",
+    "        ops.Measure | q\n",
+    "    eng.flush()\n",
+    "    return runtime\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 29,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# h = qcgpu.gate.h()\n",
+    "# x = qcgpu.gate.x()\n",
+    "# s = qcgpu.gate.s()\n",
+    "# t = qcgpu.gate.t()\n",
+    "# y = qcgpu.gate.y()\n",
+    "# z = qcgpu.gate.z()\n",
+    "\n",
+    "# # Random Circuit\n",
+    "# state = qcgpu.State(5)\n",
+    "\n",
+    "# state.apply_gate(h, 0)\n",
+    "# state.apply_gate(t, 0)\n",
+    "# state.apply_gate(y, 0)\n",
+    "# state.apply_gate(z, 0)\n",
+    "# state.apply_gate(h, 0)\n",
+    "\n",
+    "# state.num_qubits"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 35,
    "metadata": {},
    "outputs": [
     {
-     "data": {
-      "text/plain": [
-       "20"
-      ]
-     },
-     "execution_count": 9,
-     "metadata": {},
-     "output_type": "execute_result"
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "started\n",
+      "('ended: ', 0.2988259792327881)\n"
+     ]
     }
    ],
    "source": [
-    "h = qcgpu.gate.h()\n",
-    "x = qcgpu.gate.x()\n",
-    "s = qcgpu.gate.s()\n",
-    "t = qcgpu.gate.t()\n",
-    "y = qcgpu.gate.y()\n",
-    "z = qcgpu.gate.z()\n",
-    "\n",
-    "# Random Circuit\n",
-    "state = qcgpu.State(18)\n",
-    "\n",
-    "state.apply_gate(h, 0)\n",
-    "state.apply_gate(t, 0)\n",
-    "state.apply_gate(y, 0)\n",
-    "state.apply_gate(z, 0)\n",
-    "state.apply_gate(h, 0)\n",
-    "\n",
-    "state.num_qubits"
+    "%lprun -f bench_qcgpu bench_qcgpu(26,5)"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": null,
-   "metadata": {
-    "scrolled": true
-   },
+   "execution_count": 31,
+   "metadata": {},
    "outputs": [],
    "source": [
-    "plot_state(state)"
+    "# bench_projectq(26,5)"
    ]
   },
   {

+ 24 - 0
notebooks/ipython_cell_input.py

@@ -0,0 +1,24 @@
+def bench_qcgpu(n, depth):
+    state = qcgpu.State(n)
+
+    h = qcgpu.gate.h()
+    x = qcgpu.gate.x()
+    sqrt_x = qcgpu.gate.sqrt_x()
+
+    print('started')
+    start = time.time()
+
+    for level in range(depth):
+        for q in range(n):
+    
+            state.apply_gate(h, q)
+            state.apply_gate(sqrt_x, q)
+
+            if q != 0:
+                state.apply_controlled_gate(x, q, 0)
+        
+    runtime = time.time() - start
+    print('ended: ', runtime)
+    return runtime
+
+bench_qcgpu(26,5)

+ 40 - 0
other_testing.py

@@ -0,0 +1,40 @@
+from projectq import MainEngine
+import projectq.ops as ops
+from projectq.backends import Simulator
+import sys
+import time
+
+if len(sys.argv) > 1:
+    n = int(sys.argv[1])
+else:
+    n = 16
+
+if len(sys.argv) > 1:
+    depth = int(sys.argv[2])
+else:
+    depth = 10
+
+print('Qubits: %d, Depth %d' % (n, depth))
+
+
+eng = MainEngine(backend=Simulator(gate_fusion=True), engine_list=[])
+qbits = eng.allocate_qureg(n)
+
+start = time.time()
+
+for level in range(depth):
+    for q in qbits:
+        ops.H | q
+        ops.SqrtX | q
+        if q != qbits[0]:
+            ops.CNOT | (q, qbits[0])
+
+
+
+runtime = time.time() - start
+
+for q in qbits:
+    ops.Measure | q
+    
+print(runtime)
+

+ 2 - 0
qcgpu/backend.py

@@ -111,3 +111,5 @@ class Backend:
 
         return out.get()
         
+    def release(self):
+        self.buffer.base_data.release()

+ 3 - 0
qcgpu/state.py

@@ -44,6 +44,9 @@ class State:
     def probabilities(self):
         return self.backend.probabilities()
 
+    def flush(self):
+        self.backend.release()
+
     def __repr__(self):
         """A string representation of the state"""