Bläddra i källkod

Work in progress commit

Adam Kelly 6 år sedan
förälder
incheckning
26c6ef2ac9
7 ändrade filer med 269 tillägg och 121 borttagningar
  1. 0 104
      Testing.ipynb
  2. 119 0
      notebooks/Testing.ipynb
  3. 80 0
      notebooks/Untitled.ipynb
  4. 0 1
      qcgpu/backend.py
  5. 9 0
      qcgpu/gate.py
  6. 6 12
      qcgpu/state.py
  7. 55 4
      testing.py

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 104
Testing.ipynb


+ 119 - 0
notebooks/Testing.ipynb

@@ -0,0 +1,119 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "%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",
+    "\n",
+    "set_matplotlib_formats('png', 'pdf')\n",
+    "plt.style.use('ggplot')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def plot_state(state):\n",
+    "    labels = map(\n",
+    "        lambda i: np.binary_repr(i, state.num_qubits), \n",
+    "        range(0, 2**state.num_qubits)\n",
+    "    )\n",
+    "\n",
+    "    y_pos = np.arange(len(labels))\n",
+    "    performance = list(state.probabilities())\n",
+    " \n",
+    "    plt.bar(y_pos, performance, align='center')\n",
+    "    plt.xticks(y_pos, labels)\n",
+    "    plt.ylabel('Amplitude')\n",
+    "    plt.title('Probability Amplitudes')\n",
+    "\n",
+    "    plt.show()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "20"
+      ]
+     },
+     "execution_count": 9,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "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"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "scrolled": true
+   },
+   "outputs": [],
+   "source": [
+    "plot_state(state)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 2",
+   "language": "python",
+   "name": "python2"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 2
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython2",
+   "version": "2.7.15rc1"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 80 - 0
notebooks/Untitled.ipynb


+ 0 - 1
qcgpu/backend.py

@@ -30,7 +30,6 @@ class Backend:
         self.num_qubits = num_qubits
         self.dtype = dtype
 
-        # Setup the opencl context, queue and program
         self.context = cl.create_some_context()
         self.queue = cl.CommandQueue(self.context)
         self.program = cl.Program(self.context, kernel).build()

+ 9 - 0
qcgpu/gate.py

@@ -32,3 +32,12 @@ def y():
 
 def z():
     return Gate(np.matrix([[1, 0], [0, -1]]))
+
+def s():
+    return Gate(np.matrix([[1, 0], [0, 1j]]))
+
+def t():
+    return Gate(np.matrix([[1, 0], [0, np.exp(np.pi * 1j / 4)]]))
+
+def sqrt_x():
+    return Gate(0.5 * np.matrix([[1+1j, 1-1j], [1-1j, 1+1j]]))

+ 6 - 12
qcgpu/state.py

@@ -21,6 +21,12 @@ class State:
 
         self.backend.apply_gate(gate, target)
 
+
+    def apply_all(self, gate):
+        # TODO: Check that gate is correct
+        for i in range(self.num_qubits):
+            self.apply_gate(gate, i)
+
     def apply_controlled_gate(self, gate, control, target):
         if not isinstance(target, int) or target < 0:
             raise ValueError("target must be an int > 0")
@@ -32,24 +38,12 @@ class State:
 
         self.backend.apply_controlled_gate(gate, control, target)
 
-
-
-
-
-
-
-
-
-
     def amplitudes(self):
         return self.backend.amplitudes()
     
     def probabilities(self):
         return self.backend.probabilities()
 
-    def sum(self):
-        return cl.array.sum(self.backend.buffer)
-
     def __repr__(self):
         """A string representation of the state"""
 

+ 55 - 4
testing.py

@@ -1,10 +1,61 @@
+# import qcgpu
+# import perf
+
+# def run(num_qubits, depth):
+#     state = qcgpu.State(num_qubits)
+#     h = qcgpu.gate.h()
+
+#     for i in range(num_qubits * depth):
+#         state.apply_gate(h, i % num_qubits)
+
 import qcgpu
+import sys
+import time
+
+# ------------------------------------------
+# number of qubits and depth
+# ------------------------------------------
+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))
+
+
+# ------------------------------------------
+# qubit register
+# ------------------------------------------
+
+state = qcgpu.State(n)
+
+# ------------------------------------------
+# circuit
+# ------------------------------------------
 
-state = qcgpu.State(2)
 h = qcgpu.gate.h()
 x = qcgpu.gate.x()
+sqrt_x = qcgpu.gate.sqrt_x()
+
+# timing -- get the start time
+start = time.time()
+
+# random circuit
+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)
 
-state.apply_gate(x, 0)
-state.apply_controlled_gate(x, 0)
+# timing -- get the end time
+runtime = time.time() - start
 
-print(state)
+# print out the runtime
+print(runtime)