-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathbinary_polynomial_test.py
More file actions
200 lines (172 loc) · 7.51 KB
/
binary_polynomial_test.py
File metadata and controls
200 lines (172 loc) · 7.51 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests symbolic_operator.py."""
import unittest
import numpy
from openfermion.ops.operators.binary_polynomial import (
BinaryPolynomial,
BinaryPolynomialError,
_SYMBOLIC_ONE,
)
class BinaryPolynomialTest(unittest.TestCase):
def test_init_long_string(self):
operator1 = BinaryPolynomial('w1 w2 1 + 11')
self.assertEqual(operator1.terms, [(1, 2), (_SYMBOLIC_ONE,)])
operator1 = BinaryPolynomial('a1 b2 1 + 1')
self.assertEqual(operator1.terms, [(1, 2), (_SYMBOLIC_ONE,)])
with self.assertRaises(ValueError):
BinaryPolynomial('1 + wx')
def test_init_string(self):
operator1 = BinaryPolynomial('w1')
self.assertEqual(operator1.terms, [(1,)])
operator1 = BinaryPolynomial('9 w1 w2 + 5')
self.assertEqual(str(operator1), '[W1 W2] + [1]')
operator1 = BinaryPolynomial('9 w1 w2 + 5')
self.assertEqual(str(operator1), '[W1 W2] + [1]')
def test_none_init(self):
operator1 = BinaryPolynomial()
self.assertEqual(operator1.terms, [])
operator1 = BinaryPolynomial([])
self.assertEqual(operator1.terms, [])
with self.assertRaises(ValueError):
operator1 = BinaryPolynomial(12.0)
def test_int_init(self):
operator1 = BinaryPolynomial(3)
self.assertEqual(operator1.terms, [(_SYMBOLIC_ONE,)])
def test_init_list(self):
operator1 = BinaryPolynomial([(3, 4, _SYMBOLIC_ONE)])
self.assertEqual(operator1.terms, [(3, 4)])
operator1 = BinaryPolynomial([(4, 3, _SYMBOLIC_ONE)])
self.assertEqual(operator1.terms, [(3, 4)])
operator1 = BinaryPolynomial(((1, 2), (1, 2)))
self.assertEqual(operator1.terms, [])
with self.assertRaises(ValueError):
operator1 = BinaryPolynomial(((1, -2),))
with self.assertRaises(ValueError):
operator1 = BinaryPolynomial(((1, -2.0),))
def test_multiplication(self):
operator1 = BinaryPolynomial('1 + w1 w2')
operator2 = BinaryPolynomial([(3, 4, _SYMBOLIC_ONE)])
multiplication = operator1 * operator2
self.assertEqual(multiplication.terms, [(3, 4), (1, 2, 3, 4)])
operator1 = BinaryPolynomial([(_SYMBOLIC_ONE,)])
operator1 *= operator1
self.assertEqual(str(operator1), '[1]')
operator1 = 1 * operator1
self.assertEqual(str(operator1), '[1]')
for idx in numpy.arange(3):
operator1 = idx * operator1
with self.assertRaises(TypeError):
operator1 *= 4.3
with self.assertRaises(TypeError):
_ = 4.3 * operator1
with self.assertRaises(TypeError):
_ = operator1 * 4.3
def test_addition(self):
operator1 = BinaryPolynomial('w1 w2')
operator2 = BinaryPolynomial('1 + w1 w2')
addition = operator1 + operator2
self.assertEqual(addition.terms, [(_SYMBOLIC_ONE,)])
addition = addition + 1
self.assertEqual(addition.terms, [])
addition = addition + 1
self.assertEqual(addition.terms, [(_SYMBOLIC_ONE,)])
with self.assertRaises(TypeError):
_ = 4.3 + operator1
with self.assertRaises(TypeError):
operator1 += 4.3
def test_string_output(self):
operator1 = BinaryPolynomial('w15')
self.assertEqual(str(operator1), '[W15]')
operator1 = BinaryPolynomial()
self.assertEqual(operator1.__repr__(), '0')
def test_power(self):
operator1 = BinaryPolynomial('1 + w1 w2 + w3 w4')
pow_loc = operator1**2
self.assertEqual(pow_loc.terms, [(_SYMBOLIC_ONE,), (1, 2), (3, 4)])
with self.assertRaises(TypeError):
_ = operator1**4.3
with self.assertRaises(TypeError):
_ = operator1 ** (-1)
def test_init_binary_rule(self):
operator1 = BinaryPolynomial('1 + w2 w2 + w2')
self.assertEqual(operator1.terms, [(_SYMBOLIC_ONE,)])
def test_multiply_by_one(self):
operator1 = BinaryPolynomial('1 w1 w3')
self.assertEqual(operator1.terms, [(1, 3)])
def test_multiply_by_zero(self):
operator1 = BinaryPolynomial('w1 w3 0')
self.assertEqual(operator1.terms, [])
operator1 = BinaryPolynomial('w1 w3')
operator1 *= 4
self.assertEqual(operator1.terms, [])
def test_ordering(self):
operator1 = BinaryPolynomial('w3 w2 w1 w4')
self.assertEqual(operator1.terms, [(1, 2, 3, 4)])
operator1 = BinaryPolynomial('1 + w1 w2 + w2 w1')
self.assertEqual(operator1.terms, [(_SYMBOLIC_ONE,)])
def test_rmul(self):
operator1 = BinaryPolynomial('1 + w1 w2')
operator1 *= BinaryPolynomial('w1 w2')
self.assertEqual(operator1.terms, [])
def test_add_integer(self):
operator1 = BinaryPolynomial('1 + w1 w2')
operator1 += 1
self.assertEqual(operator1.terms, [(1, 2)])
def test_add_integer2(self):
operator1 = BinaryPolynomial('1 + w1 w2')
operator1 += 2
self.assertEqual(operator1.terms, [(_SYMBOLIC_ONE,), (1, 2)])
def test_shift(self):
operator1 = BinaryPolynomial('1 + w1 w2')
operator1.shift(3)
self.assertEqual(operator1.terms, [(_SYMBOLIC_ONE,), (4, 5)])
with self.assertRaises(TypeError):
operator1.shift(3.5)
def test_count_qubits(self):
operator1 = BinaryPolynomial('1 + w0 w2 w5')
qubits = operator1.enumerate_qubits()
self.assertEqual(qubits, [0, 2, 5])
def test_evaluate(self):
operator1 = BinaryPolynomial()
self.assertEqual(operator1.evaluate('1111'), 0)
operator1 = BinaryPolynomial(1)
self.assertEqual(operator1.evaluate('1111'), 1)
operator1 = BinaryPolynomial('1 + w0 w2 w1 + w0 w1 + w0 w2')
a = operator1.evaluate([0, 0, 1])
self.assertEqual(a, 1.0)
a = operator1.evaluate([1, 0, 1])
self.assertEqual(a, 0.0)
a = operator1.evaluate([1, 1, 1])
self.assertEqual(a, 0.0)
a = operator1.evaluate('1111')
self.assertEqual(a, 0.0)
with self.assertRaises(BinaryPolynomialError):
operator1.evaluate([1, 1])
def test_init_binary_rule2(self):
operator1 = BinaryPolynomial('w1 w1 + 1')
self.assertEqual(operator1.terms, [(1,), (_SYMBOLIC_ONE,)])
def test_power_null(self):
operator1 = BinaryPolynomial('w1 w1 + 1')
is_one = operator1**0
self.assertEqual(is_one.terms, [(_SYMBOLIC_ONE,)])
def test_addition_evaluations(self):
operator1 = BinaryPolynomial('w1 w1 + 1')
operator2 = operator1 + 1
self.assertEqual(operator1.terms, [(1,), (_SYMBOLIC_ONE,)])
self.assertEqual(operator2.terms, [(1,)])
operator2 = 1 + operator1
self.assertEqual(operator1.terms, [(1,), (_SYMBOLIC_ONE,)])
self.assertEqual(operator2.terms, [(1,)])
operator2 += operator1
self.assertEqual(operator1.terms, [(1,), (_SYMBOLIC_ONE,)])
self.assertEqual(operator2.terms, [(_SYMBOLIC_ONE,)])