Skip to content

Commit

Permalink
stash changes for now
Browse files Browse the repository at this point in the history
  • Loading branch information
fpapa250 committed Oct 23, 2024
1 parent 3138cae commit edbcc51
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
3 changes: 1 addition & 2 deletions qualtran/bloqs/factoring/_factoring_shims.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
# limitations under the License.

from functools import cached_property
from typing import Optional, Tuple, Union
from typing import Optional, Tuple

import sympy
from attrs import frozen

from qualtran import Bloq, CompositeBloq, DecomposeTypeError, QBit, Register, Side, Signature
Expand Down
53 changes: 32 additions & 21 deletions qualtran/bloqs/factoring/rsa/rsa_mod_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class ModExp(Bloq):
[How to factor 2048 bit RSA integers in 8 hours using 20 million noisy qubits](https://arxiv.org/abs/1905.09749).
Gidney and Ekerå. 2019.
[Circuit for Shor's algorithm using 2n+3 qubits](https://arxiv.org/abs/quant-ph/0205095).
Stephane Beauregard. 2003.
[Windowed quantum arithmetic](https://arxiv.org/abs/1905.07682).
Craig Gidney. 2019.
"""
Expand Down Expand Up @@ -133,7 +136,7 @@ def qrom(self, data):
log_block_sizes = (0,)
return QROAMClean(
[
Shaped((2**(self.exp_window_size+self.mult_window_size),)),
data,
],
selection_bitsizes=(self.exp_window_size, self.mult_window_size),
target_bitsizes=(self.x_bitsize,),
Expand Down Expand Up @@ -218,26 +221,34 @@ def build_composite_bloq(self, bb: 'BloqBuilder', exponent: 'Soquet') -> Dict[st

def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
if self.exp_window_size is not None and self.mult_window_size is not None:
cg = {IntState(val=1, bitsize=self.x_bitsize): 1, Swap(self.x_bitsize): self.exp_bitsize // self.exp_window_size}

k = self.base
for i in range(self.exp_bitsize // self.exp_window_size):
kes = [pow(k, 2**i * x_e, self.mod) for x_e in range(2**self.exp_window_size)]
kes_inv = [pow(x_e, -1, self.mod) for x_e in kes]

for j in range(self.x_bitsize // self.mult_window_size):
data = list([(ke * f * 2**j) % self.mod for f in range(2**self.mult_window_size)] for ke in kes)
cg[self.qrom(data)] = cg.get(self.qrom(data), 0) + 1
cg[ModAdd(self.x_bitsize, self.mod)] = cg.get(ModAdd(self.x_bitsize, self.mod), 0) + 1
cg[self.qrom(data).adjoint()] = cg.get(self.qrom(data).adjoint(), 0) + 1

for j in range(self.x_bitsize // self.mult_window_size):
data = list([(ke_inv * f * 2**j) % self.mod for f in range(2**self.mult_window_size)] for ke_inv in kes_inv)
cg[self.qrom(data)] = cg.get(self.qrom(data), 0) + 1
cg[ModSub(QUInt(self.x_bitsize), self.mod)] = cg.get(ModSub(QUInt(self.x_bitsize), self.mod), 0) + 1
cg[self.qrom(data).adjoint()] = cg.get(self.qrom(data).adjoint(), 0) + 1

return cg
if is_symbolic(self.exp_window_size, self.mult_window_size):
num_iterations = self.exp_bitsize // self.exp_window_size
return {self.qrom(Shaped((2**(self.exp_window_size+self.mult_window_size),))): 1,
self.qrom(Shaped((2**(self.exp_window_size+self.mult_window_size),))).adjoint(): 1,
ModAdd(self.x_bitsize, self.mod): 1,
ModSub(QUInt(self.x_bitsize), self.mod): 1,
IntState(val=1, bitsize=self.x_bitsize): 1, Swap(self.x_bitsize): self.exp_bitsize // self.exp_window_size}
else:
cg = {IntState(val=1, bitsize=self.x_bitsize): 1, Swap(self.x_bitsize): self.exp_bitsize // self.exp_window_size}

k = self.base
for i in range(self.exp_bitsize // self.exp_window_size):
kes = [pow(k, 2**i * x_e, self.mod) for x_e in range(2**self.exp_window_size)]
kes_inv = [pow(x_e, -1, self.mod) for x_e in kes]

for j in range(self.x_bitsize // self.mult_window_size):
data = list([(ke * f * 2**j) % self.mod for f in range(2**self.mult_window_size)] for ke in kes)
cg[self.qrom(data)] = cg.get(self.qrom(data), 0) + 1
cg[ModAdd(self.x_bitsize, self.mod)] = cg.get(ModAdd(self.x_bitsize, self.mod), 0) + 1
cg[self.qrom(data).adjoint()] = cg.get(self.qrom(data).adjoint(), 0) + 1

for j in range(self.x_bitsize // self.mult_window_size):
data = list([(ke_inv * f * 2**j) % self.mod for f in range(2**self.mult_window_size)] for ke_inv in kes_inv)
cg[self.qrom(data)] = cg.get(self.qrom(data), 0) + 1
cg[ModSub(QUInt(self.x_bitsize), self.mod)] = cg.get(ModSub(QUInt(self.x_bitsize), self.mod), 0) + 1
cg[self.qrom(data).adjoint()] = cg.get(self.qrom(data).adjoint(), 0) + 1

return cg
else:
k = ssa.new_symbol('k')
return {self._CtrlModMul(k=k): self.exp_bitsize, IntState(val=1, bitsize=self.x_bitsize): 1}
Expand Down

0 comments on commit edbcc51

Please sign in to comment.