Exemplo n.º 1
0
def qubo():
    model = LogicalModel(mtype="qubo")
    x = model.variables(name="x", shape=(3, ))
    model.delete_variable(x[0])
    model.add_interaction(x[1], coefficient=1.0)
    model.add_interaction(x[2], coefficient=2.0)
    model.add_interaction((x[1], x[2]), coefficient=3.0)
    return model.to_physical()
Exemplo n.º 2
0
def _create_ising_model_for_eq():
    model = LogicalModel(mtype="ising")
    x = model.variables(name="x", shape=(3, ))
    model.delete_variable(x[0])
    model.add_interaction(x[1], coefficient=1.0)
    model.add_interaction(x[2], coefficient=2.0)
    model.add_interaction((x[1], x[2]), coefficient=3.0)
    return model.to_physical()
Exemplo n.º 3
0
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
Exemplo n.º 4
0
def test_local_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 = 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 - 1

        # Execution time should be within practical seconds (20 sec).
        assert sampleset.info["timing"]["execution_sec"] <= 10.0
Exemplo n.º 5
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