From 3658abe455ac824f0c09a592c5452659e1174bd8 Mon Sep 17 00:00:00 2001 From: Hao Zhang Date: Sun, 19 Nov 2023 04:45:29 +0800 Subject: [PATCH] Fix imaginary part discarding warning in `to` function. --- tat/tensor.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tat/tensor.py b/tat/tensor.py index eec49f92f..72691ec26 100644 --- a/tat/tensor.py +++ b/tat/tensor.py @@ -422,12 +422,16 @@ def to(self: Tensor, new_type: typing.Any) -> Tensor: new_type = torch.complex64 elif new_type in ["complex128", "complex", "Z"]: new_type = torch.complex128 + if self.dtype.is_complex and new_type.is_floating_point: + data = self.data.real.to(new_type) + else: + data = self.data.to(new_type) return Tensor( names=self.names, edges=self.edges, fermion=self.fermion, dtypes=self.dtypes, - data=self.data.to(new_type), + data=data, mask=self.mask, )