{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Verification\n" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import qcgpu\n", "from projectq import MainEngine\n", "import projectq.ops as ops\n", "from projectq.backends import Simulator, CircuitDrawer" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "def qcgpu_amplitudes(n, depth):\n", " state = qcgpu.State(n)\n", "\n", " h = qcgpu.gate.h()\n", " x = qcgpu.gate.x()\n", " t = qcgpu.gate.t()\n", "\n", " for level in range(depth):\n", " for q in range(n):\n", " \n", " state.apply_gate(h, q)\n", " state.apply_gate(t, q)\n", "\n", " if q != 0:\n", " state.apply_controlled_gate(x, q, 0)\n", " \n", " return state.amplitudes()" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "def projectq_amplitudes(n, depth):\n", "# drawing_engine = CircuitDrawer()\n", "# eng = MainEngine(drawing_engine)\n", " sim = Simulator(gate_fusion=True)\n", " eng = MainEngine(backend=sim, engine_list=[])\n", " qbits = eng.allocate_qureg(n)\n", "\n", " for level in range(depth):\n", " for q in qbits:\n", " ops.H | q\n", " ops.T | q\n", " if q != qbits[0]:\n", " ops.CNOT | (q, qbits[0])\n", "\n", "\n", " eng.flush()\n", " amplitudes = sim.cheat()[1]\n", " for q in qbits:\n", " ops.Measure | q\n", " \n", " return amplitudes\n", "# return drawing_engine.get_latex()\n" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def verify(n, depth):\n", " amps_qcgpu = qcgpu_amplitudes(n,depth)\n", " amps_projectq = np.transpose(np.array([projectq_amplitudes(n,depth)]))\n", "# print(amps_qcgpu)\n", "# print(amps_projectq)\n", " return np.allclose(amps_qcgpu, amps_projectq, rtol=1e-04)\n", " \n", "verify(25,15)" ] }, { "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 }