From 2c7344a855ff2e2ff0e03563d0847016158b2cd3 Mon Sep 17 00:00:00 2001 From: Alexander Condello Date: Wed, 9 Dec 2020 10:21:33 -0800 Subject: [PATCH 1/6] Move package into dwave.plugins namespace --- dwave/__init__.py | 16 ++++++++++++ dwave/plugins/__init__.py | 16 ++++++++++++ .../plugins/networkx}/__init__.py | 22 ++++++++-------- dwave/plugins/networkx/algorithms/__init__.py | 25 +++++++++++++++++++ .../networkx}/algorithms/canonicalization.py | 2 +- .../plugins/networkx}/algorithms/clique.py | 6 ++--- .../plugins/networkx}/algorithms/coloring.py | 2 +- .../plugins/networkx}/algorithms/cover.py | 8 +++--- .../algorithms/elimination_ordering.py | 2 +- .../networkx}/algorithms/independent_set.py | 6 ++--- .../plugins/networkx}/algorithms/markov.py | 2 +- .../plugins/networkx}/algorithms/matching.py | 2 +- .../plugins/networkx}/algorithms/max_cut.py | 4 +-- .../plugins/networkx}/algorithms/social.py | 4 +-- .../plugins/networkx}/algorithms/tsp.py | 2 +- .../plugins/networkx}/default_sampler.py | 2 +- .../plugins/networkx}/drawing/__init__.py | 4 +-- .../networkx}/drawing/chimera_layout.py | 6 ++--- .../drawing/distinguishable_colors.py | 0 .../networkx}/drawing/pegasus_layout.py | 18 ++++++------- .../plugins/networkx}/drawing/qubit_layout.py | 2 +- .../plugins/networkx}/exceptions.py | 0 .../plugins/networkx}/generators/__init__.py | 6 ++--- .../plugins/networkx}/generators/chimera.py | 2 +- .../plugins/networkx}/generators/markov.py | 0 .../plugins/networkx}/generators/pegasus.py | 2 +- .../plugins/networkx}/package_info.py | 0 .../plugins/networkx}/utils/__init__.py | 2 +- .../plugins/networkx}/utils/decorators.py | 8 +++--- dwave_networkx/algorithms/__init__.py | 25 ------------------- .../examples => examples}/chimera_graph.py | 2 +- .../examples => examples}/max_cut.py | 2 +- .../max_independent_set.py | 2 +- .../min_maximal_matching.py | 2 +- .../examples => examples}/min_vertex_color.py | 2 +- .../examples => examples}/min_vertex_cover.py | 2 +- setup.py | 18 +++++++------ 37 files changed, 130 insertions(+), 96 deletions(-) create mode 100644 dwave/__init__.py create mode 100644 dwave/plugins/__init__.py rename {dwave_networkx => dwave/plugins/networkx}/__init__.py (55%) create mode 100644 dwave/plugins/networkx/algorithms/__init__.py rename {dwave_networkx => dwave/plugins/networkx}/algorithms/canonicalization.py (98%) rename {dwave_networkx => dwave/plugins/networkx}/algorithms/clique.py (97%) rename {dwave_networkx => dwave/plugins/networkx}/algorithms/coloring.py (99%) rename {dwave_networkx => dwave/plugins/networkx}/algorithms/cover.py (96%) rename {dwave_networkx => dwave/plugins/networkx}/algorithms/elimination_ordering.py (99%) rename {dwave_networkx => dwave/plugins/networkx}/algorithms/independent_set.py (97%) rename {dwave_networkx => dwave/plugins/networkx}/algorithms/markov.py (98%) rename {dwave_networkx => dwave/plugins/networkx}/algorithms/matching.py (99%) rename {dwave_networkx => dwave/plugins/networkx}/algorithms/max_cut.py (97%) rename {dwave_networkx => dwave/plugins/networkx}/algorithms/social.py (97%) rename {dwave_networkx => dwave/plugins/networkx}/algorithms/tsp.py (99%) rename {dwave_networkx => dwave/plugins/networkx}/default_sampler.py (98%) rename {dwave_networkx => dwave/plugins/networkx}/drawing/__init__.py (83%) rename {dwave_networkx => dwave/plugins/networkx}/drawing/chimera_layout.py (97%) rename {dwave_networkx => dwave/plugins/networkx}/drawing/distinguishable_colors.py (100%) rename {dwave_networkx => dwave/plugins/networkx}/drawing/pegasus_layout.py (94%) rename {dwave_networkx => dwave/plugins/networkx}/drawing/qubit_layout.py (99%) rename {dwave_networkx => dwave/plugins/networkx}/exceptions.py (100%) rename {dwave_networkx => dwave/plugins/networkx}/generators/__init__.py (77%) rename {dwave_networkx => dwave/plugins/networkx}/generators/chimera.py (99%) rename {dwave_networkx => dwave/plugins/networkx}/generators/markov.py (100%) rename {dwave_networkx => dwave/plugins/networkx}/generators/pegasus.py (99%) rename {dwave_networkx => dwave/plugins/networkx}/package_info.py (100%) rename {dwave_networkx => dwave/plugins/networkx}/utils/__init__.py (92%) rename {dwave_networkx => dwave/plugins/networkx}/utils/decorators.py (95%) delete mode 100644 dwave_networkx/algorithms/__init__.py rename {dwave_networkx/examples => examples}/chimera_graph.py (95%) rename {dwave_networkx/examples => examples}/max_cut.py (96%) rename {dwave_networkx/examples => examples}/max_independent_set.py (96%) rename {dwave_networkx/examples => examples}/min_maximal_matching.py (96%) rename {dwave_networkx/examples => examples}/min_vertex_color.py (96%) rename {dwave_networkx/examples => examples}/min_vertex_cover.py (97%) diff --git a/dwave/__init__.py b/dwave/__init__.py new file mode 100644 index 00000000..31141c58 --- /dev/null +++ b/dwave/__init__.py @@ -0,0 +1,16 @@ +# Copyright 2020 D-Wave Systems Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# 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. + +import pkgutil +__path__ = pkgutil.extend_path(__path__, __name__) diff --git a/dwave/plugins/__init__.py b/dwave/plugins/__init__.py new file mode 100644 index 00000000..31141c58 --- /dev/null +++ b/dwave/plugins/__init__.py @@ -0,0 +1,16 @@ +# Copyright 2020 D-Wave Systems Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# 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. + +import pkgutil +__path__ = pkgutil.extend_path(__path__, __name__) diff --git a/dwave_networkx/__init__.py b/dwave/plugins/networkx/__init__.py similarity index 55% rename from dwave_networkx/__init__.py rename to dwave/plugins/networkx/__init__.py index f948c483..41714b05 100644 --- a/dwave_networkx/__init__.py +++ b/dwave/plugins/networkx/__init__.py @@ -12,20 +12,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -import dwave_networkx.generators -from dwave_networkx.generators import * +import dwave.plugins.networkx.generators +from dwave.plugins.networkx.generators import * -import dwave_networkx.algorithms -from dwave_networkx.algorithms import * +import dwave.plugins.networkx.algorithms +from dwave.plugins.networkx.algorithms import * -import dwave_networkx.utils -from dwave_networkx.exceptions import * +import dwave.plugins.networkx.utils +from dwave.plugins.networkx.exceptions import * -import dwave_networkx.default_sampler -from dwave_networkx.default_sampler import * +import dwave.plugins.networkx.default_sampler +from dwave.plugins.networkx.default_sampler import * -import dwave_networkx.drawing -from dwave_networkx.drawing import * +import dwave.plugins.networkx.drawing +from dwave.plugins.networkx.drawing import * -from dwave_networkx.package_info import __version__, __author__, \ +from dwave.plugins.networkx.package_info import __version__, __author__, \ __authoremail__, __description__ diff --git a/dwave/plugins/networkx/algorithms/__init__.py b/dwave/plugins/networkx/algorithms/__init__.py new file mode 100644 index 00000000..72b393c8 --- /dev/null +++ b/dwave/plugins/networkx/algorithms/__init__.py @@ -0,0 +1,25 @@ +# Copyright 2018 D-Wave Systems Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# 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 dwave.plugins.networkx.algorithms.independent_set import * +from dwave.plugins.networkx.algorithms.canonicalization import * +from dwave.plugins.networkx.algorithms.clique import * +from dwave.plugins.networkx.algorithms.cover import * +from dwave.plugins.networkx.algorithms.matching import * +from dwave.plugins.networkx.algorithms.social import * +from dwave.plugins.networkx.algorithms.elimination_ordering import * +from dwave.plugins.networkx.algorithms.coloring import * +from dwave.plugins.networkx.algorithms.max_cut import * +from dwave.plugins.networkx.algorithms.markov import * +from dwave.plugins.networkx.algorithms.tsp import * diff --git a/dwave_networkx/algorithms/canonicalization.py b/dwave/plugins/networkx/algorithms/canonicalization.py similarity index 98% rename from dwave_networkx/algorithms/canonicalization.py rename to dwave/plugins/networkx/algorithms/canonicalization.py index af4f90c0..f4ace843 100644 --- a/dwave_networkx/algorithms/canonicalization.py +++ b/dwave/plugins/networkx/algorithms/canonicalization.py @@ -14,7 +14,7 @@ import math -from dwave_networkx.generators.chimera import chimera_coordinates +from dwave.plugins.networkx.generators.chimera import chimera_coordinates __all__ = ['canonical_chimera_labeling'] diff --git a/dwave_networkx/algorithms/clique.py b/dwave/plugins/networkx/algorithms/clique.py similarity index 97% rename from dwave_networkx/algorithms/clique.py rename to dwave/plugins/networkx/algorithms/clique.py index 89600fb6..04727461 100644 --- a/dwave_networkx/algorithms/clique.py +++ b/dwave/plugins/networkx/algorithms/clique.py @@ -13,9 +13,9 @@ # limitations under the License. import networkx as nx -import dwave_networkx as dnx +import dwave.plugins.networkx as dnx -from dwave_networkx.utils import binary_quadratic_model_sampler +from dwave.plugins.networkx.utils import binary_quadratic_model_sampler __all__ = ["maximum_clique", "clique_number", "is_clique"] @@ -172,7 +172,7 @@ def is_clique(G, clique_nodes): the horizontal tile's nodes; the second has nodes from the horizontal and verical tiles. - >>> import dwave_networkx as dnx + >>> import dwave.plugins.networkx as dnx >>> G = dnx.chimera_graph(1, 1, 4) >>> dnx.is_clique(G, [0, 1, 2, 3]) False diff --git a/dwave_networkx/algorithms/coloring.py b/dwave/plugins/networkx/algorithms/coloring.py similarity index 99% rename from dwave_networkx/algorithms/coloring.py rename to dwave/plugins/networkx/algorithms/coloring.py index 8ccf6713..e8dd4ed5 100644 --- a/dwave_networkx/algorithms/coloring.py +++ b/dwave/plugins/networkx/algorithms/coloring.py @@ -17,7 +17,7 @@ import networkx as nx -from dwave_networkx.utils import binary_quadratic_model_sampler +from dwave.plugins.networkx.utils import binary_quadratic_model_sampler __all__ = ["is_vertex_coloring", "is_cycle", diff --git a/dwave_networkx/algorithms/cover.py b/dwave/plugins/networkx/algorithms/cover.py similarity index 96% rename from dwave_networkx/algorithms/cover.py rename to dwave/plugins/networkx/algorithms/cover.py index 28888fb3..8ea15498 100644 --- a/dwave_networkx/algorithms/cover.py +++ b/dwave/plugins/networkx/algorithms/cover.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from dwave_networkx.algorithms.independent_set import maximum_weighted_independent_set -from dwave_networkx.utils import binary_quadratic_model_sampler +from dwave.plugins.networkx.algorithms.independent_set import maximum_weighted_independent_set +from dwave.plugins.networkx.utils import binary_quadratic_model_sampler __all__ = ['min_weighted_vertex_cover', 'min_vertex_cover', 'is_vertex_cover'] @@ -125,7 +125,7 @@ def min_vertex_cover(G, sampler=None, lagrange=2.0, **sampler_args): vertical (vertices 4,5,6,7) tiles connect to all 16 edges, so repeated executions can return either set. - >>> import dwave_networkx as dnx + >>> import dwave.plugins.networkx as dnx >>> import dimod >>> sampler = dimod.ExactSolver() # small testing sampler >>> G = dnx.chimera_graph(1, 1, 4) @@ -177,7 +177,7 @@ def is_vertex_cover(G, vertex_cover): unit cell. The first uses the set of the four horizontal qubits, which do constitute a cover; the second set removes one node. - >>> import dwave_networkx as dnx + >>> import dwave.plugins.networkx as dnx >>> G = dnx.chimera_graph(1, 1, 4) >>> cover = [0, 1, 2, 3] >>> dnx.is_vertex_cover(G,cover) diff --git a/dwave_networkx/algorithms/elimination_ordering.py b/dwave/plugins/networkx/algorithms/elimination_ordering.py similarity index 99% rename from dwave_networkx/algorithms/elimination_ordering.py rename to dwave/plugins/networkx/algorithms/elimination_ordering.py index eff3d14a..e3bdaed2 100644 --- a/dwave_networkx/algorithms/elimination_ordering.py +++ b/dwave/plugins/networkx/algorithms/elimination_ordering.py @@ -18,7 +18,7 @@ import networkx as nx -from dwave_networkx.generators.pegasus import pegasus_coordinates +from dwave.plugins.networkx.generators.pegasus import pegasus_coordinates __all__ = ['is_almost_simplicial', 'is_simplicial', diff --git a/dwave_networkx/algorithms/independent_set.py b/dwave/plugins/networkx/algorithms/independent_set.py similarity index 97% rename from dwave_networkx/algorithms/independent_set.py rename to dwave/plugins/networkx/algorithms/independent_set.py index 241a690b..0f2dbbb3 100644 --- a/dwave_networkx/algorithms/independent_set.py +++ b/dwave/plugins/networkx/algorithms/independent_set.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from dwave_networkx.utils import binary_quadratic_model_sampler +from dwave.plugins.networkx.utils import binary_quadratic_model_sampler __all__ = ["maximum_weighted_independent_set", "maximum_weighted_independent_set_qubo", @@ -195,7 +195,7 @@ def is_independent_set(G, indep_nodes): the horizontal tile's nodes; the second has nodes from the horizontal and verical tiles. - >>> import dwave_networkx as dnx + >>> import dwave.plugins.networkx as dnx >>> G = dnx.chimera_graph(1, 1, 4) >>> dnx.is_independent_set(G, [0, 1, 2, 3]) True @@ -230,7 +230,7 @@ def maximum_weighted_independent_set_qubo(G, weight=None, lagrange=2.0): Examples -------- - >>> from dwave_networkx.algorithms.independent_set import maximum_weighted_independent_set_qubo + >>> from dwave.plugins.networkx.algorithms.independent_set import maximum_weighted_independent_set_qubo ... >>> G = nx.path_graph(3) >>> Q = maximum_weighted_independent_set_qubo(G, weight='weight', lagrange=2.0) diff --git a/dwave_networkx/algorithms/markov.py b/dwave/plugins/networkx/algorithms/markov.py similarity index 98% rename from dwave_networkx/algorithms/markov.py rename to dwave/plugins/networkx/algorithms/markov.py index 3144cff3..910f43ff 100644 --- a/dwave_networkx/algorithms/markov.py +++ b/dwave/plugins/networkx/algorithms/markov.py @@ -14,7 +14,7 @@ import dimod -from dwave_networkx.utils import binary_quadratic_model_sampler +from dwave.plugins.networkx.utils import binary_quadratic_model_sampler __all__ = ['sample_markov_network', 'markov_network_bqm'] diff --git a/dwave_networkx/algorithms/matching.py b/dwave/plugins/networkx/algorithms/matching.py similarity index 99% rename from dwave_networkx/algorithms/matching.py rename to dwave/plugins/networkx/algorithms/matching.py index 14237626..c188f54e 100644 --- a/dwave_networkx/algorithms/matching.py +++ b/dwave/plugins/networkx/algorithms/matching.py @@ -19,7 +19,7 @@ import dimod import networkx as nx -from dwave_networkx.utils import binary_quadratic_model_sampler +from dwave.plugins.networkx.utils import binary_quadratic_model_sampler __all__ = ['is_matching', 'is_maximal_matching', diff --git a/dwave_networkx/algorithms/max_cut.py b/dwave/plugins/networkx/algorithms/max_cut.py similarity index 97% rename from dwave_networkx/algorithms/max_cut.py rename to dwave/plugins/networkx/algorithms/max_cut.py index 39baa096..7f4173ac 100644 --- a/dwave_networkx/algorithms/max_cut.py +++ b/dwave/plugins/networkx/algorithms/max_cut.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from dwave_networkx.exceptions import DWaveNetworkXException -from dwave_networkx.utils import binary_quadratic_model_sampler +from dwave.plugins.networkx.exceptions import DWaveNetworkXException +from dwave.plugins.networkx.utils import binary_quadratic_model_sampler __all__ = ["maximum_cut", "weighted_maximum_cut"] diff --git a/dwave_networkx/algorithms/social.py b/dwave/plugins/networkx/algorithms/social.py similarity index 97% rename from dwave_networkx/algorithms/social.py rename to dwave/plugins/networkx/algorithms/social.py index 91fd4834..7263b6f5 100644 --- a/dwave_networkx/algorithms/social.py +++ b/dwave/plugins/networkx/algorithms/social.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from dwave_networkx.utils import binary_quadratic_model_sampler +from dwave.plugins.networkx.utils import binary_quadratic_model_sampler __all__ = ["structural_imbalance"] @@ -159,7 +159,7 @@ def structural_imbalance_ising(S): Examples -------- >>> import dimod - >>> from dwave_networkx.algorithms.social import structural_imbalance_ising + >>> from dwave.plugins.networkx.algorithms.social import structural_imbalance_ising ... >>> S = nx.Graph() >>> S.add_edge('Alice', 'Bob', sign=1) # Alice and Bob are friendly diff --git a/dwave_networkx/algorithms/tsp.py b/dwave/plugins/networkx/algorithms/tsp.py similarity index 99% rename from dwave_networkx/algorithms/tsp.py rename to dwave/plugins/networkx/algorithms/tsp.py index d4d086e6..7aefb1a6 100644 --- a/dwave_networkx/algorithms/tsp.py +++ b/dwave/plugins/networkx/algorithms/tsp.py @@ -16,7 +16,7 @@ from collections import defaultdict -from dwave_networkx.utils import binary_quadratic_model_sampler +from dwave.plugins.networkx.utils import binary_quadratic_model_sampler __all__ = ["traveling_salesperson", "traveling_salesperson_qubo", diff --git a/dwave_networkx/default_sampler.py b/dwave/plugins/networkx/default_sampler.py similarity index 98% rename from dwave_networkx/default_sampler.py rename to dwave/plugins/networkx/default_sampler.py index 8763e663..4a0feb7a 100644 --- a/dwave_networkx/default_sampler.py +++ b/dwave/plugins/networkx/default_sampler.py @@ -62,7 +62,7 @@ """ -from dwave_networkx.utils.decorators import binary_quadratic_model_sampler +from dwave.plugins.networkx.utils.decorators import binary_quadratic_model_sampler __all__ = ['set_default_sampler', 'get_default_sampler', 'unset_default_sampler'] diff --git a/dwave_networkx/drawing/__init__.py b/dwave/plugins/networkx/drawing/__init__.py similarity index 83% rename from dwave_networkx/drawing/__init__.py rename to dwave/plugins/networkx/drawing/__init__.py index ffd58558..e236bf07 100644 --- a/dwave_networkx/drawing/__init__.py +++ b/dwave/plugins/networkx/drawing/__init__.py @@ -12,5 +12,5 @@ # See the License for the specific language governing permissions and # limitations under the License. -from dwave_networkx.drawing.chimera_layout import * -from dwave_networkx.drawing.pegasus_layout import * +from dwave.plugins.networkx.drawing.chimera_layout import * +from dwave.plugins.networkx.drawing.pegasus_layout import * diff --git a/dwave_networkx/drawing/chimera_layout.py b/dwave/plugins/networkx/drawing/chimera_layout.py similarity index 97% rename from dwave_networkx/drawing/chimera_layout.py rename to dwave/plugins/networkx/drawing/chimera_layout.py index 486a2033..0526033d 100644 --- a/dwave_networkx/drawing/chimera_layout.py +++ b/dwave/plugins/networkx/drawing/chimera_layout.py @@ -20,8 +20,8 @@ import networkx as nx from networkx import draw -from dwave_networkx.drawing.qubit_layout import draw_qubit_graph, draw_embedding, draw_yield -from dwave_networkx.generators.chimera import chimera_graph, find_chimera_indices, chimera_coordinates +from dwave.plugins.networkx.drawing.qubit_layout import draw_qubit_graph, draw_embedding, draw_yield +from dwave.plugins.networkx.generators.chimera import chimera_graph, find_chimera_indices, chimera_coordinates __all__ = ['chimera_layout', 'draw_chimera', 'draw_chimera_embedding', 'draw_chimera_yield'] @@ -219,7 +219,7 @@ def draw_chimera(G, **kwargs): -------- >>> # Plot 2x2 Chimera unit cells >>> import networkx as nx - >>> import dwave_networkx as dnx + >>> import dwave.plugins.networkx as dnx >>> import matplotlib.pyplot as plt # doctest: +SKIP >>> G = dnx.chimera_graph(2, 2, 4) >>> dnx.draw_chimera(G) # doctest: +SKIP diff --git a/dwave_networkx/drawing/distinguishable_colors.py b/dwave/plugins/networkx/drawing/distinguishable_colors.py similarity index 100% rename from dwave_networkx/drawing/distinguishable_colors.py rename to dwave/plugins/networkx/drawing/distinguishable_colors.py diff --git a/dwave_networkx/drawing/pegasus_layout.py b/dwave/plugins/networkx/drawing/pegasus_layout.py similarity index 94% rename from dwave_networkx/drawing/pegasus_layout.py rename to dwave/plugins/networkx/drawing/pegasus_layout.py index 2e8425b1..6bdb08c7 100644 --- a/dwave_networkx/drawing/pegasus_layout.py +++ b/dwave/plugins/networkx/drawing/pegasus_layout.py @@ -19,9 +19,9 @@ import networkx as nx from networkx import draw -from dwave_networkx.drawing.qubit_layout import draw_qubit_graph, draw_embedding, draw_yield -from dwave_networkx.generators.pegasus import pegasus_graph, pegasus_coordinates -from dwave_networkx.drawing.chimera_layout import chimera_node_placer_2d +from dwave.plugins.networkx.drawing.qubit_layout import draw_qubit_graph, draw_embedding, draw_yield +from dwave.plugins.networkx.generators.pegasus import pegasus_graph, pegasus_coordinates +from dwave.plugins.networkx.drawing.chimera_layout import chimera_node_placer_2d __all__ = ['pegasus_layout', @@ -40,7 +40,7 @@ def pegasus_layout(G, scale=1., center=None, dim=2, crosses=False): ---------- G : NetworkX graph A Pegasus graph or a subgraph of a Pegasus graph, as produced by - the :func:`dwave_networkx.pegasus_graph` function. + the :func:`dwave.plugins.networkx.pegasus_graph` function. scale : float (default 1.) Scale factor. A setting of ``scale = 1`` fits all positions within @@ -73,7 +73,7 @@ def pegasus_layout(G, scale=1., center=None, dim=2, crosses=False): """ if not isinstance(G, nx.Graph) or G.graph.get("family") != "pegasus": - raise ValueError("G must be generated by dwave_networkx.pegasus_graph") + raise ValueError("G must be generated by dwave.plugins.networkx.pegasus_graph") if G.graph.get('labels') == 'nice': m = 3*(G.graph['rows']-1) @@ -102,7 +102,7 @@ def pegasus_node_placer_2d(G, scale=1., center=None, dim=2, crosses=False): ---------- G : NetworkX graph A Pegasus graph or a subgraph of a Pegasus graph, as produced by - the :func:`dwave_networkx.pegasus_graph` function. + the :func:`dwave.plugins.networkx.pegasus_graph` function. scale : float (default 1.) Scale factor. A setting of ``scale = 1`` fits all positions within @@ -184,7 +184,7 @@ def draw_pegasus(G, crosses=False, **kwargs): ---------- G : NetworkX graph A Pegasus graph or a subgraph of a Pegasus graph, as produced by - the :func:`dwave_networkx.pegasus_graph` function. + the :func:`dwave.plugins.networkx.pegasus_graph` function. linear_biases : dict (optional, default {}) Biases as a dict, of form {node: bias, ...}, where keys are @@ -211,7 +211,7 @@ def draw_pegasus(G, crosses=False, **kwargs): This example plots a Pegasus graph with size parameter 2. >>> import networkx as nx - >>> import dwave_networkx as dnx + >>> import dwave.plugins.networkx as dnx >>> import matplotlib.pyplot as plt # doctest: +SKIP >>> G = dnx.pegasus_graph(2) >>> dnx.draw_pegasus(G) # doctest: +SKIP @@ -229,7 +229,7 @@ def draw_pegasus_embedding(G, *args, **kwargs): ---------- G : NetworkX graph A Pegasus graph or a subgraph of a Pegasus graph, as produced by - the :func:`dwave_networkx.pegasus_graph` function. + the :func:`dwave.plugins.networkx.pegasus_graph` function. emb : dict Chains, as a dict of form {qubit: chain, ...}, where qubits are diff --git a/dwave_networkx/drawing/qubit_layout.py b/dwave/plugins/networkx/drawing/qubit_layout.py similarity index 99% rename from dwave_networkx/drawing/qubit_layout.py rename to dwave/plugins/networkx/drawing/qubit_layout.py index e8115e9a..cd77aac9 100644 --- a/dwave_networkx/drawing/qubit_layout.py +++ b/dwave/plugins/networkx/drawing/qubit_layout.py @@ -22,7 +22,7 @@ from networkx import draw -from dwave_networkx.drawing.distinguishable_colors import distinguishable_color_map +from dwave.plugins.networkx.drawing.distinguishable_colors import distinguishable_color_map __all__ = ['draw_qubit_graph'] diff --git a/dwave_networkx/exceptions.py b/dwave/plugins/networkx/exceptions.py similarity index 100% rename from dwave_networkx/exceptions.py rename to dwave/plugins/networkx/exceptions.py diff --git a/dwave_networkx/generators/__init__.py b/dwave/plugins/networkx/generators/__init__.py similarity index 77% rename from dwave_networkx/generators/__init__.py rename to dwave/plugins/networkx/generators/__init__.py index fed0b571..9b4a3d98 100644 --- a/dwave_networkx/generators/__init__.py +++ b/dwave/plugins/networkx/generators/__init__.py @@ -12,6 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from dwave_networkx.generators.chimera import * -from dwave_networkx.generators.markov import markov_network -from dwave_networkx.generators.pegasus import * +from dwave.plugins.networkx.generators.chimera import * +from dwave.plugins.networkx.generators.markov import markov_network +from dwave.plugins.networkx.generators.pegasus import * diff --git a/dwave_networkx/generators/chimera.py b/dwave/plugins/networkx/generators/chimera.py similarity index 99% rename from dwave_networkx/generators/chimera.py rename to dwave/plugins/networkx/generators/chimera.py index c3a164ec..1901d1b5 100644 --- a/dwave_networkx/generators/chimera.py +++ b/dwave/plugins/networkx/generators/chimera.py @@ -22,7 +22,7 @@ from networkx.algorithms.bipartite import color from networkx import diameter -from dwave_networkx.exceptions import DWaveNetworkXException +from dwave.plugins.networkx.exceptions import DWaveNetworkXException __all__ = ['chimera_graph', 'chimera_coordinates', diff --git a/dwave_networkx/generators/markov.py b/dwave/plugins/networkx/generators/markov.py similarity index 100% rename from dwave_networkx/generators/markov.py rename to dwave/plugins/networkx/generators/markov.py diff --git a/dwave_networkx/generators/pegasus.py b/dwave/plugins/networkx/generators/pegasus.py similarity index 99% rename from dwave_networkx/generators/pegasus.py rename to dwave/plugins/networkx/generators/pegasus.py index 5bbb87ae..b308bb35 100644 --- a/dwave_networkx/generators/pegasus.py +++ b/dwave/plugins/networkx/generators/pegasus.py @@ -19,7 +19,7 @@ import networkx as nx -from dwave_networkx.exceptions import DWaveNetworkXException +from dwave.plugins.networkx.exceptions import DWaveNetworkXException import warnings __all__ = ['pegasus_graph', diff --git a/dwave_networkx/package_info.py b/dwave/plugins/networkx/package_info.py similarity index 100% rename from dwave_networkx/package_info.py rename to dwave/plugins/networkx/package_info.py diff --git a/dwave_networkx/utils/__init__.py b/dwave/plugins/networkx/utils/__init__.py similarity index 92% rename from dwave_networkx/utils/__init__.py rename to dwave/plugins/networkx/utils/__init__.py index 4679f8c7..4f17a923 100644 --- a/dwave_networkx/utils/__init__.py +++ b/dwave/plugins/networkx/utils/__init__.py @@ -13,4 +13,4 @@ # limitations under the License. # # ================================================================================================ -from dwave_networkx.utils.decorators import * +from dwave.plugins.networkx.utils.decorators import * diff --git a/dwave_networkx/utils/decorators.py b/dwave/plugins/networkx/utils/decorators.py similarity index 95% rename from dwave_networkx/utils/decorators.py rename to dwave/plugins/networkx/utils/decorators.py index 4aba854c..bee8d602 100644 --- a/dwave_networkx/utils/decorators.py +++ b/dwave/plugins/networkx/utils/decorators.py @@ -19,7 +19,7 @@ from decorator import decorator -import dwave_networkx as dnx +import dwave.plugins.networkx as dnx __all__ = ['binary_quadratic_model_sampler'] @@ -57,8 +57,8 @@ def maximal_matching(G, sampler, **sampler_args): missing a method. >>> import networkx as nx - >>> import dwave_networkx as dnx - >>> from dwave_networkx.utils import decorators + >>> import dwave.plugins.networkx as dnx + >>> from dwave.plugins.networkx.utils import decorators >>> # Create two placeholder samplers >>> class WellDefinedSampler: ... # an example sampler, only works for independent set on complete @@ -108,7 +108,7 @@ def maximal_matching(G, sampler, **sampler_args): in () ----> 1 independent_set(G, IllDefinedSampler) in independent_set(G, sampler, **sampler_args) - /usr/local/lib/python2.7/dist-packages/dwave_networkx/utils/decorators.pyc in _binary_quadratic_model_sampler(f, *args, **kw) + /usr/local/lib/python2.7/dist-packages/dwave.plugins.networkx/utils/decorators.pyc in _binary_quadratic_model_sampler(f, *args, **kw) 61 62 if not hasattr(sampler, "sample_qubo") or not callable(sampler.sample_qubo): ---> 63 raise TypeError("expected sampler to have a 'sample_qubo' method") diff --git a/dwave_networkx/algorithms/__init__.py b/dwave_networkx/algorithms/__init__.py deleted file mode 100644 index 3c4f4ad3..00000000 --- a/dwave_networkx/algorithms/__init__.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2018 D-Wave Systems Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# 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 dwave_networkx.algorithms.independent_set import * -from dwave_networkx.algorithms.canonicalization import * -from dwave_networkx.algorithms.clique import * -from dwave_networkx.algorithms.cover import * -from dwave_networkx.algorithms.matching import * -from dwave_networkx.algorithms.social import * -from dwave_networkx.algorithms.elimination_ordering import * -from dwave_networkx.algorithms.coloring import * -from dwave_networkx.algorithms.max_cut import * -from dwave_networkx.algorithms.markov import * -from dwave_networkx.algorithms.tsp import * diff --git a/dwave_networkx/examples/chimera_graph.py b/examples/chimera_graph.py similarity index 95% rename from dwave_networkx/examples/chimera_graph.py rename to examples/chimera_graph.py index 073c61c3..e10aed03 100644 --- a/dwave_networkx/examples/chimera_graph.py +++ b/examples/chimera_graph.py @@ -14,7 +14,7 @@ # import matplotlib.pyplot as plt -import dwave_networkx as dnx +import dwave.plugins.networkx as dnx import networkx as nx G = dnx.chimera_graph(2, 2, 4) diff --git a/dwave_networkx/examples/max_cut.py b/examples/max_cut.py similarity index 96% rename from dwave_networkx/examples/max_cut.py rename to examples/max_cut.py index 98fd1ae0..20a049cb 100644 --- a/dwave_networkx/examples/max_cut.py +++ b/examples/max_cut.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import dwave_networkx as dnx +import dwave.plugins.networkx as dnx import networkx as nx import dimod diff --git a/dwave_networkx/examples/max_independent_set.py b/examples/max_independent_set.py similarity index 96% rename from dwave_networkx/examples/max_independent_set.py rename to examples/max_independent_set.py index 194e0f46..dddd0385 100644 --- a/dwave_networkx/examples/max_independent_set.py +++ b/examples/max_independent_set.py @@ -13,7 +13,7 @@ # limitations under the License. import networkx as nx -import dwave_networkx as dnx +import dwave.plugins.networkx as dnx import dimod # Use basic simulated annealer diff --git a/dwave_networkx/examples/min_maximal_matching.py b/examples/min_maximal_matching.py similarity index 96% rename from dwave_networkx/examples/min_maximal_matching.py rename to examples/min_maximal_matching.py index 82697ee2..b71422d9 100644 --- a/dwave_networkx/examples/min_maximal_matching.py +++ b/examples/min_maximal_matching.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import dwave_networkx as dnx +import dwave.plugins.networkx as dnx import dimod # Use basic simulated annealer diff --git a/dwave_networkx/examples/min_vertex_color.py b/examples/min_vertex_color.py similarity index 96% rename from dwave_networkx/examples/min_vertex_color.py rename to examples/min_vertex_color.py index 15a7fd6d..4eada9b7 100644 --- a/dwave_networkx/examples/min_vertex_color.py +++ b/examples/min_vertex_color.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import dwave_networkx as dnx +import dwave.plugins.networkx as dnx import networkx as nx import dimod diff --git a/dwave_networkx/examples/min_vertex_cover.py b/examples/min_vertex_cover.py similarity index 97% rename from dwave_networkx/examples/min_vertex_cover.py rename to examples/min_vertex_cover.py index fe86feba..69dd87db 100644 --- a/dwave_networkx/examples/min_vertex_cover.py +++ b/examples/min_vertex_cover.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import dwave_networkx as dnx +import dwave.plugins.networkx as dnx import networkx as nx import dimod diff --git a/setup.py b/setup.py index 6bcfd124..3a7a2e04 100644 --- a/setup.py +++ b/setup.py @@ -20,13 +20,14 @@ my_loc = os.path.dirname(os.path.abspath(__file__)) os.chdir(my_loc) -exec(open(os.path.join(".", "dwave_networkx", "package_info.py")).read()) +exec(open(os.path.join(".", "dwave", "plugins", "networkx", "package_info.py")).read()) packages = ['dwave_networkx', - 'dwave_networkx.algorithms', - 'dwave_networkx.utils', - 'dwave_networkx.drawing', - 'dwave_networkx.generators', + 'dwave.plugins.networkx', + 'dwave.plugins.networkx.algorithms', + 'dwave.plugins.networkx.utils', + 'dwave.plugins.networkx.drawing', + 'dwave.plugins.networkx.generators', ] install_requires = ['networkx>=2.0,<3.0', @@ -48,17 +49,18 @@ ] setup( - name='dwave_networkx', + name='dwave-networkx', version=__version__, author=__author__, author_email=__authoremail__, description=__description__, long_description=open('README.rst').read(), - url='https://github.com/dwavesystems/dwave_networkx', - download_url='https://github.com/dwavesystems/dwave_networkx/releases', + url='https://github.com/dwavesystems/dwave-networkx', + download_url='https://github.com/dwavesystems/dwave-networkx/releases', packages=packages, license='Apache 2.0', install_requires=install_requires, python_requires=python_requires, classifiers=classifiers, + zip_safe=False, ) From 80d89cbdf8b239eec02202e3514aefee99c7f8f7 Mon Sep 17 00:00:00 2001 From: Alexander Condello Date: Wed, 9 Dec 2020 13:27:37 -0800 Subject: [PATCH 2/6] Add stub package for supporting legacy imports --- dwave_networkx.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 dwave_networkx.py diff --git a/dwave_networkx.py b/dwave_networkx.py new file mode 100644 index 00000000..79999fa5 --- /dev/null +++ b/dwave_networkx.py @@ -0,0 +1,49 @@ +# Copyright 2020 D-Wave Systems Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# 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 dwave.plugins.networkx import * +from dwave.plugins.networkx import ( + __version__, + __author__, + __authoremail__, + __description__, + ) + + +# We want `dwave_networkx` to alias `dwave.plugins.networkx` so we mess around +# with the path. We use a function to make cleaning up this namespace easier. +def alias_subpackages(): + import importlib + import pkgutil + import sys + + import warnings + warnings.warn("the dwave_networkx namespace was deprecated in " + "dwave-networkx 0.9.0, please use " + "dwave.plugins.networkx instead.", + DeprecationWarning, stacklevel=3) + + for module in pkgutil.walk_packages(dwave.plugins.networkx.__path__, + dwave.plugins.networkx.__name__ + '.'): + # only want the subpackages + if not module.ispkg: + continue + + # pretend that each subpackage lives in the dwave_networkx namespace + package_name = 'dwave_networkx.' + module.name.split('.')[-1] + sys.modules[package_name] = importlib.import_module(module.name) + + +alias_subpackages() +del alias_subpackages From b1690e548500dce697a84e55e50e75cbe322e3b6 Mon Sep 17 00:00:00 2001 From: Alexander Condello Date: Wed, 9 Dec 2020 15:59:29 -0800 Subject: [PATCH 3/6] Fix circular imports --- dwave/plugins/networkx/__init__.py | 10 +- dwave/plugins/networkx/algorithms/clique.py | 7 +- dwave/plugins/networkx/default_sampler.py | 134 ++++++++++++++++++- dwave/plugins/networkx/exceptions.py | 4 + dwave/plugins/networkx/utils/__init__.py | 3 +- dwave/plugins/networkx/utils/decorators.py | 137 +------------------- 6 files changed, 152 insertions(+), 143 deletions(-) diff --git a/dwave/plugins/networkx/__init__.py b/dwave/plugins/networkx/__init__.py index 41714b05..0d009fb3 100644 --- a/dwave/plugins/networkx/__init__.py +++ b/dwave/plugins/networkx/__init__.py @@ -19,6 +19,8 @@ from dwave.plugins.networkx.algorithms import * import dwave.plugins.networkx.utils + +import dwave.plugins.networkx.exceptions from dwave.plugins.networkx.exceptions import * import dwave.plugins.networkx.default_sampler @@ -27,5 +29,9 @@ import dwave.plugins.networkx.drawing from dwave.plugins.networkx.drawing import * -from dwave.plugins.networkx.package_info import __version__, __author__, \ - __authoremail__, __description__ +from dwave.plugins.networkx.package_info import ( + __version__, + __author__, + __authoremail__, + __description__, + ) diff --git a/dwave/plugins/networkx/algorithms/clique.py b/dwave/plugins/networkx/algorithms/clique.py index 04727461..bc5fb75f 100644 --- a/dwave/plugins/networkx/algorithms/clique.py +++ b/dwave/plugins/networkx/algorithms/clique.py @@ -13,12 +13,14 @@ # limitations under the License. import networkx as nx -import dwave.plugins.networkx as dnx +from dwave.plugins.networkx.algorithms.independent_set import maximum_independent_set from dwave.plugins.networkx.utils import binary_quadratic_model_sampler + __all__ = ["maximum_clique", "clique_number", "is_clique"] + @binary_quadratic_model_sampler(1) def maximum_clique(G, sampler=None, lagrange=2.0, **sampler_args): """ @@ -87,7 +89,7 @@ def maximum_clique(G, sampler=None, lagrange=2.0, **sampler_args): # 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) + return maximum_independent_set(complement_G, sampler, lagrange, **sampler_args) @binary_quadratic_model_sampler(1) @@ -145,6 +147,7 @@ def clique_number(G, sampler=None, lagrange=2.0, **sampler_args): """ return len(maximum_clique(G, sampler, lagrange, **sampler_args)) + def is_clique(G, clique_nodes): """Determines whether the given nodes form a clique. diff --git a/dwave/plugins/networkx/default_sampler.py b/dwave/plugins/networkx/default_sampler.py index 4a0feb7a..14af6555 100644 --- a/dwave/plugins/networkx/default_sampler.py +++ b/dwave/plugins/networkx/default_sampler.py @@ -61,15 +61,145 @@ [0] """ +from decorator import decorator -from dwave.plugins.networkx.utils.decorators import binary_quadratic_model_sampler +from dwave.plugins.networkx.exceptions import DWaveNetworkXMissingSampler -__all__ = ['set_default_sampler', 'get_default_sampler', 'unset_default_sampler'] +__all__ = ['set_default_sampler', + 'get_default_sampler', + 'unset_default_sampler', + ] _SAMPLER = None +def binary_quadratic_model_sampler(which_args): + """Decorator to validate sampler arguments. + + Parameters + ---------- + which_args : int or sequence of ints + Location of the sampler arguments of the input function in the form + `function_name(args, *kw)`. If more than one + sampler is allowed, can be a list of locations. + + Returns + ------- + _binary_quadratic_model_sampler : function + Caller function that validates the sampler format. A sampler + is expected to have `sample_qubo` and `sample_ising` methods. + Alternatively, if no sampler is provided (or sampler is None), + the sampler set by the `set_default_sampler` function is provided to + the function. + + Examples + -------- + Decorate functions like this:: + + @binary_quadratic_model_sampler(1) + def maximal_matching(G, sampler, **sampler_args): + pass + + This example validates two placeholder samplers, which return a correct + response only in the case of finding an independent set on a complete graph + (where one node is always an independent set), the first valid, the second + missing a method. + + >>> import networkx as nx + >>> import dwave.plugins.networkx as dnx + >>> from dwave.plugins.networkx.utils import decorators + >>> # Create two placeholder samplers + >>> class WellDefinedSampler: + ... # an example sampler, only works for independent set on complete + ... # graphs + ... def __init__(self, name): + ... self.name = name + ... def sample_ising(self, h, J): + ... sample = {v: -1 for v in h} + ... sample[0] = 1 # set one node to true + ... return [sample] + ... def sample_qubo(self, Q): + ... sample = {v: 0 for v in set().union(*Q)} + ... sample[0] = 1 # set one node to true + ... return [sample] + ... def __str__(self): + ... return self.name + ... + >>> class IllDefinedSampler: + ... # an example sampler missing a `sample_qubo` method + ... def __init__(self, name): + ... self.name = name + ... def sample_ising(self, h, J): + ... sample = {v: -1 for v in h} + ... sample[0] = 1 # set one node to true + ... return [sample] + ... def __str__(self): + ... return self.name + ... + >>> sampler1 = WellDefinedSampler('sampler1') + >>> sampler2 = IllDefinedSampler('sampler2') + >>> # Define a placeholder independent-set function with the decorator + >>> @dnx.utils.binary_quadratic_model_sampler(1) + ... def independent_set(G, sampler, **sampler_args): + ... Q = {(node, node): -1 for node in G} + ... Q.update({edge: 2 for edge in G.edges}) + ... response = sampler.sample_qubo(Q, **sampler_args) + ... sample = next(iter(response)) + ... return [node for node in sample if sample[node] > 0] + ... + >>> # Validate the samplers + >>> G = nx.complete_graph(5) + >>> independent_set(G, sampler1) + [0] + >>> independent_set(G, sampler2) # doctest: +SKIP + --------------------------------------------------------------------------- + TypeError Traceback (most recent call last) + in () + ----> 1 independent_set(G, IllDefinedSampler) + in independent_set(G, sampler, **sampler_args) + /usr/local/lib/python2.7/dist-packages/dwave.plugins.networkx/utils/decorators.pyc in _binary_quadratic_model_sampler(f, *args, **kw) + 61 + 62 if not hasattr(sampler, "sample_qubo") or not callable(sampler.sample_qubo): + ---> 63 raise TypeError("expected sampler to have a 'sample_qubo' method") + 64 if not hasattr(sampler, "sample_ising") or not callable(sampler.sample_ising): + 65 raise TypeError("expected sampler to have a 'sample_ising' method") + TypeError: expected sampler to have a 'sample_qubo' method + + """ + @decorator + def _binary_quadratic_model_sampler(f, *args, **kw): + # convert into a sequence if necessary + if isinstance(which_args, int): + iter_args = (which_args,) + else: + iter_args = iter(which_args) + + # check each sampler for the correct methods + new_args = [arg for arg in args] + for idx in iter_args: + sampler = args[idx] + + # if no sampler is provided, get the default sampler if it has + # been set + if sampler is None: + # this sampler has already been vetted + default_sampler = get_default_sampler() + if default_sampler is None: + raise DWaveNetworkXMissingSampler('no default sampler set') + new_args[idx] = default_sampler + continue + + if not hasattr(sampler, "sample_qubo") or not callable(sampler.sample_qubo): + raise TypeError("expected sampler to have a 'sample_qubo' method") + if not hasattr(sampler, "sample_ising") or not callable(sampler.sample_ising): + raise TypeError("expected sampler to have a 'sample_ising' method") + + # now run the function and return the results + return f(*new_args, **kw) + return _binary_quadratic_model_sampler + + @binary_quadratic_model_sampler(0) def set_default_sampler(sampler): """Sets a default binary quadratic model sampler. diff --git a/dwave/plugins/networkx/exceptions.py b/dwave/plugins/networkx/exceptions.py index 7678d3f4..bf57ec07 100644 --- a/dwave/plugins/networkx/exceptions.py +++ b/dwave/plugins/networkx/exceptions.py @@ -21,6 +21,10 @@ from networkx import NetworkXException +__all__ = ['DWaveNetworkXException', + 'DWaveNetworkXMissingSampler', + ] + class DWaveNetworkXException(NetworkXException): """Base class for exceptions in DWaveNetworkX.""" diff --git a/dwave/plugins/networkx/utils/__init__.py b/dwave/plugins/networkx/utils/__init__.py index 4f17a923..be4c0173 100644 --- a/dwave/plugins/networkx/utils/__init__.py +++ b/dwave/plugins/networkx/utils/__init__.py @@ -11,6 +11,5 @@ # 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 dwave.plugins.networkx.utils.decorators import * diff --git a/dwave/plugins/networkx/utils/decorators.py b/dwave/plugins/networkx/utils/decorators.py index bee8d602..74e185d8 100644 --- a/dwave/plugins/networkx/utils/decorators.py +++ b/dwave/plugins/networkx/utils/decorators.py @@ -12,139 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -""" -Decorators allow for input checking and default parameter setting for -algorithms. -""" +# this submodule is kept for backwards compatibility -from decorator import decorator - -import dwave.plugins.networkx as dnx - -__all__ = ['binary_quadratic_model_sampler'] - - -def binary_quadratic_model_sampler(which_args): - """Decorator to validate sampler arguments. - - Parameters - ---------- - which_args : int or sequence of ints - Location of the sampler arguments of the input function in the form - `function_name(args, *kw)`. If more than one - sampler is allowed, can be a list of locations. - - Returns - ------- - _binary_quadratic_model_sampler : function - Caller function that validates the sampler format. A sampler - is expected to have `sample_qubo` and `sample_ising` methods. - Alternatively, if no sampler is provided (or sampler is None), - the sampler set by the `set_default_sampler` function is provided to - the function. - - Examples - -------- - Decorate functions like this:: - - @binary_quadratic_model_sampler(1) - def maximal_matching(G, sampler, **sampler_args): - pass - - This example validates two placeholder samplers, which return a correct - response only in the case of finding an independent set on a complete graph - (where one node is always an independent set), the first valid, the second - missing a method. - - >>> import networkx as nx - >>> import dwave.plugins.networkx as dnx - >>> from dwave.plugins.networkx.utils import decorators - >>> # Create two placeholder samplers - >>> class WellDefinedSampler: - ... # an example sampler, only works for independent set on complete - ... # graphs - ... def __init__(self, name): - ... self.name = name - ... def sample_ising(self, h, J): - ... sample = {v: -1 for v in h} - ... sample[0] = 1 # set one node to true - ... return [sample] - ... def sample_qubo(self, Q): - ... sample = {v: 0 for v in set().union(*Q)} - ... sample[0] = 1 # set one node to true - ... return [sample] - ... def __str__(self): - ... return self.name - ... - >>> class IllDefinedSampler: - ... # an example sampler missing a `sample_qubo` method - ... def __init__(self, name): - ... self.name = name - ... def sample_ising(self, h, J): - ... sample = {v: -1 for v in h} - ... sample[0] = 1 # set one node to true - ... return [sample] - ... def __str__(self): - ... return self.name - ... - >>> sampler1 = WellDefinedSampler('sampler1') - >>> sampler2 = IllDefinedSampler('sampler2') - >>> # Define a placeholder independent-set function with the decorator - >>> @dnx.utils.binary_quadratic_model_sampler(1) - ... def independent_set(G, sampler, **sampler_args): - ... Q = {(node, node): -1 for node in G} - ... Q.update({edge: 2 for edge in G.edges}) - ... response = sampler.sample_qubo(Q, **sampler_args) - ... sample = next(iter(response)) - ... return [node for node in sample if sample[node] > 0] - ... - >>> # Validate the samplers - >>> G = nx.complete_graph(5) - >>> independent_set(G, sampler1) - [0] - >>> independent_set(G, sampler2) # doctest: +SKIP - --------------------------------------------------------------------------- - TypeError Traceback (most recent call last) - in () - ----> 1 independent_set(G, IllDefinedSampler) - in independent_set(G, sampler, **sampler_args) - /usr/local/lib/python2.7/dist-packages/dwave.plugins.networkx/utils/decorators.pyc in _binary_quadratic_model_sampler(f, *args, **kw) - 61 - 62 if not hasattr(sampler, "sample_qubo") or not callable(sampler.sample_qubo): - ---> 63 raise TypeError("expected sampler to have a 'sample_qubo' method") - 64 if not hasattr(sampler, "sample_ising") or not callable(sampler.sample_ising): - 65 raise TypeError("expected sampler to have a 'sample_ising' method") - TypeError: expected sampler to have a 'sample_qubo' method - - """ - @decorator - def _binary_quadratic_model_sampler(f, *args, **kw): - # convert into a sequence if necessary - if isinstance(which_args, int): - iter_args = (which_args,) - else: - iter_args = iter(which_args) - - # check each sampler for the correct methods - new_args = [arg for arg in args] - for idx in iter_args: - sampler = args[idx] - - # if no sampler is provided, get the default sampler if it has - # been set - if sampler is None: - # this sampler has already been vetted - default_sampler = dnx.get_default_sampler() - if default_sampler is None: - raise dnx.DWaveNetworkXMissingSampler('no default sampler set') - new_args[idx] = default_sampler - continue - - if not hasattr(sampler, "sample_qubo") or not callable(sampler.sample_qubo): - raise TypeError("expected sampler to have a 'sample_qubo' method") - if not hasattr(sampler, "sample_ising") or not callable(sampler.sample_ising): - raise TypeError("expected sampler to have a 'sample_ising' method") - - # now run the function and return the results - return f(*new_args, **kw) - return _binary_quadratic_model_sampler +from dwave.plugins.networkx.default_sampler import binary_quadratic_model_sampler From 1ccc30de6b8f1e26dd23f7f135d74b49760c5d12 Mon Sep 17 00:00:00 2001 From: Alexander Condello Date: Wed, 9 Dec 2020 16:26:54 -0800 Subject: [PATCH 4/6] Backport ModuleInfo into py35 --- dwave_networkx.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/dwave_networkx.py b/dwave_networkx.py index 79999fa5..c31d9929 100644 --- a/dwave_networkx.py +++ b/dwave_networkx.py @@ -28,14 +28,27 @@ def alias_subpackages(): import pkgutil import sys + if sys.version_info[1] <= 5: + # in python 3.5, we need a named tuple + from collections import namedtuple + + Module = namedtuple('Module', ['module_finder', 'name', 'ispkg']) + + def walk_packages(*args, **kwargs): + for module in pkgutil.walk_packages(*args, **kwargs): + yield Module(*module) + + else: + walk_packages = pkgutil.walk_packages + import warnings warnings.warn("the dwave_networkx namespace was deprecated in " "dwave-networkx 0.9.0, please use " "dwave.plugins.networkx instead.", DeprecationWarning, stacklevel=3) - for module in pkgutil.walk_packages(dwave.plugins.networkx.__path__, - dwave.plugins.networkx.__name__ + '.'): + for module in walk_packages(dwave.plugins.networkx.__path__, + dwave.plugins.networkx.__name__ + '.'): # only want the subpackages if not module.ispkg: continue From 4468c215a4b3ed49664055a52e2aed5e13c21a1e Mon Sep 17 00:00:00 2001 From: Alexander Condello Date: Mon, 21 Dec 2020 10:20:20 -0800 Subject: [PATCH 5/6] Generate coverage for new namespace --- .coveragerc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.coveragerc b/.coveragerc index a8fd2ff1..1de9d189 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,7 +1,5 @@ [run] -# omit virtualenv and test files -omit = venv/*, */tests/* -source=dwave_networkx +source=dwave_networkx, dwave/plugins/networkx [report] exclude_lines: From e2180c6b663cd7269a20a7b07552c87092292fe0 Mon Sep 17 00:00:00 2001 From: Alexander Condello Date: Mon, 21 Dec 2020 11:18:43 -0800 Subject: [PATCH 6/6] Update docs for new namespace --- README.rst | 4 ++-- docs/conf.py | 6 +++--- docs/intro.rst | 2 +- docs/reference/algorithms/canonicalization.rst | 4 ++-- docs/reference/algorithms/clique.rst | 4 ++-- docs/reference/algorithms/coloring.rst | 2 +- docs/reference/algorithms/cover.rst | 2 +- docs/reference/algorithms/elimination_ordering.rst | 2 +- docs/reference/algorithms/index.rst | 2 +- docs/reference/algorithms/markov.rst | 2 +- docs/reference/algorithms/matching.rst | 2 +- docs/reference/algorithms/max_cut.rst | 2 +- docs/reference/algorithms/packing.rst | 6 +++--- docs/reference/algorithms/social.rst | 2 +- docs/reference/algorithms/tsp.rst | 2 +- docs/reference/default_sampler.rst | 4 ++-- docs/reference/drawing.rst | 10 +++++----- docs/reference/generators.rst | 4 ++-- docs/reference/utilities.rst | 10 +++++----- docs/reference/utilities/index.rst | 2 +- 20 files changed, 37 insertions(+), 37 deletions(-) diff --git a/README.rst b/README.rst index b5b1c0bc..ee7ff622 100644 --- a/README.rst +++ b/README.rst @@ -28,7 +28,7 @@ bipartite architecture). .. code: python ->>> import dwave_networkx as dnx +>>> import dwave.plugins.networkx as dnx >>> graph = dnx.chimera_graph(1, 1, 4) See the documentation for more examples. @@ -44,7 +44,7 @@ Installation .. code-block:: bash - pip install dwave_networkx + pip install dwave-networkx **Installation from source:** diff --git a/docs/conf.py b/docs/conf.py index f54c30a3..1bcf38dd 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -62,11 +62,11 @@ # |version| and |release|, also used in various other places throughout the # built documents. # -import dwave_networkx +import dwave.plugins.networkx # The short X.Y version. -version = dwave_networkx.__version__ +version = dwave.plugins.networkx.__version__ # The full version, including alpha/beta/rc tags. -release = dwave_networkx.__version__ +release = dwave.plugins.networkx.__version__ # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/intro.rst b/docs/intro.rst index 691ad81a..560db36d 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -28,7 +28,7 @@ Below you can see how to create Chimera graphs implemented in the D-Wave 2X and .. code:: python - import dwave_networkx as dnx + import dwave.plugins.networkx as dnx # D-Wave 2X C = dnx.chimera_graph(12, 12, 4) diff --git a/docs/reference/algorithms/canonicalization.rst b/docs/reference/algorithms/canonicalization.rst index cb4d2815..146b84af 100644 --- a/docs/reference/algorithms/canonicalization.rst +++ b/docs/reference/algorithms/canonicalization.rst @@ -2,9 +2,9 @@ Canonicalization **************** -.. automodule:: dwave_networkx.algorithms.canonicalization +.. automodule:: dwave.plugins.networkx.algorithms.canonicalization -.. currentmodule:: dwave_networkx +.. currentmodule:: dwave.plugins.networkx .. autosummary:: :toctree: generated/ diff --git a/docs/reference/algorithms/clique.rst b/docs/reference/algorithms/clique.rst index 54a357f7..da5183e6 100644 --- a/docs/reference/algorithms/clique.rst +++ b/docs/reference/algorithms/clique.rst @@ -11,9 +11,9 @@ such that for every two vertices in C there exists an edge connecting the two. :align: center :scale: 40 % -.. automodule:: dwave_networkx.algorithms.clique +.. automodule:: dwave.plugins.networkx.algorithms.clique -.. currentmodule:: dwave_networkx +.. currentmodule:: dwave.plugins.networkx .. autosummary:: :toctree: generated/ diff --git a/docs/reference/algorithms/coloring.rst b/docs/reference/algorithms/coloring.rst index 199ecb51..3b244fbb 100644 --- a/docs/reference/algorithms/coloring.rst +++ b/docs/reference/algorithms/coloring.rst @@ -20,7 +20,7 @@ border (represented by an edge of the graph) have different colors. Coloring a map of Canada with four colors. -.. automodule:: dwave_networkx.algorithms.coloring +.. automodule:: dwave.plugins.networkx.algorithms.coloring .. autosummary:: :toctree: generated/ diff --git a/docs/reference/algorithms/cover.rst b/docs/reference/algorithms/cover.rst index bd19f889..e865a49b 100644 --- a/docs/reference/algorithms/cover.rst +++ b/docs/reference/algorithms/cover.rst @@ -16,7 +16,7 @@ set. (the horizontal tile of the Chimera unit cell) and the red set (vertical tile) connect to all 16 edges of the graph. -.. automodule:: dwave_networkx.algorithms.cover +.. automodule:: dwave.plugins.networkx.algorithms.cover .. autosummary:: :toctree: generated/ diff --git a/docs/reference/algorithms/elimination_ordering.rst b/docs/reference/algorithms/elimination_ordering.rst index 89641977..2f4ab907 100644 --- a/docs/reference/algorithms/elimination_ordering.rst +++ b/docs/reference/algorithms/elimination_ordering.rst @@ -14,7 +14,7 @@ whose neighborhood induces a clique. A perfect elimination ordering is an ordering of vertices :math:`1..n` such that any vertex :math:`i` is simplicial for the subset of vertices :math:`i..n`. -.. automodule:: dwave_networkx.algorithms.elimination_ordering +.. automodule:: dwave.plugins.networkx.algorithms.elimination_ordering .. autosummary:: :toctree: generated/ diff --git a/docs/reference/algorithms/index.rst b/docs/reference/algorithms/index.rst index 4fd57ec9..d6409531 100644 --- a/docs/reference/algorithms/index.rst +++ b/docs/reference/algorithms/index.rst @@ -7,7 +7,7 @@ Algorithms Implementations of graph-theory algorithms on the D-Wave system and other binary quadratic model samplers. -.. currentmodule:: dwave_networkx +.. currentmodule:: dwave.plugins.networkx .. toctree:: :maxdepth: 2 diff --git a/docs/reference/algorithms/markov.rst b/docs/reference/algorithms/markov.rst index 8831e709..9a715eaa 100644 --- a/docs/reference/algorithms/markov.rst +++ b/docs/reference/algorithms/markov.rst @@ -2,7 +2,7 @@ Markov Networks *************** -.. automodule:: dwave_networkx.algorithms.markov +.. automodule:: dwave.plugins.networkx.algorithms.markov .. autosummary:: :toctree: generated/ diff --git a/docs/reference/algorithms/matching.rst b/docs/reference/algorithms/matching.rst index a57b8483..05e6357c 100644 --- a/docs/reference/algorithms/matching.rst +++ b/docs/reference/algorithms/matching.rst @@ -13,7 +13,7 @@ A matching is a subset of graph edges in which no vertex occurs more than once. A matching for a Chimera unit cell: no vertex is incident to more than one edge in the set of blue edges -.. automodule:: dwave_networkx.algorithms.matching +.. automodule:: dwave.plugins.networkx.algorithms.matching .. autosummary:: :toctree: generated/ diff --git a/docs/reference/algorithms/max_cut.rst b/docs/reference/algorithms/max_cut.rst index 50644a59..ed2d7aab 100644 --- a/docs/reference/algorithms/max_cut.rst +++ b/docs/reference/algorithms/max_cut.rst @@ -15,7 +15,7 @@ between this subset and the remaining vertices is as large as possible. {4, 5, 6, 7} cuts 16 edges; adding or removing a node decreases the number of edges between the two complementary subsets of the graph. -.. automodule:: dwave_networkx.algorithms.max_cut +.. automodule:: dwave.plugins.networkx.algorithms.max_cut .. autosummary:: :toctree: generated/ diff --git a/docs/reference/algorithms/packing.rst b/docs/reference/algorithms/packing.rst index 3800e680..63f0eab0 100644 --- a/docs/reference/algorithms/packing.rst +++ b/docs/reference/algorithms/packing.rst @@ -16,9 +16,9 @@ of its member pairs. (vertical tile) are independent sets of the graph, with no blue node adjacent to another blue node and likewise for red nodes. -.. automodule:: dwave_networkx.algorithms.independent_set +.. automodule:: dwave.plugins.networkx.algorithms.independent_set -.. currentmodule:: dwave_networkx +.. currentmodule:: dwave.plugins.networkx .. autosummary:: :toctree: generated/ @@ -30,7 +30,7 @@ of its member pairs. Helper Functions ---------------- -.. currentmodule:: dwave_networkx.algorithms.independent_set +.. currentmodule:: dwave.plugins.networkx.algorithms.independent_set .. autosummary:: :toctree: generated/ diff --git a/docs/reference/algorithms/social.rst b/docs/reference/algorithms/social.rst index 89ba0318..548d36a4 100644 --- a/docs/reference/algorithms/social.rst +++ b/docs/reference/algorithms/social.rst @@ -16,7 +16,7 @@ friendly/hostile interactions between vertices. be cleanly divided into two subsets, {Bob, Eve} and {Alice}, with friendly relations within each subset and only hostile relations between the subsets. -.. automodule:: dwave_networkx.algorithms.social +.. automodule:: dwave.plugins.networkx.algorithms.social .. autosummary:: :toctree: generated/ diff --git a/docs/reference/algorithms/tsp.rst b/docs/reference/algorithms/tsp.rst index 72c73576..26928d91 100644 --- a/docs/reference/algorithms/tsp.rst +++ b/docs/reference/algorithms/tsp.rst @@ -13,7 +13,7 @@ graph. A traveling salesperson route of [2, 1, 0, 3]. -.. automodule:: dwave_networkx.algorithms.tsp +.. automodule:: dwave.plugins.networkx.algorithms.tsp .. autosummary:: :toctree: generated/ diff --git a/docs/reference/default_sampler.rst b/docs/reference/default_sampler.rst index e5868d52..381aec86 100644 --- a/docs/reference/default_sampler.rst +++ b/docs/reference/default_sampler.rst @@ -3,9 +3,9 @@ Default sampler *************** -.. currentmodule:: dwave_networkx.default_sampler +.. currentmodule:: dwave.plugins.networkx.default_sampler -.. automodule:: dwave_networkx.default_sampler +.. automodule:: dwave.plugins.networkx.default_sampler Functions --------- diff --git a/docs/reference/drawing.rst b/docs/reference/drawing.rst index 9556ed02..b221e9e9 100644 --- a/docs/reference/drawing.rst +++ b/docs/reference/drawing.rst @@ -5,7 +5,7 @@ Drawing Tools to visualize topologies of D-Wave QPUs and weighted graph problems on them. -.. currentmodule:: dwave_networkx +.. currentmodule:: dwave.plugins.networkx .. note:: Some functionality requires `NumPy `_ and/or `Matplotlib `_\ . @@ -13,7 +13,7 @@ Tools to visualize topologies of D-Wave QPUs and weighted graph problems on them Chimera Graph Functions ----------------------- -.. automodule:: dwave_networkx.drawing.chimera_layout +.. automodule:: dwave.plugins.networkx.drawing.chimera_layout .. autosummary:: :toctree: generated/ @@ -32,7 +32,7 @@ positions on a Chimera unit cell. .. code-block:: python >>> import networkx as nx - >>> import dwave_networkx as dnx + >>> import dwave.plugins.networkx as dnx >>> import matplotlib.pyplot as plt >>> H = nx.Graph() >>> H.add_nodes_from([0, 4, 5, 6, 7]) @@ -63,7 +63,7 @@ positions on a Chimera unit cell. Pegasus Graph Functions ----------------------- -.. automodule:: dwave_networkx.drawing.pegasus_layout +.. automodule:: dwave.plugins.networkx.drawing.pegasus_layout .. autosummary:: :toctree: generated/ @@ -81,7 +81,7 @@ of nodes of a simple 5-node graph on a small Pegasus lattice. .. code-block:: python - >>> import dwave_networkx as dnx + >>> import dwave.plugins.networkx as dnx >>> import matplotlib.pyplot as plt >>> G = dnx.pegasus_graph(2) >>> H = dnx.pegasus_graph(2, node_list=[4, 40, 41, 42, 43], diff --git a/docs/reference/generators.rst b/docs/reference/generators.rst index 95318c6f..8ebe6d81 100644 --- a/docs/reference/generators.rst +++ b/docs/reference/generators.rst @@ -5,7 +5,7 @@ Graph Generators Generators for graphs, such the graphs (topologies) of D-Wave System QPUs. -.. currentmodule:: dwave_networkx +.. currentmodule:: dwave.plugins.networkx D-Wave Systems -------------- @@ -26,7 +26,7 @@ the `find_chimera()` function to determine the Chimera indices. .. code-block:: python >>> import networkx as nx - >>> import dwave_networkx as dnx + >>> import dwave.plugins.networkx as dnx >>> G = dnx.chimera_graph(1, 1, 4) >>> chimera_indices = dnx.find_chimera_indices(G) >>> print chimera_indices diff --git a/docs/reference/utilities.rst b/docs/reference/utilities.rst index 1f177908..f5ca0ee9 100644 --- a/docs/reference/utilities.rst +++ b/docs/reference/utilities.rst @@ -2,18 +2,18 @@ Utilities ********* -.. automodule:: dwave_networkx.utils -.. currentmodule:: dwave_networkx.utils +.. automodule:: dwave.plugins.networkx.utils +.. currentmodule:: dwave.plugins.networkx.utils Decorators ---------- -.. automodule:: dwave_networkx.utils.decorators +.. automodule:: dwave.plugins.networkx.utils.decorators .. autosummary:: :toctree: generated/ binary_quadratic_model_sampler -.. currentmodule:: dwave_networkx +.. currentmodule:: dwave.plugins.networkx .. toctree:: :hidden: @@ -53,7 +53,7 @@ Pegasus Exceptions ---------- -.. automodule:: dwave_networkx.exceptions +.. automodule:: dwave.plugins.networkx.exceptions .. autosummary:: :toctree: generated/ diff --git a/docs/reference/utilities/index.rst b/docs/reference/utilities/index.rst index c635a31b..d4f44e42 100644 --- a/docs/reference/utilities/index.rst +++ b/docs/reference/utilities/index.rst @@ -3,7 +3,7 @@ Coordinates Conversion ********************** -.. automodule:: dwave_networkx +.. automodule:: dwave.plugins.networkx .. autoclass:: chimera_coordinates