From dc8b8f628199f87144d60e91a8e5eda5c9e85d2b Mon Sep 17 00:00:00 2001 From: Sharwan Tiwari Date: Thu, 4 Apr 2024 17:14:10 +0400 Subject: [PATCH] resolved error of generation of polynomials for the OR component --- claasp/components/or_component.py | 47 +++++++++++++++++----- tests/unit/components/or_component_test.py | 8 ++-- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/claasp/components/or_component.py b/claasp/components/or_component.py index b4d961c2..7a962103 100644 --- a/claasp/components/or_component.py +++ b/claasp/components/or_component.py @@ -1,4 +1,3 @@ - # **************************************************************************** # Copyright 2023 Technology Innovation Institute # @@ -44,14 +43,43 @@ def algebraic_polynomials(self, model): sage: or_component = gift.get_component_from_id("or_0_4") sage: algebraic = AlgebraicModel(gift) sage: or_component.algebraic_polynomials(algebraic) - [or_0_4_y0 + 1, - or_0_4_y1 + 1, - ... - or_0_4_y30 + 1, - or_0_4_y31 + 1] + [or_0_4_x0*or_0_4_x32 + or_0_4_y0 + or_0_4_x32 + or_0_4_x0, + or_0_4_x1*or_0_4_x33 + or_0_4_y1 + or_0_4_x33 + or_0_4_x1, + or_0_4_x2*or_0_4_x34 + or_0_4_y2 + or_0_4_x34 + or_0_4_x2, + or_0_4_x3*or_0_4_x35 + or_0_4_y3 + or_0_4_x35 + or_0_4_x3, + or_0_4_x4*or_0_4_x36 + or_0_4_y4 + or_0_4_x36 + or_0_4_x4, + or_0_4_x5*or_0_4_x37 + or_0_4_y5 + or_0_4_x37 + or_0_4_x5, + or_0_4_x6*or_0_4_x38 + or_0_4_y6 + or_0_4_x38 + or_0_4_x6, + or_0_4_x7*or_0_4_x39 + or_0_4_y7 + or_0_4_x39 + or_0_4_x7, + or_0_4_x8*or_0_4_x40 + or_0_4_y8 + or_0_4_x40 + or_0_4_x8, + or_0_4_x9*or_0_4_x41 + or_0_4_y9 + or_0_4_x41 + or_0_4_x9, + or_0_4_x10*or_0_4_x42 + or_0_4_y10 + or_0_4_x42 + or_0_4_x10, + or_0_4_x11*or_0_4_x43 + or_0_4_y11 + or_0_4_x43 + or_0_4_x11, + or_0_4_x12*or_0_4_x44 + or_0_4_y12 + or_0_4_x44 + or_0_4_x12, + or_0_4_x13*or_0_4_x45 + or_0_4_y13 + or_0_4_x45 + or_0_4_x13, + or_0_4_x14*or_0_4_x46 + or_0_4_y14 + or_0_4_x46 + or_0_4_x14, + or_0_4_x15*or_0_4_x47 + or_0_4_y15 + or_0_4_x47 + or_0_4_x15, + or_0_4_x16*or_0_4_x48 + or_0_4_y16 + or_0_4_x48 + or_0_4_x16, + or_0_4_x17*or_0_4_x49 + or_0_4_y17 + or_0_4_x49 + or_0_4_x17, + or_0_4_x18*or_0_4_x50 + or_0_4_y18 + or_0_4_x50 + or_0_4_x18, + or_0_4_x19*or_0_4_x51 + or_0_4_y19 + or_0_4_x51 + or_0_4_x19, + or_0_4_x20*or_0_4_x52 + or_0_4_y20 + or_0_4_x52 + or_0_4_x20, + or_0_4_x21*or_0_4_x53 + or_0_4_y21 + or_0_4_x53 + or_0_4_x21, + or_0_4_x22*or_0_4_x54 + or_0_4_y22 + or_0_4_x54 + or_0_4_x22, + or_0_4_x23*or_0_4_x55 + or_0_4_y23 + or_0_4_x55 + or_0_4_x23, + or_0_4_x24*or_0_4_x56 + or_0_4_y24 + or_0_4_x56 + or_0_4_x24, + or_0_4_x25*or_0_4_x57 + or_0_4_y25 + or_0_4_x57 + or_0_4_x25, + or_0_4_x26*or_0_4_x58 + or_0_4_y26 + or_0_4_x58 + or_0_4_x26, + or_0_4_x27*or_0_4_x59 + or_0_4_y27 + or_0_4_x59 + or_0_4_x27, + or_0_4_x28*or_0_4_x60 + or_0_4_y28 + or_0_4_x60 + or_0_4_x28, + or_0_4_x29*or_0_4_x61 + or_0_4_y29 + or_0_4_x61 + or_0_4_x29, + or_0_4_x30*or_0_4_x62 + or_0_4_y30 + or_0_4_x62 + or_0_4_x30, + or_0_4_x31*or_0_4_x63 + or_0_4_y31 + or_0_4_x63 + or_0_4_x31] + """ ninputs = self.input_bit_size noutputs = self.output_bit_size + ors_number = self.description[1] - 1 word_size = noutputs ring_R = model.ring() input_vars = [self.id + "_" + model.input_postfix + str(i) for i in range(ninputs)] @@ -61,10 +89,11 @@ def algebraic_polynomials(self, model): def or_polynomial(x0, x1): return x0 * x1 + x0 + x1 - x = [ring_R.one() for _ in range(noutputs)] - for word_vars in words_vars: + x = [words_vars[0][_] for _ in range(noutputs)] + for or_itr in range(ors_number): for i in range(noutputs): - x[i] = or_polynomial(x[i], word_vars[i]) + x[i] = or_polynomial(x[i], words_vars[or_itr + 1][i]) + y = list(map(ring_R, output_vars)) polynomials = [y[i] + x[i] for i in range(noutputs)] diff --git a/tests/unit/components/or_component_test.py b/tests/unit/components/or_component_test.py index d10d56b1..204a3530 100644 --- a/tests/unit/components/or_component_test.py +++ b/tests/unit/components/or_component_test.py @@ -10,10 +10,10 @@ def test_algebraic_polynomials(): algebraic = AlgebraicModel(gift) algebraic_polynomials = or_component.algebraic_polynomials(algebraic) - assert str(algebraic_polynomials[0]) == "or_0_4_y0 + 1" - assert str(algebraic_polynomials[1]) == "or_0_4_y1 + 1" - assert str(algebraic_polynomials[-2]) == "or_0_4_y30 + 1" - assert str(algebraic_polynomials[-1]) == "or_0_4_y31 + 1" + assert str(algebraic_polynomials[0]) == "or_0_4_x0*or_0_4_x32 + or_0_4_y0 + or_0_4_x32 + or_0_4_x0" + assert str(algebraic_polynomials[1]) == "or_0_4_x1*or_0_4_x33 + or_0_4_y1 + or_0_4_x33 + or_0_4_x1" + assert str(algebraic_polynomials[-2]) == "or_0_4_x30*or_0_4_x62 + or_0_4_y30 + or_0_4_x62 + or_0_4_x30" + assert str(algebraic_polynomials[-1]) == "or_0_4_x31*or_0_4_x63 + or_0_4_y31 + or_0_4_x63 + or_0_4_x31" def test_cp_constraints():