Exemple #1
0
def test_delta_shift_no_loss_still_connected():
    input = MutableGraph(nx.empty_graph(3))
    input.add_edge(1, 2)

    contact = MutableGraph(nx.empty_graph(3))
    contact.add_edge(0, 1, weight=1)
    contact.add_edge(0, 2, weight=1)
    contact.add_edge(1, 2, weight=2)

    inverse = {0: 0, 1: 0, 2: 1, 3: 2, 4: 2, 5: 2}

    assert delta_shift(input, contact, target, inverse, 1, 3) == 0
Exemple #2
0
def test_delta_shift_simple_loss():
    input = MutableGraph(nx.empty_graph(3))
    input.add_edge(0, 2)

    contact = MutableGraph(nx.empty_graph(3))
    contact.add_edge(0, 1, weight=2)
    contact.add_edge(0, 2, weight=1)
    contact.add_edge(1, 2, weight=1)

    inverse = {0: 0, 1: 0, 2: 0, 3: 1, 4: 2, 5: 2}

    assert delta_shift(input, contact, target, inverse, 3, 2) == -1
Exemple #3
0
def test_delta_shift_simple_gain_no_duplicate():
    input = MutableGraph(nx.empty_graph(4))
    input.add_edge(0, 3)

    contact = MutableGraph(nx.empty_graph(4))
    contact.add_edge(0, 1, weight=1)
    contact.add_edge(0, 2, weight=1)
    contact.add_edge(1, 3, weight=1)
    contact.add_edge(2, 3, weight=2)

    inverse = {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 3}

    assert delta_shift(input, contact, target, inverse, 0, 2) == 1
Exemple #4
0
    def _create_contact_graph(self, embed):
        self.contact_graph = MutableGraph(self.guest, include_edges=False)
        self.initial_cost = 0

        for n1 in range(len(embed)):
            e1 = embed[n1]
            for n2 in range(n1):
                e2 = embed[n2]
                weight = 0
                for g1 in e1:
                    for g2 in e2:
                        weight += 1 if self._chimera_distance(g1, g2) == 1 else 0
                if weight > 0:
                    self.contact_graph.add_edge(n1, n2, weight)
                    self.initial_cost += 1 if self.guest.has_edge(n1, n2) else 0
Exemple #5
0
def delta_swap_naive(input_graph: MutableGraph, contact_graph: MutableGraph,
                     n1: int, n2: int):
    delta = 0
    for n1_nb in contact_graph.nodes[n1].neighbours:
        if input_graph.has_edge(n1, n1_nb.val):
            delta -= 1
    for n2_nb in contact_graph.nodes[n2].neighbours:
        if input_graph.has_edge(n2, n2_nb.val):
            delta -= 1
    for n1_nb in contact_graph.nodes[n1].neighbours:
        to = n1_nb.val if n1_nb.val != n2 else n1
        if input_graph.has_edge(n2, to):
            delta += 1
    for n2_nb in contact_graph.nodes[n2].neighbours:
        to = n2_nb.val if n2_nb.val != n1 else n2
        if input_graph.has_edge(n1, to):
            delta += 1
    return delta
Exemple #6
0
def test_delta_shift_simple_gain():
    input = MutableGraph(nx.empty_graph(3))
    input.add_edge(0, 2)

    contact = MutableGraph(nx.empty_graph(3))
    contact.add_edge(0, 1, weight=2)
    contact.add_edge(1, 2, weight=2)

    inverse = {0: 0, 1: 0, 2: 1, 3: 1, 4: 2, 5: 2}

    assert delta_shift(input, contact, target, inverse, 1, 3) == 1
    assert delta_shift(input, contact, target, inverse, 0, 2) == 1
    assert delta_shift(input, contact, target, inverse, 4, 2) == 1
    assert delta_shift(input, contact, target, inverse, 5, 3) == 1
Exemple #7
0
def test_delta_shift_simple_gain_2():
    input = MutableGraph(nx.empty_graph(5))
    input.add_edge(0, 3)
    input.add_edge(0, 4)

    contact = MutableGraph(nx.empty_graph(5))
    contact.add_edge(0, 1, weight=1)
    contact.add_edge(0, 2, weight=1)
    contact.add_edge(1, 3, weight=1)
    contact.add_edge(2, 3, weight=1)
    contact.add_edge(2, 4, weight=1)
    contact.add_edge(3, 4, weight=1)

    inverse = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 4}

    assert delta_shift(input, contact, target, inverse, 0, 2) == 2
Exemple #8
0
def test_delta_shift_gain_loss():
    input = MutableGraph(nx.empty_graph(6))
    input.add_edge(1, 2)
    input.add_edge(1, 3)
    input.add_edge(2, 5)
    input.add_edge(3, 5)

    contact = MutableGraph(nx.empty_graph(6))
    contact.add_edge(0, 1, weight=1)
    contact.add_edge(0, 2, weight=1)
    contact.add_edge(1, 3, weight=1)
    contact.add_edge(2, 3, weight=1)
    contact.add_edge(2, 4, weight=1)
    contact.add_edge(3, 5, weight=1)
    contact.add_edge(4, 5, weight=1)

    inverse = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5}

    assert delta_shift(input, contact, target, inverse, 2, 3) == 0
Exemple #9
0
def test_delta_shift_multi_loss():
    input = MutableGraph(nx.empty_graph(4))
    input.add_edge(0, 1)
    input.add_edge(2, 1)
    input.add_edge(3, 1)

    contact = MutableGraph(nx.empty_graph(4))
    contact.add_edge(0, 1, weight=1)
    contact.add_edge(0, 2, weight=1)
    contact.add_edge(1, 2, weight=1)
    contact.add_edge(1, 3, weight=1)
    contact.add_edge(2, 3, weight=1)

    inverse = {0: 0, 1: 0, 2: 1, 3: 2, 4: 3, 5: 3}

    # Assume that 3 and 1 still connected....
    assert delta_shift(input, contact, target, inverse, 4, 2) == -2
Exemple #10
0
class BaseModel:

    def __init__(self, guest: Graph, host: ChimeraGraph):
        if host.faulty_nodes or host.faulty_edges:
            raise NotImplementedError(
                "Chimera graphs with faults are not supported by these algorithms")
        self.guest = guest
        self.host = host
        m, l = host.params
        dnx_coords = dnx.chimera_coordinates(m, t=l)
        self.linear_to_chimera = dnx_coords.linear_to_chimera
        self.chimera_to_linear = dnx_coords.chimera_to_linear
        self.forward_embed = None

    def _chimera_distance(self, g1: int, g2: int):
        if g1 == g2:
            return 0
        (i1, j1, u1, k1) = self.linear_to_chimera(g1)
        (i2, j2, u2, k2) = self.linear_to_chimera(g2)
        dist = abs(i1 - i2) + abs(j1 - j2)
        dist += 2 if u1 == u2 else 1
        if u1 == 0 and u2 == 0 and (j1 - j2) == 0 and k1 == k2:
            dist -= 2
        if u1 == 1 and u2 == 1 and (i1 - i2) == 0 and k1 == k2:
            dist -= 2
        return dist

    def _create_contact_graph(self, embed):
        self.contact_graph = MutableGraph(self.guest, include_edges=False)
        self.initial_cost = 0

        for n1 in range(len(embed)):
            e1 = embed[n1]
            for n2 in range(n1):
                e2 = embed[n2]
                weight = 0
                for g1 in e1:
                    for g2 in e2:
                        weight += 1 if self._chimera_distance(g1, g2) == 1 else 0
                if weight > 0:
                    self.contact_graph.add_edge(n1, n2, weight)
                    self.initial_cost += 1 if self.guest.has_edge(n1, n2) else 0

    def all_moves(self):
        pass

    def random_swap_move(self):
        pass

    def random_shift_move(self, *args):
        pass

    def delta_swap(self, swap_move):
        pass

    def swap(self, swap_move):
        pass

    def delta_shift(self, swap_move):
        pass

    def shift(self, swap_move):
        pass

    def randomize(self):
        pass
Exemple #11
0
import networkx as nx
import pytest

from ember.hardware.chimera import D_WAVE_2000Q
from ember.pssa.graph import MutableGraph
from ember.pssa.model import ProbabilisticSwapShiftModel

# Setup input graphs for test

input_builder = nx.empty_graph(4)
input_builder.add_edge(0, 1)
input_builder.add_edge(0, 2)
input_builder.add_edge(1, 3)
input_builder.add_edge(2, 3)
input_graph_1 = MutableGraph(input_builder)
input_builder.add_edge(1, 2)
input_graph_2 = MutableGraph(input_builder)

# Setup contact graphs for test
contact_builder = nx.empty_graph(4)
contact_builder.add_edge(0, 1)
contact_builder.add_edge(0, 3)
contact_builder.add_edge(1, 2)
contact_builder.add_edge(2, 3)
contact_graph_1 = MutableGraph(contact_builder)
contact_builder.add_edge(1, 3)
contact_graph_2 = MutableGraph(contact_builder)


def delta_swap_naive(input_graph: MutableGraph, contact_graph: MutableGraph,
                     n1: int, n2: int):