{ "cells": [ { "cell_type": "code", "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')" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "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", " )\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": 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": [ { "name": "stdout", "output_type": "stream", "text": [ "started\n", "('ended: ', 0.2988259792327881)\n" ] } ], "source": [ "%lprun -f bench_qcgpu bench_qcgpu(26,5)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "# bench_projectq(26,5)" ] }, { "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 }