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
def test_equality_constraint_repr():
    c = EqualityConstraint()
    assert isinstance(c.__repr__(), str)
    assert "EqualityConstraint({" in c.__repr__()
    assert "'constraint_class':" in c.__repr__()
    assert "'variables_1'" in c.__repr__()
    assert "'variables_2'" in c.__repr__()
    assert "'label'" in c.__repr__()
    assert "'strength':" in c.__repr__()
def test_logical_model_multiple_constraints_n_hot_and_equality(ising):
    x = ising.variables("x", shape=(3, 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 isinstance(ising.get_constraints_by_label("l1"), NHotConstraint)
    assert ising.get_constraints_by_label("l1")._n == 1
    with pytest.raises(KeyError):
        ising.get_constraints_by_label("l2")

    ising.add_constraint(EqualityConstraint(variables_1=x[1, :], variables_2=x[2, :], label="l2"))
    assert len(ising.get_constraints()) == 2
    assert "l1" in ising.get_constraints()
    assert "l2" in ising.get_constraints()
    assert isinstance(ising.get_constraints_by_label("l2"), EqualityConstraint)
示例#4
0
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_equality_constraint_typeerror():
    with pytest.raises(TypeError):
        EqualityConstraint(variables_1="invalid type")

    with pytest.raises(TypeError):
        EqualityConstraint(variables_1={1, 2, 3})

    with pytest.raises(TypeError):
        EqualityConstraint(variables_2="invalid type")

    with pytest.raises(TypeError):
        EqualityConstraint(variables_2={1, 2, 3})

    with pytest.raises(TypeError):
        EqualityConstraint(label=12345)

    with pytest.raises(TypeError):
        EqualityConstraint(strength="invalid type")
def test_equality_constraint_constructors():
    a = pyqubo.Array.create("a", shape=(2, 2), vartype="SPIN")

    c1 = EqualityConstraint(variables_1=a)
    assert c1.get_variables_1() == {a[0, 0], a[0, 1], a[1, 0], a[1, 1]}
    assert c1.get_variables_2() == set()
    assert c1.get_label() == "Default Equality Constraint"
    assert c1.get_strength() == 1.0

    c2 = EqualityConstraint(variables_2=a)
    assert c2.get_variables_1() == set()
    assert c2.get_variables_2() == {a[0, 0], a[0, 1], a[1, 0], a[1, 1]}
    assert c2.get_label() == "Default Equality Constraint"
    assert c2.get_strength() == 1.0

    c3 = EqualityConstraint(label="my label")
    assert c3.get_variables_1() == set()
    assert c3.get_variables_2() == set()
    assert c3.get_label() == "my label"
    assert c3.get_strength() == 1.0

    c4 = EqualityConstraint(strength=20)
    assert c4.get_variables_1() == set()
    assert c4.get_variables_2() == set()
    assert c4.get_label() == "Default Equality Constraint"
    assert c4.get_strength() == 20.0
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_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]