def blackbox_and_coarse_grain(blackbox, coarse_grain):
    """Validate that a coarse-graining properly combines the outputs of a
    blackboxing.
    """
    if blackbox is None:
        return

    for box in blackbox.partition:
        # Outputs of the box
        outputs = set(box) & set(blackbox.output_indices)

        if coarse_grain is None and len(outputs) > 1:
            raise ValueError(
                'A blackboxing with multiple outputs per box must be '
                'coarse-grained.')

        if (coarse_grain and not any(outputs.issubset(part)
                                     for part in coarse_grain.partition)):
            raise ValueError(
                'Multiple outputs from a blackbox must be partitioned into '
                'the same macro-element of the coarse-graining')
Validate that a coarse-graining properly combines the outputs of a
    blackboxing.