{ "cells": [ { "cell_type": "markdown", "id": "846e63df", "metadata": { "cq.autogen": "title_cell" }, "source": [ "# Polynomials over GF($2^m$) - Addition" ] }, { "cell_type": "code", "execution_count": 1, "id": "5dd599ca", "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": "458cf4fa", "metadata": { "cq.autogen": "GF2PolyAdd.bloq_doc.md" }, "source": [ "## `GF2PolyAdd`\n", "In place quantum-quantum addition of two polynomials defined over GF($2^m$).\n", "\n", "The bloq implements in place addition of quantum registers $|f(x)\\rangle$ and $|g(x)\\rangle$\n", "storing coefficients of two degree-n polynomials defined over GF($2^m$).\n", "Addition in GF($2^m$) simply reduces to a component wise XOR, which can be implemented via\n", "CNOT gates.\n", "\n", "$$\n", " |f(x)\\rangle |g(x)\\rangle \\rightarrow |f(x)\\rangle |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", "\n", "#### Registers\n", " - `f_x`: THRU register that stores coefficients of first polynomial defined over $GF(2^m)$.\n", " - `g_x`: THRU register that stores coefficients of second polynomial defined over $GF(2^m)$.\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "d67b7e61", "metadata": { "cq.autogen": "GF2PolyAdd.bloq_doc.py" }, "outputs": [], "source": [ "from qualtran.bloqs.gf_poly_arithmetic import GF2PolyAdd" ] }, { "cell_type": "markdown", "id": "0a0a575b", "metadata": { "cq.autogen": "GF2PolyAdd.example_instances.md" }, "source": [ "### Example Instances" ] }, { "cell_type": "code", "execution_count": 3, "id": "f77964d0", "metadata": { "cq.autogen": "GF2PolyAdd.gf2_poly_4_8_add" }, "outputs": [], "source": [ "from qualtran import QGF, QGFPoly\n", "\n", "qgf_poly = QGFPoly(4, QGF(2, 3))\n", "gf2_poly_4_8_add = GF2PolyAdd(qgf_poly)" ] }, { "cell_type": "code", "execution_count": 4, "id": "0b094507", "metadata": { "cq.autogen": "GF2PolyAdd.gf2_poly_add_symbolic" }, "outputs": [], "source": [ "import sympy\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_symbolic = GF2PolyAdd(qgf_poly)" ] }, { "cell_type": "markdown", "id": "43b66756", "metadata": { "cq.autogen": "GF2PolyAdd.graphical_signature.md" }, "source": [ "#### Graphical Signature" ] }, { "cell_type": "code", "execution_count": 5, "id": "ecf1a676", "metadata": { "cq.autogen": "GF2PolyAdd.graphical_signature.py" }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "cbde801ab4714d71a414543ff26eaf9b", "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", "GF2PolyAdd\n", "\n", "\n", "\n", "b1\n", "\n", "GF2Addition\n", "\n", "\n", "\n", "b0->b1\n", "\n", "\n", "5\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "#### Counts totals:\n", " - `GF2Addition`: 5" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from qualtran.resource_counting.generalizers import ignore_split_join\n", "gf2_poly_4_8_add_g, gf2_poly_4_8_add_sigma = gf2_poly_4_8_add.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(gf2_poly_4_8_add_g)\n", "show_counts_sigma(gf2_poly_4_8_add_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": { "3a71f0511b994b15ac44426541c4a05f": { "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 } }, "594e539d06f1419884f995ef0c62aab5": { "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_3a71f0511b994b15ac44426541c4a05f", "msg_id": "", "outputs": [ { "data": { "text/markdown": "`gf2_poly_add_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\nGF2PolyAdd\n\nGF2PolyAdd\n\nf_x\n\ng_x\n\n\n\nf_x_G1:e->GF2PolyAdd:w\n\n\n(n + 1)*ceiling(log2(floor(2**m)))\n\n\n\ng_x_G4\ng_x\n\n\n\ng_x_G4:e->GF2PolyAdd:w\n\n\n(n + 1)*ceiling(log2(floor(2**m)))\n\n\n\nf_x_G5\nf_x\n\n\n\nGF2PolyAdd:e->f_x_G5:w\n\n\n(n + 1)*ceiling(log2(floor(2**m)))\n\n\n\ng_x_G3\ng_x\n\n\n\nGF2PolyAdd:e->g_x_G3: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 } }, "868895c75112463e8eaf2f22c25f3dc0": { "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 } }, "8fe3da0387c141f394639b77331b26d7": { "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 } }, "c89fabb524434f92b4e21a30ad6ce11f": { "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_868895c75112463e8eaf2f22c25f3dc0", "msg_id": "", "outputs": [ { "data": { "text/markdown": "`gf2_poly_4_8_add`", "text/plain": "" }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": "\n\nmy_graph\n\n\n\nf_x_G4\nf_x\n\n\n\nGF2PolyAdd\n\nGF2PolyAdd\n\nf_x\n\ng_x\n\n\n\nf_x_G4:e->GF2PolyAdd:w\n\n\n15\n\n\n\ng_x_G1\ng_x\n\n\n\ng_x_G1:e->GF2PolyAdd:w\n\n\n15\n\n\n\nf_x_G5\nf_x\n\n\n\nGF2PolyAdd:e->f_x_G5:w\n\n\n15\n\n\n\ng_x_G2\ng_x\n\n\n\nGF2PolyAdd:e->g_x_G2:w\n\n\n15\n\n\n", "text/plain": "" }, "metadata": {}, "output_type": "display_data" } ], "tabbable": null, "tooltip": null } }, "cbde801ab4714d71a414543ff26eaf9b": { "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_c89fabb524434f92b4e21a30ad6ce11f", "IPY_MODEL_594e539d06f1419884f995ef0c62aab5" ], "layout": "IPY_MODEL_8fe3da0387c141f394639b77331b26d7", "tabbable": null, "tooltip": null } } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }