File size: 2,532 Bytes
bb572a8
 
 
46568f0
bb572a8
46568f0
 
 
bb572a8
 
 
46568f0
 
 
 
 
 
bb572a8
46568f0
bb572a8
 
46568f0
bb572a8
46568f0
 
 
 
bb572a8
 
46568f0
91d3e9c
 
 
 
 
46568f0
bb572a8
46568f0
bb572a8
 
46568f0
bb572a8
 
 
46568f0
bb572a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46568f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import pytest
import numpy as np

from quantum_perceptron.utils import (
    get_vector_from_int,
    get_bin_int,
    get_possible_state_strings,
    get_ones_counts_to_states
)


@pytest.mark.parametrize("data, num_qubits, expected_result", [
    (12, 4, '1100'),
    (12, 5, '01100'),
    (1, 1, '1'),
    (2, None, '10'),
    (-5, 2, False)
])
def test_get_bin_int(data, num_qubits, expected_result):
    if isinstance(expected_result, bool) and not expected_result:
        with pytest.raises(ValueError):
            get_bin_int(data, num_qubits)
    else:
        np.array_equal(
            expected_result,
            get_bin_int(data, num_qubits)
        )


@pytest.mark.parametrize("data, num_qubits, expected_result", [
    (12, 4, np.array([1]*12 + [-1, -1, 1, 1])),
    (12, 5, np.array([1]*28 + [-1, -1, 1, 1])),
    (1, 1, np.array([1, -1])),
    (12, 3, np.array([1]*4 + [-1, -1, 1, 1])),
    (16, 2, False),
    (-5, 2, False)
])
def test_get_vector_from_int(data, num_qubits, expected_result):
    if isinstance(expected_result, bool) and not expected_result:
        with pytest.raises(ValueError):
            get_vector_from_int(data, num_qubits)
    else:
        np.array_equal(
            expected_result,
            get_vector_from_int(data, num_qubits)
        )


@pytest.mark.parametrize("num_bits, expected_result", [
    (1, np.array(['0', '1'])),
    (2, np.array(['00', '01', '10', '11'])),
    (3, np.array(['000', '001', '010', '011', '100', '101', '110', '111'])),
    (-5, False),
    (0, False)
])
def test_get_possible_state_strings(num_bits, expected_result):
    if isinstance(expected_result, bool) and not expected_result:
        with pytest.raises(ValueError):
            get_possible_state_strings(num_bits)
    else:
        np.array_equal(
            expected_result,
            get_possible_state_strings(num_bits)
        )


@pytest.mark.parametrize("states, expected_result", [
    (np.array(['0', '1']), {0: [0], 1: [1]}),
    (np.array(['00', '01', '10', '11']), {0: [0], 1: [1, 2], 2: [3]}),
    (np.array(['000', '001', '010', '011', '100', '101', '110', '111']), {
        0: [0],
        1: [1, 2, 4],
        2: [3, 5, 6],
        3: [7]
    }),
    (np.array([]), False)
])
def test_get_ones_counts_to_states(states, expected_result):
    if isinstance(expected_result, bool) and not expected_result:
        with pytest.raises(ValueError):
            get_ones_counts_to_states(states)
    else:
        assert expected_result == get_ones_counts_to_states(states)