Browse Source

Added single qubit measurement

Adam Kelly 6 years ago
parent
commit
3ef1da7b70
3 changed files with 120 additions and 81 deletions
  1. 74 0
      notebooks/In-Progress Experiments.ipynb
  2. 0 80
      notebooks/Untitled.ipynb
  3. 46 1
      qcgpu/backend.py

+ 74 - 0
notebooks/In-Progress Experiments.ipynb

@@ -0,0 +1,74 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import qcgpu\n",
+    "\n",
+    "state = qcgpu.State(28)\n",
+    "\n",
+    "state.apply_all(qcgpu.gate.h())\n",
+    "\n",
+    "# state"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "0.4999062"
+      ]
+     },
+     "execution_count": 2,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "state.backend.measure_qubit(0)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  },
+  {
+   "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
+}

File diff suppressed because it is too large
+ 0 - 80
notebooks/Untitled.ipynb


+ 46 - 1
qcgpu/backend.py

@@ -1,7 +1,9 @@
 import os
+import random
+import numpy as np
 import pyopencl as cl
 import pyopencl.array as pycl_array
-import numpy as np
+from pyopencl.reduction import ReductionKernel
 
 # Get the OpenCL kernel
 kernel_path = os.path.join(
@@ -90,6 +92,49 @@ class Backend:
             gate.d
         )
     
+    def qubit_probability(self, target):
+        """Get the probability of a single qubit begin measured as '0'"""
+
+        preamble = """
+        #include <pyopencl-complex.h>
+
+        float probability(int target, int i, cfloat_t amp) {
+            if ((i & (1 << target )) != 0) {
+                return 0;
+            }
+            // return 6.0;
+            float abs = cfloat_abs(amp);
+            return abs * abs;
+        }
+        """
+        
+
+        kernel = ReductionKernel(
+            self.context, 
+            np.float, 
+            neutral = "0",
+            reduce_expr="a + b",
+            map_expr="probability(target, i, amps[i])",
+            arguments="__global cfloat_t *amps, __global int target",
+            preamble=preamble
+        )
+
+        return kernel(self.buffer, target).get()
+
+    def measure_qubit(self, target):
+        probability_of_0 = self.qubit_probability(target)
+
+        total = 0
+        samples = 10000000.0
+
+        for i in range(int(samples)):
+            outcome = 1 if random.random() > probability_of_0 else 0
+            total = total + outcome
+            
+        
+        return total / samples
+
+
     def amplitudes(self):
         """Gets the probability amplitudes"""
         return self.buffer.get()