def get_modulus_residue(value, modulus):
    """
    Computes the canonical residue of value mod modulus.

    Parameters:
        value (int): The value to compute the residue of
        modulus (int): The modulus used to calculate the residue

    Returns:
        An integer "r" between 0 and modulus - 1, where value - r is divisible
        by the modulus.

    Remarks:
        See the Q# source for the "Modulus" function at 
        https://github.com/Microsoft/QuantumLibraries/blob/master/Canon/src/Math/Functions.qs
    """

    if modulus <= 0:
        raise ValueError(f"Modulus {modulus} must be positive.")

    r = value % modulus
    if r < 0:
        return r + modulus
    else:
        return r