Skip to content

Commit 20bf101

Browse files
committed
Merge remote-tracking branch 'origin/main' into 2025-09/soquetprotocol
2 parents 1def2e4 + 7f2b156 commit 20bf101

103 files changed

Lines changed: 9044 additions & 1239 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/nightly.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ jobs:
4343
pip install -r dev_tools/requirements/envs/pytest.env.txt
4444
pip install --no-deps -e .
4545
- run: |
46-
check/pytest
46+
check/pytest --durations=10

.github/workflows/pr.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
pip install -r dev_tools/requirements/envs/pytest.env.txt
7777
pip install --no-deps -e .
7878
- run: |
79-
python dev_tools/execute-notebooks.py
79+
python dev_tools/execute-notebooks.py --n-workers=8
8080
env:
8181
NUMBA_NUM_THREADS: 4
8282

dev_tools/conf/mypy.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ follow_imports = silent
2121
ignore_missing_imports = true
2222

2323
# Non-Google
24-
[mypy-sympy.*,matplotlib.*,proto.*,pandas.*,scipy.*,freezegun.*,mpl_toolkits.*,networkx.*,ply.*,astroid.*,pytest.*,_pytest.*,pylint.*,setuptools.*,qiskit.*,quimb.*,pylatex.*,filelock.*,sortedcontainers.*,tqdm.*,plotly.*,dash.*,tensorflow_docs.*,fxpmath.*,ipywidgets.*,cachetools.*,pydot.*,nbformat.*,nbconvert.*,openfermion.*,pennylane.*]
24+
[mypy-sympy.*,matplotlib.*,proto.*,pandas.*,scipy.*,freezegun.*,mpl_toolkits.*,networkx.*,ply.*,astroid.*,pytest.*,_pytest.*,pylint.*,setuptools.*,qiskit.*,quimb.*,pylatex.*,filelock.*,sortedcontainers.*,tqdm.*,plotly.*,dash.*,tensorflow_docs.*,fxpmath.*,ipywidgets.*,cachetools.*,pydot.*,nbformat.*,nbconvert.*,openfermion.*,pennylane.*,mpmath.*]
2525
follow_imports = silent
2626
ignore_missing_imports = true
2727

dev_tools/execute-notebooks.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ def parse_args():
2222
p.add_argument('--output-nbs', action=argparse.BooleanOptionalAction, default=True)
2323
p.add_argument('--output-md', action=argparse.BooleanOptionalAction, default=False)
2424
p.add_argument('--only-out-of-date', action=argparse.BooleanOptionalAction, default=True)
25+
p.add_argument('--n-workers', type=int, default=None)
2526
args = p.parse_args()
2627
execute_and_export_notebooks(
27-
output_nbs=args.output_nbs, output_md=args.output_md, only_out_of_date=args.only_out_of_date
28+
output_nbs=args.output_nbs,
29+
output_md=args.output_md,
30+
only_out_of_date=args.only_out_of_date,
31+
n_workers=args.n_workers,
2832
)
2933

3034

dev_tools/qualtran_dev_tools/notebook_execution.py

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
# limitations under the License.
1414
import asyncio
1515
import multiprocessing
16+
import random
1617
import subprocess
1718
import sys
19+
import time
1820
from pathlib import Path
1921
from typing import List, Optional, Tuple
2022

23+
import attrs
2124
import filelock
2225
import nbconvert
2326
import nbformat
@@ -180,6 +183,13 @@ def execute_and_export_notebook(paths: _NBInOutPaths) -> Optional[Exception]:
180183
return None
181184

182185

186+
@attrs.frozen
187+
class _NotebookRunResult:
188+
nb_in: Path
189+
err: Optional[Exception]
190+
duration_s: float
191+
192+
183193
class _NotebookRunClosure:
184194
"""Used to run notebook execution logic in subprocesses."""
185195

@@ -189,7 +199,7 @@ def __init__(self, reporoot: Path, output_nbs: bool, output_md: bool, only_out_o
189199
self.output_md = output_md
190200
self.only_out_of_date = only_out_of_date
191201

192-
def __call__(self, nb_rel_path: Path, sourceroot: Path) -> Tuple[Path, Optional[Exception]]:
202+
def __call__(self, nb_rel_path: Path, sourceroot: Path) -> _NotebookRunResult:
193203
paths = _NBInOutPaths.from_nb_rel_path(
194204
nb_rel_path,
195205
reporoot=self.reporoot,
@@ -200,15 +210,21 @@ def __call__(self, nb_rel_path: Path, sourceroot: Path) -> Tuple[Path, Optional[
200210

201211
if self.only_out_of_date and not paths.needs_reexport():
202212
print(f'{nb_rel_path} up to date')
203-
return paths.nb_in, None
213+
return _NotebookRunResult(paths.nb_in, None, 0.0)
204214

215+
start = time.time()
205216
err = execute_and_export_notebook(paths)
206-
print(f"Exported {nb_rel_path}")
207-
return paths.nb_in, err
217+
end = time.time()
218+
print(f"Exported {nb_rel_path} in {end-start:.2f} seconds.")
219+
return _NotebookRunResult(paths.nb_in, err, duration_s=end - start)
208220

209221

210222
def execute_and_export_notebooks(
211-
*, output_nbs: bool, output_md: bool, only_out_of_date: bool = True
223+
*,
224+
output_nbs: bool,
225+
output_md: bool,
226+
only_out_of_date: bool = True,
227+
n_workers: Optional[int] = None,
212228
):
213229
"""Find, execute, and export all checked-in ipynbs.
214230
@@ -217,23 +233,41 @@ def execute_and_export_notebooks(
217233
output_md: Whether to save the executed notebooks as markdown
218234
only_out_of_date: Only re-execute and re-export notebooks whose output files
219235
are out of date.
236+
n_workers: If set to 1, do not use parallelization. If set to `None` (the detault),
237+
`multiprocessing.Pool()` will be used, which uses the number of processors as
238+
a default. Otherwise, this argument is passed to
239+
`multiprocessing.Pool(n_workers)` to execute notebooks in parallel on this many
240+
worker processes..
220241
"""
221242
reporoot = get_git_root()
222243
nb_rel_paths = get_nb_rel_paths(sourceroot=reporoot / 'qualtran')
223244
nb_rel_paths += get_nb_rel_paths(sourceroot=reporoot / 'tutorials')
245+
random.shuffle(nb_rel_paths)
246+
print(f"Found {len(nb_rel_paths)} notebooks.")
224247
func = _NotebookRunClosure(
225248
reporoot=reporoot,
226249
output_nbs=output_nbs,
227250
output_md=output_md,
228251
only_out_of_date=only_out_of_date,
229252
)
230-
with multiprocessing.Pool() as pool:
231-
results = pool.starmap(func, nb_rel_paths)
232-
bad_nbs = [nbname for nbname, err in results if err is not None]
253+
if n_workers == 1:
254+
print("(Not using multiprocessing, n_workers=1)")
255+
results = [func(nb_rel_path, sourceroot) for nb_rel_path, sourceroot in nb_rel_paths]
256+
else:
257+
print(f"Multiprocessing with {n_workers=}")
258+
with multiprocessing.Pool(n_workers, maxtasksperchild=1) as pool:
259+
results = pool.starmap(func, nb_rel_paths)
260+
assert results
261+
bad_nbs = [result.nb_in for result in results if result.err is not None]
233262

234263
if len(bad_nbs) > 0:
235264
print()
236265
print("Errors in notebooks:")
237266
for nb in bad_nbs:
238267
print(' ', nb)
239268
sys.exit(1)
269+
270+
duration_nbs = sorted(results, key=lambda r: r.duration_s, reverse=True)
271+
print("Slowest 10 notebooks:")
272+
for result in duration_nbs[:10]:
273+
print(f'{result.duration_s:5.2f}s {result.nb_in}')

dev_tools/qualtran_dev_tools/notebook_specs.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,24 @@
368368
],
369369
directory=f'{SOURCE_DIR}/bloqs/chemistry/trotter/hubbard',
370370
),
371+
NotebookSpecV2(
372+
title='Qubitized Hubbard',
373+
module=qualtran.bloqs.chemistry.hubbard_model.qubitization,
374+
path_stem='hubbard_model',
375+
bloq_specs=[
376+
qualtran.bloqs.chemistry.hubbard_model.qubitization.select_hubbard._SELECT_HUBBARD_DOC,
377+
qualtran.bloqs.chemistry.hubbard_model.qubitization.prepare_hubbard._PREPARE_HUBBARD,
378+
],
379+
),
380+
NotebookSpecV2(
381+
title='Qubitized Hubbard Select',
382+
module=qualtran.bloqs.chemistry.hubbard_model.qubitization.select_hubbard,
383+
bloq_specs=[
384+
qualtran.bloqs.chemistry.hubbard_model.qubitization.select_hubbard._SELECT_HUBBARD_DOC,
385+
qualtran.bloqs.chemistry.hubbard_model.qubitization.select_hubbard._HUBBARD_MAJORANNA_OPERATOR_DOC,
386+
qualtran.bloqs.chemistry.hubbard_model.qubitization.select_hubbard._HUBBARD_SPIN_UP_Z_DOC,
387+
],
388+
),
371389
NotebookSpecV2(
372390
title='Givens Rotations',
373391
module=qualtran.bloqs.chemistry.quad_fermion.givens_bloq,
@@ -930,15 +948,6 @@
930948
qualtran.bloqs.state_preparation.prepare_uniform_superposition._PREP_UNIFORM_DOC
931949
],
932950
),
933-
NotebookSpecV2(
934-
title='Qubitized Hubbard Model',
935-
module=qualtran.bloqs.chemistry.hubbard_model.qubitization,
936-
path_stem='hubbard_model',
937-
bloq_specs=[
938-
qualtran.bloqs.chemistry.hubbard_model.qubitization.select_hubbard._SELECT_HUBBARD,
939-
qualtran.bloqs.chemistry.hubbard_model.qubitization.prepare_hubbard._PREPARE_HUBBARD,
940-
],
941-
),
942951
NotebookSpecV2(
943952
title='Apply to Lth Target',
944953
module=qualtran.bloqs.multiplexers.apply_gate_to_lth_target,

dev_tools/requirements/deps/runtime.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
attrs>=23.2.0
33
cachetools>=5.3
44
networkx
5-
numpy~=1.26
5+
numpy>=1.26,<3.0
66
sympy
7-
cirq-core>=1.4.0,<1.6
7+
cirq-core~=1.4
88
fxpmath
99
galois
1010

@@ -46,4 +46,7 @@ protobuf
4646
# typing
4747
typing_extensions>=4.10.0
4848

49+
# rotation_synthesis
50+
mpmath
51+
4952
# Note: use `pipreqs` to generate a list of dependencies based on the imports in our files.

0 commit comments

Comments
 (0)