dwave.embedding.chain_breaks.weighted_random#
- weighted_random(samples, chains)[source]#
Unembed samples using weighed random choice for broken chains.
- Parameters:
samples (samples_like) – A collection of samples. samples_like is an extension of NumPy’s array_like. See
dimod.as_samples()
.chains (list[array_like]) – List of chains, where each chain is an array_like collection of the variables in the same order as their represention in the given samples.
- Returns:
A 2-tuple containing:
numpy.ndarray
: Unembedded samples as an nS-by-nC array of dtype ‘int8’, where nC is the number of chains and nS the number of samples. Broken chains are resolved by setting the sample value to a random value weighted by frequency of the value in the chain.numpy.ndarray
: Indicies of the samples. Equivalent tonp.arange(nS)
because all samples are kept and no samples are added.- Return type:
Examples
This example unembeds samples from a target graph that chains nodes 0 and 1 to represent one source node and nodes 2, 3, and 4 to represent another. The sample has broken chains for both source nodes.
>>> import dimod >>> import numpy as np ... >>> chains = [(0, 1), (2, 3, 4)] >>> samples = np.array([[1, 0, 1, 0, 1]], dtype=np.int8) >>> unembedded, idx = dwave.embedding.weighted_random(samples, chains) >>> unembedded array([[1, 1]], dtype=int8) >>> idx array([0, 1])