Skip to content

[Compatibility Issue] np.einsum_path contraction tuple format change in numpy >= 2.4 #1870

Description

@xyf5432

Affected File

src/qibo/quantum_info/quantum_networks.py:1023-1048

Current Code

_, contracrtion_list = np.einsum_path(
    subscripts, *tensors, optimize=False, einsum_call=True
)

inds, idx_rm, einsum_str, _, _ = contracrtion_list[0]  # 5-element unpack
input_str, results_index = einsum_str.split("->")
inputs = input_str.split(",")

for ind in idx_rm:   # line 1033 — idx_rm actively used
    ...

Root Cause

In numpy 2.4, the contraction tuples returned by einsum_path were reduced from 5 elements to 3 elements (einsumfunc.py:874):

numpy version Contraction tuple Count
≤ 2.3.3 (contract_inds, idx_removed, einsum_str, remaining, do_blas) 5
≥ 2.4.0 (contract_inds, einsum_str, remaining) 3

The idx_removed and do_blas fields were removed. Line 1027 unpacks 5 elements → crash:

ValueError: not enough values to unpack (expected 5, got 3)

Impact

  • Severity: High — runtime crash on numpy >= 2.4.0
  • idx_rm (i.e., idx_removed) is used in business logic at line 1033 and cannot simply be discarded

Solution

Inspect the tuple length rather than parsing the numpy version string. This is robust across numpy versions, forks, and backports.

_, contraction_list = np.einsum_path(
    subscripts, *tensors, optimize=False, einsum_call=True
)

contraction = contraction_list[0]

if len(contraction) == 5:
    # numpy <= 2.3.x: (contract_inds, idx_removed, einsum_str, remaining, do_blas)
    inds, idx_rm, einsum_str, _, _ = contraction
elif len(contraction) == 3:
    # numpy >= 2.4.x: (contract_inds, einsum_str, remaining)
    inds, einsum_str, _remaining = contraction
    # idx_rm: indices contracted away in this step
    # = indices appearing in the LHS of einsum_str but not in the RHS
    input_str, results_index = einsum_str.split("->")
    idx_rm = set(input_str.replace(",", "")) - set(results_index)
else:
    raise ValueError(
        f"Unexpected einsum_path contraction tuple length: "
        f"{len(contraction)} (expected 3 or 5)"
    )

input_str, results_index = einsum_str.split("->")
inputs = input_str.split(",")

# ... rest of the code unchanged ...

References

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions