Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep up with quimb-1.5.0 #6087

Merged
merged 7 commits into from
May 6, 2023

Conversation

pavoljuhas
Copy link
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, vtomole and cduck as code owners May 4, 2023 22:10
@pavoljuhas pavoljuhas requested a review from dstrain115 May 4, 2023 22:10
Copy link
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
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?

@@ -177,6 +177,8 @@ def tensor_expectation_value(
if ram_gb > max_ram_gb:
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
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
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.

@@ -73,8 +73,11 @@ def require_packages_not_changed():
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
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
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