Ejemplo n.º 1
0
def get_embedding_from_tag(embedding_tag,
                           target_nodelist,
                           target_edgelist,
                           presorted=False):
    with cache_connect() as cur:
        embedding = select_embedding_from_tag(cur, embedding_tag,
                                              target_nodelist, target_edgelist)
    return embedding
Ejemplo n.º 2
0
def get_flux_biases(sampler,
                    embedding,
                    num_reads,
                    chain_strength=1,
                    max_age=3600):
    # try to read the chip_id, otherwise get the name
    system_name = sampler.properties.get('chip_id', str(sampler.__class__))

    try:
        with cache_connect() as cur:
            fbo = get_flux_biases_from_cache(cur,
                                             embedding.values(),
                                             system_name,
                                             chain_strength=chain_strength)
    except MissingFluxBias:

        # if dwave-system-tuning is not available, then we can't calculate the biases
        try:
            import dwave.system.tuning as dst
        except ImportError:
            return []

        fbo = dst.oneshot_flux_bias(sampler,
                                    embedding.values(),
                                    num_reads=num_reads,
                                    chain_strength=chain_strength)

        # store them in the cache
        fbo_dict = dict(fbo)
        with cache_connect() as cur:
            for chain in embedding.values():
                flux_bias = fbo_dict[next(iter(chain))]
                insert_flux_bias(cur, chain, system_name, flux_bias,
                                 chain_strength)

    return fbo
Ejemplo n.º 3
0
def load_embedding(target_nodelist, target_edgelist, embedding, embedding_tag):

    target_adjacency = {v: set() for v in target_nodelist}
    for u, v in target_edgelist:
        target_adjacency[u].add(v)
        target_adjacency[v].add(u)

    source_adjacency = embutil.target_to_source(target_adjacency, embedding)
    source_nodelist = sorted(source_adjacency)
    source_edgelist = sorted(
        sorted(edge) for edge in _adjacency_to_edges(source_adjacency))

    with cache_connect() as cur:
        insert_embedding(cur, source_nodelist, source_edgelist,
                         target_nodelist, target_edgelist, embedding,
                         embedding_tag)
Ejemplo n.º 4
0
def get_flux_biases(sampler,
                    embedding,
                    chain_strength,
                    num_reads=1000,
                    max_age=3600):
    """Get the flux bias offsets for sampler and embedding.

    Args:
        sampler (:obj:`.DWaveSampler`):
            A D-Wave sampler.

        embedding (dict[hashable, iterable]):
            Mapping from a source graph to the specified sampler’s graph (the target graph). The
            keys of embedding should be nodes in the source graph, the values should be an iterable
            of nodes in the target graph.

        chain_strength (number):
            Desired chain coupling strength. This is the magnitude of couplings between qubits
            in a chain.

        num_reads (int, optional, default=1000):
            The number of reads per system call if new flux biases need to be calculated.

        max_age (int, optional, default=3600):
            The maximum age (in seconds) allowed for previously calculated flux bias offsets.

    Returns:
        dict: A dict where the keys are the nodes in the chains and the values are the flux biases.

    """

    if not isinstance(sampler, dimod.Sampler):
        raise TypeError("input sampler should be DWaveSampler")

    # try to read the chip_id, otherwise get the name
    system_name = sampler.properties.get('chip_id', str(sampler.__class__))

    try:
        with cache_connect() as cur:
            fbo = get_flux_biases_from_cache(cur,
                                             embedding.values(),
                                             system_name,
                                             chain_strength=chain_strength,
                                             max_age=max_age)
        return fbo
    except MissingFluxBias:
        pass

    # if dwave-drivers is not available, then we can't calculate the biases
    try:
        import dwave.drivers as drivers
    except ImportError:
        msg = (
            "dwave-drivers not found, cannot calculate flux biases. dwave-drivers can be "
            "installed with "
            "'pip install dwave-drivers --extra-index-url https://pypi.dwavesys.com/simple'. "
            "See documentation for dwave-drivers license.")
        raise RuntimeError(msg)

    fbo = drivers.oneshot_flux_bias(
        sampler,
        embedding.values(),
        num_reads=num_reads,
        chain_strength=chain_strength,
        label='VirtualGraph flux bias measurements')

    # store them in the cache
    with cache_connect() as cur:
        for chain in embedding.values():
            v = next(iter(chain))
            flux_bias = fbo.get(v, 0.0)
            insert_flux_bias(cur, chain, system_name, flux_bias,
                             chain_strength)

    return fbo