|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "a7be8545", |
| 6 | + "metadata": { |
| 7 | + "cq.autogen": "title_cell" |
| 8 | + }, |
| 9 | + "source": [ |
| 10 | + "# Alloc / Free" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": null, |
| 16 | + "id": "01fdc670", |
| 17 | + "metadata": { |
| 18 | + "cq.autogen": "top_imports" |
| 19 | + }, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", |
| 23 | + "from qualtran import QBit, QInt, QUInt, QAny\n", |
| 24 | + "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", |
| 25 | + "from typing import *\n", |
| 26 | + "import numpy as np\n", |
| 27 | + "import sympy\n", |
| 28 | + "import cirq" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "markdown", |
| 33 | + "id": "9639fbe4", |
| 34 | + "metadata": { |
| 35 | + "cq.autogen": "Allocate.bloq_doc.md" |
| 36 | + }, |
| 37 | + "source": [ |
| 38 | + "## `Allocate`\n", |
| 39 | + "Allocate an `n` bit register.\n", |
| 40 | + "\n", |
| 41 | + "#### Parameters\n", |
| 42 | + " - `dtype`: the quantum data type of the allocated register.\n", |
| 43 | + " - `dirty`: If true, represents a borrowing operation where allocated qubits can be dirty. \n", |
| 44 | + "\n", |
| 45 | + "#### Registers\n", |
| 46 | + " - `reg [right]`: The allocated register.\n" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": null, |
| 52 | + "id": "3a32133e", |
| 53 | + "metadata": { |
| 54 | + "cq.autogen": "Allocate.bloq_doc.py" |
| 55 | + }, |
| 56 | + "outputs": [], |
| 57 | + "source": [ |
| 58 | + "from qualtran.bloqs.bookkeeping import Allocate" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "markdown", |
| 63 | + "id": "3e445f38", |
| 64 | + "metadata": { |
| 65 | + "cq.autogen": "Allocate.example_instances.md" |
| 66 | + }, |
| 67 | + "source": [ |
| 68 | + "### Example Instances" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": null, |
| 74 | + "id": "64eae920", |
| 75 | + "metadata": { |
| 76 | + "cq.autogen": "Allocate.alloc" |
| 77 | + }, |
| 78 | + "outputs": [], |
| 79 | + "source": [ |
| 80 | + "n = sympy.Symbol('n')\n", |
| 81 | + "alloc = Allocate(QUInt(n))" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "markdown", |
| 86 | + "id": "9799a674", |
| 87 | + "metadata": { |
| 88 | + "cq.autogen": "Allocate.graphical_signature.md" |
| 89 | + }, |
| 90 | + "source": [ |
| 91 | + "#### Graphical Signature" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": null, |
| 97 | + "id": "cc30ba5a", |
| 98 | + "metadata": { |
| 99 | + "cq.autogen": "Allocate.graphical_signature.py" |
| 100 | + }, |
| 101 | + "outputs": [], |
| 102 | + "source": [ |
| 103 | + "from qualtran.drawing import show_bloqs\n", |
| 104 | + "show_bloqs([alloc],\n", |
| 105 | + " ['`alloc`'])" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "markdown", |
| 110 | + "id": "b3279532", |
| 111 | + "metadata": { |
| 112 | + "cq.autogen": "Allocate.call_graph.md" |
| 113 | + }, |
| 114 | + "source": [ |
| 115 | + "### Call Graph" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": null, |
| 121 | + "id": "9109defa", |
| 122 | + "metadata": { |
| 123 | + "cq.autogen": "Allocate.call_graph.py" |
| 124 | + }, |
| 125 | + "outputs": [], |
| 126 | + "source": [ |
| 127 | + "from qualtran.resource_counting.generalizers import ignore_split_join\n", |
| 128 | + "alloc_g, alloc_sigma = alloc.call_graph(max_depth=1, generalizer=ignore_split_join)\n", |
| 129 | + "show_call_graph(alloc_g)\n", |
| 130 | + "show_counts_sigma(alloc_sigma)" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "markdown", |
| 135 | + "id": "d97992a2", |
| 136 | + "metadata": { |
| 137 | + "cq.autogen": "Free.bloq_doc.md" |
| 138 | + }, |
| 139 | + "source": [ |
| 140 | + "## `Free`\n", |
| 141 | + "Free (i.e. de-allocate) a register.\n", |
| 142 | + "\n", |
| 143 | + "The tensor decomposition assumes the register is uncomputed and is in the zero\n", |
| 144 | + "state before getting freed. To verify that is the case, one can compute the resulting state\n", |
| 145 | + "vector after freeing qubits and make sure it is normalized.\n", |
| 146 | + "\n", |
| 147 | + "#### Parameters\n", |
| 148 | + " - `dtype`: The quantum data type of the register to be freed.\n", |
| 149 | + " - `dirty`: If true, represents adjoint of a borrowing operation where deallocated qubits were borrowed dirty from another part of the algorithm and must be free'd in the same dirty state. \n", |
| 150 | + "\n", |
| 151 | + "#### Registers\n", |
| 152 | + " - `reg [left]`: The register to free.\n" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "execution_count": null, |
| 158 | + "id": "02f58e9f", |
| 159 | + "metadata": { |
| 160 | + "cq.autogen": "Free.bloq_doc.py" |
| 161 | + }, |
| 162 | + "outputs": [], |
| 163 | + "source": [ |
| 164 | + "from qualtran.bloqs.bookkeeping import Free" |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "cell_type": "markdown", |
| 169 | + "id": "2330f781", |
| 170 | + "metadata": { |
| 171 | + "cq.autogen": "Free.example_instances.md" |
| 172 | + }, |
| 173 | + "source": [ |
| 174 | + "### Example Instances" |
| 175 | + ] |
| 176 | + }, |
| 177 | + { |
| 178 | + "cell_type": "code", |
| 179 | + "execution_count": null, |
| 180 | + "id": "57cb3a2d", |
| 181 | + "metadata": { |
| 182 | + "cq.autogen": "Free.free" |
| 183 | + }, |
| 184 | + "outputs": [], |
| 185 | + "source": [ |
| 186 | + "n = sympy.Symbol('n')\n", |
| 187 | + "free = Free(QUInt(n))" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "markdown", |
| 192 | + "id": "20d2be05", |
| 193 | + "metadata": { |
| 194 | + "cq.autogen": "Free.graphical_signature.md" |
| 195 | + }, |
| 196 | + "source": [ |
| 197 | + "#### Graphical Signature" |
| 198 | + ] |
| 199 | + }, |
| 200 | + { |
| 201 | + "cell_type": "code", |
| 202 | + "execution_count": null, |
| 203 | + "id": "b5a9e693", |
| 204 | + "metadata": { |
| 205 | + "cq.autogen": "Free.graphical_signature.py" |
| 206 | + }, |
| 207 | + "outputs": [], |
| 208 | + "source": [ |
| 209 | + "from qualtran.drawing import show_bloqs\n", |
| 210 | + "show_bloqs([free],\n", |
| 211 | + " ['`free`'])" |
| 212 | + ] |
| 213 | + }, |
| 214 | + { |
| 215 | + "cell_type": "markdown", |
| 216 | + "id": "203cfda5", |
| 217 | + "metadata": { |
| 218 | + "cq.autogen": "Free.call_graph.md" |
| 219 | + }, |
| 220 | + "source": [ |
| 221 | + "### Call Graph" |
| 222 | + ] |
| 223 | + }, |
| 224 | + { |
| 225 | + "cell_type": "code", |
| 226 | + "execution_count": null, |
| 227 | + "id": "34f77821", |
| 228 | + "metadata": { |
| 229 | + "cq.autogen": "Free.call_graph.py" |
| 230 | + }, |
| 231 | + "outputs": [], |
| 232 | + "source": [ |
| 233 | + "from qualtran.resource_counting.generalizers import ignore_split_join\n", |
| 234 | + "free_g, free_sigma = free.call_graph(max_depth=1, generalizer=ignore_split_join)\n", |
| 235 | + "show_call_graph(free_g)\n", |
| 236 | + "show_counts_sigma(free_sigma)" |
| 237 | + ] |
| 238 | + } |
| 239 | + ], |
| 240 | + "metadata": { |
| 241 | + "kernelspec": { |
| 242 | + "display_name": "Python 3", |
| 243 | + "language": "python", |
| 244 | + "name": "python3" |
| 245 | + }, |
| 246 | + "language_info": { |
| 247 | + "name": "python" |
| 248 | + } |
| 249 | + }, |
| 250 | + "nbformat": 4, |
| 251 | + "nbformat_minor": 5 |
| 252 | +} |
0 commit comments