|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "5e1ac363", |
| 6 | + "metadata": { |
| 7 | + "cq.autogen": "title_cell" |
| 8 | + }, |
| 9 | + "source": [ |
| 10 | + "# Subtraction" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": null, |
| 16 | + "id": "3a2de513", |
| 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": "b91ab938", |
| 34 | + "metadata": { |
| 35 | + "cq.autogen": "Subtract.bloq_doc.md" |
| 36 | + }, |
| 37 | + "source": [ |
| 38 | + "## `Subtract`\n", |
| 39 | + "An n-bit subtraction gate.\n", |
| 40 | + "\n", |
| 41 | + "Implements $U|a\\rangle|b\\rangle \\rightarrow |a\\rangle|a-b\\rangle$ using $4n - 4 T$ gates.\n", |
| 42 | + "\n", |
| 43 | + "This construction uses `XGate` and `AddK` to compute the twos-compliment of `b` before\n", |
| 44 | + "doing a standard `Add`.\n", |
| 45 | + "\n", |
| 46 | + "#### Parameters\n", |
| 47 | + " - `a_dtype`: Quantum datatype used to represent the integer a.\n", |
| 48 | + " - `b_dtype`: Quantum datatype used to represent the integer b. Must be large enough to hold the result in the output register of a - b, or else it simply drops the most significant bits. If not specified, b_dtype is set to a_dtype. \n", |
| 49 | + "\n", |
| 50 | + "#### Registers\n", |
| 51 | + " - `a`: A a_dtype.bitsize-sized input register (register a above).\n", |
| 52 | + " - `b`: A b_dtype.bitsize-sized input/output register (register b above).\n" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": null, |
| 58 | + "id": "64dd238f", |
| 59 | + "metadata": { |
| 60 | + "cq.autogen": "Subtract.bloq_doc.py" |
| 61 | + }, |
| 62 | + "outputs": [], |
| 63 | + "source": [ |
| 64 | + "from qualtran.bloqs.arithmetic import Subtract" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "markdown", |
| 69 | + "id": "4f77ad26", |
| 70 | + "metadata": { |
| 71 | + "cq.autogen": "Subtract.example_instances.md" |
| 72 | + }, |
| 73 | + "source": [ |
| 74 | + "### Example Instances" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": null, |
| 80 | + "id": "2cd50aa6", |
| 81 | + "metadata": { |
| 82 | + "cq.autogen": "Subtract.sub_symb" |
| 83 | + }, |
| 84 | + "outputs": [], |
| 85 | + "source": [ |
| 86 | + "n = sympy.Symbol('n')\n", |
| 87 | + "sub_symb = Subtract(QInt(bitsize=n))" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": null, |
| 93 | + "id": "e2862687", |
| 94 | + "metadata": { |
| 95 | + "cq.autogen": "Subtract.sub_small" |
| 96 | + }, |
| 97 | + "outputs": [], |
| 98 | + "source": [ |
| 99 | + "sub_small = Subtract(QInt(bitsize=4))" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": null, |
| 105 | + "id": "8b6584fc", |
| 106 | + "metadata": { |
| 107 | + "cq.autogen": "Subtract.sub_large" |
| 108 | + }, |
| 109 | + "outputs": [], |
| 110 | + "source": [ |
| 111 | + "sub_large = Subtract(QInt(bitsize=64))" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": null, |
| 117 | + "id": "27312995", |
| 118 | + "metadata": { |
| 119 | + "cq.autogen": "Subtract.sub_diff_size_regs" |
| 120 | + }, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "sub_diff_size_regs = Subtract(QInt(bitsize=4), QInt(bitsize=16))" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "markdown", |
| 128 | + "id": "82c580af", |
| 129 | + "metadata": { |
| 130 | + "cq.autogen": "Subtract.graphical_signature.md" |
| 131 | + }, |
| 132 | + "source": [ |
| 133 | + "#### Graphical Signature" |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "code", |
| 138 | + "execution_count": null, |
| 139 | + "id": "4e508f72", |
| 140 | + "metadata": { |
| 141 | + "cq.autogen": "Subtract.graphical_signature.py" |
| 142 | + }, |
| 143 | + "outputs": [], |
| 144 | + "source": [ |
| 145 | + "from qualtran.drawing import show_bloqs\n", |
| 146 | + "show_bloqs([sub_symb, sub_small, sub_large, sub_diff_size_regs],\n", |
| 147 | + " ['`sub_symb`', '`sub_small`', '`sub_large`', '`sub_diff_size_regs`'])" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "markdown", |
| 152 | + "id": "a282a369", |
| 153 | + "metadata": { |
| 154 | + "cq.autogen": "Subtract.call_graph.md" |
| 155 | + }, |
| 156 | + "source": [ |
| 157 | + "### Call Graph" |
| 158 | + ] |
| 159 | + }, |
| 160 | + { |
| 161 | + "cell_type": "code", |
| 162 | + "execution_count": null, |
| 163 | + "id": "43f1884d", |
| 164 | + "metadata": { |
| 165 | + "cq.autogen": "Subtract.call_graph.py" |
| 166 | + }, |
| 167 | + "outputs": [], |
| 168 | + "source": [ |
| 169 | + "from qualtran.resource_counting.generalizers import ignore_split_join\n", |
| 170 | + "sub_symb_g, sub_symb_sigma = sub_symb.call_graph(max_depth=1, generalizer=ignore_split_join)\n", |
| 171 | + "show_call_graph(sub_symb_g)\n", |
| 172 | + "show_counts_sigma(sub_symb_sigma)" |
| 173 | + ] |
| 174 | + } |
| 175 | + ], |
| 176 | + "metadata": { |
| 177 | + "kernelspec": { |
| 178 | + "display_name": "Python 3", |
| 179 | + "language": "python", |
| 180 | + "name": "python3" |
| 181 | + }, |
| 182 | + "language_info": { |
| 183 | + "name": "python" |
| 184 | + } |
| 185 | + }, |
| 186 | + "nbformat": 4, |
| 187 | + "nbformat_minor": 5 |
| 188 | +} |
0 commit comments