def unembed_sampleset(target_sampleset, embedding, source_bqm,
                      chain_break_method=None, chain_break_fraction=False):
    """Unembed the samples set.

    Construct a sample set for the source binary quadratic model (BQM) by
    unembedding the given samples from the target BQM.

    Args:
        target_sampleset (:obj:`dimod.SampleSet`):
            SampleSet from the target BQM.

        embedding (dict):
            Mapping from source graph to target graph as a dict of form
            {s: {t, ...}, ...}, where s is a source variable and t is a target
            variable.

        source_bqm (:obj:`dimod.BinaryQuadraticModel`):
            Source binary quadratic model.

        chain_break_method (function, optional):
            Method used to resolve chain breaks.
            See :mod:`dwave.embedding.chain_breaks`.

        chain_break_fraction (bool, optional, default=False):
            If True, a 'chain_break_fraction' field is added to the unembedded
            samples which report what fraction of the chains were broken before
            unembedding.

    Returns:
        :obj:`.SampleSet`:

    Examples:

        >>> import dimod
        ...
        >>> # say we have a bqm on a triangle and an embedding
        >>> J = {('a', 'b'): -1, ('b', 'c'): -1, ('a', 'c'): -1}
        >>> bqm = dimod.BinaryQuadraticModel.from_ising({}, J)
        >>> embedding = {'a': [0, 1], 'b': [2], 'c': [3]}
        ...
        >>> # and some samples from the embedding
        >>> samples = [{0: -1, 1: -1, 2: -1, 3: -1},  # [0, 1] is unbroken
                       {0: -1, 1: +1, 2: +1, 3: +1}]  # [0, 1] is broken
        >>> energies = [-3, 1]
        >>> embedded = dimod.SampleSet.from_samples(samples, dimod.SPIN, energies)
        ...
        >>> # unembed
        >>> samples = dwave.embedding.unembed_sampleset(embedded, embedding, bqm)
        >>> samples.record.sample   # doctest: +SKIP
        array([[-1, -1, -1],
               [ 1,  1,  1]], dtype=int8)

    """

    if chain_break_method is None:
        chain_break_method = majority_vote

    variables = list(source_bqm)
    try:
        chains = [embedding[v] for v in variables]
    except KeyError:
        raise ValueError("given bqm does not match the embedding")

    chain_idxs = [[target_sampleset.variables.index[v] for v in chain] for chain in chains]

    record = target_sampleset.record

    unembedded, idxs = chain_break_method(record.sample, chain_idxs)

    # dev note: this is a bug in dimod that empty unembedded is not handled,
    # in the future this try-except can be removed
    try:
        energies = source_bqm.energies((unembedded, variables))
    except ValueError:
        datatypes = [('sample', np.dtype(np.int8), (len(variables),)), ('energy', np.float)]
        datatypes.extend((name, record[name].dtype, record[name].shape[1:])
                         for name in record.dtype.names
                         if name not in {'sample', 'energy'})
        if chain_break_fraction:
            datatypes.append(('chain_break_fraction', np.float64))
        # there are no samples so everything is empty
        data = np.rec.array(np.empty(0, dtype=datatypes))
        return dimod.SampleSet(data, variables, target_sampleset.info.copy(), target_sampleset.vartype)

    reserved = {'sample', 'energy'}
    vectors = {name: record[name][idxs]
               for name in record.dtype.names if name not in reserved}

    if chain_break_fraction:
        vectors['chain_break_fraction'] = broken_chains(record.sample, chain_idxs).mean(axis=1)[idxs]

    return dimod.SampleSet.from_samples((unembedded, variables),
                                        target_sampleset.vartype,
                                        energy=energies,
                                        info=target_sampleset.info.copy(),
                                        **vectors)
Unembed the samples set.

    Construct a sample set for the source binary quadratic model (BQM) by
    unembedding the given samples from the target BQM.

    Args:
        target_sampleset (:obj:`dimod.SampleSet`):
            SampleSet from the target BQM.

        embedding (dict):
            Mapping from source graph to target graph as a dict of form
            {s: {t, ...}, ...}, where s is a source variable and t is a target
            variable.

        source_bqm (:obj:`dimod.BinaryQuadraticModel`):
            Source binary quadratic model.

        chain_break_method (function, optional):
            Method used to resolve chain breaks.
            See :mod:`dwave.embedding.chain_breaks`.

        chain_break_fraction (bool, optional, default=False):
            If True, a 'chain_break_fraction' field is added to the unembedded
            samples which report what fraction of the chains were broken before
            unembedding.

    Returns:
        :obj:`.SampleSet`:

    Examples:

        >>> import dimod
        ...
        >>> # say we have a bqm on a triangle and an embedding
        >>> J = {('a', 'b'): -1, ('b', 'c'): -1, ('a', 'c'): -1}
        >>> bqm = dimod.BinaryQuadraticModel.from_ising({}, J)
        >>> embedding = {'a': [0, 1], 'b': [2], 'c': [3]}
        ...
        >>> # and some samples from the embedding
        >>> samples = [{0: -1, 1: -1, 2: -1, 3: -1},  # [0, 1] is unbroken
                       {0: -1, 1: +1, 2: +1, 3: +1}]  # [0, 1] is broken
        >>> energies = [-3, 1]
        >>> embedded = dimod.SampleSet.from_samples(samples, dimod.SPIN, energies)
        ...
        >>> # unembed
        >>> samples = dwave.embedding.unembed_sampleset(embedded, embedding, bqm)
        >>> samples.record.sample   # doctest: +SKIP
        array([[-1, -1, -1],
               [ 1,  1,  1]], dtype=int8)