Example #1
0
 def _update_variables_type(self):
     vartype = self._modeltype_to_vartype(self._mtype)
     for name, variable in self._variables.items():
         self._variables[name] = pyqubo.Array.create(name,
                                                     shape=variable.shape,
                                                     vartype=vartype)
     df = self.select_interaction(query="removed == False")
     for index, interaction in df.iterrows():
         interacts = interaction["interacts"]
         if self._mtype == constants.MODEL_ISING:
             if isinstance(interacts, tuple):
                 self._interactions_array["interacts"][index] = (
                     pyqubo.Spin(interaction["interacts"][0].label),
                     pyqubo.Spin(interaction["interacts"][1].label),
                 )
             else:
                 self._interactions_array["interacts"][index] = pyqubo.Spin(
                     interaction["interacts"].label)
         elif self._mtype == constants.MODEL_QUBO:
             if isinstance(interacts, tuple):
                 self._interactions_array["interacts"][index] = (
                     pyqubo.Binary(interaction["interacts"][0].label),
                     pyqubo.Binary(interaction["interacts"][1].label),
                 )
             else:
                 self._interactions_array["interacts"][
                     index] = pyqubo.Binary(interaction["interacts"].label)
Example #2
0
def test_zero_or_one_hot_constraint():
    c = ZeroOrOneHotConstraint()
    assert c.get_constraint_class() == "ZeroOrOneHotConstraint"
    assert c.get_variables() == set()
    assert c.get_label() == "Default Zero-or-One-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():
    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() == {x0}
    assert len(c.get_variables()) == 1

    c.add_variable([x1, x2])
    assert c.get_variables() == {x0, x1, x2}
    assert len(c.get_variables()) == 3

    c.add_variable({x0})
    assert c.get_variables() == {x0, x1, x2}
    assert len(c.get_variables()) == 3

    c.add_variable(variables=[x0, x1])
    assert c.get_variables() == {x0, x1, x2}
    assert len(c.get_variables()) == 3

    c.remove_variable(variables=[x0])
    assert c.get_variables() == {x1, x2}
    assert len(c.get_variables()) == 2

    with pytest.raises(ValueError):
        c.remove_variable(variables=[x0])
def test_equality_constraint():
    c = EqualityConstraint()
    assert c.get_constraint_class() == "EqualityConstraint"
    assert c.get_variables_1() == set()
    assert c.get_variables_2() == set()
    assert c.get_label() == "Default Equality Constraint"
    assert c.get_strength() == 1.0

    x0 = pyqubo.Spin("x0")
    x1 = pyqubo.Spin("x1")
    y0 = pyqubo.Binary("y0")
    y1 = pyqubo.Binary("y1")

    c.add_variable_to_1(x0)
    assert c.get_variables_1() == {x0}
    assert c.get_variables_2() == set()

    c.add_variable_to_1([x1])
    assert c.get_variables_1() == {x0, x1}
    assert c.get_variables_2() == set()

    c.add_variable_to_2({y0})
    assert c.get_variables_1() == {x0, x1}
    assert c.get_variables_2() == {y0}

    c.add_variable_to_2(variables=[y0, y1])
    assert c.get_variables_1() == {x0, x1}
    assert c.get_variables_2() == {y0, y1}

    c.remove_variable_from_1(variables=x0)
    assert c.get_variables_1() == {x1}
    assert c.get_variables_2() == {y0, y1}

    c.remove_variable_from_2(variables=y1)
    assert c.get_variables_1() == {x1}
    assert c.get_variables_2() == {y0}

    with pytest.raises(ValueError):
        c.remove_variable_from_1(variables=[x0])

    with pytest.raises(ValueError):
        c.remove_variable_from_2(variables=[y1])
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
Example #6
0
def test_zero_or_one_hot_constraint_eq():
    assert ZeroOrOneHotConstraint() == ZeroOrOneHotConstraint()

    a = pyqubo.Spin("a")
    b = pyqubo.Binary("b")
    c1 = ZeroOrOneHotConstraint(variables=set([a, b]),
                                label="my label",
                                strength=20)
    c2 = ZeroOrOneHotConstraint(variables=[a, b],
                                label="my label",
                                strength=2 * 10)
    assert c1 == c2
def test_equality_constraint_eq():
    assert EqualityConstraint() == EqualityConstraint()

    a = pyqubo.Spin("a")
    b = pyqubo.Binary("b")
    c1 = EqualityConstraint(variables_1={a},
                            variables_2=b,
                            label="my label",
                            strength=20)
    c2 = EqualityConstraint(variables_1=[a],
                            variables_2=b,
                            label="my label",
                            strength=2 * 10)
    assert c1 == c2
Example #8
0
def test_zero_or_one_hot_constraint_ne():
    a = pyqubo.Spin("a")
    b = pyqubo.Binary("b")
    c = []
    c.append(ZeroOrOneHotConstraint())
    c.append(
        ZeroOrOneHotConstraint(variables=[a, b], label="my label",
                               strength=20))
    c.append(ZeroOrOneHotConstraint(variables=set([a, b])))
    c.append(ZeroOrOneHotConstraint(variables=a))
    c.append(ZeroOrOneHotConstraint(label="my label"))
    c.append(ZeroOrOneHotConstraint(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_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={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_equality_constraint_ne():
    a = pyqubo.Spin("a")
    b = pyqubo.Binary("b")
    c = []
    c.append(EqualityConstraint())
    c.append(
        EqualityConstraint(variables_1=[a],
                           variables_2=[b],
                           label="my label",
                           strength=20))
    c.append(EqualityConstraint(variables_1=[a], variables_2=[b]))
    c.append(EqualityConstraint(variables_1={a, b}))
    c.append(EqualityConstraint(variables_2={a, b}))
    c.append(EqualityConstraint(variables_1=a))
    c.append(EqualityConstraint(variables_2=a))
    c.append(EqualityConstraint(variables_1=b))
    c.append(EqualityConstraint(variables_2=b))
    c.append(EqualityConstraint(label="my label"))
    c.append(EqualityConstraint(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]