Skip to content

Commit

Permalink
Avoid DivisionByZero error when TensorNetwork simplifies to a scalar (#…
Browse files Browse the repository at this point in the history
…6586)

Problem: Python 3.12 requires quimb-1.8.0, but quimb-1.8.0 cannot
evaluate path-info for TensorNetwork consisting of a scalar Tensor.

Solution: Skip path-info evaluation for scalar TensorNetwork.
Path-info is used for RAM estimation only which is not a problem
for scalar TensorNetwork-s.

This fixes unit test failure for
cirq-core/cirq/contrib/quimb/grid_circuits_test.py::test_tensor_expectation_value

Ref: jcmgray/quimb#231
  • Loading branch information
pavoljuhas authored May 7, 2024
1 parent 69e3de1 commit bfba965
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 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,15 @@ 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
# TODO(#6586): revert when our minimum quimb version has bugfix for quimb#231
# 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
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

0 comments on commit bfba965

Please sign in to comment.