BitBasis.jl icon indicating copy to clipboard operation
BitBasis.jl copied to clipboard

Can create malformed `DitStr`'s

Open jlbosse opened this issue 3 years ago • 2 comments

When explicitly stating the type parameter N of a DitStr it is possible to form a malformed DitStr where Int(dit_str) > D^N - 1. See the following example:

using BitBasis, Yao

julia> d = DitStr{2,2}(5) 
101 ₍₂₎

julia> length(d)
2

julia> product_state(d)
ERROR: BoundsError: ...

I am not sure if this will ever cause problems, but I am happy to open a PR with a consistency check in the inner constructor of DitStr.

jlbosse avatar Nov 22 '22 14:11 jlbosse

Thanks for the issue. I knew this one. I wanted to make some change but I am also worried about it may cause performance issue if we make any type of check since it is such an elementary type. On GPU, throwing an error is even not allowed. It also make sense if we do not make any check because our floating point numbers and integers never throw an error when the number overflows.

But I agree if we can add a checker for certain interface functions like product_state to prevent malformed user input.

GiggleLiu avatar Nov 22 '22 19:11 GiggleLiu

FYI DitStr is technically a "ieee-like integer" so it overflows instead of an error.

Roger-luo avatar Nov 24 '22 01:11 Roger-luo