Пример #1
0
def test_decode():
    """Tests decoding for unambiguous inputs"""

    model = AngularModel(n=1, tau=1)
    input_data = 1  # unambiguous input
    model.encode(input_data, dim=0)
    assert model.decode() == "1"

    model = AngularModel(n=1, tau=1)
    input_data = 0  # unambiguous input
    model.encode(input_data, dim=0)
    assert model.decode() == "0"

    model = AngularModel(n=3, tau=1)
    input_data = 1  # unambiguous input
    model.encode(input_data, dim=1)
    assert model.decode() == "010"
Пример #2
0
def test_query():
    """Tests query on the input itself"""

    # 1-dimensional model
    model = AngularModel(n=1, tau=1)
    # Define an input data value
    input_data = 1
    # Encode input_data one time (tau = 1)
    model.encode(input_data, dim=0)
    # Apply a query on the input_data (to obtain an unambiguous result)
    model.query(input_data)
    # See if the actual output is the |00...0> state
    assert model.decode() == "0"

    # 3-dimensional model, 2-events time window
    model = AngularModel(n=5, tau=2)
    # Define an input data value
    input_data = [0.1, 0.4, 0.5, 0.2, 0.1]
    # Encode input_data two times (tau = 2)
    for _ in range(1, model.tau):
        for dim in range(1, model.n):
            model.encode(input_data[dim], dim)
    # Apply a query on the input_data (to obtain an unambiguous result)
    model.query(input_data)
    # See if the actual output is the |00...0> state or a close one
    # (at most one zero)
    assert (
        model.decode() == "00000" or "10000" or "01000" or "00100" or "00010" or "00001"
    )

    # Check the exception for wrong targets:
    with pytest.raises(ValueError):
        assert model.query([1, 0.2])  # size < n
    with pytest.raises(ValueError):
        assert model.query([1, 0.2, 0, 0, 0, 1, 0.2, 0])  # size > n

    # Check the exception for wrong target elements:
    with pytest.raises(TypeError):
        assert model.query(["1", 0, 0, 0, 0])  # wrong type
    with pytest.raises(ValueError):
        assert model.query([0.1, 0.4, 5, 0.2, 0.1])  # third element is a 5