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

isel issue with cupy #276

Open
miscop1n opened this issue Jan 13, 2025 · 0 comments
Open

isel issue with cupy #276

miscop1n opened this issue Jan 13, 2025 · 0 comments
Labels

Comments

@miscop1n
Copy link

What happened?

I've been using MPSs with cupy as backend and, since v1.9, I get a bug when using the measure method. I believe it is due to the changes in isel, because of the new "r" value for selectors as cupy apparently cannot compare cupy.array and string.
I think that changing the order of the tests in isel to have this case in the else part could do the trick.
Also worth noting that afaik this only happens with cupy, with other backends such as jax, torch, numpy all goes well.

What did you expect to happen?

I was trying to measure an MPS and this uses isel. I expected it to keep working as before.

Minimal Complete Verifiable Example

import quimb.tensor as qtn
import numpy as np
import cupy
import autoray

from functools import partial

to_cupy = lambda x: cupy.array(x, dtype=cupy.complex64)

def cupy_choice(a, p=None):
    return partial(cupy.random.choice, size=1)(a, p=p)[0]

autoray.register_function(
    backend="cupy",
    name="random.choice",
    fn=cupy_choice
)

def gen_gates(N=40, x=0.1, depth=10, seed=42):
    """Generate a long range circuit that only slowly builds up entanglement.

    Parameters
    ----------
    N : int, optional
        The number of qubits.
    x : float, optional
        The average angle magnitude of U3 rotations.
    depth : int, optional
        The number of fully entangling gate layers.
    seed : int, optional
        A random seed.

    Yields
    ------
    qtn.Gate
    """

    rng = np.random.default_rng(seed)
    qubits = list(range(N))

    for _ in range(depth):
        # random small single qubit rotations
        for q in qubits:
            yield qtn.Gate("U3", params=rng.normal(scale=x, size=3), qubits=[q])

        # random CZs between arbitrary qubit pairs
        rng.shuffle(qubits)
        for i in range(0, N, 2):
            qa = qubits[i]
            qb = qubits[i + 1]
            yield qtn.Gate("CZ", params=(), qubits=[qa, qb])

circ = qtn.CircuitMPS(N=16, to_backend=to_cupy)
for gate in gen_gates(N=circ.N):
    circ.apply_gate(gate)

psi = circ.psi
m, psi = psi.measure(site=8)

Relevant log output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[15], line 58
     55     circ.apply_gate(gate)
     57 psi = circ.psi
---> 58 m, psi = psi.measure(site=8)

File /home/qit/gpu_env/lib/python3.12/site-packages/quimb/tensor/tensor_1d.py:379, in convert_cur_orthog.<locals>.wrapped(self, cur_orthog, info, *args, **kwargs)
    376 @functools.wraps(fn)
    377 def wrapped(self, *args, cur_orthog=None, info=None, **kwargs):
    378     info = parse_cur_orthog(cur_orthog, info)
--> 379     return fn(self, *args, info=info, **kwargs)

File /home/qit/gpu_env/lib/python3.12/site-packages/quimb/tensor/tensor_1d.py:3569, in MatrixProductState.measure(self, site, remove, outcome, renorm, info, get, inplace)
   3566     return outcome
   3568 # project the outcome and renormalize
-> 3569 t.isel_({ind: outcome})
   3571 if renorm:
   3572     t.modify(data=t.data / p[outcome] ** 0.5)

File /home/qit/gpu_env/lib/python3.12/site-packages/quimb/tensor/tensor_core.py:1667, in Tensor.isel(self, selectors, inplace)
   1665     new_inds.append(ix)
   1666     data_loc.append(sel)
-> 1667 elif sel == "r":
   1668     # eagerly remove any 'random' selections
   1669     T.rand_reduce_(ix)
   1670 else:
   1671     # index will be removed by selecting a specific index

File cupy/_core/core.pyx:1217, in cupy._core.core._ndarray_base.__richcmp__()

TypeError: operand type(s) all returned NotImplemented from __array_ufunc__(<ufunc 'equal'>, '__call__', array(0), 'r'): 'ndarray', 'str'

Anything else we need to know?

No response

Environment

I came across it on v1.10.0 but according to the changelog it dates back to v1.9.0

@miscop1n miscop1n added the bug label Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant