Skip to content

Commit

Permalink
Make consistent with minorminer new modules dwavesystems/minorminer#259
Browse files Browse the repository at this point in the history
…. Minor refactor
  • Loading branch information
jackraymond committed Dec 27, 2024
1 parent d921407 commit 3fa30bc
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 48 deletions.
9 changes: 6 additions & 3 deletions tutorial_code/embed_loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
from minorminer.utils.parallel_embeddings import (
find_sublattice_embeddings,
embeddings_to_array,
lattice_size,
)

from minorminer.utils.feasibility import (
lattice_size,
embedding_feasibility_filter,
lattice_size_lower_bound,
)

Expand Down Expand Up @@ -75,6 +76,8 @@ def embed_loops(
G = dimod.to_networkx_graph(bqm)
A = sampler.to_networkx_graph()

if not embedding_feasibility_filter(S=G, T=A):
raise ValueError(f"Embedding {G} on {A} is infeasible")
sublattice_size = kwargs.pop(
"sublattice_size",
min(lattice_size_lower_bound(S=G, T=A) + 1, lattice_size(T=A)),
Expand All @@ -90,7 +93,7 @@ def embed_loops(
"\nTo accelerate the process a smaller lattice (L) might be "
"considered and/or the search restricted to max_num_emb=1."
)
max_num_emb = kwargs.pop("max_num_emb", float("Inf"))
max_num_emb = kwargs.pop("max_num_emb", G.number_of_nodes()//A.number_of_nodes())
embedder_kwargs = {"timeout": kwargs.pop("timeout", 10)}
embeddings = embeddings_to_array(
find_sublattice_embeddings(
Expand Down Expand Up @@ -123,7 +126,7 @@ def embed_loops(
return embeddings


def main(max_num_emb=float("inf")):
def main(max_num_emb=None):
from time import perf_counter

L = 2048 # L=2048 anticipate ~ 2.5 seconds on i7
Expand Down
6 changes: 3 additions & 3 deletions tutorial_code/embed_square_lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
from minorminer.utils.parallel_embeddings import (
find_sublattice_embeddings,
embeddings_to_array,
lattice_size,
)

from minorminer.utils.feasibility import (
embedding_feasibility_filter,
lattice_size,
lattice_size_lower_bound,
)

Expand Down Expand Up @@ -102,7 +102,7 @@ def embed_square_lattice(
"\nTo accelerate the process a smaller lattice (L) might be "
"considered and/or the search restricted to max_num_emb=1."
)
max_num_emb = kwargs.pop("max_num_emb", float("Inf"))
max_num_emb = kwargs.pop("max_num_emb", 1) # many can be slow
embedder_kwargs = {"timeout": kwargs.pop("timeout", 10)}
embeddings = embeddings_to_array(
find_sublattice_embeddings(
Expand Down Expand Up @@ -135,7 +135,7 @@ def embed_square_lattice(
return embeddings, bqm


def main(max_num_emb=float("inf")):
def main(max_num_emb=1):
from time import perf_counter

L = 10 # L=2048 anticipate ~ 14 seconds on i7
Expand Down
6 changes: 3 additions & 3 deletions tutorial_code/example0_1_orbits.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
# 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 dimod
import networkx as nx

import networkx as nx
from matplotlib import pyplot as plt

import dimod

from helpers.helper_functions import get_coupler_colors, get_qubit_colors
from helpers import orbits

Expand Down
17 changes: 9 additions & 8 deletions tutorial_code/example1_1_fm_loop_balancing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
# 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 dimod
from typing import Optional

from tqdm import tqdm
import numpy as np

from helpers.sampler_wrapper import ShimmingMockSampler
import dimod
from dwave.system.samplers import DWaveSampler
from tqdm import tqdm

from embed_loops import embed_loops
from helpers.helper_functions import load_experiment_data, save_experiment_data
from helpers.paper_plotting_functions import paper_plots_example1_1
from helpers.sampler_wrapper import ShimmingMockSampler


def make_fbo_dict(param: dict, shim: dict, embeddings: list) -> dict:
Expand Down Expand Up @@ -244,7 +245,7 @@ def main(
coupling: float = -0.2,
num_iters: int = 100,
num_iters_unshimmed_flux: int = 10,
max_num_emb: float = float('Inf'),
max_num_emb: Optional[int] = None,
use_cache: bool = True,
) -> None:
"""Main function to run example.
Expand All @@ -264,9 +265,9 @@ def main(
of flux_biases. Defaults to 100.
num_iters_unshimmed_J (int): Number of iterations without shimming of
couplings. Defaults to 200.
max_num_emb (float): Maximum number of embeddings to use per programming.
Published tutorial data uses the maximum number the process can
accommodate.
max_num_emb (optional, int): Maximum number of embeddings to use per
programming. Published tutorial data uses the maximum number the
process can accommodate (defaults to max_num_emb=None).
use_cache (bool): When True embeddings and data are read from
(and saved to) local directories, repeated executions can reuse
collected data. When False embeddings and data are recalculated on
Expand Down
18 changes: 10 additions & 8 deletions tutorial_code/example1_2_fm_loop_correlations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
# 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 dimod

from typing import Optional

from tqdm import tqdm
import numpy as np

from helpers.sampler_wrapper import ShimmingMockSampler
import dimod
from dwave.system.samplers import DWaveSampler
from tqdm import tqdm

from embed_loops import embed_loops
from helpers.helper_functions import load_experiment_data, save_experiment_data
from helpers.paper_plotting_functions import paper_plots_example1_2
from helpers.sampler_wrapper import ShimmingMockSampler


def make_fbo_dict(param: dict, shim: dict, embeddings: list) -> dict:
Expand Down Expand Up @@ -243,7 +245,7 @@ def main(
num_iters: int = 300,
num_iters_unshimmed_flux: int = 100,
num_iters_unshimmed_J: int = 200,
max_num_emb: float = float('Inf'),
max_num_emb: Optional[int] = None,
use_cache: bool = True,
) -> None:
"""Main function to run example.
Expand All @@ -259,9 +261,9 @@ def main(
num_iters (int): Number of sequential programmings.
num_iters_unshimmed_flux (int): Number of sequential programmings without flux shimming.
num_iters_unshimmed_J (int): Number of sequential programmings without J shimming.
max_num_emb (float): Maximum number of embeddings to use per programming.
Published tutorial data uses the maximum number the process can
accommodate.
max_num_emb (optional, int): Maximum number of embeddings to use per
programming. Published tutorial data uses the maximum number the
process can accommodate (defaults to max_num_emb=None).
use_cache (bool): When True embeddings and data are read from
(and saved to) local directories, repeated executions can reuse
collected data. When False embeddings and data are recalculated on
Expand Down
6 changes: 3 additions & 3 deletions tutorial_code/example2_1_frustrated_loop_orbits.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
# 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 dimod

import matplotlib
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt

from matplotlib import pyplot as plt
import dimod

from helpers import paper_plotting_functions, orbits

Expand Down
21 changes: 11 additions & 10 deletions tutorial_code/example2_2_frustrated_loop_anneal.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
# 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 dimod

from typing import Optional

from tqdm import tqdm
import numpy as np

from helpers.sampler_wrapper import ShimmingMockSampler
import dimod
from dwave.system.samplers import DWaveSampler
from tqdm import tqdm

from embed_loops import embed_loops
from helpers.helper_functions import load_experiment_data, save_experiment_data
from helpers.paper_plotting_functions import paper_plots_example2_2

from helpers.sampler_wrapper import ShimmingMockSampler

def make_fbo_dict(param: dict, shim: dict, embeddings: list) -> dict:
"""Makes the FBO dict from the matrix of FBOs.
Expand Down Expand Up @@ -270,12 +271,12 @@ def run_experiment(


def main(
solver_name: str = None,
solver_name: Optional[str] = None,
coupling: float = -0.9,
num_iters: int = 300,
num_iters_unshimmed_flux: int = 100,
num_iters_unshimmed_J: int = 200,
max_num_emb: float = float('Inf'),
max_num_emb: Optional[int] = None,
use_cache: bool = True,
) -> None:
"""Main function to run example
Expand All @@ -296,9 +297,9 @@ def main(
iteratrions that doesn't shim flux_biases. Defaults to 100.
num_iters_unshimmed_J (int): option to specify number of iterations
that doesn't shim alpha_J. Defaults to 200.
max_num_emb (float): Maximum number of embeddings to use per programming.
Published tutorial data uses the maximum number the process can
accommodate.
max_num_emb (optional, int): Maximum number of embeddings to use per
programming. Published tutorial data uses the maximum number the
process can accommodate (defaults to max_num_emb=None).
use_cache (bool): When True embeddings and data are read from
(and saved to) local directories, repeated executions can reuse
collected data. When False embeddings and data are recalculated on
Expand Down
4 changes: 2 additions & 2 deletions tutorial_code/example2_3_buckyball_orbits.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
#
from os.path import exists

import dimod
import matplotlib
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt

from matplotlib import pyplot as plt
import dimod

from helpers import orbits
from helpers.orbits import get_orbits
Expand Down
18 changes: 10 additions & 8 deletions tutorial_code/example3_2_tafm_forward_anneal.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
# 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 dimod
import numpy as np

from typing import Optional

from tqdm import tqdm
import numpy as np

import dimod

from helpers.sampler_wrapper import ShimmingMockSampler
from dwave.system.samplers import DWaveSampler
Expand Down Expand Up @@ -459,7 +461,7 @@ def main(
num_iters: int = 800,
num_iters_unshimmed_flux: int = 100,
num_iters_unshimmed_J: int = 300,
max_num_emb: int = 1,
max_num_emb: Optional[int] = 1,
use_cache: bool = True,
) -> None:
"""Main function to run example.
Expand All @@ -476,10 +478,10 @@ def main(
num_iters (int): Number of sequential programmings.
num_iters_unshimmed_flux (int): Number of sequential programmings without flux shimming.
num_iters_unshimmed_J (int): Number of sequential programmings without J shimming.
max_num_emb (int): Maximum number of embeddings to use per programming.
Published tutorial data uses several parallel embeddings, but this
tutorial uses 1 (max_num_emb=1) by default to bypass the otherwise
slow search process.
max_num_emb (optional, int): Maximum number of embeddings to use per
programming. Published tutorial data uses several parallel
embeddings (value None, unbounded), but this refactored
defaults to 1 to bypass the otherwise slow search process.
use_cache (bool): When True embeddings and data are read from
(and saved to) local directories, repeated executions can reuse
collected data. When False embeddings and data are recalculated on
Expand Down

0 comments on commit 3fa30bc

Please sign in to comment.