Skip to content

Sampling with backend fails for large qudit/qubit systems due to integer overflow #41

@WeiguoMa

Description

@WeiguoMa

Issue Description

When sampling (especially with the JAX backend), large qudit or qubit systems fail due to integer overflow in the internal conversion from bitstrings to integer keys.
• For qudits (e.g. d=3), computing d^j quickly exceeds the int32 range (default in JAX) and even int64 when n is large.
• For qubits (e.g. L=100), using format="count_dict_bin" raises an OverflowError because the code attempts to fold a 100-bit string into a single Python integer (2**99 scale), which NumPy/JAX cannot represent as a standard C long.

I think it is related to the complex mutual conversion between sample/count int/tuple/bin.

Expected behavior:
Sampling should work for large system sizes without crashing, ideally by returning results keyed by strings or tuples instead of oversized integers.

How to Reproduce

import tensorcircuit as tc

L = 100
c = tc.Circuit(L)
c.h(0)
c.cnot([i for i in range(L - 1)], [i + 1 for i in range(L - 1)])
results = c.sample(
    allow_state=True, batch=1024, format="count_dict_bin", jittable=False
)

Error Output

File .../jax/_src/numpy/array_constructors.py", line 244, in array
    out = np.asarray(object, dtype=dtype)
OverflowError: Python int too large to convert to C long

Environment Context

TensorNetwork version: 0.4.6
Cotengra is not installed
TensorFlow is not installed
Jax version: 0.7.0
Jax installation doesn't support GPU
JaxLib version: 0.7.0
PyTorch version: 2.6.0
PyTorch GPU support: False
PyTorch GPUs: []
Cupy is not installed
Qiskit is not installed
Cirq is not installed
TensorCircuit version 1.3.0

tc.version == '1.3.0'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions