microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.18.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

library/src/tests/arithmetic.rs

973lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use super::{test_expression, test_expression_with_lib};
5use qsc::interpret::Value;
6
7// Tests for Microsoft.Quantum.Unstable.Arithmetic namespace
8
9#[test]
10fn check_maj() {
11 test_expression(
12 {
13 "{
14 import Microsoft.Quantum.Unstable.Arithmetic.*;
15 use q = Qubit[3];
16 mutable r = [];
17 for i in 0..7 {
18 ApplyXorInPlace(i, q);
19 MAJ(q[0],q[1],q[2]);
20 set r += [MeasureInteger(q)];
21 ResetAll(q);
22 }
23 r
24 }"
25 },
26 &Value::Array(
27 vec![
28 Value::Int(0),
29 Value::Int(1),
30 Value::Int(2),
31 Value::Int(7),
32 Value::Int(3),
33 Value::Int(6),
34 Value::Int(5),
35 Value::Int(4),
36 ]
37 .into(),
38 ),
39 );
40}
41
42#[test]
43fn check_reflect_about_integer() {
44 test_expression(
45 {
46 "{
47 import Microsoft.Quantum.Unstable.Arithmetic.*;
48 import Std.Diagnostics.*;
49 operation ManuallyReflectAboutFive(register : Qubit[]) : Unit is Adj + Ctl {
50 within {
51 X(register[1]);
52 } apply {
53 Controlled Z(register[0..1], register[2]);
54 }
55 }
56 CheckOperationsAreEqual(3,
57 ReflectAboutInteger(5, _),
58 ManuallyReflectAboutFive
59 )
60 }"
61 },
62 &Value::Bool(true),
63 );
64}
65
66// ============================ Adders ============================
67
68//
69// IncByLE
70//
71
72const INC_BY_LE_TEST_LIB: &str = include_str!("resources/src/inc_by_le.qs");
73
74#[test]
75fn check_inc_by_le_exhaustive_bitwidth_1() {
76 test_expression_with_lib(
77 "Test.TestIncByLE(\"Check IncByLE\",
78 Microsoft.Quantum.Unstable.Arithmetic.IncByLE, 1)",
79 INC_BY_LE_TEST_LIB,
80 &Value::Tuple(vec![].into()),
81 );
82}
83
84#[test]
85fn check_inc_by_le_exhaustive_bitwidth_2() {
86 test_expression_with_lib(
87 "Test.TestIncByLE(\"Check IncByLE\",
88 Microsoft.Quantum.Unstable.Arithmetic.IncByLE, 2)",
89 INC_BY_LE_TEST_LIB,
90 &Value::Tuple(vec![].into()),
91 );
92}
93
94#[test]
95fn check_inc_by_le_exhaustive_bitwidth_3() {
96 test_expression_with_lib(
97 "Test.TestIncByLE(\"Check IncByLE\",
98 Microsoft.Quantum.Unstable.Arithmetic.IncByLE, 3)",
99 INC_BY_LE_TEST_LIB,
100 &Value::Tuple(vec![].into()),
101 );
102}
103
104#[test]
105fn check_inc_by_le_general() {
106 test_expression(
107 {
108 "{ // General cases for IncByLE
109 import Microsoft.Quantum.Unstable.Arithmetic.*;
110 use x1 = Qubit[10];
111 use y1 = Qubit[10];
112 ApplyXorInPlace(279, x1);
113 ApplyXorInPlace(383, y1);
114 IncByLE(x1,y1); // 383 += 279
115 let i = MeasureInteger(y1);
116 ResetAll(x1+y1);
117
118 return i;
119 }"
120 },
121 &Value::Int(383 + 279),
122 );
123}
124
125//
126// RippleCarryTTKIncByLE
127//
128
129#[test]
130fn check_ripple_carry_ttk_inc_by_le_exhaustive_bitwidth_1() {
131 test_expression_with_lib(
132 "Test.TestIncByLE(\"Check RippleCarryTTKIncByLE\",
133 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryTTKIncByLE, 1)",
134 INC_BY_LE_TEST_LIB,
135 &Value::Tuple(vec![].into()),
136 );
137}
138
139#[test]
140fn check_ripple_carry_ttk_inc_by_le_exhaustive_bitwidth_2() {
141 test_expression_with_lib(
142 "Test.TestIncByLE(\"Check RippleCarryTTKIncByLE\",
143 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryTTKIncByLE, 2)",
144 INC_BY_LE_TEST_LIB,
145 &Value::Tuple(vec![].into()),
146 );
147}
148
149#[test]
150fn check_ripple_carry_ttk_inc_by_le_exhaustive_bitwidth_3() {
151 test_expression_with_lib(
152 "Test.TestIncByLE(\"Check RippleCarryTTKIncByLE\",
153 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryTTKIncByLE, 3)",
154 INC_BY_LE_TEST_LIB,
155 &Value::Tuple(vec![].into()),
156 );
157}
158
159#[test]
160fn check_ripple_carry_ttk_inc_by_le_exhaustive_bitwidth_4() {
161 test_expression_with_lib(
162 "Test.TestIncByLE(\"Check RippleCarryTTKIncByLE\",
163 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryTTKIncByLE, 4)",
164 INC_BY_LE_TEST_LIB,
165 &Value::Tuple(vec![].into()),
166 );
167}
168
169#[test]
170fn check_ripple_carry_ttk_inc_by_le_general() {
171 test_expression(
172 {
173 "{ // General cases for RippleCarryTTKIncByLE
174 import Microsoft.Quantum.Unstable.Arithmetic.*;
175
176 use x1 = Qubit[10];
177 use y1 = Qubit[10];
178 ApplyXorInPlace(245, x1);
179 ApplyXorInPlace(674, y1);
180 RippleCarryTTKIncByLE(x1,y1); // 674 += 245
181 let i = MeasureInteger(y1);
182 ResetAll(x1+y1);
183 return i;
184 }"
185 },
186 &Value::Int(674 + 245),
187 );
188}
189
190//
191// RippleCarryCGIncByLE
192//
193
194#[test]
195fn check_ripple_carry_cg_inc_by_le_exhaustive_bitwidth_1() {
196 test_expression_with_lib(
197 "Test.TestIncByLE(\"Check RippleCarryCGIncByLE\",
198 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGIncByLE, 1)",
199 INC_BY_LE_TEST_LIB,
200 &Value::Tuple(vec![].into()),
201 );
202}
203
204#[test]
205fn check_ripple_carry_cg_inc_by_le_exhaustive_bitwidth_2() {
206 test_expression_with_lib(
207 "Test.TestIncByLE(\"Check RippleCarryCGIncByLE\",
208 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGIncByLE, 2)",
209 INC_BY_LE_TEST_LIB,
210 &Value::Tuple(vec![].into()),
211 );
212}
213
214#[test]
215fn check_ripple_carry_cg_inc_by_le_exhaustive_bitwidth_3() {
216 test_expression_with_lib(
217 "Test.TestIncByLE(\"Check RippleCarryCGIncByLE\",
218 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGIncByLE, 3)",
219 INC_BY_LE_TEST_LIB,
220 &Value::Tuple(vec![].into()),
221 );
222}
223
224#[test]
225fn check_ripple_carry_cg_inc_by_le_exhaustive_bitwidth_4() {
226 test_expression_with_lib(
227 "Test.TestIncByLE(\"Check RippleCarryCGIncByLE\",
228 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGIncByLE, 4)",
229 INC_BY_LE_TEST_LIB,
230 &Value::Tuple(vec![].into()),
231 );
232}
233
234#[test]
235fn check_ripple_carry_inc_by_le_ctl_exhaustive_bitwidth_1() {
236 test_expression_with_lib(
237 "Test.TestIncByLECtl(\"Check RippleCarryCGIncByLE(Ctl)\",
238 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGIncByLE, 1)",
239 INC_BY_LE_TEST_LIB,
240 &Value::Tuple(vec![].into()),
241 );
242}
243
244#[test]
245fn check_ripple_carry_inc_by_le_ctl_exhaustive_bitwidth_2() {
246 test_expression_with_lib(
247 "Test.TestIncByLECtl(\"Check RippleCarryCGIncByLE(Ctl)\",
248 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGIncByLE, 2)",
249 INC_BY_LE_TEST_LIB,
250 &Value::Tuple(vec![].into()),
251 );
252}
253
254#[test]
255fn check_ripple_carry_inc_by_le_ctl_exhaustive_bitwidth_3() {
256 test_expression_with_lib(
257 "Test.TestIncByLECtl(\"Check RippleCarryCGIncByLE(Ctl)\",
258 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGIncByLE, 3)",
259 INC_BY_LE_TEST_LIB,
260 &Value::Tuple(vec![].into()),
261 );
262}
263
264#[test]
265fn check_ripple_carry_inc_by_le_ctl_exhaustive_bitwidth_4() {
266 test_expression_with_lib(
267 "Test.TestIncByLECtl(\"Check RippleCarryCGIncByLE(Ctl)\",
268 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGIncByLE, 4)",
269 INC_BY_LE_TEST_LIB,
270 &Value::Tuple(vec![].into()),
271 );
272}
273
274#[test]
275fn check_ripple_carry_cg_inc_by_le_general() {
276 test_expression(
277 {
278 "{ // General cases for RippleCarryCGIncByLE
279 import Microsoft.Quantum.Unstable.Arithmetic.*;
280
281 use x1 = Qubit[10];
282 use y1 = Qubit[10];
283 ApplyXorInPlace(743, x1);
284 ApplyXorInPlace(112, y1);
285 RippleCarryTTKIncByLE(x1,y1); // 112 += 743
286 let i = MeasureInteger(y1);
287 ResetAll(x1+y1);
288
289 return i;
290 }"
291 },
292 &Value::Int(112 + 743),
293 );
294}
295
296//
297// FourierTDIncByLE
298//
299
300#[test]
301fn check_fourier_td_inc_by_le_exhaustive_bitwidth_1() {
302 test_expression_with_lib(
303 "Test.TestIncByLE(\"Check FourierTDIncByLE\",
304 Microsoft.Quantum.Unstable.Arithmetic.FourierTDIncByLE, 1)",
305 INC_BY_LE_TEST_LIB,
306 &Value::Tuple(vec![].into()),
307 );
308}
309
310#[test]
311fn check_fourier_td_inc_by_le_exhaustive_bitwidth_2() {
312 test_expression_with_lib(
313 "Test.TestIncByLE(\"Check FourierTDIncByLE\",
314 Microsoft.Quantum.Unstable.Arithmetic.FourierTDIncByLE, 2)",
315 INC_BY_LE_TEST_LIB,
316 &Value::Tuple(vec![].into()),
317 );
318}
319
320#[test]
321fn check_fourier_td_inc_by_le_exhaustive_bitwidth_3() {
322 test_expression_with_lib(
323 "Test.TestIncByLE(\"Check FourierTDIncByLE\",
324 Microsoft.Quantum.Unstable.Arithmetic.FourierTDIncByLE, 3)",
325 INC_BY_LE_TEST_LIB,
326 &Value::Tuple(vec![].into()),
327 );
328}
329
330//
331// IncByLEUsingAddLE
332//
333
334#[test]
335fn check_inc_by_le_using_add_le_exhaustive_bitwidth_1() {
336 test_expression_with_lib(
337 "{import Microsoft.Quantum.Unstable.Arithmetic.*;
338 Test.TestIncByLE2(\"Check IncByLEUsingAddLE\",
339 IncByLEUsingAddLE(LookAheadDKRSAddLE,RippleCarryCGAddLE,_,_),
340 1, 1)}",
341 INC_BY_LE_TEST_LIB,
342 &Value::Tuple(vec![].into()),
343 );
344}
345
346#[test]
347fn check_inc_by_le_using_add_le_exhaustive_bitwidth_2() {
348 test_expression_with_lib(
349 "{import Microsoft.Quantum.Unstable.Arithmetic.*;
350 Test.TestIncByLE2(\"Check IncByLEUsingAddLE\",
351 IncByLEUsingAddLE(LookAheadDKRSAddLE,RippleCarryCGAddLE,_,_),
352 2, 2)}",
353 INC_BY_LE_TEST_LIB,
354 &Value::Tuple(vec![].into()),
355 );
356}
357
358#[test]
359fn check_inc_by_le_using_add_le_exhaustive_bitwidth_3() {
360 test_expression_with_lib(
361 "{import Microsoft.Quantum.Unstable.Arithmetic.*;
362 Test.TestIncByLE2(\"Check IncByLEUsingAddLE\",
363 IncByLEUsingAddLE(LookAheadDKRSAddLE,RippleCarryCGAddLE,_,_),
364 3, 3)}",
365 INC_BY_LE_TEST_LIB,
366 &Value::Tuple(vec![].into()),
367 );
368}
369
370#[test]
371fn check_inc_by_le_using_add_le_exhaustive_bitwidth_4() {
372 test_expression_with_lib(
373 "{import Microsoft.Quantum.Unstable.Arithmetic.*;
374 Test.TestIncByLE2(\"Check IncByLEUsingAddLE\",
375 IncByLEUsingAddLE(LookAheadDKRSAddLE,RippleCarryCGAddLE,_,_),
376 4, 4)}",
377 INC_BY_LE_TEST_LIB,
378 &Value::Tuple(vec![].into()),
379 );
380}
381
382#[test]
383fn check_inc_by_le_using_add_le_ctl_exhaustive_bitwidth_1() {
384 test_expression_with_lib(
385 "{import Microsoft.Quantum.Unstable.Arithmetic.*;
386 Test.TestIncByLECtl2(\"Check IncByLEUsingAddLE(Ctl)\",
387 IncByLEUsingAddLE(LookAheadDKRSAddLE,RippleCarryCGAddLE,_,_),
388 1, 1)}",
389 INC_BY_LE_TEST_LIB,
390 &Value::Tuple(vec![].into()),
391 );
392}
393
394#[test]
395fn check_inc_by_le_using_add_le_ctl_exhaustive_bitwidth_2() {
396 test_expression_with_lib(
397 "{import Microsoft.Quantum.Unstable.Arithmetic.*;
398 Test.TestIncByLECtl2(\"Check IncByLEUsingAddLE(Ctl)\",
399 IncByLEUsingAddLE(LookAheadDKRSAddLE,RippleCarryCGAddLE,_,_),
400 2, 2)}",
401 INC_BY_LE_TEST_LIB,
402 &Value::Tuple(vec![].into()),
403 );
404}
405
406#[test]
407fn check_inc_by_le_using_add_le_ctl_exhaustive_bitwidth_3() {
408 test_expression_with_lib(
409 "{import Microsoft.Quantum.Unstable.Arithmetic.*;
410 Test.TestIncByLECtl2(\"Check IncByLEUsingAddLE(Ctl)\",
411 IncByLEUsingAddLE(LookAheadDKRSAddLE,RippleCarryCGAddLE,_,_),
412 3, 3)}",
413 INC_BY_LE_TEST_LIB,
414 &Value::Tuple(vec![].into()),
415 );
416}
417
418#[test]
419fn check_inc_by_le_using_add_le_ctl_exhaustive_bitwidth_4() {
420 test_expression_with_lib(
421 "{import Microsoft.Quantum.Unstable.Arithmetic.*;
422 Test.TestIncByLECtl2(\"Check IncByLEUsingAddLE(Ctl)\",
423 IncByLEUsingAddLE(LookAheadDKRSAddLE,RippleCarryCGAddLE,_,_),
424 4, 4)}",
425 INC_BY_LE_TEST_LIB,
426 &Value::Tuple(vec![].into()),
427 );
428}
429
430#[test]
431fn check_inc_by_le_using_add_le_general() {
432 test_expression(
433 {
434 "{ // General cases for IncByLEUsingAddLE
435 import Microsoft.Quantum.Unstable.Arithmetic.*;
436
437 use x1 = Qubit[10];
438 use y1 = Qubit[10];
439 ApplyXorInPlace(743, x1);
440 ApplyXorInPlace(112, y1);
441 IncByLEUsingAddLE(LookAheadDKRSAddLE,RippleCarryCGAddLE,x1,y1); // 112 += 743
442 let i = MeasureInteger(y1);
443 ResetAll(x1+y1);
444
445 return i;
446 }"
447 },
448 &Value::Int(112 + 743),
449 );
450}
451
452//
453// IncByI
454//
455
456#[test]
457fn check_inc_by_i_general() {
458 test_expression(
459 {
460 "{ // General cases for IncByI
461 import Microsoft.Quantum.Unstable.Arithmetic.*;
462
463 use y0 = Qubit[1];
464 IncByI(0,y0); // 0 += 0
465 let i0 = MeasureInteger(y0);
466 ResetAll(y0);
467
468 use y1 = Qubit[1];
469 IncByI(1,y1); // 0 += 1
470 let i1 = MeasureInteger(y1);
471 ResetAll(y1);
472
473 use y2 = Qubit[1];
474 X(y2[0]);
475 IncByI(0,y2); // 1 += 0
476 let i2 = MeasureInteger(y2);
477 ResetAll(y2);
478
479 use y3 = Qubit[1];
480 X(y3[0]);
481 IncByI(1,y3); // 1 += 1
482 let i3 = MeasureInteger(y3);
483 ResetAll(y3);
484
485 use y4 = Qubit[20];
486 ApplyXorInPlace(279, y4);
487 IncByI(7895,y4); // 279 += 7895
488 let i4 = MeasureInteger(y4);
489 ResetAll(y4);
490
491 return (i0, i1, i2, i3, i4);
492 }"
493 },
494 &Value::Tuple(
495 vec![
496 Value::Int(0),
497 Value::Int(1),
498 Value::Int(1),
499 Value::Int(0),
500 Value::Int(279 + 7_895),
501 ]
502 .into(),
503 ),
504 );
505}
506
507//
508// IncByIUsingIncByLE
509//
510
511#[test]
512fn check_ripple_carry_cg_inc_by_i_general() {
513 test_expression(
514 {
515 "{ // General cases for IncByIUsingIncByLE
516 import Microsoft.Quantum.Unstable.Arithmetic.*;
517
518 use y0 = Qubit[10];
519 ApplyXorInPlace(172, y0);
520 IncByIUsingIncByLE(RippleCarryCGIncByLE, 128, y0);
521 let i0 = MeasureInteger(y0);
522 ResetAll(y0);
523
524 use y1 = Qubit[10];
525 ApplyXorInPlace(172, y1);
526 IncByIUsingIncByLE(RippleCarryCGIncByLE, 0, y1);
527 let i1 = MeasureInteger(y1);
528 ResetAll(y1);
529
530 use y2 = Qubit[10];
531 ApplyXorInPlace(172, y2);
532 IncByIUsingIncByLE(RippleCarryCGIncByLE, 14, y2);
533 let i2 = MeasureInteger(y2);
534 ResetAll(y2);
535
536 return (i0, i1, i2);
537 }"
538 },
539 &Value::Tuple(vec![Value::Int(300), Value::Int(172), Value::Int(186)].into()),
540 );
541}
542
543//
544// IncByL
545//
546
547#[test]
548fn check_inc_by_l_general() {
549 test_expression(
550 {
551 "{ // General cases for IncByL
552 import Microsoft.Quantum.Unstable.Arithmetic.*;
553
554 use y0 = Qubit[1];
555 IncByL(0L,y0); // 0 += 0
556 let i0 = MeasureInteger(y0);
557 ResetAll(y0);
558
559 use y1 = Qubit[1];
560 IncByL(1L,y1); // 0 += 1
561 let i1 = MeasureInteger(y1);
562 ResetAll(y1);
563
564 use y2 = Qubit[1];
565 X(y2[0]);
566 IncByL(0L,y2); // 1 += 0
567 let i2 = MeasureInteger(y2);
568 ResetAll(y2);
569
570 use y3 = Qubit[1];
571 X(y3[0]);
572 IncByL(1L,y3); // 1 += 1
573 let i3 = MeasureInteger(y3);
574 ResetAll(y3);
575
576 use y4 = Qubit[20];
577 ApplyXorInPlace(279, y4);
578 IncByL(7895L,y4); // 279 += 7895
579 let i4 = MeasureInteger(y4);
580 ResetAll(y4);
581
582 return (i0, i1, i2, i3, i4);
583 }"
584 },
585 &Value::Tuple(
586 vec![
587 Value::Int(0),
588 Value::Int(1),
589 Value::Int(1),
590 Value::Int(0),
591 Value::Int(279 + 7_895),
592 ]
593 .into(),
594 ),
595 );
596}
597
598//
599// IncByLUsingIncByLE
600//
601
602#[test]
603fn check_ripple_carry_cg_inc_by_l_general() {
604 test_expression(
605 {
606 "{ // Branching cases for IncByLUsingIncByLE
607 import Microsoft.Quantum.Unstable.Arithmetic.*;
608
609 use y0 = Qubit[10];
610 ApplyXorInPlace(172, y0);
611 IncByLUsingIncByLE(RippleCarryCGIncByLE, 128L, y0);
612 let i0 = MeasureInteger(y0);
613 ResetAll(y0);
614
615 use y1 = Qubit[10];
616 ApplyXorInPlace(172, y1);
617 IncByLUsingIncByLE(RippleCarryCGIncByLE, 0L, y1);
618 let i1 = MeasureInteger(y1);
619 ResetAll(y1);
620
621 use y2 = Qubit[10];
622 ApplyXorInPlace(172, y2);
623 IncByLUsingIncByLE(RippleCarryCGIncByLE, 14L, y2);
624 let i2 = MeasureInteger(y2);
625 ResetAll(y2);
626
627 return (i0, i1, i2);
628 }"
629 },
630 &Value::Tuple(vec![Value::Int(300), Value::Int(172), Value::Int(186)].into()),
631 );
632}
633
634//
635// AddLE
636//
637
638const ADD_LE_TEST_LIB: &str = include_str!("resources/src/add_le.qs");
639
640#[test]
641fn check_add_le_exhaustive_bitwidth_1() {
642 test_expression_with_lib(
643 "Test.TestAddLE(\"Check AddLE\",
644 Microsoft.Quantum.Unstable.Arithmetic.AddLE, 1)",
645 ADD_LE_TEST_LIB,
646 &Value::Tuple(vec![].into()),
647 );
648}
649
650#[test]
651fn check_add_le_exhaustive_bitwidth_2() {
652 test_expression_with_lib(
653 "Test.TestAddLE(\"Check AddLE\",
654 Microsoft.Quantum.Unstable.Arithmetic.AddLE, 2)",
655 ADD_LE_TEST_LIB,
656 &Value::Tuple(vec![].into()),
657 );
658}
659
660#[test]
661fn check_add_le_exhaustive_bitwidth_3() {
662 test_expression_with_lib(
663 "Test.TestAddLE(\"Check AddLE\",
664 Microsoft.Quantum.Unstable.Arithmetic.AddLE, 3)",
665 ADD_LE_TEST_LIB,
666 &Value::Tuple(vec![].into()),
667 );
668}
669
670#[test]
671fn check_add_le_exhaustive_bitwidth_4() {
672 test_expression_with_lib(
673 "Test.TestAddLE(\"Check AddLE\",
674 Microsoft.Quantum.Unstable.Arithmetic.AddLE, 4)",
675 ADD_LE_TEST_LIB,
676 &Value::Tuple(vec![].into()),
677 );
678}
679
680#[test]
681fn check_add_le_general() {
682 test_expression(
683 {
684 "{ // General cases for AddLE
685 import Microsoft.Quantum.Unstable.Arithmetic.*;
686
687 use x1 = Qubit[10];
688 use y1 = Qubit[10];
689 use z1 = Qubit[10];
690 ApplyXorInPlace(279, x1);
691 ApplyXorInPlace(383, y1);
692 AddLE(x1,y1,z1); // z = 279 + 383
693 let i1 = MeasureInteger(z1);
694 ResetAll(x1);
695 ResetAll(y1);
696 ResetAll(z1);
697
698 return i1;
699 }"
700 },
701 &Value::Int(279 + 383),
702 );
703}
704
705//
706// RippleCarryCGAddLE
707//
708
709#[test]
710fn check_ripple_carry_cg_add_le_exhaustive_bitwidth_1() {
711 test_expression_with_lib(
712 "Test.TestAddLE(\"Check RippleCarryCGAddLE\",
713 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGAddLE, 1)",
714 ADD_LE_TEST_LIB,
715 &Value::Tuple(vec![].into()),
716 );
717}
718
719#[test]
720fn check_ripple_carry_cg_add_le_exhaustive_bitwidth_2() {
721 test_expression_with_lib(
722 "Test.TestAddLE(\"Check RippleCarryCGAddLE\",
723 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGAddLE, 2)",
724 ADD_LE_TEST_LIB,
725 &Value::Tuple(vec![].into()),
726 );
727}
728
729#[test]
730fn check_ripple_carry_cg_add_le_exhaustive_bitwidth_3() {
731 test_expression_with_lib(
732 "Test.TestAddLE(\"Check RippleCarryCGAddLE\",
733 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGAddLE, 3)",
734 ADD_LE_TEST_LIB,
735 &Value::Tuple(vec![].into()),
736 );
737}
738
739#[test]
740fn check_ripple_carry_cg_add_le_exhaustive_bitwidth_4() {
741 test_expression_with_lib(
742 "Test.TestAddLE(\"Check RippleCarryCGAddLE\",
743 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGAddLE, 4)",
744 ADD_LE_TEST_LIB,
745 &Value::Tuple(vec![].into()),
746 );
747}
748
749#[test]
750fn check_ripple_carry_cg_add_le_general() {
751 test_expression(
752 {
753 "{ // General cases for RippleCarryAddLE
754 import Microsoft.Quantum.Unstable.Arithmetic.*;
755
756 use x1 = Qubit[10];
757 use y1 = Qubit[10];
758 use z1 = Qubit[15];
759 ApplyXorInPlace(978, x1);
760 ApplyXorInPlace(456, y1);
761 RippleCarryCGAddLE(x1,y1,z1);
762 let i1 = MeasureInteger(z1);
763 ResetAll(x1+y1+z1);
764
765 return i1;
766 }"
767 },
768 &Value::Int(978 + 456),
769 );
770}
771
772//
773// LookAheadDKRSAddLE
774//
775
776#[test]
777fn check_lookahead_dkrs_add_le_exhaustive_bitwidth_1() {
778 test_expression_with_lib(
779 "Test.TestAddLE(\"Check LookAheadDKRSAddLE\",
780 Microsoft.Quantum.Unstable.Arithmetic.LookAheadDKRSAddLE, 1)",
781 ADD_LE_TEST_LIB,
782 &Value::Tuple(vec![].into()),
783 );
784}
785
786#[test]
787fn check_lookahead_dkrs_add_le_exhaustive_bitwidth_2() {
788 test_expression_with_lib(
789 "Test.TestAddLE(\"Check LookAheadDKRSAddLE\",
790 Microsoft.Quantum.Unstable.Arithmetic.LookAheadDKRSAddLE, 2)",
791 ADD_LE_TEST_LIB,
792 &Value::Tuple(vec![].into()),
793 );
794}
795
796#[test]
797fn check_lookahead_dkrs_add_le_exhaustive_bitwidth_3() {
798 test_expression_with_lib(
799 "Test.TestAddLE(\"Check LookAheadDKRSAddLE\",
800 Microsoft.Quantum.Unstable.Arithmetic.LookAheadDKRSAddLE, 3)",
801 ADD_LE_TEST_LIB,
802 &Value::Tuple(vec![].into()),
803 );
804}
805
806#[test]
807fn check_lookahead_dkrs_add_le_exhaustive_bitwidth_4() {
808 test_expression_with_lib(
809 "Test.TestAddLE(\"Check LookAheadDKRSAddLE\",
810 Microsoft.Quantum.Unstable.Arithmetic.LookAheadDKRSAddLE, 4)",
811 ADD_LE_TEST_LIB,
812 &Value::Tuple(vec![].into()),
813 );
814}
815
816#[test]
817fn check_lookahead_dkrs_add_le_general() {
818 test_expression(
819 {
820 "{ // General cases for LookAheadDKRSAddLE
821 import Microsoft.Quantum.Unstable.Arithmetic.*;
822
823 use x1 = Qubit[10];
824 use y1 = Qubit[10];
825 use z1 = Qubit[15];
826 ApplyXorInPlace(939, x1);
827 ApplyXorInPlace(578, y1);
828 LookAheadDKRSAddLE(x1,y1,z1);
829 let i1 = MeasureInteger(z1);
830 ResetAll(x1+y1+z1);
831
832 return i1;
833 }"
834 },
835 &Value::Int(939 + 578),
836 );
837}
838
839const COMPARE_TEST_LIB: &str = include_str!("resources/src/compare.qs");
840
841#[test]
842fn check_apply_if_less_l_exhaustive() {
843 test_expression_with_lib(
844 "Test.CompareWithBigInt(\"Check ApplyIfLessL\", 3,
845 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfLessL(X,_,_,_),
846 (a, b) -> {a < b} )",
847 COMPARE_TEST_LIB,
848 &Value::Tuple(vec![].into()),
849 );
850}
851
852#[test]
853fn check_apply_if_less_or_equal_l_exhaustive() {
854 test_expression_with_lib(
855 "Test.CompareWithBigInt(\"Check ApplyIfLessOrEqualL\", 3,
856 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfLessOrEqualL(X,_,_,_),
857 (a, b) -> {a <= b} )",
858 COMPARE_TEST_LIB,
859 &Value::Tuple(vec![].into()),
860 );
861}
862
863#[test]
864fn check_apply_if_equal_l_exhaustive() {
865 test_expression_with_lib(
866 "Test.CompareWithBigInt(\"Check ApplyIfEqualL\", 3,
867 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfEqualL(X,_,_,_),
868 (a, b) -> {a == b} )",
869 COMPARE_TEST_LIB,
870 &Value::Tuple(vec![].into()),
871 );
872}
873
874#[test]
875fn check_apply_if_greater_or_equal_l_exhaustive() {
876 test_expression_with_lib(
877 "Test.CompareWithBigInt(\"Check ApplyIfGreaterOrEqualL\", 3,
878 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfGreaterOrEqualL(X,_,_,_),
879 (a, b) -> {a >= b} )",
880 COMPARE_TEST_LIB,
881 &Value::Tuple(vec![].into()),
882 );
883}
884
885#[test]
886fn check_apply_if_greater_l_exhaustive() {
887 test_expression_with_lib(
888 "Test.CompareWithBigInt(\"Check ApplyIfGreaterL\", 3,
889 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfGreaterL(X,_,_,_),
890 (a, b) -> {a > b} )",
891 COMPARE_TEST_LIB,
892 &Value::Tuple(vec![].into()),
893 );
894}
895
896#[test]
897fn check_apply_if_less_le_exhaustive() {
898 test_expression_with_lib(
899 "Test.CompareWithLE(\"Check ApplyIfLessLE\", 3,
900 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfLessLE(X,_,_,_),
901 (a, b) -> {a < b} )",
902 COMPARE_TEST_LIB,
903 &Value::Tuple(vec![].into()),
904 );
905}
906
907#[test]
908fn check_apply_if_less_or_equal_le_exhaustive() {
909 test_expression_with_lib(
910 "Test.CompareWithLE(\"Check ApplyIfLessOrEqualLE\", 3,
911 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfLessOrEqualLE(X,_,_,_),
912 (a, b) -> {a <= b} )",
913 COMPARE_TEST_LIB,
914 &Value::Tuple(vec![].into()),
915 );
916}
917
918#[test]
919fn check_apply_if_equal_le_exhaustive() {
920 test_expression_with_lib(
921 "Test.CompareWithLE(\"Check ApplyIfEqualLE\", 3,
922 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfEqualLE(X,_,_,_),
923 (a, b) -> {a == b} )",
924 COMPARE_TEST_LIB,
925 &Value::Tuple(vec![].into()),
926 );
927}
928
929#[test]
930fn check_apply_if_greater_or_equal_le_exhaustive() {
931 test_expression_with_lib(
932 "Test.CompareWithLE(\"Check ApplyIfGreaterOrEqualLE\", 3,
933 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfGreaterOrEqualLE(X,_,_,_),
934 (a, b) -> {a >= b} )",
935 COMPARE_TEST_LIB,
936 &Value::Tuple(vec![].into()),
937 );
938}
939
940#[test]
941fn check_apply_if_greater_le_exhaustive() {
942 test_expression_with_lib(
943 "Test.CompareWithLE(\"Check ApplyIfGreaterLE\", 3,
944 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfGreaterLE(X,_,_,_),
945 (a, b) -> {a > b} )",
946 COMPARE_TEST_LIB,
947 &Value::Tuple(vec![].into()),
948 );
949}
950
951#[test]
952fn check_apply_if_less_l_non_x_action() {
953 test_expression(
954 "{
955 import Microsoft.Quantum.Unstable.Arithmetic.*;
956 use input = Qubit[10];
957 use output1 = Qubit[10];
958 use output2 = Qubit[10];
959 ApplyXorInPlace(569, input);
960 ApplyXorInPlace(753, output1);
961 ApplyXorInPlace(753, output2);
962 ApplyIfGreaterL(IncByI(5, _), 572L, input, output1);
963 ApplyIfLessL(IncByI(5, _), 572L, input, output2);
964 let result1 = MeasureInteger(output1);
965 let result2 = MeasureInteger(output2);
966 ResetAll(input);
967 ResetAll(output1);
968 ResetAll(output2);
969 (result1, result2)
970 }",
971 &Value::Tuple(vec![Value::Int(758), Value::Int(753)].into()),
972 );
973}
974