示例#1
0
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
示例#2
0
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])