diff --git a/scratchpads/ak2.ipynb b/scratchpads/ak2.ipynb index 4ee7a37b..c3ca377a 100644 --- a/scratchpads/ak2.ipynb +++ b/scratchpads/ak2.ipynb @@ -2,15 +2,14 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 101, "id": "aa06120b", "metadata": {}, "outputs": [], "source": [ "import sys, os; sys.path.append('..')\n", - "import random\n", + "import random, logging, math\n", "import pyzx as zx\n", - "import logging\n", "from fractions import Fraction\n", "from pyzx.gadget_extract import *\n", "Z = zx.VertexType.Z\n", @@ -22,25 +21,78 @@ }, { "cell_type": "code", - "execution_count": 4, - "id": "44aad5a9", + "execution_count": 32, + "id": "30c952b3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.00+0.00i = sqrt(2)^2\n", + "2\n" + ] + } + ], + "source": [ + "c = zx.qasm(\"\"\"\n", + "qreg q[4];\n", + "cx q[0], q[1];\n", + "cx q[1], q[2];\n", + "s q[2];\n", + "\"\"\")\n", + "g = c.to_graph()\n", + "print(g.scalar)\n", + "print(g.num_edges() - g.num_vertices() + g.num_outputs())" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "a41b4410", "metadata": {}, "outputs": [], "source": [ - "random.seed(1337)\n", - "g = zx.generate.cliffordT(8, 100, p_t=0.2)" + "def num_spiders(g):\n", + " return len([v for v in g.vertices() if g.type(v) == Z or g.type(v) == X])\n", + "\n", + "def inv(g):\n", + " return (g.scalar.power2 == g.num_edges() - num_spiders(g) - g.num_outputs())" ] }, { "cell_type": "code", - "execution_count": 5, - "id": "e2482a26", + "execution_count": 172, + "id": "4d802b98", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "575\n", + "True\n" + ] + } + ], + "source": [ + "c = zx.Circuit.load('../circuits/QFT_and_Adders/QFTAdd16_before')\n", + "g = c.to_graph()\n", + "zx.full_reduce(g)\n", + "print(g.num_edges() - g.num_vertices() + g.num_outputs())\n", + "print(g.scalar.power2 == g.num_edges() - num_spiders(g) - g.num_outputs())" + ] + }, + { + "cell_type": "code", + "execution_count": 173, + "id": "ac388e99", "metadata": {}, "outputs": [ { "data": { "text/html": [ - "
\n", + "
\n", "" ], @@ -360,7 +435,137 @@ } ], "source": [ - "zx.draw(g)" + "zx.draw(c)" + ] + }, + { + "cell_type": "code", + "execution_count": 174, + "id": "42b7f92c", + "metadata": {}, + "outputs": [], + "source": [ + "def squash_reduce(g, err_budget):\n", + " total_err = 0\n", + " for i in range(50, 1, -1):\n", + " zx.full_reduce(g)\n", + " th = Fraction(1, 2**i)\n", + " print('T=2^-' + str(i), end=' ')\n", + " for v in list(g.vertices()):\n", + " p = g.phase(v)\n", + " new_p = Fraction(round(2*p), 2)\n", + " err = abs(float(p)-round(new_p))\n", + " if p.denominator != 1 and err <= th:\n", + " if total_err + err > err_budget:\n", + " zx.full_reduce(g)\n", + " print('exceeded budget, done.')\n", + " return\n", + " else:\n", + " g.set_phase(v, new_p)\n", + " total_err += abs(math.sin(err/2))" + ] + }, + { + "cell_type": "code", + "execution_count": 175, + "id": "7bdcdf3d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Circuit QFTAdd16_before on 32 qubits with 1822 gates.\n", + " 1026 is the T-count\n", + " 796 Cliffords among which\n", + " 716 2-qubit gates (716 CNOT, 0 other) and\n", + " 32 Hadamard gates.\n" + ] + } + ], + "source": [ + "print(c.to_basic_gates().stats())" + ] + }, + { + "cell_type": "code", + "execution_count": 177, + "id": "44aad5a9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T=2^-50 T=2^-49 T=2^-48 T=2^-47 T=2^-46 T=2^-45 T=2^-44 T=2^-43 T=2^-42 T=2^-41 T=2^-40 T=2^-39 T=2^-38 T=2^-37 T=2^-36 T=2^-35 T=2^-34 T=2^-33 T=2^-32 T=2^-31 T=2^-30 T=2^-29 T=2^-28 T=2^-27 T=2^-26 T=2^-25 T=2^-24 T=2^-23 T=2^-22 T=2^-21 T=2^-20 T=2^-19 T=2^-18 T=2^-17 T=2^-16 T=2^-15 T=2^-14 T=2^-13 exceeded budget, done.\n", + "extracting circuit...\n", + "Circuit on 32 qubits with 2145 gates.\n", + " 402 is the T-count\n", + " 1743 Cliffords among which\n", + " 967 2-qubit gates (631 CNOT, 336 other) and\n", + " 776 Hadamard gates.\n" + ] + } + ], + "source": [ + "g1 = c.to_graph()\n", + "squash_reduce(g1, err_budget=0.0)\n", + "print(\"extracting circuit...\")\n", + "c1 = zx.extract_circuit(g1, up_to_perm=True)\n", + "zx.basic_optimization(c1)\n", + "#zx.draw(c1)\n", + "print(c1.stats())" + ] + }, + { + "cell_type": "code", + "execution_count": 180, + "id": "5fd3a5b2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T=2^-50 T=2^-49 T=2^-48 T=2^-47 T=2^-46 T=2^-45 T=2^-44 T=2^-43 T=2^-42 T=2^-41 T=2^-40 T=2^-39 T=2^-38 T=2^-37 T=2^-36 T=2^-35 T=2^-34 T=2^-33 T=2^-32 T=2^-31 T=2^-30 T=2^-29 T=2^-28 T=2^-27 T=2^-26 T=2^-25 T=2^-24 T=2^-23 T=2^-22 T=2^-21 T=2^-20 T=2^-19 T=2^-18 T=2^-17 T=2^-16 T=2^-15 T=2^-14 T=2^-13 T=2^-12 T=2^-11 T=2^-10 T=2^-9 T=2^-8 exceeded budget, done.\n", + "extracting circuit...\n", + "Circuit on 32 qubits with 1516 gates.\n", + " 270 is the T-count\n", + " 1246 Cliffords among which\n", + " 710 2-qubit gates (424 CNOT, 286 other) and\n", + " 532 Hadamard gates.\n" + ] + } + ], + "source": [ + "g1 = c.to_graph()\n", + "squash_reduce(g1, err_budget=0.1)\n", + "print(\"extracting circuit...\")\n", + "c1 = zx.extract_circuit(g1, up_to_perm=True)\n", + "#zx.draw(c1)\n", + "print(c1.stats())" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "e2482a26", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "290\n", + "388\n", + "114\n" + ] + } + ], + "source": [ + "print(g.num_vertices())\n", + "print(g.num_edges())\n", + "print(g.num_edges() - g.num_vertices() + g.num_outputs())" ] }, { @@ -1084,7 +1289,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.6" + "version": "3.10.12" } }, "nbformat": 4,