pythonpytorchcastingtype-conversioncomplex-numbers

How to cast a tensor to `complex` type in PyTorch?


I want to do some quantum mechanics calculations with PyTorch, where the quantities are sometimes complex. I would like to know how can I cast an existing real tensor to complex type.


Solution

  • PyTorch does have complex number support.

    Try this:

    import torch
    
    a = torch.tensor([1.0, 2.0], dtype=torch.double)
    b = a.type(torch.complex64)