forked from microsoft/QuantumKatas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.py
407 lines (371 loc) · 13.8 KB
/
testing.py
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import math as m
import random as r
from pytest import approx
tests = {}
# Exercise decorator, specifying that this function needs to be tested
def exercise(fun):
tests[fun.__name__](fun)
return fun
# Test decorator, specifying that this is a test for an exercise
def test(fun):
tests[fun.__name__[:-5]] = fun
return fun
# ------------------------------------------------------
# Generates a random complex number in Cartesian form
def prep_random_cartesian():
real = (r.random() - 0.5) * r.randint(0, 100)
imag = (r.random() - 0.5) * r.randint(0, 100)
return (real, imag)
# Generates a random complex number in polar form
def prep_random_polar():
rad = r.random() * r.randint(0, 100)
theta = (r.random() - 0.5) * m.pi
return (rad, theta)
# ------------------------------------------------------
# Assert that checks if the result is a tuple of length 2
def assert_tuple(result):
if result == None: return "Your function must return a value!"
if not type(result) is tuple: return "Your function must return a tuple, returned " + type(result).__name__ + "."
if result[0] == ... or result[1] == ...: return "You should not return an ellipsis (...) as part of your answer."
if len(result) != 2:
return "Your function must return a tuple of length 2, but returned tuple is of length " + str(len(result))
# Assert that verifies the output is a valid complex tuple, and checks that it matches expected output
def assert_cartesian(expected, actual, message):
if actual != approx(expected): return message
# Assert that verifies the output is a valid polar tuple, and checks that it matches expected output
def assert_polar(expected, actual, message):
(ar, atheta) = actual
if ar == 0:
if ar != approx(expected[0]): return message
if not (-m.pi < atheta <= m.pi): return "Even for 0 + 0i, the phase must be between -pi and pi."
return
if actual != approx(expected): return message
# ------------------------------------------------------
# Formats a complex number in Cartesian form neatly
def format_cartesian(x):
return "{0:.3f}".format(x[0]) + (" + " if x[1] >= 0 else " - ") + "{0:.3f}i".format(abs(x[1]))
# Formats a complex number in polar form neatly
def format_polar(x):
return "{0:.3f} * e^({1:.3f}i)".format(x[0], x[1])
# ------------------------------------------------------
def imaginary_power_ref(n):
return 1 if n % 4 == 0 else -1
@test
def imaginary_power_test(fun):
for i in range(-25, 25):
n = 2 * i
expected = imaginary_power_ref(n)
actual = fun(n)
if actual == None:
print("Your function must return a value!")
return
if expected != actual:
message = "Result of exponentiation doesn't seem to match expected value: expected (i)^{0} = {1}, got {2}"
print(message.format(n, expected, actual))
return
print("Success!")
# ------------------------------------------------------
def complex_add_ref(x, y):
return (x[0] + y[0], x[1] + y[1])
@test
def complex_add_test(fun):
for i in range(25):
x = prep_random_cartesian()
y = prep_random_cartesian()
expected = complex_add_ref(x, y)
actual = fun(x, y)
msg = assert_tuple(actual)
if msg != None:
print(msg)
return
msg = assert_cartesian(expected, actual,
"Sum doesn't seem to match expected value: expected ("
+ format_cartesian(x)
+ ") + ("
+ format_cartesian(y)
+ ") = "
+ format_cartesian(expected)
+ ", got "
+ format_cartesian(actual))
if msg != None:
print(msg)
return
print("Success!")
# ------------------------------------------------------
def complex_mult_ref(x, y):
return ((x[0] * y[0]) - (x[1] * y[1]), (x[0] * y[1]) + (x[1] * y[0]))
@test
def complex_mult_test(fun):
for i in range(25):
x = prep_random_cartesian()
y = prep_random_cartesian()
expected = complex_mult_ref(x, y)
actual = fun(x, y)
msg = assert_tuple(actual)
if msg != None:
print(msg)
return
msg = assert_cartesian(expected, actual,
"Product doesn't seem to match expected value: expected ("
+ format_cartesian(x)
+ ") * ("
+ format_cartesian(y)
+ ") = "
+ format_cartesian(expected)
+ ", got "
+ format_cartesian(actual))
if msg != None:
print(msg)
return
print("Success!")
# ------------------------------------------------------
def conjugate_ref(x):
return (x[0], -x[1])
@test
def conjugate_test(fun):
for i in range(25):
x = prep_random_cartesian()
expected = conjugate_ref(x)
actual = fun(x)
msg = assert_tuple(actual)
if msg != None:
print(msg)
return
msg = assert_cartesian(expected, actual,
"Conjugate doesn't seem to match expected value: expected conjugate of "
+ format_cartesian(x)
+ " to be "
+ format_cartesian(expected)
+ ", got "
+ format_cartesian(actual))
if msg != None:
print(msg)
return
print("Success!")
# ------------------------------------------------------
def complex_div_ref(x, y):
ybar = conjugate_ref(y)
numer = complex_mult_ref(x, ybar)
denom = complex_mult_ref(y, ybar)[0]
return complex_mult_ref(numer, (1 / denom, 0))
@test
def complex_div_test(fun):
for i in range(25):
x = prep_random_cartesian()
y = (0, 0)
while y == (0, 0):
y = prep_random_cartesian()
expected = complex_div_ref(x, y)
actual = fun(x, y)
msg = assert_tuple(actual)
if msg != None:
print(msg)
return
msg = assert_cartesian(expected, actual,
"Quotient doesn't seem to match expected value: expected ("
+ format_cartesian(x)
+ ") / ("
+ format_cartesian(y)
+ ") = "
+ format_cartesian(expected)
+ ", got "
+ format_cartesian(actual))
if msg != None:
print(msg)
return
print("Success!")
# ------------------------------------------------------
def modulus_ref(x):
return m.sqrt(complex_mult_ref(x, conjugate_ref(x))[0])
@test
def modulus_test(fun):
for i in range(25):
x = prep_random_cartesian()
expected = modulus_ref(x)
actual = fun(x)
if actual == None:
print("Your function must return a value!")
return
if not (type(actual) is float or type(actual) is int):
print("Your function must return a number, returned " + type(actual).__name__ + ".")
return
if actual != approx(expected):
print("Modulus doesn't seem to match expected value: expected |"
+ format_cartesian(x)
+ "| = {0:.3f}, got {1:.3f}".format(expected, actual))
return
print("Success!")
# ------------------------------------------------------
def complex_exp_ref(x):
realpow = m.e ** x[0]
return (realpow * m.cos(x[1]), realpow * m.sin(x[1]))
@test
def complex_exp_test(fun):
for i in range(25):
x = prep_random_cartesian()
expected = complex_exp_ref(x)
actual = fun(x)
msg = assert_tuple(actual)
if msg != None:
print(msg)
return
msg = assert_cartesian(expected, actual,
"Result of exponentiation doesn't seem to match expected value: expected e^("
+ format_cartesian(x)
+ ") = "
+ format_cartesian(expected)
+ ", got "
+ format_cartesian(actual))
if msg != None:
print(msg)
return
print("Success!")
# ------------------------------------------------------
def complex_exp_real_ref(r, x):
if r == 0: return (0, 0)
lnr = m.log(r)
return complex_exp_ref(complex_mult_ref((lnr, 0), x))
@test
def complex_exp_real_test(fun):
for i in range(25):
base = r.random() * r.randint(1, 100)
if i == 0:
base = 0
x = prep_random_cartesian()
expected = complex_exp_real_ref(base, x)
actual = fun(base, x)
msg = assert_tuple(actual)
if msg != None:
print(msg)
return
msg = assert_cartesian(expected, actual,
"Result of exponentiation doesn't seem to match expected value: "
+ "expected {0:.3f}^(".format(base)
+ format_cartesian(x)
+ ") = "
+ format_cartesian(expected)
+ ", got "
+ format_cartesian(actual))
if msg != None:
print(msg)
return
print("Success!")
# ------------------------------------------------------
def polar_convert_ref(x):
return (modulus_ref(x), m.atan2(x[1], x[0]))
@test
def polar_convert_test(fun):
for i in range(25):
x = prep_random_cartesian()
if i == 0:
x = (0, 0)
expected = polar_convert_ref(x)
actual = fun(x)
msg = assert_tuple(actual)
if msg != None:
print(msg)
return
msg = assert_polar(expected, actual,
"Polar conversion doesn't seem to match expected value: expected "
+ format_cartesian(x)
+ " to be converted to "
+ format_polar(expected)
+ ", got "
+ format_polar(actual))
if msg != None:
print(msg)
return
print("Success!")
# ------------------------------------------------------
def cartesian_convert_ref(x):
return (x[0] * m.cos(x[1]), x[0] * m.sin(x[1]))
@test
def cartesian_convert_test(fun):
for i in range(25):
x = prep_random_polar()
expected = cartesian_convert_ref(x)
actual = fun(x)
msg = assert_tuple(actual)
if msg != None:
print(msg)
return
msg = assert_cartesian(expected, actual,
"Cartesian conversion doesn't seem to match expected value: expected "
+ format_polar(x)
+ " to be converted to "
+ format_cartesian(expected)
+ ", got "
+ format_cartesian(actual))
if msg != None:
print(msg)
return
print("Success!")
# ------------------------------------------------------
def polar_mult_ref(x, y):
angle = x[1] + y[1]
if angle > m.pi: angle -= 2 * m.pi
elif angle <= -m.pi: angle += 2 * m.pi
return (x[0] * y[0], angle)
@test
def polar_mult_test(fun):
for i in range(25):
x = prep_random_polar()
y = prep_random_polar()
if i == 0:
x = (3, 2)
y = x
elif i == 1:
x = (3, -2)
y = x
expected = polar_mult_ref(x, y)
actual = fun(x, y)
msg = assert_tuple(actual)
if msg != None:
print(msg)
return
msg = assert_polar(expected, actual,
"Product doesn't seem to match expected value: expected ("
+ format_polar(x)
+ ") * ("
+ format_polar(y)
+ ") = "
+ format_polar(expected)
+ ", got "
+ format_polar(actual))
if msg != None:
print(msg)
return
print("Success!")
# ------------------------------------------------------
def complex_exp_arbitrary_ref(x, y):
xp = polar_convert_ref(x)
return complex_mult_ref(complex_exp_real_ref(xp[0], y), complex_exp_ref(complex_mult_ref((0, xp[1]), y)))
@test
def complex_exp_arbitrary_test(fun):
for i in range(25):
x = prep_random_cartesian()
y = prep_random_cartesian()
if i == 0:
x = (0, 0)
expected = complex_exp_arbitrary_ref(x, y)
actual = fun(x, y)
msg = assert_tuple(actual)
if msg != None:
print(msg)
return
msg = assert_cartesian(expected, actual,
"Result of exponentiation doesn't seem to match expected value: expected ("
+ format_cartesian(x)
+ ")^("
+ format_cartesian(y)
+ ") = "
+ format_cartesian(expected)
+ ", got "
+ format_cartesian(actual))
if msg != None:
print(msg)
return
print("Success!")
print("Success!")