quantumlib/Qualtran

Classical simulation doesn't work for registers with non-empty shape and dtype != QUInt

Closed this issue · 1 comments

The following sample code fails with an error:

import attrs
from qualtran import QInt, Bloq, Signature, Register

@attrs.frozen
class TestMultiDimensionalReg(Bloq):
    n: int

    @property
    def signature(self):
        return Signature([Register('x', QInt(32), shape=(self.n,))])


    def on_classical_vals(self, x):
        print(x)
        return {'x': x}

bloq = TestMultiDimensionalReg(10)
bloq.call_classically(x=[-1] * 10)

Issue is due to hardcoded casts to unsiged types np.uints in

if reg.bitsize <= 8:
dtype: Type = np.uint8
elif reg.bitsize <= 16:
dtype = np.uint16
elif reg.bitsize <= 32:
dtype = np.uint32
elif reg.bitsize <= 64:
dtype = np.uint64
else:
raise NotImplementedError(

I suspect those casts harken back to the time when there weren't data types and the classical simulation only did unsigned integers. This is used to initialize a multidimensional numpy array to contain the values. The data type of this numpy array should probably delegate to the QDType