diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 888a114b..ff0a9c50 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -49,7 +49,7 @@ To run all tests with unittest, execute .. code-block:: shell - python -m unittest discover test + python -m unittest discover tests To generate coverage reports, execute diff --git a/docs/conf.py b/docs/conf.py index 0617e93b..48f3f8ff 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -25,9 +25,9 @@ author = u'Author' # The short X.Y version -version = u'0.1' +version = u'1.0' # The full version, including alpha/beta/rc tags -release = u'0.0.1' +release = u'1.0.5' # -- General configuration --------------------------------------------------- diff --git a/pyqubo/integer/integer.py b/pyqubo/integer/integer.py index 2dc5a469..8e0e6cb3 100644 --- a/pyqubo/integer/integer.py +++ b/pyqubo/integer/integer.py @@ -13,7 +13,6 @@ # limitations under the License. import abc -import six from cpp_pyqubo import UserDefinedExpress, WithPenalty diff --git a/pyqubo/package_info.py b/pyqubo/package_info.py index d4a763e6..c0105bac 100644 --- a/pyqubo/package_info.py +++ b/pyqubo/package_info.py @@ -1,6 +1,6 @@ # (major, minor, patch, prerelease) -VERSION = (1, 0, 4, "") +VERSION = (1, 0, 5, "") __shortversion__ = '.'.join(map(str, VERSION[:3])) __version__ = '.'.join(map(str, VERSION[:3])) + "".join(VERSION[3:]) diff --git a/setup.py b/setup.py index 6f1d41e7..564952c1 100644 --- a/setup.py +++ b/setup.py @@ -104,7 +104,8 @@ def run(self): 'numpy>=1.16.0,<2.0.0', 'dimod>=0.9.2', 'dwave-neal>=0.5.4', - 'Deprecated>=1.2.10' + 'Deprecated>=1.2.10', + 'six>=1.11.0' ] python_requires = '>=3.5' diff --git a/src/reduce_order.cpp b/src/reduce_order.cpp index 7864cc88..1f178c4b 100644 --- a/src/reduce_order.cpp +++ b/src/reduce_order.cpp @@ -10,11 +10,8 @@ namespace reduce_order{ void add_term(Terms* terms, Prod prod, CoeffPtr coeff){ auto result = terms->find(prod); if(result == terms->end()){ - //terms->at(prod) = coeff; terms->insert(TermsPair{prod, coeff}); }else{ - //terms->at(prod) = coeff->add(result->second); - //terms->insert(TermsPair{prod, coeff->add(result->second)}); result->second = coeff->add(result->second); } } @@ -71,9 +68,7 @@ namespace reduce_order{ } void replace_variable(Poly* mp, QuboIndex index_pair, int new_variable){ - int loop_cnt = 0; for(auto it = mp->terms->begin(); it != mp->terms->end(); it++){ - loop_cnt++; Prod prod = it->first; CoeffPtr coeff = it->second; bool first_in, second_in = false; @@ -82,7 +77,7 @@ namespace reduce_order{ if(prod.get_var(i) == index_pair.second) second_in = true; } if(first_in && second_in){ - uint32_t indices[4] = {0}; + uint32_t* indices = new uint32_t[it->first.length+1]; int index = 0; for(int i=0; i < it->first.length; i++){ if(prod.get_var(i) != index_pair.first && prod.get_var(i) != index_pair.second){ diff --git a/tests/_test_main.py b/tests/_test_main.py deleted file mode 100644 index b82dc736..00000000 --- a/tests/_test_main.py +++ /dev/null @@ -1,89 +0,0 @@ -from pyqubo import Binary, Add, Num, Mul, Dog, DogAdd, WithPenalty, SubH -#from pyqubo import Binary, Add, Num, Mul -import unittest -import time -#from memory_profiler import profile -#from memory_profiler import memory_usage -import gc -import sys -from copy import copy - - -class TestExpressEquality(unittest.TestCase): - - def test_main3(self): - t0 = time.time() - n = 40 - H = Binary("a") - for i in range(n): - a = Binary("a") - for j in range(n): - b = Binary(f"b_{i}_{j}") - a = a + b - H = Add(H, Mul(a, a)) - - for j in range(n): - a = Binary("a") - for i in range(n): - b = Binary(f"b_{i}_{j}") - a = a + b - H = Add(H, Mul(a, a)) - - for i in range(n): - for j in range(n): - for k in range(n): - b1 = Binary(f"b_{i}_{j}") - b2 = Binary(f"b_{j}_{k}") - H = H + b1*b2 - - model = H.compile(1.0) - t1 = time.time() - print("compile:", t1-t0) - qubo, offset = model.to_qubo(False, {}) - #print(qubo, offset) - t2 = time.time() - print(t2-t1) - - def _test_main2(self): - class CustomPenalty(WithPenalty): - def __init__(self): - WithPenalty.__init__(self) - self.hamiltonian = Binary("a") - self.penalty = Binary("b") - - custom_penalty = CustomPenalty() - print(custom_penalty) - - def _test_main1(self): - pass - array_index = ArrayIndex("b", "array_name", (2, 2), [0, 0]) - a = Binary("a", array_index) - array_index2 = ArrayIndex("b", "array_name", [2, 2], [0, 1]) - b = Binary("b", array_index2) - model = (a+b).compile(1.0) - print(model) - - def _test_main4(self): - array_index = ArrayIndex("b", "array_name", [2, 2], [0, 0]) - h = Binary("b", array_index) - subh = SubH(h, "label1") - H = subh+Binary("a") - model = H.compile(1) - print(model) - sol = model.decode_solutions([{"a": 1, "b": 1}], "BINARY", {}) - array = sol[0].arrays["array_name"] - print("array.keys()", array.keys()) - print("array[0,0]", array[0, 0]) - - class Sample: - def __init__(self): - pass - def __getitem__(self, key): - print(key) - s = Sample() - print(s[0]) - print(s[0:10]) - print(s[0:1,:]) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/test_express.py b/tests/test_express.py index 791e14f7..284c9635 100644 --- a/tests/test_express.py +++ b/tests/test_express.py @@ -106,7 +106,6 @@ def test_compile_subh(self): self.compile_check(exp, expected_qubo, expected_offset, feed_dict) def test_compile_constraint(self): - import dimod a, b, c = Binary("a"), Binary("b"), Binary("c") exp = Constraint(a*b*c, label="constraint") expected_qubo = {('a', '0*1'): -10.0, ('b', '0*1'): -10.0, ('0*1', '0*1'): 15.0, ('a', 'a'): 0.0, ('a', 'b'): 5.0, ('c', '0*1'): 1.0, ('b', 'b'): 0.0, ('c', 'c'): 0.0} @@ -126,6 +125,6 @@ def __init__(self, hamiltonian, penalty, label): expected_offset = 0.0 feed_dict={"p": 2} self.compile_check(custom_penalty, expected_qubo, expected_offset, feed_dict) - + if __name__ == '__main__': unittest.main() diff --git a/tests/test_model.py b/tests/test_model.py index 85182f18..536df3cd 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -236,6 +236,22 @@ def test_constraint(self): if sol.energy==1.0: self.assertEqual(sol.subh['C1'], 1.0) + def test_higher_order(self): + x = Array.create('x', 5, 'BINARY') + exp = x[0]*x[1]*x[2]*x[3] + model = exp.compile(strength=10) + + sample = {'x[0]': 1, 'x[1]': 1, 'x[2]': 1, 'x[3]': 1, '0*1': 1, '2*3': 1} + e = model.energy(sample, vartype='BINARY') + self.assertEqual(e, 1.0) + + sample = {'x[0]': 0, 'x[1]': 1, 'x[2]': 1, 'x[3]': 1, '0*1': 0, '2*3': 1} + e = model.energy(sample, vartype='BINARY') + self.assertEqual(e, 0.0) + + sample = {'x[0]': 1, 'x[1]': 1, 'x[2]': 1, 'x[3]': 1, '0*1': 0, '2*3': 1} + e = model.energy(sample, vartype='BINARY') + self.assertEqual(e, 10.0) if __name__ == '__main__': unittest.main()