def test_logical_model_n_hot_constraint_x(ising): x = ising.variables("x", shape=(3, )) default_label = "Default N-hot Constraint" ising.add_constraint(NHotConstraint(variables=x[0], n=1)) assert len(ising.get_constraints()) == 1 assert default_label in ising.get_constraints() assert len(ising.get_constraints_by_label(default_label)._variables) == 1 assert ising.get_constraints_by_label(default_label)._n == 1 ising.add_constraint(NHotConstraint(variables=x[(slice(1, 3), )], n=1)) assert len(ising.get_constraints()) == 1 assert default_label in ising.get_constraints() assert len(ising.get_constraints_by_label(default_label)._variables) == 2 assert ising.get_constraints_by_label(default_label)._n == 1 ising.add_constraint(NHotConstraint(variables=x, n=2)) # n = 2 assert len(ising.get_constraints()) == 1 assert default_label in ising.get_constraints() assert len(ising.get_constraints_by_label(default_label)._variables) == 3 assert ising.get_constraints_by_label(default_label)._n == 2 ising.add_constraint(NHotConstraint(variables=x, n=2)) # double assert len(ising.get_constraints()) == 1 assert default_label in ising.get_constraints() assert len(ising.get_constraints_by_label(default_label)._variables) == 3 assert ising.get_constraints_by_label(default_label)._n == 2 ising.add_constraint(NHotConstraint(variables=x, n=2)) # partially assert len(ising.get_constraints()) == 1 assert default_label in ising.get_constraints() assert len(ising.get_constraints_by_label(default_label)._variables) == 3 assert ising.get_constraints_by_label(default_label)._n == 2
def test_n_hot_constraint_eq(): assert NHotConstraint() == NHotConstraint() a = pyqubo.Spin("a") b = pyqubo.Binary("b") c1 = NHotConstraint(variables={a, b}, n=2, label="my label", strength=20) c2 = NHotConstraint(variables=[a, b], n=2, label="my label", strength=2 * 10) assert c1 == c2
def test_n_hot_constraint_repr(): c = NHotConstraint() assert isinstance(c.__repr__(), str) assert "NHotConstraint({" in c.__repr__() assert "'constraint_class':" in c.__repr__() assert "'variables'" in c.__repr__() assert "'n':" in c.__repr__() assert "'label'" in c.__repr__() assert "'strength':" in c.__repr__()
def test_logical_model_multiple_constraints_n_hot_and_n_hot(ising): x = ising.variables("x", shape=(2, 4)) ising.add_constraint(NHotConstraint(variables=x[0, :], n=1, label="l1")) assert len(ising.get_constraints()) == 1 assert "l1" in ising.get_constraints() assert "l2" not in ising.get_constraints() assert ising.get_constraints_by_label("l1")._n == 1 with pytest.raises(KeyError): ising.get_constraints_by_label("l2") ising.add_constraint(NHotConstraint(variables=x[1, :], n=1, label="l2")) assert len(ising.get_constraints()) == 2 assert "l1" in ising.get_constraints() assert "l2" in ising.get_constraints() assert ising.get_constraints_by_label("l1")._n == 1 assert ising.get_constraints_by_label("l2")._n == 1
def test_logical_model_n_hot_constraint_remove(ising): y = ising.variables("y", shape=(2, 2)) ising.add_constraint(NHotConstraint(variables=y, n=1, label="my label")) assert len(ising.get_constraints()) == 1 assert "my label" in ising.get_constraints() ising.remove_constraint(label="my label") assert len(ising.get_constraints()) == 0
def test_logical_model_n_hot_constraint_y(ising): y = ising.variables("y", shape=(2, 2)) ising.add_constraint(NHotConstraint(variables=y[0, :], n=2, label="my label")) assert len(ising.get_constraints()) == 1 assert "my label" in ising.get_constraints() assert ising.get_constraints_by_label("my label")._n == 2 # variables in list representation c = NHotConstraint(variables=[y[1, 0], y[1, 1]], n=2, label="my label") ising.add_constraint(c) assert len(ising.get_constraints()) == 1 assert "my label" in ising.get_constraints() assert len(ising.get_constraints_by_label("my label")._variables) == 2 assert ising.get_constraints_by_label("my label")._n == 2 # Constraint content changes c.add_variable(variables=[y[0, 0]]) assert len(ising.get_constraints()) == 1 assert "my label" in ising.get_constraints() assert len(ising.get_constraints_by_label("my label")._variables) == 3 assert ising.get_constraints_by_label("my label")._n == 2
def test_sawatabi_solver_n_hot_ising_with_deleting(n, s, i): # n out of (s - 1) variables should be 1 model = LogicalModel(mtype="ising") x = model.variables("x", shape=(s,)) model.add_constraint(NHotConstraint(variables=x, n=n)) model.delete_variable(x[i]) solver = SawatabiSolver() sampleset = solver.solve(model.to_physical(), seed=12345) result = np.array(sampleset.record[0].sample) assert np.count_nonzero(result == 1) == n assert np.count_nonzero(result == -1) == s - n - 1
def test_logical_model_to_physical_with_n_hot_constraint_randomly_ising(ising): x = ising.variables("x", shape=(4,)) c = NHotConstraint(x[(slice(0, 2),)], n=1) ising.add_constraint(c) c.add_variable(x[1]) physical = ising.to_physical() c.add_variable(x[2]) physical = ising.to_physical() c.add_variable(x) physical = ising.to_physical() for i in range(3): assert physical._raw_interactions[constants.INTERACTION_LINEAR][f"x[{i}]"] == -1.0 for i in range(2): for j in range(i + 1, 3): assert physical._raw_interactions[constants.INTERACTION_QUADRATIC][(f"x[{i}]", f"x[{j}]")] == -0.5
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 test_sawatabi_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 = SawatabiSolver() sampleset = solver.solve(model.to_physical(), seed=12345) 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_logical_model_to_physical_with_n_hot_constraint_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(x, n=n)) physical = model.to_physical() for i in range(s): assert physical._raw_interactions[constants.INTERACTION_LINEAR][f"x[{i}]"] == 2 * n - 1.0 for i in range(s): for j in range(s): l1 = f"x[{i}]" l2 = f"x[{j}]" if l1 < l2: assert physical._raw_interactions[constants.INTERACTION_QUADRATIC][(l1, l2)] == -2.0
def test_logical_model_merge_with_constraints_both(ising_x22, ising_z3): z = ising_x22.variables("z", shape=(2,)) ising_x22.add_constraint(NHotConstraint(z, n=2, label="my label")) ising_x22.merge(ising_z3) assert len(ising_x22._constraints) == 2 assert "Default N-hot Constraint" in ising_x22.get_constraints() assert "my label" in ising_x22.get_constraints() assert ising_x22.get_constraints_by_label("Default N-hot Constraint")._n == 1 assert len(ising_x22.get_constraints_by_label("Default N-hot Constraint")._variables) == 3 assert ising_x22.get_constraints_by_label("my label")._n == 2 assert len(ising_x22.get_constraints_by_label("my label")._variables) == 2 assert len(ising_x22._interactions_array["name"]) == 2 physical = ising_x22.to_physical() assert len(physical._raw_interactions[constants.INTERACTION_LINEAR]) == 1 + 3 assert len(physical._raw_interactions[constants.INTERACTION_QUADRATIC]) == 1 + 3
def test_local_solver_n_hot_ising(n, s): # n out of s spins should be +1 model = LogicalModel(mtype="ising") 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 == -1) == s - n # Execution time should be within practical seconds (20 sec). assert sampleset.info["timing"]["execution_sec"] <= 20.0
def test_logical_model_to_physical_with_n_hot_constraint_ising(n, s): # n out of s spins should be +1 model = LogicalModel(mtype="ising") x = model.variables("x", shape=(s,)) model.add_constraint(NHotConstraint(x, n=n)) physical = model.to_physical() for i in range(s): if s != 2 * n: assert physical._raw_interactions[constants.INTERACTION_LINEAR][f"x[{i}]"] == -0.5 * (s - 2 * n) else: assert f"x[{i}]" not in physical._raw_interactions[constants.INTERACTION_LINEAR] for i in range(s): for j in range(s): l1 = f"x[{i}]" l2 = f"x[{j}]" if l1 < l2: assert physical._raw_interactions[constants.INTERACTION_QUADRATIC][(l1, l2)] == -0.5
def test_n_hot_constraint_ne(): a = pyqubo.Spin("a") b = pyqubo.Binary("b") c = [] c.append(NHotConstraint()) c.append( NHotConstraint(variables=[a, b], n=2, label="my label", strength=20)) c.append(NHotConstraint(variables=set([a, b]))) c.append(NHotConstraint(variables=a)) c.append(NHotConstraint(n=2)) c.append(NHotConstraint(label="my label")) c.append(NHotConstraint(strength=20)) c.append("another type") for i in range(len(c) - 1): for j in range(i + 1, len(c)): assert c[i] != c[j]
def test_logical_model_to_physical_with_n_hot_constraint_randomly_qubo(qubo): x = qubo.variables("x", shape=(4,)) c = NHotConstraint(x[(slice(0, 2),)], n=1, strength=10) qubo.add_constraint(c) c.add_variable(x[1]) c.add_variable(x[2]) c.add_variable(x) physical = qubo.to_physical() for i in range(3): assert physical._raw_interactions[constants.INTERACTION_LINEAR][f"x[{i}]"] == 10.0 for i in range(2): for j in range(i + 1, 3): assert physical._raw_interactions[constants.INTERACTION_QUADRATIC][(f"x[{i}]", f"x[{j}]")] == -20.0
def test_logical_model_delete_dealing_with_nhot_constraints_qubo(): model = LogicalModel(mtype="qubo") x = model.variables("x", shape=(4, )) default_label = "Default N-hot Constraint" model.add_constraint(NHotConstraint(x, n=1, strength=1.0)) assert len(model.get_constraints()) == 1 assert default_label in model.get_constraints() assert model.get_constraints_by_label(default_label)._n == 1 assert len(model.get_constraints_by_label(default_label)._variables) == 4 model.delete_variable(x[0]) assert len(model.get_constraints()) == 1 assert default_label in model.get_constraints() assert model.get_constraints_by_label(default_label)._n == 1 assert len(model.get_constraints_by_label(default_label)._variables) == 3 physical = model.to_physical() assert physical._raw_interactions[ constants.INTERACTION_LINEAR]["x[1]"] == 1.0 assert physical._raw_interactions[constants.INTERACTION_QUADRATIC][( "x[1]", "x[2]")] == -2.0
def test_n_hot_constraint_typeerror(): with pytest.raises(TypeError): NHotConstraint(variables="invalid type") with pytest.raises(TypeError): NHotConstraint(variables=set([1, 2, 3])) with pytest.raises(TypeError): NHotConstraint(n="invalid type") with pytest.raises(TypeError): NHotConstraint(n=1.0) with pytest.raises(TypeError): NHotConstraint(label=12345) with pytest.raises(TypeError): NHotConstraint(strength="invalid type")
def test_logical_model_merge_with_constraints_both_invalid(ising_x22, ising_z3): # Both models have constraints with the same label name, cannot merge. z = ising_x22.variables("z", shape=(2,)) ising_x22.add_constraint(NHotConstraint(z, n=1)) with pytest.raises(ValueError): ising_x22.merge(ising_z3)
def ising_z3(): model = LogicalModel(mtype="ising") z = model.variables("z", shape=(3,)) model.add_constraint(NHotConstraint(variables=z, n=1)) return model
def test_n_hot_constraint(): c = NHotConstraint() assert c.get_constraint_class() == "NHotConstraint" assert c.get_variables() == set() assert c.get_n() == 1 assert c.get_label() == "Default N-hot Constraint" assert c.get_strength() == 1.0 x0 = pyqubo.Spin("x0") x1 = pyqubo.Spin("x1") x2 = pyqubo.Binary("x2") c.add_variable(x0) assert c.get_variables() == set([x0]) assert len(c.get_variables()) == 1 c.add_variable([x1, x2]) assert c.get_variables() == set([x0, x1, x2]) assert len(c.get_variables()) == 3 c.add_variable(set([x0])) assert c.get_variables() == set([x0, x1, x2]) assert len(c.get_variables()) == 3 c.add_variable(variables=[x0, x1]) assert c.get_variables() == set([x0, x1, x2]) assert len(c.get_variables()) == 3 c.remove_variable(variables=[x0]) assert c.get_variables() == set([x1, x2]) assert len(c.get_variables()) == 2 with pytest.raises(ValueError): c.remove_variable(variables=[x0])
def test_n_hot_constraint_valueerror(n): with pytest.raises(ValueError): NHotConstraint(n=n) with pytest.raises(ValueError): NHotConstraint(label="")
def test_n_hot_constraint_constructors(): a = pyqubo.Array.create("a", shape=(2, 2), vartype="SPIN") c1 = NHotConstraint(variables=a) assert c1.get_variables() == set([a[0, 0], a[0, 1], a[1, 0], a[1, 1]]) assert c1.get_n() == 1 assert c1.get_label() == "Default N-hot Constraint" assert c1.get_strength() == 1.0 c2 = NHotConstraint(n=2) assert c2.get_variables() == set() assert c2.get_n() == 2 assert c2.get_label() == "Default N-hot Constraint" assert c2.get_strength() == 1.0 c3 = NHotConstraint(label="my label") assert c3.get_variables() == set() assert c3.get_n() == 1 assert c3.get_label() == "my label" assert c3.get_strength() == 1.0 c4 = NHotConstraint(strength=20) assert c4.get_variables() == set() assert c4.get_n() == 1 assert c4.get_label() == "Default N-hot Constraint" assert c4.get_strength() == 20.0