Skip to content

Commit

Permalink
Update docstring, notebook and add test for symbolic costs
Browse files Browse the repository at this point in the history
  • Loading branch information
tanujkhattar committed Oct 6, 2024
1 parent 8c58ae2 commit 43d185a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
7 changes: 4 additions & 3 deletions qualtran/bloqs/gf_arithmetic/gf2_inverse.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"\n",
"#### Registers\n",
" - `x`: Input THRU register of size $m$ that stores elements from $GF(2^m)$.\n",
" - `result`: Output RIGHT register of size $m$ that stores $x^{-1}$ from $GF(2^m)$.\n"
" - `result`: Output RIGHT register of size $m$ that stores $x^{-1}$ from $GF(2^m)$.\n",
" - `junk`: Output RIGHT register of size $m$ and shape ($m - 2$) that stores results from intermediate multiplications.\n"
]
},
{
Expand Down Expand Up @@ -137,8 +138,8 @@
"outputs": [],
"source": [
"from qualtran.drawing import show_bloqs\n",
"show_bloqs([gf16_inverse, gf2_inverse_symbolic],\n",
" ['`gf16_inverse`', '`gf2_inverse_symbolic`'])"
"show_bloqs([gf16_inverse],\n",
" ['`gf16_inverse`'])"
]
},
{
Expand Down
16 changes: 13 additions & 3 deletions qualtran/bloqs/gf_arithmetic/gf2_inverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

if TYPE_CHECKING:
from qualtran import BloqBuilder, Soquet
from qualtran.resource_counting import BloqCountDictT, BloqCountT, SympySymbolAllocator
from qualtran.resource_counting import BloqCountDictT, BloqCountT, CostKey, SympySymbolAllocator
from qualtran.simulation.classical_sim import ClassicalValT


Expand Down Expand Up @@ -71,6 +71,8 @@ class GF2Inverse(Bloq):
Registers:
x: Input THRU register of size $m$ that stores elements from $GF(2^m)$.
result: Output RIGHT register of size $m$ that stores $x^{-1}$ from $GF(2^m)$.
junk: Output RIGHT register of size $m$ and shape ($m - 2$) that stores
results from intermediate multiplications.
"""

bitsize: SymbolicInt
Expand All @@ -94,6 +96,14 @@ def signature(self) -> 'Signature':
def qgf(self) -> QGF:
return QGF(characteristic=2, degree=self.bitsize)

def my_static_costs(self, cost_key: 'CostKey'):
from qualtran.resource_counting import QubitCount

if isinstance(cost_key, QubitCount):
return self.signature.n_qubits()

return NotImplemented

def build_composite_bloq(self, bb: 'BloqBuilder', *, x: 'Soquet') -> Dict[str, 'Soquet']:
if is_symbolic(self.bitsize):
raise DecomposeTypeError(f"Cannot decompose symbolic {self}")
Expand Down Expand Up @@ -149,9 +159,9 @@ def _gf16_inverse() -> GF2Inverse:
def _gf2_inverse_symbolic() -> GF2Inverse:
import sympy

m = sympy.Symbol('m')
m = sympy.Symbol('m', positive=True, integer=True)
gf2_inverse_symbolic = GF2Inverse(m)
return gf2_inverse_symbolic


_GF2_INVERSE_DOC = BloqDocSpec(bloq_cls=GF2Inverse, examples=(_gf16_inverse, _gf2_inverse_symbolic))
_GF2_INVERSE_DOC = BloqDocSpec(bloq_cls=GF2Inverse, examples=(_gf16_inverse,))
9 changes: 9 additions & 0 deletions qualtran/bloqs/gf_arithmetic/gf2_inverse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
# limitations under the License.

import pytest
import sympy
from galois import GF

from qualtran.bloqs.gf_arithmetic.gf2_inverse import (
_gf2_inverse_symbolic,
_gf16_inverse,
GF2Inverse,
)
from qualtran.resource_counting import get_cost_value, QECGatesCost, QubitCount
from qualtran.testing import assert_consistent_classical_action


Expand All @@ -31,6 +33,13 @@ def test_gf2_multiplication_symbolic(bloq_autotester):
bloq_autotester(_gf2_inverse_symbolic)


def test_gf2_multiplication_symbolic_toffoli_complexity():
bloq = _gf2_inverse_symbolic.make()
m = bloq.bitsize
assert get_cost_value(bloq, QECGatesCost()).toffoli - m**2 * (m - 2) == 0
assert sympy.simplify(get_cost_value(bloq, QubitCount()) - m**2) == 0


def test_gf2_multiplication_classical_sim_quick():
m = 1
bloq = GF2Inverse(m)
Expand Down

0 comments on commit 43d185a

Please sign in to comment.