Skip to content

Commit ceb9432

Browse files
pavoljuhasmhucka
andauthored
Fix failure of qvm_stabilizer_example.ipynb with numpy-2.4.0 (#7813)
- Avoid conversion of a one-element numpy array to float which throws TypeError in numpy-2.4.0 (was a DeprecationWarning before). - Also run notebook tests with `PIP_CONFIG_FILE=/dev/null` environment. Notebook tests run in a temporary virtual environment to simulate generic colab experience. Here we make them more isolated from local pip settings. --------- Co-authored-by: Michael Hucka <mhucka@google.com>
1 parent 0ba2dbc commit ceb9432

3 files changed

Lines changed: 21 additions & 13 deletions

File tree

dev_tools/notebooks/isolated_notebook_test.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,10 @@ def _rewrite_and_run_notebook(notebook_path, cloned_env):
154154
check=False,
155155
cwd=notebook_env,
156156
capture_output=True,
157-
# important to get rid of PYTHONPATH specifically, which contains
158-
# the Cirq repo path due to check/pytest
159-
env={},
157+
# Important to get rid of PYTHONPATH specifically, which contains
158+
# the Cirq repo path due to check/pytest. Also isolate the execution
159+
# from pip settings in local configuration files or environment.
160+
env={'PIP_CONFIG_FILE': '/dev/null'},
160161
)
161162

162163
if result.returncode != 0:

dev_tools/notebooks/notebook_test.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ def require_packages_not_changed() -> Iterator[None]:
7373

7474
@pytest.fixture
7575
def env_with_temporary_pip_target() -> Iterator[dict[str, str]]:
76-
"""Setup system environment that tells pip to install packages to a temporary directory."""
76+
"""Set up system environment that tells pip to install packages to a temporary directory.
77+
78+
Also isolate the run from local pip settings in configuration files or environment.
79+
"""
7780
with tempfile.TemporaryDirectory(suffix='-notebook-site-packages') as tmpdirname:
7881
# Note: We need to append tmpdirname to the PYTHONPATH, because PYTHONPATH may
7982
# already point to the development sources of Cirq (as happens with check/pytest).
@@ -84,7 +87,12 @@ def env_with_temporary_pip_target() -> Iterator[dict[str, str]]:
8487
if 'PYTHONPATH' in os.environ
8588
else tmpdirname
8689
)
87-
env = {**os.environ, 'PYTHONPATH': pythonpath, 'PIP_TARGET': tmpdirname}
90+
env = {
91+
**os.environ,
92+
'PYTHONPATH': pythonpath,
93+
'PIP_TARGET': tmpdirname,
94+
'PIP_CONFIG_FILE': '/dev/null',
95+
}
8896
yield env
8997

9098

docs/simulate/qvm_stabilizer_example.ipynb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
" import qsimcirq\n",
111111
"\n",
112112
"# Other modules used in this colab\n",
113+
"import numpy as np\n",
113114
"import matplotlib.pyplot as plt\n",
114115
"import time\n",
115116
"import random"
@@ -402,11 +403,9 @@
402403
},
403404
"outputs": [],
404405
"source": [
405-
"meas_map = {qubit_map[measure_qubit]: sum(results.measurements['meas']) / repetitions}\n",
406+
"meas_map = {qubit_map[measure_qubit]: np.mean(results.measurements['meas'])}\n",
406407
"\n",
407-
"data_map = {\n",
408-
" qubit_map[dq]: sum(results.measurements[str(dq)[-1]]) / repetitions for dq in data_qubits\n",
409-
"}\n",
408+
"data_map = {qubit_map[dq]: np.mean(results.measurements[str(dq)[-1]]) for dq in data_qubits}\n",
410409
"\n",
411410
"heatmap = cirq.Heatmap({**meas_map, **data_map})\n",
412411
"heatmap.plot();"
@@ -555,11 +554,11 @@
555554
"# Set to view results from each repetition.\n",
556555
"repetition = 0 # @param {type:\"number\"}\n",
557556
"meas_map = {\n",
558-
" qubit_map[q]: results.measurements[str(q)][repetition] for q in grid_meas_qubits.values()\n",
557+
" qubit_map[q]: results.measurements[str(q)][repetition].item() for q in grid_meas_qubits.values()\n",
559558
"}\n",
560559
"\n",
561560
"data_map = {\n",
562-
" qubit_map[q]: results.measurements[str(q)][repetition] for q in grid_data_qubits.values()\n",
561+
" qubit_map[q]: results.measurements[str(q)][repetition].item() for q in grid_data_qubits.values()\n",
563562
"}\n",
564563
"\n",
565564
"heatmap = cirq.Heatmap({**meas_map, **data_map})\n",
@@ -623,11 +622,11 @@
623622
"# Set to view results from each repetition.\n",
624623
"repetition = 0 # @param {type:\"number\"}\n",
625624
"meas_map = {\n",
626-
" qubit_map[q]: results.measurements[str(q)][repetition] for q in grid_meas_qubits.values()\n",
625+
" qubit_map[q]: results.measurements[str(q)][repetition].item() for q in grid_meas_qubits.values()\n",
627626
"}\n",
628627
"\n",
629628
"data_map = {\n",
630-
" qubit_map[q]: results.measurements[str(q)][repetition] for q in grid_data_qubits.values()\n",
629+
" qubit_map[q]: results.measurements[str(q)][repetition].item() for q in grid_data_qubits.values()\n",
631630
"}\n",
632631
"\n",
633632
"heatmap = cirq.Heatmap({**meas_map, **data_map})\n",

0 commit comments

Comments
 (0)