catalyst icon indicating copy to clipboard operation
catalyst copied to clipboard

Internal issue with `DataView` iterator increment operator when size along at least one axis is zero

Open joeycarter opened this issue 7 months ago • 0 comments
trafficstars

We discovered this issue with the DataView iterator increment operator when putting together #1598:

https://github.com/PennyLaneAI/catalyst/blob/46d839e57b09b125a7baf25a02b19d7268901426/runtime/include/DataView.hpp#L56-L72

See the comment here for more details.

In short, when the size along at least one axis of the data view is zero, there's an issue with the multidimensional indexing computation that results in the loc variable increasing indefinitely until the program segfaults. Specifically, the issue is that view.sizes[idx] == 0 with idx == 1 on the first iteration, resulting in an underflow error when subtracting 1 from it (since view.sizes is of type size_t). We added an assert in #1598 to ensure that the iteration terminates before an underflow error occurs, but the original issue with the multidimensional indexing should be fixed properly.


As far as we are aware, there are no user-facing or internal functions that will trigger this error. To reproduce it, you can modify the NullQubit::Sample() function to the following

void Sample(DataView<double, 2> &samples, size_t)
{
    std::fill(samples.begin(), samples.end(), 0.0);
}

and execute the following workload:

import pennylane as qml

@qml.qjit
@qml.qnode(qml.device("null.qubit", wires=0, shots=10))
def circuit():
    return qml.sample()

joeycarter avatar Apr 07 '25 22:04 joeycarter