import torch


def complex_to_chan(x: torch.Tensor) -> torch.Tensor:
    """
    Move the real and imaginary part of a complex-valued tensor from a separate dimension to the channel dimension.

    Args:
        x: Tensor of shape (B, C, H, W, 2), where the last dimension is the complex dimension.

    Returns:
        A tensor of shape (B, C*2, H, W), with the complex dimension as part of the channel dimension.

    Notes:
        Based on NormUnet from https://github.com/facebookresearch/fastMRI/blob/master/fastmri/models/varnet.py
    """
    B, C, H, W, two = x.shape
    assert two == 2
    x = x.permute(0, 4, 1, 2, 3)
    return x.reshape(B, 2 * C, H, W)