Skip to content

Commit

Permalink
create DirtyOutOfPlaceMontgomeryModMul (#1395)
Browse files Browse the repository at this point in the history
* create DirtyOutOfPlaceMontgomeryModMul

* reduce test size

* Use QROAMClean instead of QROM and address comments

* fix conflicts

* generate notebooks

* fix typo

* fix validation logic

* nit

* fix validation

* address comments and add tests for SingleWindowModMul

* address comments

---------

Co-authored-by: Tanuj Khattar <[email protected]>
  • Loading branch information
NoureldinYosri and tanujkhattar authored Sep 13, 2024
1 parent 0f4deee commit db8b993
Show file tree
Hide file tree
Showing 8 changed files with 796 additions and 65 deletions.
1 change: 1 addition & 0 deletions dev_tools/autogenerate-bloqs-notebooks-v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@
bloq_specs=[
qualtran.bloqs.mod_arithmetic.mod_multiplication._MOD_DBL_DOC,
qualtran.bloqs.mod_arithmetic.mod_multiplication._C_MOD_MUL_K_DOC,
qualtran.bloqs.mod_arithmetic.mod_multiplication._DIRTY_OUT_OF_PLACE_MONTGOMERY_MOD_MUL_DOC,
],
),
NotebookSpecV2(
Expand Down
17 changes: 13 additions & 4 deletions qualtran/bloqs/factoring/ecc/ec_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@

from qualtran import Bloq, bloq_example, BloqDocSpec, QUInt, Register, Signature
from qualtran.bloqs.arithmetic._shims import MultiCToffoli
from qualtran.bloqs.mod_arithmetic import CModAdd, CModNeg, CModSub, ModAdd, ModNeg, ModSub
from qualtran.bloqs.mod_arithmetic._shims import ModDbl, ModInv, ModMul
from qualtran.bloqs.mod_arithmetic import (
CModAdd,
CModNeg,
CModSub,
DirtyOutOfPlaceMontgomeryModMul,
ModAdd,
ModDbl,
ModNeg,
ModSub,
)
from qualtran.bloqs.mod_arithmetic._shims import ModInv
from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator


Expand Down Expand Up @@ -63,7 +72,7 @@ def signature(self) -> 'Signature':
]
)

def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> BloqCountDictT:
# litinksi
return {
MultiCToffoli(n=self.n): 18,
Expand All @@ -74,7 +83,7 @@ def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
ModNeg(QUInt(self.n), mod=self.mod): 2,
CModNeg(QUInt(self.n), mod=self.mod): 1,
ModDbl(QUInt(self.n), mod=self.mod): 2,
ModMul(n=self.n, mod=self.mod): 10,
DirtyOutOfPlaceMontgomeryModMul(bitsize=self.n, window_size=4, mod=self.mod): 10,
ModInv(n=self.n, mod=self.mod): 4,
}

Expand Down
4 changes: 2 additions & 2 deletions qualtran/bloqs/mod_arithmetic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from ._shims import ModInv, ModMul
from ._shims import ModInv
from .mod_addition import CModAdd, CModAddK, CtrlScaleModAdd, ModAdd, ModAddK
from .mod_multiplication import CModMulK, ModDbl
from .mod_multiplication import CModMulK, DirtyOutOfPlaceMontgomeryModMul, ModDbl
from .mod_subtraction import CModNeg, CModSub, ModNeg, ModSub
35 changes: 0 additions & 35 deletions qualtran/bloqs/mod_arithmetic/_shims.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from qualtran.bloqs.basic_gates import CNOT, CSwap, Swap, Toffoli
from qualtran.bloqs.mod_arithmetic.mod_multiplication import ModDbl
from qualtran.drawing import Text, TextBox, WireSymbol
from qualtran.symbolics import ceil, log2

if TYPE_CHECKING:
from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator
Expand Down Expand Up @@ -114,37 +113,3 @@ def wire_symbol(
elif reg.name == 'out':
return TextBox('$x^{-1}$')
raise ValueError(f'Unrecognized register name {reg.name}')


@frozen
class ModMul(Bloq):
n: int
mod: int

@cached_property
def signature(self) -> 'Signature':
return Signature(
[
Register('x', QUInt(self.n)),
Register('y', QUInt(self.n)),
Register('out', QUInt(self.n)),
]
)

def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
# Roetteler montgomery
return {Toffoli(): ceil(16 * self.n**2 * log2(self.n) - 26.3 * self.n**2)}

def wire_symbol(
self, reg: Optional['Register'], idx: Tuple[int, ...] = tuple()
) -> 'WireSymbol':
if reg is None:
return Text("")
if reg.name in ['x', 'y']:
return TextBox(reg.name)
elif reg.name == 'out':
return TextBox('x*y')
raise ValueError(f'Unrecognized register name {reg.name}')

def __str__(self):
return self.__class__.__name__
Loading

0 comments on commit db8b993

Please sign in to comment.