コード例 #1
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])
コード例 #2
0
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
コード例 #3
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
コード例 #4
0
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