Skip to content

Commit 65704ab

Browse files
authored
Add BloqExample.docstring (#1536)
Use the functions docstring if using the `@bloq_example` annotation
1 parent 65b35a0 commit 65704ab

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

dev_tools/ui-export.ipynb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"metadata": {},
77
"outputs": [],
88
"source": [
9-
"from qualtran.bloqs.factoring.mod_exp import ModExp\n",
9+
"from qualtran.bloqs.factoring.rsa import ModExp\n",
1010
"from qualtran.drawing import get_musical_score_data, draw_musical_score\n",
1111
"\n",
1212
"modexp_small = ModExp(base=3, mod=16, exp_bitsize=3, x_bitsize=2048)\n",
@@ -105,6 +105,7 @@
105105
" 'examples': list(\n",
106106
" {\n",
107107
" 'name': example.name,\n",
108+
" 'example_docstring': example.docstring,\n",
108109
" 'filename': bloq_filename(example.make())\n",
109110
" }\n",
110111
" for example in bloq_spec.examples\n",
@@ -211,7 +212,7 @@
211212
],
212213
"metadata": {
213214
"kernelspec": {
214-
"display_name": "qualtran",
215+
"display_name": "Python 3 (ipykernel)",
215216
"language": "python",
216217
"name": "python3"
217218
},
@@ -225,9 +226,9 @@
225226
"name": "python",
226227
"nbconvert_exporter": "python",
227228
"pygments_lexer": "ipython3",
228-
"version": "3.11.9"
229+
"version": "3.11.8"
229230
}
230231
},
231232
"nbformat": 4,
232-
"nbformat_minor": 2
233+
"nbformat_minor": 4
233234
}

qualtran/_infra/bloq_example.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class BloqExample(Generic[_BloqType]):
3939
name: A name for the bloq instantiation.
4040
bloq_cls: The `Bloq` class that this instantiation is an instance of.
4141
generalizer: Passed to `get_bloq_counts_graph` calls for bloq-counts equivalence checking.
42+
docstring: An optional one-line description of the example.
4243
"""
4344

4445
_func: Callable[[], _BloqType] = field(repr=False, hash=False)
@@ -47,6 +48,7 @@ class BloqExample(Generic[_BloqType]):
4748
generalizer: _GeneralizerType = field(
4849
converter=lambda x: tuple(x) if isinstance(x, Sequence) else x, default=lambda x: x
4950
)
51+
docstring: Optional[str] = None
5052

5153
def make(self) -> _BloqType:
5254
"""Make the bloq."""
@@ -106,6 +108,7 @@ def _inner(func: Callable[[], _BloqType]) -> BloqExample:
106108
name=_name_from_func_name(func),
107109
bloq_cls=_bloq_cls_from_func_annotation(func),
108110
generalizer=generalizer,
111+
docstring=func.__doc__,
109112
)
110113

111114
if _func is None:

qualtran/bloqs/cryptography/rsa/rsa.ipynb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
},
108108
"outputs": [],
109109
"source": [
110+
"\"\"\"Modular exponentiation with symbolic attributes.\"\"\"\n",
110111
"g, N, n_e, n_x = sympy.symbols('g N n_e, n_x')\n",
111112
"modexp_symb = ModExp(base=g, mod=N, exp_bitsize=n_e, x_bitsize=n_x)"
112113
]
@@ -120,6 +121,7 @@
120121
},
121122
"outputs": [],
122123
"source": [
124+
"\"\"\"A small-exponent modular exponentiation demo.\"\"\"\n",
123125
"modexp_small = ModExp(base=4, mod=15, exp_bitsize=3, x_bitsize=2048)"
124126
]
125127
},
@@ -132,6 +134,7 @@
132134
},
133135
"outputs": [],
134136
"source": [
137+
"\"\"\"An example modular exponentiation to factor 13 * 17.\"\"\"\n",
135138
"modexp = ModExp.make_for_shor(big_n=13 * 17, g=9)"
136139
]
137140
},
@@ -318,12 +321,21 @@
318321
],
319322
"metadata": {
320323
"kernelspec": {
321-
"display_name": "Python 3",
324+
"display_name": "Python 3 (ipykernel)",
322325
"language": "python",
323326
"name": "python3"
324327
},
325328
"language_info": {
326-
"name": "python"
329+
"codemirror_mode": {
330+
"name": "ipython",
331+
"version": 3
332+
},
333+
"file_extension": ".py",
334+
"mimetype": "text/x-python",
335+
"name": "python",
336+
"nbconvert_exporter": "python",
337+
"pygments_lexer": "ipython3",
338+
"version": "3.11.8"
327339
}
328340
},
329341
"nbformat": 4,

qualtran/bloqs/cryptography/rsa/rsa_mod_exp.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,21 @@ def _generalize_k(b: Bloq) -> Optional[Bloq]:
162162

163163
@bloq_example(generalizer=(ignore_split_join, _generalize_k))
164164
def _modexp_small() -> ModExp:
165+
"""A small-exponent modular exponentiation demo."""
165166
modexp_small = ModExp(base=4, mod=15, exp_bitsize=3, x_bitsize=2048)
166167
return modexp_small
167168

168169

169170
@bloq_example(generalizer=(ignore_split_join, _generalize_k))
170171
def _modexp() -> ModExp:
172+
"""An example modular exponentiation to factor 13 * 17."""
171173
modexp = ModExp.make_for_shor(big_n=13 * 17, g=9)
172174
return modexp
173175

174176

175177
@bloq_example
176178
def _modexp_symb() -> ModExp:
179+
"""Modular exponentiation with symbolic attributes."""
177180
g, N, n_e, n_x = sympy.symbols('g N n_e, n_x')
178181
modexp_symb = ModExp(base=g, mod=N, exp_bitsize=n_e, x_bitsize=n_x)
179182
return modexp_symb

0 commit comments

Comments
 (0)