Cytnx icon indicating copy to clipboard operation
Cytnx copied to clipboard

Unexpected dtype returned when operating with Float32 / ComplexFloat32 tensors and numpy scalars

Open Chan-Sing-Hong opened this issue 2 months ago • 3 comments

Description Unexpected dtype are returned when performing operations involving Float, ComplexFloat Tensors and numpy.float32, numpy.complex64.

Expected Behavior cytnx.Type.Float / numpy.float32 -> cytnx.Float cytnx.Type.ComplexFloat / numpy.float32 -> cytnx.Type.ComplexFloat cytnx.Type.ComplexFloat / numpy.complex64 -> cytnx.Type.ComplexFloat

Actual Behavior However, all of the above cases currently return ComplexDouble (Complex Float64).

A notebook demonstrating the issue is attached: Float32_issue.ipynb

Chan-Sing-Hong avatar Oct 08 '25 02:10 Chan-Sing-Hong

Thank you for the report and the helpful notebook. This is an issue on the python side: numpy objects are converted to python-specific types (ComplexDouble, Double, Int, Bool) before they are handed to Cytnx vie pybind. Then, arithmetic operations result in an output type that corresponds to the higher precision, which is then the Python-native type.

I wrote explicit pybindings for numpy scalars in #699. This should solve the issue in most cases (and covers all cases in the example provided).

manuschneider avatar Nov 01 '25 09:11 manuschneider

The following code is copied from the notebook for easier reference.

A = cytnx.ones([2,2,2,2]).astype(cytnx.Type.ComplexFloat)
B = numpy.float32(1)
C = A / B

print(A.dtype_str())
print(B.dtype)
print(C.dtype_str())

output:

Complex Float (Complex Float32)
float32
Complex Double (Complex Float64)

IvanaGyro avatar Nov 09 '25 07:11 IvanaGyro

Is operating with numpy scalar a feature we want to support?

IvanaGyro avatar Nov 09 '25 07:11 IvanaGyro

All cases mentioned here are fixed by #699 However, there are still some cases that do not work yet and have to be fixed. In particular, an operation between a numpy object and a UniTensor object does not work if the numpy object is on the left. For example

A = cytnx.ones([2,2,2,2]).astype(cytnx.Type.ComplexFloat)
B = numpy.float32(1)
C = A * B  # works correctly
D = B * A  # does not work correctly

manuschneider avatar Nov 28 '25 00:11 manuschneider