Skip to content

Avoid DivisionByZero error when TensorNetwork simplifies to a scalar #6586

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

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cirq-core/cirq/contrib/quimb/state_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,14 @@ def tensor_expectation_value(
)
else:
tn.rank_simplify(inplace=True)
path_info = tn.contract(get='path-info')
ram_gb = path_info.largest_intermediate * 128 / 8 / 1024 / 1024 / 1024
# Skip path-info evaluation when TensorNetwork consists of scalar Tensors.
# Avoid bug in quimb-1.8.0.
# Ref: https://github.com/jcmgray/quimb/issues/231
if tn.ind_map:
path_info = tn.contract(get='path-info')
ram_gb = path_info.largest_intermediate * 128 / 8 / 1024 / 1024 / 1024
else:
ram_gb = 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we raise an error or log a warning?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think it is needed. The error happens for a trivial TensorNetwork([Tensor()]) for which the memory requirements will be tiny.
The bug was BTW already fixed upstream, but I think we should still merge this so we support the current quimb-1.8.

Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe add a TODO to remove this when we upgrade quimb ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, done.

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)
Expand Down