import torch


def matmul(X, Y):
    """
    Computes complex-valued matrix product of X and Y.
    """
    assert X.size(-1) == 2
    assert Y.size(-1) == 2

    A = X[..., 0]
    B = X[..., 1]
    C = Y[..., 0]
    D = Y[..., 1]
    real = torch.matmul(A, C) - torch.matmul(B, D)
    imag = torch.matmul(A, D) + torch.matmul(B, C)
    return torch.stack((real, imag), dim=-1)