Пример #1
0
def test_probabilities():
    """Tests aggregated probabilities for multiple measurementr in a workflow."""

    # 3-dimensional model, 2-events time window
    model = AngularModel(3, 2)
    # Define an input data sequence (tau = 2)
    input_data = list()
    input_data.append([0.8, 0.8, 1])
    input_data.append([0.9, 0.6, 0.9])
    # Encode the sequence in the model
    for t in range(model.tau):
        for dim in range(model.n):
            model.encode(input_data[t][dim], dim)
    # Check if at least 70% of the shots are 111 (coherent with the input)
    shots = 10000
    result = model.measure(shots)
    assert result["111"] / shots >= 0.7

    # Check again for a different input
    model.clear()
    # Define an input data sequence (tau = 2)
    input_data = list()
    input_data.append([0.1, 0.2, 1])
    input_data.append([0.0, 0.1, 0.9])
    # Encode the sequence in the model
    for t in range(model.tau):
        for dim in range(model.n):
            model.encode(input_data[t][dim], dim)
    # Check if at least 70% of the shots are 111 (coherent with the input)
    result = model.measure(shots)
    assert result["100"] / shots >= 0.8
Пример #2
0
def test_init():
    """Tests if exceptions are raised for n and tau not being correct"""

    # Testing wrong n
    with pytest.raises(TypeError):
        assert AngularModel(n=1.2, tau=2)
    with pytest.raises(ValueError):
        assert AngularModel(n=-1, tau=3)
    with pytest.raises(ValueError):
        assert AngularModel(n=0, tau=2)

    # Testing wrong tau
    with pytest.raises(TypeError):
        assert AngularModel(n=1, tau=1.2)
    with pytest.raises(ValueError):
        assert AngularModel(n=5, tau=0)
Пример #3
0
def test_qunit():
    # Layer 0
    l0_unit0 = QUnit(
        name="lo_unit0",
        model=AngularModel(n=2, tau=10),
        burst=ZeroBurst(),
        Ts=0.1,
        query=[0.1, 0.5],  # Query target initialized
        # No input
    )

    # Layer 1
    l1_unit0 = QUnit(
        name="l1_unit0",
        model=AngularModel(n=1, tau=3),
        burst=ZeroBurst(),
        Ts=1,
        in_qunits={0: l0_unit0.id},  # Will receive Input from l0_unit0, dim 0
    )

    l1_unit1 = QUnit(
        name="l1_unit1",
        model=AngularModel(n=1, tau=5),
        burst=ZeroBurst(),
        Ts=1,
        in_qunits={0: l0_unit0.id},  # Will receive input from l0_unit0, dim 1
    )

    print(l0_unit0)
    print(l1_unit0)
    print(l1_unit1)
    print(l0_unit0.in_qunits)
    print(l1_unit0.in_qunits)
    print(l1_unit1.in_qunits)

    l0_unit0.start()
    l1_unit0.start()
    l1_unit1.start()
    sleep(2)
    l0_unit0.stop()
    l1_unit0.stop()
    l1_unit1.stop()

    # Redis functions
    qrobot.qunits.qunit.redis_status()
    qrobot.qunits.qunit.flush_redis()
Пример #4
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
Пример #5
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"
Пример #6
0
def test_measure():
    """Tests measuring for unambiguous inputs"""

    model = AngularModel(n=1, tau=1)
    input_data = 1  # unambiguous input
    model.encode(input_data, dim=0)
    assert model.measure(shots=1) == {"1": 1}

    model = AngularModel(n=1, tau=1)
    input_data = 0  # unambiguous input
    model.encode(input_data, dim=0)
    assert model.measure(shots=1) == {"0": 1}

    model = AngularModel(n=3, tau=1)
    input_data = 1  # unambiguous input
    model.encode(input_data, dim=1)
    assert model.measure(shots=1) == {"010": 1}
Пример #7
0
def test_encode():
    """Tests if exceptions are raised for input and dimension not being correct"""

    model = AngularModel(n=2, tau=2)

    # Testing correct way of use encode
    model.encode(0.55, 1)

    # Testing wrong input
    with pytest.raises(TypeError):
        assert model.encode([0.1, 0.2], 1)
    with pytest.raises(TypeError):
        assert model.encode("a", 0)
    with pytest.raises(ValueError):
        assert model.encode(1.1, 1)
    with pytest.raises(ValueError):
        assert model.encode(-0.1, 0)

    # Testing wrong dim
    with pytest.raises(TypeError):
        assert model.encode(0.55, 2.1)
    with pytest.raises(ValueError):
        assert model.encode(0.55, -1)
    with pytest.raises(IndexError):
        assert model.encode(0.55, 2)
Пример #8
0
def test_clear():
    """Tests if clear is working correctly"""
    model = AngularModel(n=2, tau=2)
    model.clear()
Пример #9
0
def test_plot():
    """Tests if the print and plot functions cause any error"""
    model = AngularModel(1, 1)
    model.print_circuit()
    model.plot_state_mat()
Пример #10
0
def test_simulation():
    """Tests if the state and density simulation functions cause any error"""
    model = AngularModel(1, 1)
    model.get_statevector()
    model.get_density_matrix()
Пример #11
0
    print("\n========================================\n" + string +
          "\n========================================\n")


def redis_status_ui():
    status = qrobot.qunits.qunit.redis_status()
    print("Redis status:")
    print(json.dumps(status, indent=1))


heading_ui(f"Script {__file__} started")

heading_ui("Initializing qUnit")
l0_unit0 = qrobot.qunits.QUnit(
    name="lo_unit0",
    model=AngularModel(n=1, tau=10),
    burst=ZeroBurst(),
    Ts=0.05,
    query=[0.5],  # Query target initialized
    # No input
)
print(l0_unit0)

heading_ui("Starting qUnit")
l0_unit0.start()

heading_ui("Check redis status for 10 seconds")
for _ in range(20):
    redis_status_ui()
    time.sleep(1)