Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create KaliskiModInverse #1464

Merged
merged 14 commits into from
Oct 30, 2024
5 changes: 5 additions & 0 deletions dev_tools/qualtran_dev_tools/notebook_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,11 @@
qualtran.bloqs.mod_arithmetic.mod_multiplication._DIRTY_OUT_OF_PLACE_MONTGOMERY_MOD_MUL_DOC,
],
),
NotebookSpecV2(
title='Modular Divison',
module=qualtran.bloqs.mod_arithmetic.mod_division,
bloq_specs=[qualtran.bloqs.mod_arithmetic.mod_division._KALISKI_MOD_INVERSE_DOC],
),
NotebookSpecV2(
title='Factoring RSA',
module=qualtran.bloqs.factoring.rsa,
Expand Down
1 change: 1 addition & 0 deletions docs/bloqs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Bloqs Library
mod_arithmetic/mod_addition.ipynb
mod_arithmetic/mod_subtraction.ipynb
mod_arithmetic/mod_multiplication.ipynb
mod_arithmetic/mod_division.ipynb
factoring/rsa/rsa.ipynb
factoring/ecc/ec_add.ipynb
factoring/ecc/ecc.ipynb
Expand Down
1 change: 1 addition & 0 deletions qualtran/bloqs/mod_arithmetic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@

from ._shims import ModInv
from .mod_addition import CModAdd, CModAddK, CtrlScaleModAdd, ModAdd, ModAddK
from .mod_division import KaliskiModInverse
from .mod_multiplication import CModMulK, DirtyOutOfPlaceMontgomeryModMul, ModDbl
from .mod_subtraction import CModNeg, CModSub, ModNeg, ModSub
170 changes: 170 additions & 0 deletions qualtran/bloqs/mod_arithmetic/mod_division.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "1c5f2b28",
"metadata": {
"cq.autogen": "title_cell"
},
"source": [
"# Modular Divison"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8751aa36",
"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": "d680443c",
"metadata": {
"cq.autogen": "KaliskiModInverse.bloq_doc.md"
},
"source": [
"## `KaliskiModInverse`\n",
"Compute modular multiplicative inverse -inplace- of numbers in montgomery form.\n",
"\n",
"Applies the transformation\n",
"$$\n",
" \\ket{x} \\ket{0} \\rightarrow \\ket{x^{-1} 2^{2n} \\mod p} \\ket{\\mathrm{garbage}}\n",
"$$\n",
"\n",
"#### Parameters\n",
" - `bitsize`: size of the number.\n",
" - `mod`: The integer modulus.\n",
" - `uncompute`: whether to compute or uncompute. \n",
"\n",
"#### Registers\n",
" - `x`: The register for which we compute the multiplicative inverse.\n",
" - `m`: A 2*bitsize register of intermediate values needed for uncomputation. \n",
"\n",
"#### References\n",
" - [Performance Analysis of a Repetition Cat Code Architecture: Computing 256-bit Elliptic Curve Logarithm in 9 Hours with 126 133 Cat Qubits](https://arxiv.org/abs/2302.06639). Appendix C5.\n",
" - [Improved quantum circuits for elliptic curve discrete logarithms](https://arxiv.org/abs/2001.09580). Fig 7(b)\n",
" - [How to compute a 256-bit elliptic curve private key with only 50 million Toffoli gates](https://arxiv.org/abs/2306.08585). page 8.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5917d72",
"metadata": {
"cq.autogen": "KaliskiModInverse.bloq_doc.py"
},
"outputs": [],
"source": [
"from qualtran.bloqs.mod_arithmetic import KaliskiModInverse"
]
},
{
"cell_type": "markdown",
"id": "d44329eb",
"metadata": {
"cq.autogen": "KaliskiModInverse.example_instances.md"
},
"source": [
"### Example Instances"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "31a37cf6",
"metadata": {
"cq.autogen": "KaliskiModInverse.kaliskimodinverse_example"
},
"outputs": [],
"source": [
"kaliskimodinverse_example = KaliskiModInverse(32, 10**9 + 7)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58c697e6",
"metadata": {
"cq.autogen": "KaliskiModInverse.kaliskimodinverse_symbolic"
},
"outputs": [],
"source": [
"n, p = sympy.symbols('n p')\n",
"kaliskimodinverse_symbolic = KaliskiModInverse(n, p)"
]
},
{
"cell_type": "markdown",
"id": "9bf1e17c",
"metadata": {
"cq.autogen": "KaliskiModInverse.graphical_signature.md"
},
"source": [
"#### Graphical Signature"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eca3a706",
"metadata": {
"cq.autogen": "KaliskiModInverse.graphical_signature.py"
},
"outputs": [],
"source": [
"from qualtran.drawing import show_bloqs\n",
"show_bloqs([kaliskimodinverse_example, kaliskimodinverse_symbolic],\n",
" ['`kaliskimodinverse_example`', '`kaliskimodinverse_symbolic`'])"
]
},
{
"cell_type": "markdown",
"id": "69fd8906",
"metadata": {
"cq.autogen": "KaliskiModInverse.call_graph.md"
},
"source": [
"### Call Graph"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15c6fabe",
"metadata": {
"cq.autogen": "KaliskiModInverse.call_graph.py"
},
"outputs": [],
"source": [
"from qualtran.resource_counting.generalizers import ignore_split_join\n",
"kaliskimodinverse_example_g, kaliskimodinverse_example_sigma = kaliskimodinverse_example.call_graph(max_depth=1, generalizer=ignore_split_join)\n",
"show_call_graph(kaliskimodinverse_example_g)\n",
"show_counts_sigma(kaliskimodinverse_example_sigma)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading