ashutosh1919 commited on
Commit
bb572a8
1 Parent(s): 47efae2

Added data utilities

Browse files
mypy.ini ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Global options:
2
+
3
+ [mypy]
4
+ plugins = numpy.typing.mypy_plugin
5
+ warn_return_any = True
6
+ warn_unused_configs = True
7
+
8
+ # Per-module options:
9
+
10
+ [mypy-mycode.foo.*]
11
+ disallow_untyped_defs = True
12
+
13
+ [mypy-mycode.bar]
14
+ warn_return_any = False
15
+
16
+ [mypy-somelibrary]
17
+ ignore_missing_imports = True
quantum_perceptron/__init__.py ADDED
File without changes
quantum_perceptron/tests/test_utils.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import numpy as np
3
+
4
+ from quantum_perceptron.utils.data_utils import (
5
+ get_vector_from_int,
6
+ get_num_bits,
7
+ get_possible_state_strings
8
+ )
9
+
10
+
11
+ @pytest.mark.parametrize("data, expected_result", [
12
+ (12, 4),
13
+ (8, 4),
14
+ (7, 3),
15
+ (0, 1),
16
+ (-5, False),
17
+ ])
18
+ def test_get_num_bits(data, expected_result):
19
+ if isinstance(expected_result, bool) and not expected_result:
20
+ with pytest.raises(ValueError):
21
+ get_num_bits(data)
22
+ else:
23
+ assert expected_result == get_num_bits(data)
24
+
25
+
26
+ @pytest.mark.parametrize("data, expected_result", [
27
+ (12, np.array([-1, -1, 1, 1])),
28
+ (1, np.array([1, 1, 1, -1])),
29
+ (0, np.array([1, 1, 1, 1])),
30
+ (-5, False)
31
+ ])
32
+ def test_get_vector_from_int(data, expected_result):
33
+ if isinstance(expected_result, bool) and not expected_result:
34
+ with pytest.raises(ValueError):
35
+ get_vector_from_int(data)
36
+ else:
37
+ np.array_equal(
38
+ expected_result,
39
+ get_vector_from_int(data)
40
+ )
41
+
42
+
43
+ @pytest.mark.parametrize("num_bits, expected_result", [
44
+ (1, np.array(['0', '1'])),
45
+ (2, np.array(['00', '01', '10', '11'])),
46
+ (3, np.array(['000', '001', '010', '011', '100', '101', '110', '111'])),
47
+ (-5, False),
48
+ (0, False)
49
+ ])
50
+ def test_get_possible_state_strings(num_bits, expected_result):
51
+ if isinstance(expected_result, bool) and not expected_result:
52
+ with pytest.raises(ValueError):
53
+ get_possible_state_strings(num_bits)
54
+ else:
55
+ np.array_equal(
56
+ expected_result,
57
+ get_possible_state_strings(num_bits)
58
+ )
quantum_perceptron/utils/__init__.py ADDED
File without changes
quantum_perceptron/utils/data_utils.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import numpy.typing as npt
3
+
4
+
5
+ def assert_negative(data: int):
6
+ """
7
+ General method to prevent negative values of integers.
8
+ """
9
+ if data < 0:
10
+ raise ValueError("Currently we do not support negative data values.")
11
+
12
+
13
+ def get_bin_int(data: int) -> str:
14
+ """
15
+ Get binary representation of integer.
16
+ """
17
+ assert_negative(data)
18
+ return bin(data)[2:]
19
+
20
+
21
+ def get_num_bits(data: int) -> int:
22
+ """
23
+ Get number of bits in an integer.
24
+ """
25
+ assert_negative(data)
26
+ return len(get_bin_int(data))
27
+
28
+
29
+ def get_vector_from_int(data: int) -> np.ndarray:
30
+ """
31
+ This method returns the vector where each element is (-1)^b_i where b_i is
32
+ the bit value at index i.
33
+
34
+ Args:
35
+ data: `int` representing data value
36
+ (correspponding toinput or weight vector)
37
+
38
+ Returns: Vector in form of `np.ndarray`.
39
+ """
40
+ assert_negative(data)
41
+
42
+ bin_data = get_bin_int(data)
43
+ num_qubits = get_num_bits(data)
44
+ data_vector = np.empty(num_qubits)
45
+
46
+ for i, bit in enumerate(bin_data):
47
+ data_vector[i] = np.power(-1, int(bit))
48
+
49
+ return data_vector
50
+
51
+
52
+ def get_possible_state_strings(num_bits: int) -> np.ndarray:
53
+ """
54
+ Get all the state bit strings corresponding to given number of bits.
55
+ For example for 2 bits, we get ['00', '01', '10', '11'].
56
+ Note that we are only allowing bits < 10 as of now.
57
+
58
+ Args:
59
+ num_bits: `int` representing number of bits
60
+
61
+ Returns: `np.ndarray` containing all states of `num_bits` bits.
62
+ """
63
+ assert_negative(num_bits)
64
+ if num_bits == 0:
65
+ raise ValueError("Number of bits cannot be 0")
66
+ if num_bits >= 10:
67
+ raise ValueError("We are not currently supporting bits >= 10")
68
+
69
+ total_states = np.power(2, num_bits)
70
+ states = np.empty(total_states, dtype="<U10")
71
+ state_template = "{:0" + str(num_bits) + "b}"
72
+ for i in range(total_states):
73
+ states[i] = state_template.format(i)
74
+
75
+ return states
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ numpy
2
+ qiskit
3
+ pycodestyle
4
+ pytest
5
+ mypy
run_tests.sh ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ echo "#### RUNNING MYPY TESTS ####"
2
+ PYTHONDONTWRITEBYTECODE=1 mypy quantum_perceptron/
3
+
4
+ echo "#### RUNNING PYCODESTYLE TESTS ####"
5
+ pycodestyle quantum_perceptron/
6
+
7
+ echo "#### RUNNING PYTEST TESTS ####"
8
+ PYTHONDONTWRITEBYTECODE=1 python -m pytest -p no:cacheprovider