import torch


def mul(x, y):
    """
    Multiplies two complex-valued tensors x and y.
     i.e. z = (a + ib) * (c + id)
    """
    assert x.size(-1) == 2
    assert y.size(-1) == 2

    a = x[..., 0]
    b = x[..., 1]
    c = y[..., 0]
    d = y[..., 1]

    real = a * c - b * d
    imag = a * d + b * c

    return torch.stack((real, imag), dim=-1)