Exemple #1
0
    def test_maximum_independent_set_basic(self):
        """Runs the function on some small and simple graphs, just to make
        sure it works in basic functionality.
        """
        G = dnx.chimera_graph(1, 2, 2)
        indep_set = dnx.maximum_independent_set(G, dimod.ExactSolver())
        self.assertTrue(dnx.is_independent_set(G, indep_set))

        G = nx.path_graph(5)
        indep_set = dnx.maximum_independent_set(G, dimod.ExactSolver())
        self.assertTrue(dnx.is_independent_set(G, indep_set))
Exemple #2
0
    def test_default_sampler(self):
        G = nx.complete_graph(5)

        dnx.set_default_sampler(dimod.ExactSolver())
        self.assertIsNot(dnx.get_default_sampler(), None)
        indep_set = dnx.maximum_independent_set(G)
        dnx.unset_default_sampler()
        self.assertEqual(dnx.get_default_sampler(), None, "sampler did not unset correctly")
    def test_maximum_independent_set_basic(self):
        """Runs the function on some small and simple graphs, just to make
        sure it works in basic functionality.
        """

        G = dnx.chimera_graph(1, 2, 2)
        indep_set = dnx.maximum_independent_set(G, ExactSolver())
        self.set_independence_check(G, indep_set)

        G = nx.path_graph(5)
        indep_set = dnx.maximum_independent_set(G, ExactSolver())
        self.set_independence_check(G, indep_set)

        for __ in range(10):
            G = nx.gnp_random_graph(5, .5)
            indep_set = dnx.maximum_independent_set(G, ExactSolver())
            self.set_independence_check(G, indep_set)
Exemple #4
0
def solve_problem(G, sampler):
    '''Returns a solution to to the minimum vertex cover on graph G using 
    the D-Wave QPU.

    Args:
        G(networkx.Graph): a graph representing a problem
        sampler(dimod.Sampler): sampler used to find solutions

    Returns:
        A list of nodes
    '''

    # Find the maximum independent set, S
    S = dnx.maximum_independent_set(G, sampler=sampler, num_reads=10)

    return S
#import matplotlib.pyplot as plt

# Set the solver we're going to use
from dwave.system.samplers import DWaveSampler
from dwave.system.composites import EmbeddingComposite

sampler = EmbeddingComposite(DWaveSampler())

#Create empty graph
G = nx.Graph()

#Add edges to graph - this also adds the nodes
G.add_edges_from([(3, 4), (1, 2)])

#Find the maximum independent set, S
S = dnx.maximum_independent_set(G, sampler=sampler, num_reads=10)

# Print the solution for the user
#print('Maximum independent set size found is', len(S))
#print(S)

#----------------------------------
#zz =S[0]
#print (zz, "testing")

#if zz ==1:
#print("HEADS")
#if zz  == 2:
#print("TAILS")

#print("  ")
Exemple #6
0
def maximum_clique(G, sampler=None, lagrange=2.0, **sampler_args):
    """
    Returns an approximate maximum clique.
    A clique in an undirected graph G = (V, E) is a subset of the vertex set
    `C \subseteq V` such that for every two vertices in C there exists an edge
    connecting the two. This is equivalent to saying that the subgraph
    induced by C is complete (in some cases, the term clique may also refer
    to the subgraph).A maximum clique is a clique of the largest
    possible size in a given graph.

    This function works by finding the maximum independent set of the compliment
    graph of the given graph G which is equivalent to finding maximum clique.
    It defines a QUBO with ground states corresponding
    to a maximum weighted independent set and uses the sampler to sample from it.

    Parameters
    ----------
    G : NetworkX graph
        The graph on which to find a maximum clique.

    sampler
        A binary quadratic model sampler. A sampler is a process that
        samples from low energy states in models defined by an Ising
        equation or a Quadratic Unconstrained Binary Optimization
        Problem (QUBO). A sampler is expected to have a 'sample_qubo'
        and 'sample_ising' method. A sampler is expected to return an
        iterable of samples, in order of increasing energy. If no
        sampler is provided, one must be provided using the
        `set_default_sampler` function.

    lagrange : optional (default 2)
        Lagrange parameter to weight constraints (no edges within set)
        versus objective (largest set possible).

    sampler_args
        Additional keyword parameters are passed to the sampler.

    Returns
    -------
    clique_nodes : list
       List of nodes that form a maximum clique, as
       determined by the given sampler.

    Notes
    -----
    Samplers by their nature may not return the optimal solution. This
    function does not attempt to confirm the quality of the returned
    sample.

    References
    ----------

    `Maximum Clique on Wikipedia <https://en.wikipedia.org/wiki/Maximum_clique(graph_theory)>`_

    `Independent Set on Wikipedia <https://en.wikipedia.org/wiki/Independent_set_(graph_theory)>`_

    `QUBO on Wikipedia <https://en.wikipedia.org/wiki/Quadratic_unconstrained_binary_optimization>`_

    .. [AL] Lucas, A. (2014). Ising formulations of many NP problems.
       Frontiers in Physics, Volume 2, Article 5.
    """
    if G is None:
        raise ValueError("Expected NetworkX graph!")

    # finding the maximum clique in a graph is equivalent to finding
    # the independent set in the complementary graph
    complement_G = nx.complement(G)
    return dnx.maximum_independent_set(complement_G, sampler, lagrange,
                                       **sampler_args)
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#
# ================================================================================================
from __future__ import print_function
import networkx as nx
import dwave_networkx as dnx
import dimod

# Use basic simulated annealer
sampler = dimod.SimulatedAnnealingSampler()

# Set up a Networkx Graph
G = nx.Graph()
G.add_edges_from([(1, 2), (1, 3), (2, 3), (3, 4), (3, 5), (4, 5), (4, 6),
                  (5, 6), (6, 7)])

# Find the maximum independent set, which is known in this case to be of length 3
candidate = dnx.maximum_independent_set(G, sampler)
if dnx.is_independent_set(G, candidate) and len(candidate) == 3:
    print(candidate, " is a maximum independent set")
else:
    print(candidate, " is not a minimum vertex coloring")
import networkx
import dwave_networkx

from dwave.system.samplers import DWaveSampler
from dwave.system.composites import EmbeddingComposite

# implementation includes
from NQueensEdgeGenerator import getEdges
from DrawBoard import drawNQueensSolution
DIMENSION = 4

# a general sampler for handling an unstructured QUBO problem
my_sampler = EmbeddingComposite(DWaveSampler())

# generate the graph
Graph = networkx.Graph()
Graph.add_edges_from(getEdges(DIMENSION))

# library call for creating and calculating QUBO problem
Solution = dwave_networkx.maximum_independent_set(Graph, sampler=my_sampler)

# Visualization of the found solution
drawNQueensSolution(DIMENSION, Solution)
Exemple #9
0
    def test_dimod_vs_list(self):
        G = nx.path_graph(5)

        indep_set = dnx.maximum_independent_set(G, dimod.ExactSolver())
        indep_set = dnx.maximum_independent_set(G, dimod.SimulatedAnnealingSampler())
Exemple #10
0
def maximum_independent_set(graph, sampler, params=None):
    res = dnx.maximum_independent_set(graph, sampler)
    return res, 'node'
# Set the solver we're going to use
from dwave.system.samplers import DWaveSampler
from dwave.system.composites import EmbeddingComposite

sampler = EmbeddingComposite(DWaveSampler())

# Create empty graph
G = nx.Graph()

# Add edges to graph - this also adds the nodes
G.add_edges_from([(1, 2), (1, 3), (2, 3), (3, 4), (3, 5), (4, 5), (4, 6),
                  (5, 6), (6, 7)])

# Find the maximum independent set, S
S = dnx.maximum_independent_set(G,
                                sampler=sampler,
                                num_reads=10,
                                label='Example - Antenna Selection')

# Print the solution for the user
print('Maximum independent set size found is', len(S))
print(S)

# Visualize the results
k = G.subgraph(S)
notS = list(set(G.nodes()) - set(S))
othersubgraph = G.subgraph(notS)
pos = nx.spring_layout(G)
plt.figure()

# Save original problem graph
original_name = "antenna_plot_original.png"