Skip to content

Keep up with quimb-1.5.0#6087

Merged
pavoljuhas merged 7 commits intoquantumlib:masterfrom
pavoljuhas:adjust-for-quimb-1.5.0
May 6, 2023
Merged

Keep up with quimb-1.5.0#6087
pavoljuhas merged 7 commits intoquantumlib:masterfrom
pavoljuhas:adjust-for-quimb-1.5.0

Conversation

@pavoljuhas
Copy link
Copy Markdown
Collaborator

As of quimb-1.5.0 TensorNetwork.contract(inplace=True) returns
TensorNetwork instead of scalar. Convert to scalar if needed.

As of quimb-1.5.0 `TensorNetwork.contract(inplace=True)` returns
TensorNetwork instead of scalar.  Convert to scalar if needed.
@pavoljuhas pavoljuhas requested review from a team, cduck and vtomole as code owners May 4, 2023 22:10
@pavoljuhas pavoljuhas requested a review from dstrain115 May 4, 2023 22:10
Copy link
Copy Markdown
Collaborator

@vtomole vtomole left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

pavoljuhas added 4 commits May 5, 2023 14:08
The test should actually pass in `dev_tools/notebooks/notebook_test.py`
which tests with the head version of Cirq.

This reverts commit 3817f54.
When `dev_tools/notebooks/notebook_test.py` is run from the CI
job `Notebook Tests against PR` it is executed with check/pytest
which adjusts PYTHONPATH to use the work-tree sources of Cirq.

Rename `test_notebooks_against_released_cirq` --> `test_notebooks_against_cirq_head`.

Note: Notebooks are tested with released Cirq in isolated_notebook_test.py.
The `test_notebooks_against_cirq_head` uses a temporary directory for
notebook installed packages.  Put this directory as last to PYTHONPATH
so when some notebooks pip-install Cirq, the working sources of Cirq
are still loaded first.

This fixes the test of `Contract-a-Grid-Circuit.ipynb` which needs an
unreleased Cirq update.
@pavoljuhas
Copy link
Copy Markdown
Collaborator Author

pavoljuhas commented May 5, 2023

@vtomole - actually the original CI failure was for a notebook tested with cirq head so it should have the fix in this PR.
This was caused by other notebook pip-installing released cirq; this interference should be resolved by 2f6b1a0.

The isolated_notebook_test.py tests w/r to the released cirq so I have disabled the affected notebook there.

PS: can you PTAL again?

raise MemoryError(f"We estimate that this contraction will take too much RAM! {ram_gb} GB")
e_val = tn.contract(inplace=True)
if isinstance(e_val, qtn.TensorNetwork):
e_val = e_val.item()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT, the contraction above will always produce a scalar, so why not call item unconditionally?

e_val = tn.contract(inplace=True).item()

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With quimb-1.4.2 tn.contract() may return a standard float which does not have the item method.
We could do e_val = np.asarray(tn.contract(...)).item(), but that is IMO harder to parse for humans than above. :)

We could also require quimb==1.5, but I feel that is a bit imposing on anyone installing cirq.

Comment thread dev_tools/notebooks/notebook_test.py Outdated
def env_with_temporary_pip_target():
"""Setup system environment that tells pip to install packages to a temporary directory."""
with tempfile.TemporaryDirectory(suffix='-notebook-site-packages') as tmpdirname:
# Note: We need to append the PYTHONPATH here so that development Cirq,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: s/append/prepend/

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack. This test is run with check/pytest which sets PYTHONPATH to use the work-tree sources.
The line below appends tmpdirname to the PYTHONPATH.
When some notebook executes pip install cirq, that stable version would appear in PYTHONPATH after the worktree sources (which we want to use in this test).

Let me reword for clarity.

# Note: We need to append the PYTHONPATH here so that development Cirq,
# if configured by dev_tools/pypath, would remain the active Cirq even
# if some notebook executes `!pip install cirq`
pythonpath = (
f'{os.environ["PYTHONPATH"]}{os.pathsep}{tmpdirname}'
if 'PYTHONPATH' in os.environ
else tmpdirname
)

@pavoljuhas pavoljuhas merged commit dc23f87 into quantumlib:master May 6, 2023
@pavoljuhas pavoljuhas deleted the adjust-for-quimb-1.5.0 branch May 6, 2023 01:18
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
As of quimb-1.5.0 `TensorNetwork.contract(inplace=True)` returns
TensorNetwork instead of scalar.  Convert to scalar if needed.

Also correct CI issues that showed for testing notebooks w/r to the cirq head here:

* Fix misleading unit test name by renaming
  `test_notebooks_against_released_cirq` --> `test_notebooks_against_cirq_head`.
  Note: Notebooks are tested with released Cirq in isolated_notebook_test.py.

* Append to PYTHONPATH when tested notebooks pip-install any packages.
  Prefer use of development Cirq sources in `test_notebooks_against_cirq_head`.

* Disable isolated CI test of Contract-a-Grid-Circuit.ipynb
  Requires next cirq release.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants