{ "cells": [ { "cell_type": "markdown", "id": "71e0dbd8", "metadata": { "cq.autogen": "title_cell" }, "source": [ "# Polynomials over GF($2^m$) - Add Constant" ] }, { "cell_type": "code", "execution_count": 1, "id": "c02448cd", "metadata": { "cq.autogen": "top_imports" }, "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", "import sympy\n", "import cirq" ] }, { "cell_type": "markdown", "id": "a5f23a08", "metadata": { "cq.autogen": "GF2PolyAddK.bloq_doc.md" }, "source": [ "## `GF2PolyAddK`\n", "In place addition of a constant polynomial defined over GF($2^m$).\n", "\n", "The bloq implements in place addition of a classical constant polynomial $g(x)$ and\n", "a quantum register $|f(x)\\rangle$ storing coefficients of a degree-n polynomial defined\n", "over GF($2^m$). Addition in GF($2^m$) simply reduces to a component wise XOR, which can\n", "be implemented via X gates.\n", "\n", "$$\n", " |f(x)\\rangle \\rightarrow |f(x) + g(x)\\rangle\n", "$$\n", "\n", "#### Parameters\n", " - `qgf_poly`: An instance of `QGFPoly` type that defines the data type for quantum register $|f(x)\\rangle$ storing coefficients of a degree-n polynomial defined over GF($2^m$).\n", " - `g_x`: An instance of `galois.Poly` that specifies that constant polynomial g(x) defined over GF($2^m$) that should be added to the input register f(x). \n", "\n", "#### Registers\n", " - `f_x`: Input THRU register that stores coefficients of polynomial defined over $GF(2^m)$.\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "d40e23be", "metadata": { "cq.autogen": "GF2PolyAddK.bloq_doc.py" }, "outputs": [], "source": [ "from qualtran.bloqs.gf_poly_arithmetic import GF2PolyAddK" ] }, { "cell_type": "markdown", "id": "101615d7", "metadata": { "cq.autogen": "GF2PolyAddK.example_instances.md" }, "source": [ "### Example Instances" ] }, { "cell_type": "code", "execution_count": 3, "id": "2cc9165f", "metadata": { "cq.autogen": "GF2PolyAddK.gf2_poly_4_8_add_k" }, "outputs": [], "source": [ "from galois import Poly\n", "\n", "from qualtran import QGF, QGFPoly\n", "\n", "qgf_poly = QGFPoly(4, QGF(2, 3))\n", "g_x = Poly(qgf_poly.qgf.gf_type([1, 2, 3, 4, 5]))\n", "gf2_poly_4_8_add_k = GF2PolyAddK(qgf_poly, g_x)" ] }, { "cell_type": "code", "execution_count": 4, "id": "9081edee", "metadata": { "cq.autogen": "GF2PolyAddK.gf2_poly_add_k_symbolic" }, "outputs": [], "source": [ "import sympy\n", "from galois import Poly\n", "\n", "from qualtran import QGF, QGFPoly\n", "\n", "n, m = sympy.symbols('n, m', positive=True, integers=True)\n", "qgf_poly = QGFPoly(n, QGF(2, m))\n", "gf2_poly_add_k_symbolic = GF2PolyAddK(qgf_poly, Poly([0, 0, 0, 0]))" ] }, { "cell_type": "markdown", "id": "11a4d71f", "metadata": { "cq.autogen": "GF2PolyAddK.graphical_signature.md" }, "source": [ "#### Graphical Signature" ] }, { "cell_type": "code", "execution_count": 5, "id": "66b4a211", "metadata": { "cq.autogen": "GF2PolyAddK.graphical_signature.py" }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "281bec0dcd3946eab6be05274a6da138", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(Output(outputs=({'output_type': 'display_data', 'data': {'text/plain': '\n", "\n", "counts\n", "\n", "\n", "\n", "b0\n", "\n", "GF2PolyAddK\n", "\n", "\n", "\n", "b1\n", "\n", "GFPolyJoin\n", "\n", "\n", "\n", "b0->b1\n", "\n", "\n", "1\n", "\n", "\n", "\n", "b2\n", "\n", "GF2AddK\n", "\n", "\n", "\n", "b0->b2\n", "\n", "\n", "1\n", "\n", "\n", "\n", "b3\n", "\n", "GFPolySplit\n", "\n", "\n", "\n", "b0->b3\n", "\n", "\n", "1\n", "\n", "\n", "\n", "b4\n", "\n", "GF2AddK\n", "\n", "\n", "\n", "b0->b4\n", "\n", "\n", "1\n", "\n", "\n", "\n", "b5\n", "\n", "GF2AddK\n", "\n", "\n", "\n", "b0->b5\n", "\n", "\n", "1\n", "\n", "\n", "\n", "b6\n", "\n", "GF2AddK\n", "\n", "\n", "\n", "b0->b6\n", "\n", "\n", "1\n", "\n", "\n", "\n", "b7\n", "\n", "GF2AddK\n", "\n", "\n", "\n", "b0->b7\n", "\n", "\n", "1\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "#### Counts totals:\n", " - `GF2AddK`: 1\n", " - `GF2AddK`: 1\n", " - `GF2AddK`: 1\n", " - `GF2AddK`: 1\n", " - `GF2AddK`: 1\n", " - `GFPolyJoin`: 1\n", " - `GFPolySplit`: 1" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from qualtran.resource_counting.generalizers import ignore_split_join\n", "gf2_poly_4_8_add_k_g, gf2_poly_4_8_add_k_sigma = gf2_poly_4_8_add_k.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(gf2_poly_4_8_add_k_g)\n", "show_counts_sigma(gf2_poly_4_8_add_k_sigma)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.8" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "281bec0dcd3946eab6be05274a6da138": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HBoxModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "2.0.0", "_model_name": "HBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "2.0.0", "_view_name": "HBoxView", "box_style": "", "children": [ "IPY_MODEL_5d32f38be42f4bacb7840379a4360a7a", "IPY_MODEL_ee6d548c345440f4b3bd1a40ca28e459" ], "layout": "IPY_MODEL_8d0565c1fb52432294560e82bef4208e", "tabbable": null, "tooltip": null } }, "3240a3de4c0f41e981a043c493300594": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "2.0.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "2.0.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border_bottom": null, "border_left": null, "border_right": null, "border_top": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "3fd0a44bf2834d2094a516ea57d36a38": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "2.0.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "2.0.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border_bottom": null, "border_left": null, "border_right": null, "border_top": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "5d32f38be42f4bacb7840379a4360a7a": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_3fd0a44bf2834d2094a516ea57d36a38", "msg_id": "", "outputs": [ { "data": { "text/markdown": "`gf2_poly_4_8_add_k`", "text/plain": "" }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": "\n\nmy_graph\n\n\n\nf_x_G2\nf_x\n\n\n\nGF2PolyAddK\n\nGF2PolyAddK\n\nf_x\n\n\n\nf_x_G2:e->GF2PolyAddK:w\n\n\n15\n\n\n\nf_x_G0\nf_x\n\n\n\nGF2PolyAddK:e->f_x_G0:w\n\n\n15\n\n\n", "text/plain": "" }, "metadata": {}, "output_type": "display_data" } ], "tabbable": null, "tooltip": null } }, "8d0565c1fb52432294560e82bef4208e": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "2.0.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "2.0.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border_bottom": null, "border_left": null, "border_right": null, "border_top": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "ee6d548c345440f4b3bd1a40ca28e459": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_3240a3de4c0f41e981a043c493300594", "msg_id": "", "outputs": [ { "data": { "text/markdown": "`gf2_poly_add_k_symbolic`", "text/plain": "" }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": "\n\nmy_graph\n\n\n\nf_x_G1\nf_x\n\n\n\nGF2PolyAddK\n\nGF2PolyAddK\n\nf_x\n\n\n\nf_x_G1:e->GF2PolyAddK:w\n\n\n(n + 1)*ceiling(log2(floor(2**m)))\n\n\n\nf_x_G0\nf_x\n\n\n\nGF2PolyAddK:e->f_x_G0:w\n\n\n(n + 1)*ceiling(log2(floor(2**m)))\n\n\n", "text/plain": "" }, "metadata": {}, "output_type": "display_data" } ], "tabbable": null, "tooltip": null } } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }