Skip to content

Commit

Permalink
Switch SubtractFrom to use BitwiseNot (#1164)
Browse files Browse the repository at this point in the history
* Switch `SubtractFrom` to use `BitwiseNot`

* Update notebook
  • Loading branch information
charlesyuan314 authored Jul 19, 2024
1 parent 22b3d5e commit bb83da3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 1 addition & 2 deletions qualtran/bloqs/arithmetic/multiplication.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,7 @@
" - `a`: `bitsize`-sized input register.\n",
" - `result`: `bitsize`-sized output register. \n",
" - `References`: \n",
" - `[Quantum Algorithms and Circuits for Scientific Computing](`: \n",
" - `https`: //arxiv.org/pdf/1511.08253). Section 2.1.\n"
" - `[Quantum Algorithms and Circuits for Scientific Computing](https`: //arxiv.org/pdf/1511.08253). Section 2.1.\n"
]
},
{
Expand Down
8 changes: 5 additions & 3 deletions qualtran/bloqs/arithmetic/subtraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
SoquetT,
)
from qualtran.bloqs.arithmetic.addition import Add
from qualtran.bloqs.arithmetic.bitwise import BitwiseNot
from qualtran.bloqs.arithmetic.negate import Negate
from qualtran.drawing import Text

Expand Down Expand Up @@ -219,11 +220,12 @@ def wire_symbol(
raise ValueError()

def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']:
return {(Negate(self.dtype), 1), (Subtract(self.dtype, self.dtype), 1)}
return {(BitwiseNot(self.dtype), 2), (Add(self.dtype, self.dtype), 1)}

def build_composite_bloq(self, bb: 'BloqBuilder', a: Soquet, b: Soquet) -> Dict[str, 'SoquetT']:
a, b = bb.add_t(Subtract(self.dtype, self.dtype), a=a, b=b) # a, a - b
b = bb.add(Negate(self.dtype), x=b) # a, b - a
b = bb.add(BitwiseNot(self.dtype), x=b) # a, -1 - b
a, b = bb.add_t(Add(self.dtype, self.dtype), a=a, b=b) # a, a - 1 - b
b = bb.add(BitwiseNot(self.dtype), x=b) # a, -1 - (a - 1 - b) = a, -a + b
return {'a': a, 'b': b}


Expand Down

0 comments on commit bb83da3

Please sign in to comment.