complextensor


Namecomplextensor JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryA PyTorch-based library for complex tensor operations.
upload_time2024-09-14 05:15:42
maintainerNone
docs_urlNone
authorNone
requires_python>=3.6
licenseNone
keywords complex tensor pytorch machine learning signal processing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# ComplexTensor


![License](https://img.shields.io/github/license/yourusername/ComplexTensor)
![PyPI](https://img.shields.io/pypi/v/complextensor)
![Python Versions](https://img.shields.io/pypi/pyversions/complextensor)
![Build Status](https://img.shields.io/github/actions/workflow/status/yourusername/ComplexTensor/ci.yml?branch=main)


## Table of Contents

- [📦 ComplexTensor](#-complextensor)
  - [Table of Contents](#table-of-contents)
  - [Introduction](#introduction)
  - [Features](#features)
  - [Installation](#installation)
  - [Usage](#usage)
    - [Basic Operations](#basic-operations)
    - [Advanced Functions](#advanced-functions)
    - [FFT Operations](#fft-operations)
    - [Gradient Manipulation](#gradient-manipulation)
  - [API Reference](#api-reference)
  - [Limitations](#limitations)
  - [Contributing](#contributing)
  - [License](#license)
  - [Acknowledgements](#acknowledgements)

## Introduction

**ComplexTensor** is a Python library built on top of [PyTorch](https://pytorch.org/) that provides comprehensive support for complex number operations within neural network models. It extends PyTorch's capabilities by handling complex tensors seamlessly, enabling advanced mathematical operations, custom gradient manipulations, and optimized performance on both CPU and GPU devices.

## Features

- **Arithmetic Operations**: Addition, subtraction, multiplication, and division of complex tensors.
- **Complex Functions**: Implements common complex mathematical functions like exponential (`exp`), logarithm (`log`), sine (`sin`), cosine (`cos`), and tangent (`tan`).
- **FFT Operations**: Native support for Fast Fourier Transform (FFT) and Inverse FFT (IFFT).
- **Autograd Support**: Custom autograd functions supporting higher-order gradients and per-component gradient manipulation.
- **Device Compatibility**: Optimized for both CPU and CUDA (GPU) devices with support for mixed precision.
- **Utility Functions**: Magnitude (`abs`), phase (`angle`), polar conversion (`to_polar`), complex activation functions (`complex_relu`, `complex_sigmoid`).
- **State Management**: Save and load the state of complex tensors for model persistence.

## Installation

You can install **ComplexTensor** via `pip`. Ensure you have [PyTorch](https://pytorch.org/) installed before proceeding.

```bash
pip install complextensor
```

Alternatively, you can clone the repository and install it manually:

```bash
git clone https://github.com/NeoVertex1/ComplexTensor.git
cd ComplexTensor
pip install -e .
```
# Usage
#### Basic Operations

```python
import torch
from complex_tensor import ComplexTensor

# Initialize real and imaginary parts
real = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
imag = torch.tensor([4.0, 5.0, 6.0], requires_grad=True)

# Create ComplexTensor instances
ct1 = ComplexTensor(real, imag)
ct2 = ComplexTensor(real * 2, imag * 2)

# Addition
ct_sum = ct1 + ct2
print(ct_sum)

# Subtraction
ct_diff = ct1 - ct2
print(ct_diff)

# Multiplication
ct_mul = ct1 * ct2
print(ct_mul)

# Division
ct_div = ct1 / ct2
print(ct_div)
```

### Advanced Functions


```python
# Compute exponential
ct_exp = ct1.exp()
print(ct_exp)

# Compute logarithm
ct_log = ct1.log()
print(ct_log)

# Compute sine
ct_sin = ct1.sin()
print(ct_sin)

# Compute cosine
ct_cos = ct1.cos()
print(ct_cos)

# Compute tangent
ct_tan = ct1.tan()
print(ct_tan)

```

### FFT Operations

```python
# Fast Fourier Transform
ct_fft = ct1.fft()
print(ct_fft)

# Inverse Fast Fourier Transform
ct_ifft = ct_fft.ifft()
print(ct_ifft)
```

### Gradient Manipulation

```python
# Apply gradient scaling
ct_scaled = ct1.apply_gradient_manipulation(grad_scale_real=0.5, grad_scale_imag=2.0)

# Example loss computation
loss = ct_scaled.abs().sum()
loss.backward()

print(real.grad)
print(imag.grad)
```



## API Reference

### `ComplexTensor` Class

- **Initialization**
  ```python
  ComplexTensor(real: Tensor, imag: Optional[Tensor] = None)
  ```
  Initializes a complex tensor with real and imaginary parts.

- **Arithmetic Operations**
  - `__add__(self, other: 'ComplexTensor') -> 'ComplexTensor'`
  - `__sub__(self, other: 'ComplexTensor') -> 'ComplexTensor'`
  - `__mul__(self, other: 'ComplexTensor') -> 'ComplexTensor'`
  - `__truediv__(self, other: 'ComplexTensor') -> 'ComplexTensor'`

- **Complex Functions**
  - `exp(self) -> 'ComplexTensor'`
  - `log(self) -> 'ComplexTensor'`
  - `sin(self) -> 'ComplexTensor'`
  - `cos(self) -> 'ComplexTensor'`
  - `tan(self) -> 'ComplexTensor'`
  - `power(self, exponent: Union[float, 'ComplexTensor']) -> 'ComplexTensor'`
  - `__pow__(self, exponent: Union[float, 'ComplexTensor']) -> 'ComplexTensor'`

- **FFT Operations**
  - `fft(self, n: Optional[int] = None, dim: int = -1, norm: Optional[str] = None) -> 'ComplexTensor'`
  - `ifft(self, n: Optional[int] = None, dim: int = -1, norm: Optional[str] = None) -> 'ComplexTensor'`

- **Utility Functions**
  - `conj(self) -> 'ComplexTensor'`
  - `abs(self) -> Tensor`
  - `angle(self) -> Tensor`
  - `to_polar(self) -> Tuple[Tensor, Tensor]`
  - `complex_relu(self) -> 'ComplexTensor'`
  - `complex_sigmoid(self) -> 'ComplexTensor'`
  - `detach(self) -> 'ComplexTensor'`
  - `requires_grad_(self, requires_grad: bool = True) -> 'ComplexTensor'`
  - `to(self, device: torch.device, dtype: Optional[torch.dtype] = None) -> 'ComplexTensor'`

- **State Management**
  - `state_dict(self) -> Dict[str, Tensor]`
  - `load_state_dict(self, state_dict: Dict[str, Tensor]) -> None`

- **Gradient Manipulation**
  - `apply_gradient_manipulation(self, grad_scale_real: float = 1.0, grad_scale_imag: float = 1.0) -> 'ComplexTensor'`

- **Miscellaneous**
  - `__repr__(self) -> str`
  - `__len__(self) -> int`
  - `__getitem__(self, index) -> 'ComplexTensor'`
  - `__setitem__(self, index, value: 'ComplexTensor') -> None`
  - `shape` (property)
  - `dtype` (property)
  - `device` (property)

### `ComplexFunction` Class

- **Forward and Backward Methods**
  Custom autograd functions to handle complex tensor operations, supporting higher-order gradients and per-component gradient manipulation.

## Limitations

While **ComplexTensor** offers a robust set of features for handling complex numbers in PyTorch, there are certain limitations to be aware of:

- **Performance Optimization**: Although device-aware operations are supported, further optimizations can be implemented for specific hardware accelerations.
- **Limited Advanced Functions**: Currently, only a subset of complex functions is implemented. Future versions can include more specialized functions as needed.
- **Interoperability**: Integration with other complex number libraries or frameworks may require additional adapters or compatibility layers.
- **Documentation**: While comprehensive, the API reference can be expanded with more detailed examples and edge case explanations.

## Contributing

Contributions are welcome! Whether it's reporting bugs, suggesting features, or submitting pull requests, your input helps improve the project.

1. **Fork the Repository**

2. **Create a Feature Branch**
   ```bash
   git checkout -b feature/YourFeature
   ```

3. **Commit Your Changes**
   ```bash
   git commit -m "Add YourFeature"
   ```

4. **Push to the Branch**
   ```bash
   git push origin feature/YourFeature
   ```

5. **Open a Pull Request**

## Acknowledgements

- [PyTorch](https://pytorch.org/) for providing the foundational deep learning framework.
- Contributors and the open-source community for their invaluable feedback and support.




# running tests:

for the unit test for the main complex_tensor use this command on the root folder:

```bash
python -m unittest tests.test_complex_tensor
```


## Reporting Issues

If you encounter any bugs or have suggestions for improvements, please open an issue [here](https://github.com/yourusername/ComplexTensor/issues).




## 🚀 **Getting Started**


```python
import torch
import numpy as np
from complextensor import ComplexTensor

def quantum_inspired_superposition(states, amplitudes):
    """
    Create a superposition of quantum-inspired states using ComplexTensor.
    
    Args:
    states (list of ComplexTensor): The basis states to superpose.
    amplitudes (list of ComplexTensor): The amplitudes for each state.
    
    Returns:
    ComplexTensor: The superposed state.
    """
    superposed_state = ComplexTensor(torch.zeros_like(states[0].real), torch.zeros_like(states[0].imag))
    for state, amplitude in zip(states, amplitudes):
        superposed_state += ComplexTensor(amplitude.real * state.real - amplitude.imag * state.imag,
                                          amplitude.real * state.imag + amplitude.imag * state.real)
    return superposed_state

def quantum_inspired_entanglement(state1, state2):
    """
    Create an entangled state from two quantum-inspired states using ComplexTensor.
    
    Args:
    state1, state2 (ComplexTensor): The states to entangle.
    
    Returns:
    ComplexTensor: The entangled state.
    """
    real_kron = torch.kron(state1.real, state2.real) - torch.kron(state1.imag, state2.imag)
    imag_kron = torch.kron(state1.real, state2.imag) + torch.kron(state1.imag, state2.real)
    return ComplexTensor(real_kron, imag_kron)

def measure_state(state):
    """
    Perform a measurement on the quantum-inspired state using ComplexTensor.
    
    Args:
    state (ComplexTensor): The state to measure.
    
    Returns:
    int: The index of the measured basis state.
    """
    probabilities = state.abs() ** 2
    probabilities = probabilities / torch.sum(probabilities)
    probabilities_np = probabilities.cpu().detach().numpy()  # Convert to numpy for choice
    return np.random.choice(len(state.real), p=probabilities_np)

# Example usage with ComplexTensor
if __name__ == "__main__":
    # Define two basis states using ComplexTensor
    state0_real = torch.tensor([1.0, 0.0], dtype=torch.float32)
    state1_real = torch.tensor([0.0, 1.0], dtype=torch.float32)
    state0 = ComplexTensor(state0_real)
    state1 = ComplexTensor(state1_real)

    # Create a superposition
    amplitude_real = torch.tensor([1/torch.sqrt(torch.tensor(2.0)), 0.0], dtype=torch.float32)
    amplitude_imag = torch.tensor([0.0, 1/torch.sqrt(torch.tensor(2.0))], dtype=torch.float32)
    superposed_state = quantum_inspired_superposition(
        [state0, state1],
        [ComplexTensor(amplitude_real[0:1], amplitude_imag[0:1]), ComplexTensor(amplitude_real[1:2], amplitude_imag[1:2])]
    )
    print("Superposed state:", superposed_state)

    # Create an entangled state
    entangled_state = quantum_inspired_entanglement(state0, state1)
    print("Entangled state:", entangled_state)

    # Perform measurements
    for _ in range(5):
        result = measure_state(superposed_state)
        print("Measurement result:", result)

```


This code simulates quantum-inspired operations like **superposition**, **entanglement**, and **measurement**. Here's a breakdown of the quantum-inspired approach:

### Key Functions:
1. **Superposition**: 
   - This creates a linear combination of quantum basis states, where each state is weighted by a complex amplitude. Superposition is a key feature in quantum mechanics that allows a quantum system to exist in multiple states simultaneously.
   
2. **Entanglement**:
   - This creates an entangled state using the **Kronecker product** (or tensor product) of two states. Entanglement is a quantum phenomenon where two qubits become linked, such that the state of one qubit instantaneously influences the state of the other, regardless of distance.

3. **Measurement**:
   - A simulated quantum measurement collapses the superposed state into one of the basis states with a probability proportional to the square of the state's amplitude (following the **Born rule**).

### Example Output:

- **Superposed State**: This creates a superposition of two basis states (|0⟩ and |1⟩), with a real coefficient for |0⟩ and an imaginary coefficient for |1⟩.
  
- **Entangled State**: When state0 and state1 are entangled, this forms a combined quantum system. This entanglement would be represented as:
  \[
  |0⟩ ⊗ |1⟩ = \begin{bmatrix} 0 \\ 1 \\ 0 \\ 0 \end{bmatrix}
  \]
  
- **Measurements**: When you measure the superposed state multiple times, the results should follow a probabilistic distribution based on the square of the absolute values of the coefficients.

### Sample Run (using the code):
```python
Superposed state: [0.70710678+0.j         0.        +0.70710678j]
Entangled state: [0.+0.j 0.+0.j 1.+0.j 0.+0.j]
Measurement result: 0
Measurement result: 1
Measurement result: 0
Measurement result: 0
Measurement result: 1
```

Here, you can see that the superposed state has a 50-50 chance of being in state `|0⟩` or state `|1⟩`, with a phase difference introduced by the complex coefficients.


2. **Advanced Quantum-Inspired Operations**:
   - You can expand this further to include other operations like **quantum gates** (Hadamard, Pauli-X), **quantum Fourier transforms**, or **Grover’s algorithm** in the quantum-inspired framework using the complex tensor system.


more examples:


```python
import torch
from complex_tensor import ComplexTensor

def main():
    # Initialize real and imaginary parts
    real = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
    imag = torch.tensor([4.0, 5.0, 6.0], requires_grad=True)
    
    # Create ComplexTensor instances
    ct1 = ComplexTensor(real, imag)
    ct2 = ComplexTensor(real * 2, imag * 2)
    
    # Perform arithmetic operations
    ct_sum = ct1 + ct2
    print("Sum:", ct_sum)
    
    ct_diff = ct1 - ct2
    print("Difference:", ct_diff)
    
    ct_mul = ct1 * ct2
    print("Product:", ct_mul)
    
    ct_div = ct1 / ct2
    print("Division:", ct_div)
    
    # Compute complex exponential
    ct_exp = ct1.exp()
    print("Exponential:", ct_exp)
    
    # Perform FFT
    ct_fft = ct1.fft()
    print("FFT:", ct_fft)
    
    # Backpropagation example
    loss = ct_sum.abs().sum()
    loss.backward()
    print("Gradients - Real:", real.grad)
    print("Gradients - Imaginary:", imag.grad)

if __name__ == "__main__":
    main()
```

Run the example:

```bash
python examples/example_usage.py
```

---

## 🧪 **Testing**

 Here's a basic example of a test case (`test_complex_tensor.py`):

```python
import unittest
import torch
from src.complextensor.complex_tensor import ComplexTensor, ComplexFunction

class TestComplexFunction(unittest.TestCase):
    def setUp(self):
        self.real = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
        self.imag = torch.tensor([4.0, 5.0, 6.0], requires_grad=True)

    def test_complex_function_forward(self):
        result = ComplexFunction.apply(self.real, self.imag)
        self.assertTrue(torch.is_complex(result))
        self.assertTrue(torch.allclose(result.real, self.real))
        self.assertTrue(torch.allclose(result.imag, self.imag))

    def test_complex_function_backward(self):
        result = ComplexFunction.apply(self.real, self.imag)
        loss = result.abs().sum()
        loss.backward()

        self.assertIsNotNone(self.real.grad)
        self.assertIsNotNone(self.imag.grad)
        self.assertFalse(torch.allclose(self.real.grad, torch.zeros_like(self.real)))
        self.assertFalse(torch.allclose(self.imag.grad, torch.zeros_like(self.imag)))

class TestComplexTensor(unittest.TestCase):
    def setUp(self):
        self.real = torch.tensor([1.0, 2.0, 3.0])
        self.imag = torch.tensor([4.0, 5.0, 6.0])
        self.ct = ComplexTensor(self.real, self.imag)

    def test_initialization(self):
        self.assertTrue(torch.allclose(self.ct.real, self.real))
        self.assertTrue(torch.allclose(self.ct.imag, self.imag))

    def test_forward(self):
        result = self.ct.forward()
        self.assertTrue(torch.is_complex(result))
        self.assertTrue(torch.allclose(result.real, self.real))
        self.assertTrue(torch.allclose(result.imag, self.imag))

    def test_addition(self):
        other = ComplexTensor(torch.tensor([1.0, 1.0, 1.0]), torch.tensor([2.0, 2.0, 2.0]))
        result = self.ct + other
        self.assertTrue(torch.allclose(result.real, self.real + 1))
        self.assertTrue(torch.allclose(result.imag, self.imag + 2))

    def test_subtraction(self):
        other = ComplexTensor(torch.tensor([1.0, 1.0, 1.0]), torch.tensor([2.0, 2.0, 2.0]))
        result = self.ct - other
        self.assertTrue(torch.allclose(result.real, self.real - 1))
        self.assertTrue(torch.allclose(result.imag, self.imag - 2))

    def test_multiplication(self):
        other = ComplexTensor(torch.tensor([2.0, 2.0, 2.0]), torch.tensor([3.0, 3.0, 3.0]))
        result = self.ct * other
        expected_real = self.real * 2 - self.imag * 3
        expected_imag = self.real * 3 + self.imag * 2
        self.assertTrue(torch.allclose(result.real, expected_real))
        self.assertTrue(torch.allclose(result.imag, expected_imag))

    def test_division(self):
        other = ComplexTensor(torch.tensor([2.0, 2.0, 2.0]), torch.tensor([3.0, 3.0, 3.0]))
        result = self.ct / other
        denominator = 4 + 9  # 2^2 + 3^2
        expected_real = (self.real * 2 + self.imag * 3) / denominator
        expected_imag = (self.imag * 2 - self.real * 3) / denominator
        self.assertTrue(torch.allclose(result.real, expected_real))
        self.assertTrue(torch.allclose(result.imag, expected_imag))

    def test_conjugate(self):
        result = self.ct.conj()
        self.assertTrue(torch.allclose(result.real, self.real))
        self.assertTrue(torch.allclose(result.imag, -self.imag))

    def test_abs(self):
        result = self.ct.abs()
        expected = torch.sqrt(self.real**2 + self.imag**2)
        self.assertTrue(torch.allclose(result, expected))

    def test_angle(self):
        result = self.ct.angle()
        expected = torch.atan2(self.imag, self.real)
        self.assertTrue(torch.allclose(result, expected))

    def test_to_polar(self):
        magnitude, phase = self.ct.to_polar()
        expected_magnitude = torch.sqrt(self.real**2 + self.imag**2)
        expected_phase = torch.atan2(self.imag, self.real)
        self.assertTrue(torch.allclose(magnitude, expected_magnitude))
        self.assertTrue(torch.allclose(phase, expected_phase))

    def test_complex_relu(self):
        result = self.ct.complex_relu()
        self.assertTrue(torch.allclose(result.real, torch.relu(self.real)))
        self.assertTrue(torch.allclose(result.imag, torch.relu(self.imag)))

    def test_complex_sigmoid(self):
        result = self.ct.complex_sigmoid()
        self.assertTrue(torch.allclose(result.real, torch.sigmoid(self.real)))
        self.assertTrue(torch.allclose(result.imag, torch.sigmoid(self.imag)))

    def test_exp(self):
        result = self.ct.exp()
        expected_real = torch.exp(self.real) * torch.cos(self.imag)
        expected_imag = torch.exp(self.real) * torch.sin(self.imag)
        self.assertTrue(torch.allclose(result.real, expected_real))
        self.assertTrue(torch.allclose(result.imag, expected_imag))

    def test_log(self):
        result = self.ct.log()
        expected_real = torch.log(self.ct.abs())
        expected_imag = self.ct.angle()
        self.assertTrue(torch.allclose(result.real, expected_real))
        self.assertTrue(torch.allclose(result.imag, expected_imag))

    def test_sin(self):
        result = self.ct.sin()
        expected_real = torch.sin(self.real) * torch.cosh(self.imag)
        expected_imag = torch.cos(self.real) * torch.sinh(self.imag)
        self.assertTrue(torch.allclose(result.real, expected_real))
        self.assertTrue(torch.allclose(result.imag, expected_imag))

    def test_cos(self):
        result = self.ct.cos()
        expected_real = torch.cos(self.real) * torch.cosh(self.imag)
        expected_imag = -torch.sin(self.real) * torch.sinh(self.imag)
        self.assertTrue(torch.allclose(result.real, expected_real))
        self.assertTrue(torch.allclose(result.imag, expected_imag))

    def test_tan(self):
        result = self.ct.tan()
        sin_ct = self.ct.sin()
        cos_ct = self.ct.cos()
        expected_real = (sin_ct.real * cos_ct.real + sin_ct.imag * cos_ct.imag) / (cos_ct.real**2 + cos_ct.imag**2)
        expected_imag = (sin_ct.imag * cos_ct.real - sin_ct.real * cos_ct.imag) / (cos_ct.real**2 + cos_ct.imag**2)
        self.assertTrue(torch.allclose(result.real, expected_real, atol=1e-5))
        self.assertTrue(torch.allclose(result.imag, expected_imag, atol=1e-5))

    def test_power(self):
        exponent = 2
        result = self.ct.power(exponent)
        expected = self.ct * self.ct
        self.assertTrue(torch.allclose(result.real, expected.real))
        self.assertTrue(torch.allclose(result.imag, expected.imag))

    def test_fft(self):
        result = self.ct.fft()
        expected = torch.fft.fft(self.ct.forward())
        self.assertTrue(torch.allclose(result.real, expected.real))
        self.assertTrue(torch.allclose(result.imag, expected.imag))

    def test_ifft(self):
        result = self.ct.ifft()
        expected = torch.fft.ifft(self.ct.forward())
        self.assertTrue(torch.allclose(result.real, expected.real))
        self.assertTrue(torch.allclose(result.imag, expected.imag))

    def test_gradient_computation(self):
        real = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
        imag = torch.tensor([4.0, 5.0, 6.0], requires_grad=True)
        ct = ComplexTensor(real, imag)
        
        output = ct.forward().abs().sum()
        output.backward()
        
        self.assertIsNotNone(real.grad)
        self.assertIsNotNone(imag.grad)
        self.assertFalse(torch.allclose(real.grad, torch.zeros_like(real.grad)))
        self.assertFalse(torch.allclose(imag.grad, torch.zeros_like(imag.grad)))

    def test_to_device(self):
        if torch.cuda.is_available():
            ct_gpu = self.ct.to(torch.device('cuda'))
            self.assertEqual(ct_gpu.real.device.type, 'cuda')
            self.assertEqual(ct_gpu.imag.device.type, 'cuda')

    def test_detach(self):
        ct_detached = self.ct.detach()
        self.assertFalse(ct_detached.real.requires_grad)
        self.assertFalse(ct_detached.imag.requires_grad)

    def test_requires_grad(self):
        self.ct.requires_grad_(True)
        self.assertTrue(self.ct.real.requires_grad)
        self.assertTrue(self.ct.imag.requires_grad)

    def test_complex_function_integration(self):
        real = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
        imag = torch.tensor([4.0, 5.0, 6.0], requires_grad=True)
        ct = ComplexTensor(real, imag)
        
        result = ct.forward()
        self.assertTrue(torch.is_complex(result))
        self.assertTrue(torch.allclose(result.real, real))
        self.assertTrue(torch.allclose(result.imag, imag))

        loss = result.abs().sum()
        loss.backward()

        self.assertIsNotNone(real.grad)
        self.assertIsNotNone(imag.grad)
        self.assertFalse(torch.allclose(real.grad, torch.zeros_like(real)))
        self.assertFalse(torch.allclose(imag.grad, torch.zeros_like(imag)))

if __name__ == '__main__':
    unittest.main()
```

Run the tests:

```bash
python -m unittest discover tests
```

---

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "complextensor",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "complex, tensor, pytorch, machine learning, signal processing",
    "author": null,
    "author_email": "bluecow009 <0xbluecow@protonmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/f4/36/1f0cff8c024825c6a602e90073aa4af7a69562f21443e67c84eb6ec98fd8/complextensor-0.1.0.tar.gz",
    "platform": null,
    "description": "\n# ComplexTensor\n\n\n![License](https://img.shields.io/github/license/yourusername/ComplexTensor)\n![PyPI](https://img.shields.io/pypi/v/complextensor)\n![Python Versions](https://img.shields.io/pypi/pyversions/complextensor)\n![Build Status](https://img.shields.io/github/actions/workflow/status/yourusername/ComplexTensor/ci.yml?branch=main)\n\n\n## Table of Contents\n\n- [\ud83d\udce6 ComplexTensor](#-complextensor)\n  - [Table of Contents](#table-of-contents)\n  - [Introduction](#introduction)\n  - [Features](#features)\n  - [Installation](#installation)\n  - [Usage](#usage)\n    - [Basic Operations](#basic-operations)\n    - [Advanced Functions](#advanced-functions)\n    - [FFT Operations](#fft-operations)\n    - [Gradient Manipulation](#gradient-manipulation)\n  - [API Reference](#api-reference)\n  - [Limitations](#limitations)\n  - [Contributing](#contributing)\n  - [License](#license)\n  - [Acknowledgements](#acknowledgements)\n\n## Introduction\n\n**ComplexTensor** is a Python library built on top of [PyTorch](https://pytorch.org/) that provides comprehensive support for complex number operations within neural network models. It extends PyTorch's capabilities by handling complex tensors seamlessly, enabling advanced mathematical operations, custom gradient manipulations, and optimized performance on both CPU and GPU devices.\n\n## Features\n\n- **Arithmetic Operations**: Addition, subtraction, multiplication, and division of complex tensors.\n- **Complex Functions**: Implements common complex mathematical functions like exponential (`exp`), logarithm (`log`), sine (`sin`), cosine (`cos`), and tangent (`tan`).\n- **FFT Operations**: Native support for Fast Fourier Transform (FFT) and Inverse FFT (IFFT).\n- **Autograd Support**: Custom autograd functions supporting higher-order gradients and per-component gradient manipulation.\n- **Device Compatibility**: Optimized for both CPU and CUDA (GPU) devices with support for mixed precision.\n- **Utility Functions**: Magnitude (`abs`), phase (`angle`), polar conversion (`to_polar`), complex activation functions (`complex_relu`, `complex_sigmoid`).\n- **State Management**: Save and load the state of complex tensors for model persistence.\n\n## Installation\n\nYou can install **ComplexTensor** via `pip`. Ensure you have [PyTorch](https://pytorch.org/) installed before proceeding.\n\n```bash\npip install complextensor\n```\n\nAlternatively, you can clone the repository and install it manually:\n\n```bash\ngit clone https://github.com/NeoVertex1/ComplexTensor.git\ncd ComplexTensor\npip install -e .\n```\n# Usage\n#### Basic Operations\n\n```python\nimport torch\nfrom complex_tensor import ComplexTensor\n\n# Initialize real and imaginary parts\nreal = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)\nimag = torch.tensor([4.0, 5.0, 6.0], requires_grad=True)\n\n# Create ComplexTensor instances\nct1 = ComplexTensor(real, imag)\nct2 = ComplexTensor(real * 2, imag * 2)\n\n# Addition\nct_sum = ct1 + ct2\nprint(ct_sum)\n\n# Subtraction\nct_diff = ct1 - ct2\nprint(ct_diff)\n\n# Multiplication\nct_mul = ct1 * ct2\nprint(ct_mul)\n\n# Division\nct_div = ct1 / ct2\nprint(ct_div)\n```\n\n### Advanced Functions\n\n\n```python\n# Compute exponential\nct_exp = ct1.exp()\nprint(ct_exp)\n\n# Compute logarithm\nct_log = ct1.log()\nprint(ct_log)\n\n# Compute sine\nct_sin = ct1.sin()\nprint(ct_sin)\n\n# Compute cosine\nct_cos = ct1.cos()\nprint(ct_cos)\n\n# Compute tangent\nct_tan = ct1.tan()\nprint(ct_tan)\n\n```\n\n### FFT Operations\n\n```python\n# Fast Fourier Transform\nct_fft = ct1.fft()\nprint(ct_fft)\n\n# Inverse Fast Fourier Transform\nct_ifft = ct_fft.ifft()\nprint(ct_ifft)\n```\n\n### Gradient Manipulation\n\n```python\n# Apply gradient scaling\nct_scaled = ct1.apply_gradient_manipulation(grad_scale_real=0.5, grad_scale_imag=2.0)\n\n# Example loss computation\nloss = ct_scaled.abs().sum()\nloss.backward()\n\nprint(real.grad)\nprint(imag.grad)\n```\n\n\n\n## API Reference\n\n### `ComplexTensor` Class\n\n- **Initialization**\n  ```python\n  ComplexTensor(real: Tensor, imag: Optional[Tensor] = None)\n  ```\n  Initializes a complex tensor with real and imaginary parts.\n\n- **Arithmetic Operations**\n  - `__add__(self, other: 'ComplexTensor') -> 'ComplexTensor'`\n  - `__sub__(self, other: 'ComplexTensor') -> 'ComplexTensor'`\n  - `__mul__(self, other: 'ComplexTensor') -> 'ComplexTensor'`\n  - `__truediv__(self, other: 'ComplexTensor') -> 'ComplexTensor'`\n\n- **Complex Functions**\n  - `exp(self) -> 'ComplexTensor'`\n  - `log(self) -> 'ComplexTensor'`\n  - `sin(self) -> 'ComplexTensor'`\n  - `cos(self) -> 'ComplexTensor'`\n  - `tan(self) -> 'ComplexTensor'`\n  - `power(self, exponent: Union[float, 'ComplexTensor']) -> 'ComplexTensor'`\n  - `__pow__(self, exponent: Union[float, 'ComplexTensor']) -> 'ComplexTensor'`\n\n- **FFT Operations**\n  - `fft(self, n: Optional[int] = None, dim: int = -1, norm: Optional[str] = None) -> 'ComplexTensor'`\n  - `ifft(self, n: Optional[int] = None, dim: int = -1, norm: Optional[str] = None) -> 'ComplexTensor'`\n\n- **Utility Functions**\n  - `conj(self) -> 'ComplexTensor'`\n  - `abs(self) -> Tensor`\n  - `angle(self) -> Tensor`\n  - `to_polar(self) -> Tuple[Tensor, Tensor]`\n  - `complex_relu(self) -> 'ComplexTensor'`\n  - `complex_sigmoid(self) -> 'ComplexTensor'`\n  - `detach(self) -> 'ComplexTensor'`\n  - `requires_grad_(self, requires_grad: bool = True) -> 'ComplexTensor'`\n  - `to(self, device: torch.device, dtype: Optional[torch.dtype] = None) -> 'ComplexTensor'`\n\n- **State Management**\n  - `state_dict(self) -> Dict[str, Tensor]`\n  - `load_state_dict(self, state_dict: Dict[str, Tensor]) -> None`\n\n- **Gradient Manipulation**\n  - `apply_gradient_manipulation(self, grad_scale_real: float = 1.0, grad_scale_imag: float = 1.0) -> 'ComplexTensor'`\n\n- **Miscellaneous**\n  - `__repr__(self) -> str`\n  - `__len__(self) -> int`\n  - `__getitem__(self, index) -> 'ComplexTensor'`\n  - `__setitem__(self, index, value: 'ComplexTensor') -> None`\n  - `shape` (property)\n  - `dtype` (property)\n  - `device` (property)\n\n### `ComplexFunction` Class\n\n- **Forward and Backward Methods**\n  Custom autograd functions to handle complex tensor operations, supporting higher-order gradients and per-component gradient manipulation.\n\n## Limitations\n\nWhile **ComplexTensor** offers a robust set of features for handling complex numbers in PyTorch, there are certain limitations to be aware of:\n\n- **Performance Optimization**: Although device-aware operations are supported, further optimizations can be implemented for specific hardware accelerations.\n- **Limited Advanced Functions**: Currently, only a subset of complex functions is implemented. Future versions can include more specialized functions as needed.\n- **Interoperability**: Integration with other complex number libraries or frameworks may require additional adapters or compatibility layers.\n- **Documentation**: While comprehensive, the API reference can be expanded with more detailed examples and edge case explanations.\n\n## Contributing\n\nContributions are welcome! Whether it's reporting bugs, suggesting features, or submitting pull requests, your input helps improve the project.\n\n1. **Fork the Repository**\n\n2. **Create a Feature Branch**\n   ```bash\n   git checkout -b feature/YourFeature\n   ```\n\n3. **Commit Your Changes**\n   ```bash\n   git commit -m \"Add YourFeature\"\n   ```\n\n4. **Push to the Branch**\n   ```bash\n   git push origin feature/YourFeature\n   ```\n\n5. **Open a Pull Request**\n\n## Acknowledgements\n\n- [PyTorch](https://pytorch.org/) for providing the foundational deep learning framework.\n- Contributors and the open-source community for their invaluable feedback and support.\n\n\n\n\n# running tests:\n\nfor the unit test for the main complex_tensor use this command on the root folder:\n\n```bash\npython -m unittest tests.test_complex_tensor\n```\n\n\n## Reporting Issues\n\nIf you encounter any bugs or have suggestions for improvements, please open an issue [here](https://github.com/yourusername/ComplexTensor/issues).\n\n\n\n\n## \ud83d\ude80 **Getting Started**\n\n\n```python\nimport torch\nimport numpy as np\nfrom complextensor import ComplexTensor\n\ndef quantum_inspired_superposition(states, amplitudes):\n    \"\"\"\n    Create a superposition of quantum-inspired states using ComplexTensor.\n    \n    Args:\n    states (list of ComplexTensor): The basis states to superpose.\n    amplitudes (list of ComplexTensor): The amplitudes for each state.\n    \n    Returns:\n    ComplexTensor: The superposed state.\n    \"\"\"\n    superposed_state = ComplexTensor(torch.zeros_like(states[0].real), torch.zeros_like(states[0].imag))\n    for state, amplitude in zip(states, amplitudes):\n        superposed_state += ComplexTensor(amplitude.real * state.real - amplitude.imag * state.imag,\n                                          amplitude.real * state.imag + amplitude.imag * state.real)\n    return superposed_state\n\ndef quantum_inspired_entanglement(state1, state2):\n    \"\"\"\n    Create an entangled state from two quantum-inspired states using ComplexTensor.\n    \n    Args:\n    state1, state2 (ComplexTensor): The states to entangle.\n    \n    Returns:\n    ComplexTensor: The entangled state.\n    \"\"\"\n    real_kron = torch.kron(state1.real, state2.real) - torch.kron(state1.imag, state2.imag)\n    imag_kron = torch.kron(state1.real, state2.imag) + torch.kron(state1.imag, state2.real)\n    return ComplexTensor(real_kron, imag_kron)\n\ndef measure_state(state):\n    \"\"\"\n    Perform a measurement on the quantum-inspired state using ComplexTensor.\n    \n    Args:\n    state (ComplexTensor): The state to measure.\n    \n    Returns:\n    int: The index of the measured basis state.\n    \"\"\"\n    probabilities = state.abs() ** 2\n    probabilities = probabilities / torch.sum(probabilities)\n    probabilities_np = probabilities.cpu().detach().numpy()  # Convert to numpy for choice\n    return np.random.choice(len(state.real), p=probabilities_np)\n\n# Example usage with ComplexTensor\nif __name__ == \"__main__\":\n    # Define two basis states using ComplexTensor\n    state0_real = torch.tensor([1.0, 0.0], dtype=torch.float32)\n    state1_real = torch.tensor([0.0, 1.0], dtype=torch.float32)\n    state0 = ComplexTensor(state0_real)\n    state1 = ComplexTensor(state1_real)\n\n    # Create a superposition\n    amplitude_real = torch.tensor([1/torch.sqrt(torch.tensor(2.0)), 0.0], dtype=torch.float32)\n    amplitude_imag = torch.tensor([0.0, 1/torch.sqrt(torch.tensor(2.0))], dtype=torch.float32)\n    superposed_state = quantum_inspired_superposition(\n        [state0, state1],\n        [ComplexTensor(amplitude_real[0:1], amplitude_imag[0:1]), ComplexTensor(amplitude_real[1:2], amplitude_imag[1:2])]\n    )\n    print(\"Superposed state:\", superposed_state)\n\n    # Create an entangled state\n    entangled_state = quantum_inspired_entanglement(state0, state1)\n    print(\"Entangled state:\", entangled_state)\n\n    # Perform measurements\n    for _ in range(5):\n        result = measure_state(superposed_state)\n        print(\"Measurement result:\", result)\n\n```\n\n\nThis code simulates quantum-inspired operations like **superposition**, **entanglement**, and **measurement**. Here's a breakdown of the quantum-inspired approach:\n\n### Key Functions:\n1. **Superposition**: \n   - This creates a linear combination of quantum basis states, where each state is weighted by a complex amplitude. Superposition is a key feature in quantum mechanics that allows a quantum system to exist in multiple states simultaneously.\n   \n2. **Entanglement**:\n   - This creates an entangled state using the **Kronecker product** (or tensor product) of two states. Entanglement is a quantum phenomenon where two qubits become linked, such that the state of one qubit instantaneously influences the state of the other, regardless of distance.\n\n3. **Measurement**:\n   - A simulated quantum measurement collapses the superposed state into one of the basis states with a probability proportional to the square of the state's amplitude (following the **Born rule**).\n\n### Example Output:\n\n- **Superposed State**: This creates a superposition of two basis states (|0\u27e9 and |1\u27e9), with a real coefficient for |0\u27e9 and an imaginary coefficient for |1\u27e9.\n  \n- **Entangled State**: When state0 and state1 are entangled, this forms a combined quantum system. This entanglement would be represented as:\n  \\[\n  |0\u27e9 \u2297 |1\u27e9 = \\begin{bmatrix} 0 \\\\ 1 \\\\ 0 \\\\ 0 \\end{bmatrix}\n  \\]\n  \n- **Measurements**: When you measure the superposed state multiple times, the results should follow a probabilistic distribution based on the square of the absolute values of the coefficients.\n\n### Sample Run (using the code):\n```python\nSuperposed state: [0.70710678+0.j         0.        +0.70710678j]\nEntangled state: [0.+0.j 0.+0.j 1.+0.j 0.+0.j]\nMeasurement result: 0\nMeasurement result: 1\nMeasurement result: 0\nMeasurement result: 0\nMeasurement result: 1\n```\n\nHere, you can see that the superposed state has a 50-50 chance of being in state `|0\u27e9` or state `|1\u27e9`, with a phase difference introduced by the complex coefficients.\n\n\n2. **Advanced Quantum-Inspired Operations**:\n   - You can expand this further to include other operations like **quantum gates** (Hadamard, Pauli-X), **quantum Fourier transforms**, or **Grover\u2019s algorithm** in the quantum-inspired framework using the complex tensor system.\n\n\nmore examples:\n\n\n```python\nimport torch\nfrom complex_tensor import ComplexTensor\n\ndef main():\n    # Initialize real and imaginary parts\n    real = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)\n    imag = torch.tensor([4.0, 5.0, 6.0], requires_grad=True)\n    \n    # Create ComplexTensor instances\n    ct1 = ComplexTensor(real, imag)\n    ct2 = ComplexTensor(real * 2, imag * 2)\n    \n    # Perform arithmetic operations\n    ct_sum = ct1 + ct2\n    print(\"Sum:\", ct_sum)\n    \n    ct_diff = ct1 - ct2\n    print(\"Difference:\", ct_diff)\n    \n    ct_mul = ct1 * ct2\n    print(\"Product:\", ct_mul)\n    \n    ct_div = ct1 / ct2\n    print(\"Division:\", ct_div)\n    \n    # Compute complex exponential\n    ct_exp = ct1.exp()\n    print(\"Exponential:\", ct_exp)\n    \n    # Perform FFT\n    ct_fft = ct1.fft()\n    print(\"FFT:\", ct_fft)\n    \n    # Backpropagation example\n    loss = ct_sum.abs().sum()\n    loss.backward()\n    print(\"Gradients - Real:\", real.grad)\n    print(\"Gradients - Imaginary:\", imag.grad)\n\nif __name__ == \"__main__\":\n    main()\n```\n\nRun the example:\n\n```bash\npython examples/example_usage.py\n```\n\n---\n\n## \ud83e\uddea **Testing**\n\n Here's a basic example of a test case (`test_complex_tensor.py`):\n\n```python\nimport unittest\nimport torch\nfrom src.complextensor.complex_tensor import ComplexTensor, ComplexFunction\n\nclass TestComplexFunction(unittest.TestCase):\n    def setUp(self):\n        self.real = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)\n        self.imag = torch.tensor([4.0, 5.0, 6.0], requires_grad=True)\n\n    def test_complex_function_forward(self):\n        result = ComplexFunction.apply(self.real, self.imag)\n        self.assertTrue(torch.is_complex(result))\n        self.assertTrue(torch.allclose(result.real, self.real))\n        self.assertTrue(torch.allclose(result.imag, self.imag))\n\n    def test_complex_function_backward(self):\n        result = ComplexFunction.apply(self.real, self.imag)\n        loss = result.abs().sum()\n        loss.backward()\n\n        self.assertIsNotNone(self.real.grad)\n        self.assertIsNotNone(self.imag.grad)\n        self.assertFalse(torch.allclose(self.real.grad, torch.zeros_like(self.real)))\n        self.assertFalse(torch.allclose(self.imag.grad, torch.zeros_like(self.imag)))\n\nclass TestComplexTensor(unittest.TestCase):\n    def setUp(self):\n        self.real = torch.tensor([1.0, 2.0, 3.0])\n        self.imag = torch.tensor([4.0, 5.0, 6.0])\n        self.ct = ComplexTensor(self.real, self.imag)\n\n    def test_initialization(self):\n        self.assertTrue(torch.allclose(self.ct.real, self.real))\n        self.assertTrue(torch.allclose(self.ct.imag, self.imag))\n\n    def test_forward(self):\n        result = self.ct.forward()\n        self.assertTrue(torch.is_complex(result))\n        self.assertTrue(torch.allclose(result.real, self.real))\n        self.assertTrue(torch.allclose(result.imag, self.imag))\n\n    def test_addition(self):\n        other = ComplexTensor(torch.tensor([1.0, 1.0, 1.0]), torch.tensor([2.0, 2.0, 2.0]))\n        result = self.ct + other\n        self.assertTrue(torch.allclose(result.real, self.real + 1))\n        self.assertTrue(torch.allclose(result.imag, self.imag + 2))\n\n    def test_subtraction(self):\n        other = ComplexTensor(torch.tensor([1.0, 1.0, 1.0]), torch.tensor([2.0, 2.0, 2.0]))\n        result = self.ct - other\n        self.assertTrue(torch.allclose(result.real, self.real - 1))\n        self.assertTrue(torch.allclose(result.imag, self.imag - 2))\n\n    def test_multiplication(self):\n        other = ComplexTensor(torch.tensor([2.0, 2.0, 2.0]), torch.tensor([3.0, 3.0, 3.0]))\n        result = self.ct * other\n        expected_real = self.real * 2 - self.imag * 3\n        expected_imag = self.real * 3 + self.imag * 2\n        self.assertTrue(torch.allclose(result.real, expected_real))\n        self.assertTrue(torch.allclose(result.imag, expected_imag))\n\n    def test_division(self):\n        other = ComplexTensor(torch.tensor([2.0, 2.0, 2.0]), torch.tensor([3.0, 3.0, 3.0]))\n        result = self.ct / other\n        denominator = 4 + 9  # 2^2 + 3^2\n        expected_real = (self.real * 2 + self.imag * 3) / denominator\n        expected_imag = (self.imag * 2 - self.real * 3) / denominator\n        self.assertTrue(torch.allclose(result.real, expected_real))\n        self.assertTrue(torch.allclose(result.imag, expected_imag))\n\n    def test_conjugate(self):\n        result = self.ct.conj()\n        self.assertTrue(torch.allclose(result.real, self.real))\n        self.assertTrue(torch.allclose(result.imag, -self.imag))\n\n    def test_abs(self):\n        result = self.ct.abs()\n        expected = torch.sqrt(self.real**2 + self.imag**2)\n        self.assertTrue(torch.allclose(result, expected))\n\n    def test_angle(self):\n        result = self.ct.angle()\n        expected = torch.atan2(self.imag, self.real)\n        self.assertTrue(torch.allclose(result, expected))\n\n    def test_to_polar(self):\n        magnitude, phase = self.ct.to_polar()\n        expected_magnitude = torch.sqrt(self.real**2 + self.imag**2)\n        expected_phase = torch.atan2(self.imag, self.real)\n        self.assertTrue(torch.allclose(magnitude, expected_magnitude))\n        self.assertTrue(torch.allclose(phase, expected_phase))\n\n    def test_complex_relu(self):\n        result = self.ct.complex_relu()\n        self.assertTrue(torch.allclose(result.real, torch.relu(self.real)))\n        self.assertTrue(torch.allclose(result.imag, torch.relu(self.imag)))\n\n    def test_complex_sigmoid(self):\n        result = self.ct.complex_sigmoid()\n        self.assertTrue(torch.allclose(result.real, torch.sigmoid(self.real)))\n        self.assertTrue(torch.allclose(result.imag, torch.sigmoid(self.imag)))\n\n    def test_exp(self):\n        result = self.ct.exp()\n        expected_real = torch.exp(self.real) * torch.cos(self.imag)\n        expected_imag = torch.exp(self.real) * torch.sin(self.imag)\n        self.assertTrue(torch.allclose(result.real, expected_real))\n        self.assertTrue(torch.allclose(result.imag, expected_imag))\n\n    def test_log(self):\n        result = self.ct.log()\n        expected_real = torch.log(self.ct.abs())\n        expected_imag = self.ct.angle()\n        self.assertTrue(torch.allclose(result.real, expected_real))\n        self.assertTrue(torch.allclose(result.imag, expected_imag))\n\n    def test_sin(self):\n        result = self.ct.sin()\n        expected_real = torch.sin(self.real) * torch.cosh(self.imag)\n        expected_imag = torch.cos(self.real) * torch.sinh(self.imag)\n        self.assertTrue(torch.allclose(result.real, expected_real))\n        self.assertTrue(torch.allclose(result.imag, expected_imag))\n\n    def test_cos(self):\n        result = self.ct.cos()\n        expected_real = torch.cos(self.real) * torch.cosh(self.imag)\n        expected_imag = -torch.sin(self.real) * torch.sinh(self.imag)\n        self.assertTrue(torch.allclose(result.real, expected_real))\n        self.assertTrue(torch.allclose(result.imag, expected_imag))\n\n    def test_tan(self):\n        result = self.ct.tan()\n        sin_ct = self.ct.sin()\n        cos_ct = self.ct.cos()\n        expected_real = (sin_ct.real * cos_ct.real + sin_ct.imag * cos_ct.imag) / (cos_ct.real**2 + cos_ct.imag**2)\n        expected_imag = (sin_ct.imag * cos_ct.real - sin_ct.real * cos_ct.imag) / (cos_ct.real**2 + cos_ct.imag**2)\n        self.assertTrue(torch.allclose(result.real, expected_real, atol=1e-5))\n        self.assertTrue(torch.allclose(result.imag, expected_imag, atol=1e-5))\n\n    def test_power(self):\n        exponent = 2\n        result = self.ct.power(exponent)\n        expected = self.ct * self.ct\n        self.assertTrue(torch.allclose(result.real, expected.real))\n        self.assertTrue(torch.allclose(result.imag, expected.imag))\n\n    def test_fft(self):\n        result = self.ct.fft()\n        expected = torch.fft.fft(self.ct.forward())\n        self.assertTrue(torch.allclose(result.real, expected.real))\n        self.assertTrue(torch.allclose(result.imag, expected.imag))\n\n    def test_ifft(self):\n        result = self.ct.ifft()\n        expected = torch.fft.ifft(self.ct.forward())\n        self.assertTrue(torch.allclose(result.real, expected.real))\n        self.assertTrue(torch.allclose(result.imag, expected.imag))\n\n    def test_gradient_computation(self):\n        real = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)\n        imag = torch.tensor([4.0, 5.0, 6.0], requires_grad=True)\n        ct = ComplexTensor(real, imag)\n        \n        output = ct.forward().abs().sum()\n        output.backward()\n        \n        self.assertIsNotNone(real.grad)\n        self.assertIsNotNone(imag.grad)\n        self.assertFalse(torch.allclose(real.grad, torch.zeros_like(real.grad)))\n        self.assertFalse(torch.allclose(imag.grad, torch.zeros_like(imag.grad)))\n\n    def test_to_device(self):\n        if torch.cuda.is_available():\n            ct_gpu = self.ct.to(torch.device('cuda'))\n            self.assertEqual(ct_gpu.real.device.type, 'cuda')\n            self.assertEqual(ct_gpu.imag.device.type, 'cuda')\n\n    def test_detach(self):\n        ct_detached = self.ct.detach()\n        self.assertFalse(ct_detached.real.requires_grad)\n        self.assertFalse(ct_detached.imag.requires_grad)\n\n    def test_requires_grad(self):\n        self.ct.requires_grad_(True)\n        self.assertTrue(self.ct.real.requires_grad)\n        self.assertTrue(self.ct.imag.requires_grad)\n\n    def test_complex_function_integration(self):\n        real = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)\n        imag = torch.tensor([4.0, 5.0, 6.0], requires_grad=True)\n        ct = ComplexTensor(real, imag)\n        \n        result = ct.forward()\n        self.assertTrue(torch.is_complex(result))\n        self.assertTrue(torch.allclose(result.real, real))\n        self.assertTrue(torch.allclose(result.imag, imag))\n\n        loss = result.abs().sum()\n        loss.backward()\n\n        self.assertIsNotNone(real.grad)\n        self.assertIsNotNone(imag.grad)\n        self.assertFalse(torch.allclose(real.grad, torch.zeros_like(real)))\n        self.assertFalse(torch.allclose(imag.grad, torch.zeros_like(imag)))\n\nif __name__ == '__main__':\n    unittest.main()\n```\n\nRun the tests:\n\n```bash\npython -m unittest discover tests\n```\n\n---\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A PyTorch-based library for complex tensor operations.",
    "version": "0.1.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/NeoVertex1/ComplexTensor/issues",
        "Documentation": "https://complextensor.readthedocs.io",
        "Homepage": "https://github.com/NeoVertex1/ComplexTensor",
        "Repository": "https://github.com/NeoVertex1/ComplexTensor"
    },
    "split_keywords": [
        "complex",
        " tensor",
        " pytorch",
        " machine learning",
        " signal processing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0d5cdabe11cf377492df32d2636d67d2fe8dfb9dbb87d7433f0b9045146254c2",
                "md5": "bd2679ffd6e3edc578a14f77dae41a0b",
                "sha256": "91e3630eb900647c4640417db6db96f694c2ea23f081912c44d0177f943ef309"
            },
            "downloads": -1,
            "filename": "complextensor-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bd2679ffd6e3edc578a14f77dae41a0b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 14858,
            "upload_time": "2024-09-14T05:15:40",
            "upload_time_iso_8601": "2024-09-14T05:15:40.442604Z",
            "url": "https://files.pythonhosted.org/packages/0d/5c/dabe11cf377492df32d2636d67d2fe8dfb9dbb87d7433f0b9045146254c2/complextensor-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f4361f0cff8c024825c6a602e90073aa4af7a69562f21443e67c84eb6ec98fd8",
                "md5": "881cb874f10109b8039a0e333ad46578",
                "sha256": "2adb22601a57ea7ee8b127f90616d501f144b16f02982f1566bcba8f9c0be632"
            },
            "downloads": -1,
            "filename": "complextensor-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "881cb874f10109b8039a0e333ad46578",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 20118,
            "upload_time": "2024-09-14T05:15:42",
            "upload_time_iso_8601": "2024-09-14T05:15:42.239819Z",
            "url": "https://files.pythonhosted.org/packages/f4/36/1f0cff8c024825c6a602e90073aa4af7a69562f21443e67c84eb6ec98fd8/complextensor-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-14 05:15:42",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "NeoVertex1",
    "github_project": "ComplexTensor",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "complextensor"
}
        
Elapsed time: 2.41527s