diff --git a/tutorial_code/embed_loops.py b/tutorial_code/embed_loops.py index 4b54e85..98ca7bf 100644 --- a/tutorial_code/embed_loops.py +++ b/tutorial_code/embed_loops.py @@ -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, ) @@ -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)), @@ -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( @@ -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 diff --git a/tutorial_code/embed_square_lattice.py b/tutorial_code/embed_square_lattice.py index eadd2bb..3c8c94c 100644 --- a/tutorial_code/embed_square_lattice.py +++ b/tutorial_code/embed_square_lattice.py @@ -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, ) @@ -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( @@ -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 diff --git a/tutorial_code/example0_1_orbits.py b/tutorial_code/example0_1_orbits.py index 4d0b09a..57c290a 100644 --- a/tutorial_code/example0_1_orbits.py +++ b/tutorial_code/example0_1_orbits.py @@ -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 diff --git a/tutorial_code/example1_1_fm_loop_balancing.py b/tutorial_code/example1_1_fm_loop_balancing.py index a705716..1d073e4 100644 --- a/tutorial_code/example1_1_fm_loop_balancing.py +++ b/tutorial_code/example1_1_fm_loop_balancing.py @@ -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: @@ -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. @@ -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 diff --git a/tutorial_code/example1_2_fm_loop_correlations.py b/tutorial_code/example1_2_fm_loop_correlations.py index c84f8fe..851294f 100644 --- a/tutorial_code/example1_2_fm_loop_correlations.py +++ b/tutorial_code/example1_2_fm_loop_correlations.py @@ -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: @@ -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. @@ -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 diff --git a/tutorial_code/example2_1_frustrated_loop_orbits.py b/tutorial_code/example2_1_frustrated_loop_orbits.py index 0b37b2b..001994b 100644 --- a/tutorial_code/example2_1_frustrated_loop_orbits.py +++ b/tutorial_code/example2_1_frustrated_loop_orbits.py @@ -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 diff --git a/tutorial_code/example2_2_frustrated_loop_anneal.py b/tutorial_code/example2_2_frustrated_loop_anneal.py index 33e09d7..dba60ff 100644 --- a/tutorial_code/example2_2_frustrated_loop_anneal.py +++ b/tutorial_code/example2_2_frustrated_loop_anneal.py @@ -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. @@ -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 @@ -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 diff --git a/tutorial_code/example2_3_buckyball_orbits.py b/tutorial_code/example2_3_buckyball_orbits.py index 2d4aea8..77ac311 100644 --- a/tutorial_code/example2_3_buckyball_orbits.py +++ b/tutorial_code/example2_3_buckyball_orbits.py @@ -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 diff --git a/tutorial_code/example3_2_tafm_forward_anneal.py b/tutorial_code/example3_2_tafm_forward_anneal.py index 5900e49..fa957b9 100644 --- a/tutorial_code/example3_2_tafm_forward_anneal.py +++ b/tutorial_code/example3_2_tafm_forward_anneal.py @@ -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 @@ -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. @@ -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