dwavesystems / dwave-system

An API for easily incorporating the D-Wave system as a sampler, either directly or through Leap's cloud-based hybrid samplers
https://docs.ocean.dwavesys.com/
Apache License 2.0
90 stars 64 forks source link

Feature Request: `embed_sampleset` or similar #501

Open arcondello opened 1 year ago

arcondello commented 1 year ago

It would be useful sometimes to be able to "embed" a sampleset. Something like

import numpy as np

def embed_sampleset(sampleset, embedding, *, num_qubits=None, fill_value=2):

    if num_qubits is None:
        num_qubits = max(max(chain) for chain in embedding.values()) + 1

    num_samples = len(sampleset)

    samples = np.full((num_samples, num_qubits), fill_value)

    for i, v in enumerate(sampleset.variables):
        for c in embedding[v]:
            samples[:, c] = sampleset.record.sample[:, i]

    return samples

if __name__ == '__main__':
    import dimod

    from dwave.system import DWaveSampler, EmbeddingComposite

    bqm = dimod.generators.gnm_random_bqm(100, 1000, "SPIN")

    sampleset = EmbeddingComposite(DWaveSampler()).sample(bqm, return_embedding=True, num_reads=100, answer_mode="raw")

    embedding = sampleset.info["embedding_context"]["embedding"]

    unembedded_samples = embed_sampleset(sampleset, embedding)