Temporal PyTorch Complex Tensor Class

0.4.4 · maintenance · verified Thu Apr 16

torch-complex is a Python library that provides a custom `ComplexTensor` class and related functional operations for PyTorch. It serves as a temporal solution to enable complex-valued tensor computations in PyTorch, developed primarily because PyTorch historically lacked comprehensive native support for complex tensors. The project's stated goal is to be superseded and eventually 'thrown away' once PyTorch's native complex tensor capabilities are fully mature and performant. The current version is 0.4.4, with a focused release cadence driven by specific needs for complex tensor operations.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `ComplexTensor` from NumPy arrays, perform basic arithmetic and functional operations (like matrix multiplication), and move the tensor to a CUDA device if available.

import numpy as np
import torch
from torch_complex.tensor import ComplexTensor
import torch_complex.functional as F

# Create ComplexTensor from real and imaginary parts
real_part = np.random.randn(3, 10, 10)
imag_part = np.random.randn(3, 10, 10)
x = ComplexTensor(real_part, imag_part)

# Perform basic mathematical operations
y = x + x
z = F.matmul(x, x) # Equivalent to x @ x
w = x.conj()

print(f"Original ComplexTensor shape: {x.shape}")
print(f"Result of addition (y) shape: {y.shape}")
print(f"Result of matrix multiplication (z) shape: {z.shape}")
print(f"Conjugate (w) shape: {w.shape}")

# Move to CUDA if available
if torch.cuda.is_available():
    x_cuda = x.cuda()
    print(f"ComplexTensor moved to CUDA: {x_cuda.device}")
else:
    print("CUDA not available, running on CPU.")

view raw JSON →