def test_local_solver_sa_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 = LocalSolver() sampleset1 = solver.solve(model.to_physical(), seed=12345) assert sampleset1.variables == ["s[0]", "s[1]"] assert len(sampleset1.record) == 1 # Check the ground state assert np.array_equal(sampleset1.record[0].sample, [-1, 1]) assert sampleset1.record[0].energy == 6.0 assert sampleset1.record[0].num_occurrences == 1 # Check the second solve with the same solver instance sampleset2 = solver.solve(model.to_physical(), seed=54321) assert sampleset2.variables == ["s[0]", "s[1]"] assert len(sampleset2.record) == 1 # Check the ground state assert np.array_equal(sampleset2.record[0].sample, [-1, 1]) assert sampleset2.record[0].energy == 6.0 assert sampleset2.record[0].num_occurrences == 1
def test_logical_model_from_pyqubo_spins(qubo): x = qubo.variables("x", shape=(10, )) y = qubo.variables("y", shape=(10, )) sum_x = sum(x[i] for i in range(10)) sum_y = sum(y[i] for i in range(10)) hamiltonian = (sum_x - sum_y)**2 qubo.from_pyqubo(hamiltonian) physical = qubo.to_physical() solver = LocalSolver(exact=False) sampleset = solver.solve(physical, num_reads=1, num_sweeps=10000) spins = sampleset.record[0][0] assert np.count_nonzero(spins[:10]) == np.count_nonzero(spins[10:])
def test_local_solver_zero_or_one_hot_qubo(n): model = LogicalModel(mtype="qubo") x = model.variables("x", shape=(n,)) model.add_constraint(ZeroOrOneHotConstraint(variables=x)) solver = LocalSolver() physical = model.to_physical() for seed in [11, 22, 33, 44, 55]: sampleset = solver.solve(physical, seed=seed) result = np.array(sampleset.record[0].sample) assert np.count_nonzero(result == 1) in [0, 1] # Execution time should be within practical seconds (20 sec). assert sampleset.info["timing"]["execution_sec"] <= 20.0
def test_local_solver_n_hot_qubo(n, s): # n out of s variables should be 1 model = LogicalModel(mtype="qubo") x = model.variables("x", shape=(s,)) model.add_constraint(NHotConstraint(variables=x, n=n)) solver = LocalSolver() physical = model.to_physical() for seed in [11, 22, 33, 44, 55]: sampleset = solver.solve(physical, seed=seed) result = np.array(sampleset.record[0].sample) assert np.count_nonzero(result == 1) == n assert np.count_nonzero(result == 0) == s - n # Execution time should be within practical seconds (20 sec). assert sampleset.info["timing"]["execution_sec"] <= 20.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_local_solver_equality_qubo(m, n): model = LogicalModel(mtype="qubo") x = model.variables("x", shape=(m,)) y = model.variables("y", shape=(n,)) model.add_constraint(EqualityConstraint(variables_1=x, variables_2=y)) solver = LocalSolver() physical = model.to_physical() for seed in [11, 22, 33, 44, 55]: sampleset = solver.solve(physical, seed=seed) result = np.array(sampleset.record[0].sample) result_1 = result[0:m] result_2 = result[m : (m + n)] # noqa: E203 assert np.count_nonzero(result_1 == 1) == np.count_nonzero(result_2 == 1) # Execution time should be within practical seconds (20 sec). assert sampleset.info["timing"]["execution_sec"] <= 20.0
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 test_local_solver_with_empty_model_fails(): model = LogicalModel(mtype="ising") solver = LocalSolver() with pytest.raises(ValueError): solver.solve(model.to_physical(), seed=12345)
def test_local_solver_with_logical_model_fails(): model = LogicalModel(mtype="ising") solver = LocalSolver() with pytest.raises(TypeError): solver.solve(model, seed=12345)