|
@@ -2,16 +2,22 @@
|
|
"cells": [
|
|
"cells": [
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
- "execution_count": 5,
|
|
|
|
|
|
+ "execution_count": 32,
|
|
"metadata": {},
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"outputs": [],
|
|
"source": [
|
|
"source": [
|
|
|
|
+ "%load_ext line_profiler\n",
|
|
"%matplotlib inline\n",
|
|
"%matplotlib inline\n",
|
|
"\n",
|
|
"\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"from IPython.display import set_matplotlib_formats\n",
|
|
"from IPython.display import set_matplotlib_formats\n",
|
|
"import numpy as np\n",
|
|
"import numpy as np\n",
|
|
"import qcgpu\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",
|
|
"\n",
|
|
"set_matplotlib_formats('png', 'pdf')\n",
|
|
"set_matplotlib_formats('png', 'pdf')\n",
|
|
"plt.style.use('ggplot')"
|
|
"plt.style.use('ggplot')"
|
|
@@ -19,11 +25,11 @@
|
|
},
|
|
},
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
- "execution_count": 6,
|
|
|
|
|
|
+ "execution_count": 27,
|
|
"metadata": {},
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"outputs": [],
|
|
"source": [
|
|
"source": [
|
|
- "def plot_state(state):\n",
|
|
|
|
|
|
+ "def plot_qcgpu_state(state):\n",
|
|
" labels = map(\n",
|
|
" labels = map(\n",
|
|
" lambda i: np.binary_repr(i, state.num_qubits), \n",
|
|
" lambda i: np.binary_repr(i, state.num_qubits), \n",
|
|
" range(0, 2**state.num_qubits)\n",
|
|
" range(0, 2**state.num_qubits)\n",
|
|
@@ -42,49 +48,106 @@
|
|
},
|
|
},
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"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": {},
|
|
"metadata": {},
|
|
"outputs": [
|
|
"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": [
|
|
"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",
|
|
"cell_type": "code",
|
|
- "execution_count": null,
|
|
|
|
- "metadata": {
|
|
|
|
- "scrolled": true
|
|
|
|
- },
|
|
|
|
|
|
+ "execution_count": 31,
|
|
|
|
+ "metadata": {},
|
|
"outputs": [],
|
|
"outputs": [],
|
|
"source": [
|
|
"source": [
|
|
- "plot_state(state)"
|
|
|
|
|
|
+ "# bench_projectq(26,5)"
|
|
]
|
|
]
|
|
},
|
|
},
|
|
{
|
|
{
|