示例#1
0
    def chain_break(self, sampleset, embedding):
        if as_action(self.action) is IGNORE:
            return

        ground = sampleset.lowest()
        variables = list(embedding)
        chains = [embedding[v] for v in variables]
        broken = broken_chains(ground, chains)

        if not (len(sampleset) and broken.any()):
            return

        for nc, chain in enumerate(chains):
            for row in range(broken.shape[0]):
                if not broken[row, nc]:
                    continue

                self.issue(
                    "Lowest-energy samples contain a broken chain",
                    category=ChainBreakWarning,
                    level=logging.ERROR,
                    data=dict(target_variables=chain,
                              source_variables=[variables[nc]],
                              sample_index=row),
                )
A = dwave_sampler.edgelist
Adj = dwave_sampler.adjacency
embedding = find_embedding(Q, A)
print(embedding)

bqm = BinaryQuadraticModel.from_qubo(Q)

# Cannot use a Composite to get the broken chains, so do the embedding
# directly
bqm_embedded = embed_bqm(bqm, embedding, Adj, chain_strength=chainstrength)
response = DWaveSampler().sample(bqm_embedded, num_reads=numruns)

# We need to get the chains directly, as a list
chains = [embedding[v] for v in list(bqm)]

# Obtain the broken chains
broken = broken_chains(response, chains)

# Interpret the results in terms of the embedding. Be sure to
# tell the method to compute the chain_break_frequency.
print(
    unembed_sampleset(response,
                      embedding,
                      source_bqm=bqm,
                      chain_break_fraction=True), broken)

# Use NumPy method to obtain the indices of the broken chains
w = np.where(broken == True)
indices = list(zip(*w))
print(indices)