ITensors.jl
ITensors.jl copied to clipboard
[ITensors] [ENHANCEMENT] ITensors.contract!(a, b, c, 1, 1) promotion to the type of a
Is your feature request related to a problem? Please describe.
ITensors.contract!(a, b, c, 1, 1) throws an error if promoted tensor element type of a and b ("greater" type) does not match tensor element type of a. For example, the code below
indices = [Index(2), Index(3), Index(4)]
a = randomITensor(type_1, indices[1])
b = randomITensor(type_2, indices)
c = randomITensor(type_3, indices[2:3])
ITensors.contract!(a, b, c, 1, 1)
does not work if a promoted type of type_2 and type_3 does not match type_1. For example, it would not work if type_1=Float64 and type_2=Float32, type_3=Float16. It would also not work if type_3=Float32. It would only work if one or both of type_2 and type_3 is of Float64 type.
Describe the solution you'd like
Rather than promoting the result of contraction of b and c to the common type (I believe it is done natively by Julia under the hood), promote it to the type of a if possible (may not be possible, e.g., if a is real, while b and c are complex).
Describe alternatives you've considered
Keep types consistent with the signature of ITensors.contract!(a, b, c, alpha, beta).
Thanks for the report @ArtemStrashko, I agree we should be handling that promotion better using information from the buffer tensor.