def _create_ising_model_for_eq(): model = LogicalModel(mtype="ising") x = model.variables(name="x", shape=(3, )) model.delete_variable(x[0]) model.add_interaction(x[1], coefficient=1.0) model.add_interaction(x[2], coefficient=2.0) model.add_interaction((x[1], x[2]), coefficient=3.0) return model.to_physical()
def test_optigan_solver_with_empty_config_fails(): model = LogicalModel(mtype="qubo") x = model.variables("x", shape=(2, )) model.add_interaction((x[0], x[1]), coefficient=-1.0) physical = model.to_physical() solver = OptiganSolver(config="/tmp/.optigan.yml") with pytest.raises(FileNotFoundError): solver.solve(physical)
def test_optigan_solver_with_ising_model_fails(): model = LogicalModel(mtype="ising") s = model.variables("s", shape=(2, )) model.add_interaction((s[0], s[1]), coefficient=-1.0) physical = model.to_physical() solver = OptiganSolver() with pytest.raises(ValueError): solver.solve(physical)
def qubo(): model = LogicalModel(mtype="qubo") x = model.variables(name="x", shape=(3, )) model.delete_variable(x[0]) model.add_interaction(x[1], coefficient=1.0) model.add_interaction(x[2], coefficient=2.0) model.add_interaction((x[1], x[2]), coefficient=3.0) return model.to_physical()
def test_sawatabi_solver_invalid_pickup_mode(): model = LogicalModel(mtype="ising") x = model.variables("x", shape=(2,)) for i in range(2): model.add_interaction(x[i], coefficient=-1.0) solver = SawatabiSolver() with pytest.raises(ValueError): solver.solve(model.to_physical(), pickup_mode="invalid")
def test_local_solver_default_beta_range(): model = LogicalModel(mtype="ising") s = model.variables("s", shape=(2,)) model.add_interaction(s[0], coefficient=1.0) model.add_interaction(s[1], coefficient=2.0) model.add_interaction((s[0], s[1]), coefficient=-3.0) solver = LocalSolver() beta_range = solver.default_beta_range(model.to_physical()) assert beta_range == [0.13862943611198905, 4.605170185988092]
def test_sawatabi_solver_with_initial_states_fails(): model = LogicalModel(mtype="ising") x = model.variables("x", shape=(2,)) for i in range(2): model.add_interaction(x[i], coefficient=-1.0) solver = SawatabiSolver() initial_states = [{"x[0]": 1, "x[1]": 1}] with pytest.raises(ValueError): solver.solve(model.to_physical(), num_reads=2, initial_states=initial_states)
def _create_n_variable_random_complete_model(n=4, seed=None): if seed is not None: random.seed(seed) model = LogicalModel(mtype="ising") x = model.variables("x", shape=(n, )) for i in range(n): model.add_interaction(x[i], coefficient=random.random()) for i in range(n - 1): for j in range(i + 1, n): model.add_interaction((x[i], x[j]), coefficient=random.random())
def test_logical_model_empty(mtype): model = LogicalModel(mtype=mtype) x = model.variables("x", shape=(10, 10)) model.add_interaction(x[0, 0], coefficient=10.0) empty_model = model.empty() assert empty_model.get_mtype() == mtype assert len(empty_model._variables) == 0 assert len(empty_model._interactions_array) == len(empty_model._default_keys) for k, v in empty_model._interactions_array.items(): assert len(v) == 0 assert empty_model._interactions_length == 0
def test_sawatabi_solver_with_stats(): model = LogicalModel(mtype="ising") x = model.variables("x", shape=(2,)) for i in range(2): model.add_interaction(x[i], coefficient=-1.0) solver = SawatabiSolver() sampleset, stats = solver.solve(model.to_physical(), num_reads=1, num_sweeps=10, cooling_rate=0.5, seed=12345, need_stats=True) assert stats[0]["acceptance_history"][-1] == 0 assert stats[0]["energy_history"][-1] == -2.0 assert stats[0]["temperature_history"] == [100.0, 50.0, 25.0, 12.5, 6.25, 3.125, 1.5625, 0.78125, 0.390625, 0.1953125]
def test_sawatabi_solver_invalid_reverse_options(): model = LogicalModel(mtype="ising") x = model.variables("x", shape=(2,)) for i in range(2): model.add_interaction(x[i], coefficient=-1.0) solver = SawatabiSolver() with pytest.raises(ValueError): solver.solve(model.to_physical(), reverse_options={"reverse_period": 5}) with pytest.raises(ValueError): solver.solve(model.to_physical(), reverse_options={"reverse_temperature": 10.0})
def test_sawatabi_solver_reuse_solver_instance(): model = LogicalModel(mtype="ising") x = model.variables("x", shape=(2,)) for i in range(2): model.add_interaction(x[i], coefficient=-1.0) solver = SawatabiSolver() sampleset_1 = solver.solve(model.to_physical(), num_reads=1, num_sweeps=10, cooling_rate=0.5, seed=12345) assert np.array_equal(sampleset_1.record[0].sample, [-1, -1]) assert sampleset_1.record[0].energy == -2.0 sampleset_2 = solver.solve(model.to_physical(), num_reads=1, num_sweeps=10, cooling_rate=0.5, seed=12345) assert np.array_equal(sampleset_2.record[0].sample, [-1, -1]) assert sampleset_2.record[0].energy == -2.0
def test_local_solver_sa_qubo(): model = LogicalModel(mtype="qubo") x = model.variables("x", shape=(2,)) model.add_interaction(x[0], coefficient=1.0) model.add_interaction(x[1], coefficient=2.0) model.add_interaction((x[0], x[1]), coefficient=-5.0) model.offset(10.0) solver = LocalSolver(exact=False) sampleset = solver.solve(model.to_physical(), seed=12345) assert sampleset.variables == ["x[0]", "x[1]"] assert len(sampleset.record) == 1 # Check the ground state assert np.array_equal(sampleset.record[0].sample, [0, 1]) assert sampleset.record[0].energy == 8.0 assert sampleset.record[0].num_occurrences == 1
def test_sawatabi_solver_qubo(): model = LogicalModel(mtype="qubo") x = model.variables("x", shape=(2,)) model.add_interaction(x[0], coefficient=1.0) model.add_interaction(x[1], coefficient=2.0) model.add_interaction((x[0], x[1]), coefficient=-5.0) model.offset(10.0) solver = SawatabiSolver() sampleset = solver.solve(model.to_physical(), num_reads=1, num_sweeps=10, cooling_rate=0.55, seed=12345) assert sampleset.variables == ["x[0]", "x[1]"] assert len(sampleset.record) == 1 # Check the ground state record = sorted(sampleset.record, key=lambda r: r.energy) # sort by energy assert np.array_equal(record[0].sample, [0, 1]) assert record[0].energy == 8.0
def test_sawatabi_solver_ising(): model = LogicalModel(mtype="ising") s = model.variables("s", shape=(2,)) model.add_interaction(s[0], coefficient=1.0) model.add_interaction(s[1], coefficient=2.0) model.add_interaction((s[0], s[1]), coefficient=-3.0) model.offset(10.0) solver = SawatabiSolver() sampleset = solver.solve(model.to_physical(), num_reads=2, num_sweeps=10, cooling_rate=0.5, initial_temperature=10.0, seed=12345) assert sampleset.variables == ["s[0]", "s[1]"] assert len(sampleset.record) == 1 # Check the ground state assert np.array_equal(sampleset.record[0].sample, [-1, 1]) assert sampleset.record[0].energy == 6.0 assert sampleset.record[0].num_occurrences == 2
def test_sawatabi_solver_qubo_without_active_var(): model = LogicalModel(mtype="qubo") x = model.variables("x", shape=(2, 2)) model.add_interaction(x[0, 1], coefficient=1.0) model.add_interaction((x[1, 0], x[1, 1]), coefficient=2.0) physical = model.to_physical() assert len(physical._label_to_index) == 3 assert len(physical._index_to_label) == 3 solver = SawatabiSolver() sampleset = solver.solve(physical, seed=12345) assert sampleset.variables == ["x[0][1]", "x[1][0]", "x[1][1]"] assert len(sampleset.record) == 1 # Check the ground state assert np.array_equal(sampleset.record[0].sample, [1, 1, 1]) assert sampleset.record[0].energy == -3.0 assert sampleset.record[0].num_occurrences == 1
def test_local_solver_exact_qubo(): model = LogicalModel(mtype="qubo") x = model.variables("x", shape=(2,)) model.add_interaction(x[0], coefficient=1.0) model.add_interaction(x[1], coefficient=2.0) model.add_interaction((x[0], x[1]), coefficient=-5.0) solver = LocalSolver(exact=True) sampleset = solver.solve(model.to_physical()) assert sampleset.variables == ["x[0]", "x[1]"] assert len(sampleset.record) == 4 for r in sampleset.record: # Check the ground state if np.array_equal(r.sample, [0, 1]): assert r.energy == -2.0 assert r.num_occurrences == 1 break else: assert False
def ising_x44(): model = LogicalModel(mtype="ising") x = model.variables("x", shape=(4, 4)) model.add_interaction(x[0, 0], coefficient=20.0) model.add_interaction((x[1, 1], x[2, 2]), coefficient=21.0, attributes={"foo2": "bar2"}) model.add_interaction(x[3, 3], coefficient=22.0, scale=2.0, timestamp=12345, attributes={"myattr": "mymymymy"}) return model
def test_sawatabi_solver_ising_without_active_var(): model = LogicalModel(mtype="ising") s = model.variables("s", shape=(2, 2)) model.add_interaction(s[0, 1], coefficient=1.0) model.add_interaction((s[1, 0], s[1, 1]), coefficient=2.0) physical = model.to_physical() assert len(physical._label_to_index) == 3 assert len(physical._index_to_label) == 3 solver = SawatabiSolver() sampleset = solver.solve(physical, num_reads=10, seed=12345) assert sampleset.variables == ["s[0][1]", "s[1][0]", "s[1][1]"] assert len(sampleset.record) == 2 # Check the ground state assert np.array_equal(sampleset.record[0].sample, [1, -1, -1]) assert np.array_equal(sampleset.record[1].sample, [1, 1, 1]) assert sampleset.record[0].energy == -3 assert sampleset.record[1].energy == -3 assert sampleset.record[0].num_occurrences + sampleset.record[1].num_occurrences == 10
def _create_nxn_random_lattice_model(n=4, seed=None): if seed is not None: random.seed(seed) model = LogicalModel(mtype="qubo") x = model.variables("x", shape=(n, n)) for i in range(n): for j in range(n): model.add_interaction(x[i, j], coefficient=random.random()) if 0 < i: model.add_interaction((x[i - 1, j], x[i, j]), coefficient=random.random()) if 0 < j: model.add_interaction((x[i, j - 1], x[i, j]), coefficient=random.random())
def _create_ising_model_for_eq(): model = LogicalModel(mtype="ising") x = model.variables(name="x", shape=(4,)) z = model.variables(name="z", shape=(4,)) model.add_interaction(target=x[0], coefficient=1.1) model.add_interaction(target=(x[0], x[1]), coefficient=2.2, scale=3.3, attributes={"foo": "bar"}) model.add_interaction(target=x[2], coefficient=4.4) model.add_interaction(target=x[3], coefficient=5.5) model.remove_interaction(target=x[2]) model.fix_variable(target=x[3], value=1) model.add_constraint(NHotConstraint(variables=z, n=1)) return model
def ising_x22(): model = LogicalModel(mtype="ising") x = model.variables("x", shape=(2, 2)) model.add_interaction(x[0, 0], coefficient=10.0) model.add_interaction((x[0, 0], x[1, 1]), coefficient=11.0, attributes={"foo1": "bar1", "myattr": "mymy"}) return model
def qubo_a22(): model = LogicalModel(mtype="qubo") a = model.variables("a", shape=(2, 2)) model.add_interaction(a[0, 0], coefficient=10.0) model.add_interaction((a[0, 0], a[1, 1]), coefficient=11.0) return model
def ising_y22(): model = LogicalModel(mtype="ising") y = model.variables("y", shape=(2, 2)) model.add_interaction(y[0, 0], coefficient=10.0) model.add_interaction((y[0, 0], y[1, 1]), coefficient=11.0) return model