Skip to content

Commit

Permalink
Add GF2Multiplication bloq for multiplication over GF($2^m$) (#1436)
Browse files Browse the repository at this point in the history
* Add GF2Multiplication for multiplication over GF(2^m)

* Fix formatting

* Address nits
  • Loading branch information
tanujkhattar authored and shab5 committed Oct 7, 2024
1 parent 4937e7d commit 93b7d96
Show file tree
Hide file tree
Showing 9 changed files with 1,293 additions and 3 deletions.
812 changes: 811 additions & 1 deletion dev_tools/autogenerate-bloqs-notebooks-v2.py

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions docs/bloqs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ Bloqs Library
factoring/ecc/ec_add.ipynb
factoring/ecc/ecc.ipynb

.. toctree::
:maxdepth: 2
:caption: GF Arithmetic:

gf_arithmetic/gf2_multiplication.ipynb

.. toctree::
:maxdepth: 2
:caption: Rotations:
Expand Down
2 changes: 1 addition & 1 deletion qualtran/bloqs/bookkeeping/allocate.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def adjoint(self) -> 'Bloq':
return Free(self.dtype, self.dirty)

def on_classical_vals(self) -> Dict[str, int]:
return {'reg': 0}
return {'reg': self.dtype.from_bits([0] * self.dtype.num_qubits)}

def my_tensors(
self, incoming: Dict[str, 'ConnectionT'], outgoing: Dict[str, 'ConnectionT']
Expand Down
15 changes: 15 additions & 0 deletions qualtran/bloqs/gf_arithmetic/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from qualtran.bloqs.gf_arithmetic.gf2_multiplication import GF2Multiplication
182 changes: 182 additions & 0 deletions qualtran/bloqs/gf_arithmetic/gf2_multiplication.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "87c95c4a",
"metadata": {
"cq.autogen": "title_cell"
},
"source": [
"# GF($2^m$) Multiplication"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "31c1f087",
"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": "307679ec",
"metadata": {
"cq.autogen": "GF2Multiplication.bloq_doc.md"
},
"source": [
"## `GF2Multiplication`\n",
"Out of place multiplication over GF($2^m$).\n",
"\n",
"The bloq implements out of place multiplication of two quantum registers storing elements\n",
"from GF($2^m$) using construction described in Ref[1], which extends the classical construction\n",
"of Ref[2].\n",
"\n",
"To multiply two m-bit inputs $a = [a_0, a_1, ..., a_{m-1}]$ and $b = [b_0, b_1, ..., b_{m-1}]$,\n",
"the construction computes the output vector $c$ via the following three steps:\n",
" 1. Compute $e = U.b$ where $U$ is an upper triangular matrix constructed using $a$.\n",
" 2. Compute $Q.e$ where $Q$ is an $m \\times (m - 1)$ reduction matrix that depends upon the\n",
" irreducible polynomial $P(x)$ of the galois field $GF(2^m)$. The i'th column of the\n",
" matrix corresponds to coefficients of the polynomial $x^{m + i} % P(x)$. This matrix $Q$\n",
" is a linear reversible circuit that can be implemented only using CNOT gates.\n",
" 3. Compute $d = L.b$ where $L$ is a lower triangular matrix constructed using $a$.\n",
" 4. Compute $c = d + Q.e$ to obtain the final product.\n",
"\n",
"Steps 1 and 3 are performed using $n^2$ Toffoli gates and step 2 is performed only using CNOT\n",
"gates.\n",
"\n",
"#### Parameters\n",
" - `bitsize`: The degree $m$ of the galois field $GF(2^m)$. Also corresponds to the number of qubits in each of the two input registers $a$ and $b$ that should be multiplied. \n",
"\n",
"#### Registers\n",
" - `x`: Input THRU register of size $m$ that stores elements from $GF(2^m)$.\n",
" - `y`: Input THRU register of size $m$ that stores elements from $GF(2^m)$.\n",
" - `result`: Output RIGHT register of size $m$ that stores the product $x * y$ in $GF(2^m)$. \n",
"\n",
"#### References\n",
" - [On the Design and Optimization of a Quantum Polynomial-Time Attack on Elliptic Curve Cryptography](https://arxiv.org/abs/0710.1093). \n",
" - [Low complexity bit parallel architectures for polynomial basis multiplication over GF(2m)](https://ieeexplore.ieee.org/abstract/document/1306989). \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "872a44d1",
"metadata": {
"cq.autogen": "GF2Multiplication.bloq_doc.py"
},
"outputs": [],
"source": [
"from qualtran.bloqs.gf_arithmetic import GF2Multiplication"
]
},
{
"cell_type": "markdown",
"id": "d0f0db7d",
"metadata": {
"cq.autogen": "GF2Multiplication.example_instances.md"
},
"source": [
"### Example Instances"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "131bc962",
"metadata": {
"cq.autogen": "GF2Multiplication.gf16_multiplication"
},
"outputs": [],
"source": [
"gf16_multiplication = GF2Multiplication(4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69f564d8",
"metadata": {
"cq.autogen": "GF2Multiplication.gf2_multiplication_symbolic"
},
"outputs": [],
"source": [
"import sympy\n",
"\n",
"m = sympy.Symbol('m')\n",
"gf2_multiplication_symbolic = GF2Multiplication(m)"
]
},
{
"cell_type": "markdown",
"id": "2a62c2b8",
"metadata": {
"cq.autogen": "GF2Multiplication.graphical_signature.md"
},
"source": [
"#### Graphical Signature"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf003e98",
"metadata": {
"cq.autogen": "GF2Multiplication.graphical_signature.py"
},
"outputs": [],
"source": [
"from qualtran.drawing import show_bloqs\n",
"show_bloqs([gf16_multiplication, gf2_multiplication_symbolic],\n",
" ['`gf16_multiplication`', '`gf2_multiplication_symbolic`'])"
]
},
{
"cell_type": "markdown",
"id": "f14ef0c5",
"metadata": {
"cq.autogen": "GF2Multiplication.call_graph.md"
},
"source": [
"### Call Graph"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4b7bf2c",
"metadata": {
"cq.autogen": "GF2Multiplication.call_graph.py"
},
"outputs": [],
"source": [
"from qualtran.resource_counting.generalizers import ignore_split_join\n",
"gf16_multiplication_g, gf16_multiplication_sigma = gf16_multiplication.call_graph(max_depth=1, generalizer=ignore_split_join)\n",
"show_call_graph(gf16_multiplication_g)\n",
"show_counts_sigma(gf16_multiplication_sigma)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 93b7d96

Please sign in to comment.