microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dmitryv/multiplex-z

Branches

Tags

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

Clone

HTTPS

Download ZIP

library/src/tests/arithmetic.rs

981lines · 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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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(), None),
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 None,
504 ),
505 );
506}
507
508//
509// IncByIUsingIncByLE
510//
511
512#[test]
513fn check_ripple_carry_cg_inc_by_i_general() {
514 test_expression(
515 {
516 "{ // General cases for IncByIUsingIncByLE
517 import Microsoft.Quantum.Unstable.Arithmetic.*;
518
519 use y0 = Qubit[10];
520 ApplyXorInPlace(172, y0);
521 IncByIUsingIncByLE(RippleCarryCGIncByLE, 128, y0);
522 let i0 = MeasureInteger(y0);
523 ResetAll(y0);
524
525 use y1 = Qubit[10];
526 ApplyXorInPlace(172, y1);
527 IncByIUsingIncByLE(RippleCarryCGIncByLE, 0, y1);
528 let i1 = MeasureInteger(y1);
529 ResetAll(y1);
530
531 use y2 = Qubit[10];
532 ApplyXorInPlace(172, y2);
533 IncByIUsingIncByLE(RippleCarryCGIncByLE, 14, y2);
534 let i2 = MeasureInteger(y2);
535 ResetAll(y2);
536
537 return (i0, i1, i2);
538 }"
539 },
540 &Value::Tuple(
541 vec![Value::Int(300), Value::Int(172), Value::Int(186)].into(),
542 None,
543 ),
544 );
545}
546
547//
548// IncByL
549//
550
551#[test]
552fn check_inc_by_l_general() {
553 test_expression(
554 {
555 "{ // General cases for IncByL
556 import Microsoft.Quantum.Unstable.Arithmetic.*;
557
558 use y0 = Qubit[1];
559 IncByL(0L,y0); // 0 += 0
560 let i0 = MeasureInteger(y0);
561 ResetAll(y0);
562
563 use y1 = Qubit[1];
564 IncByL(1L,y1); // 0 += 1
565 let i1 = MeasureInteger(y1);
566 ResetAll(y1);
567
568 use y2 = Qubit[1];
569 X(y2[0]);
570 IncByL(0L,y2); // 1 += 0
571 let i2 = MeasureInteger(y2);
572 ResetAll(y2);
573
574 use y3 = Qubit[1];
575 X(y3[0]);
576 IncByL(1L,y3); // 1 += 1
577 let i3 = MeasureInteger(y3);
578 ResetAll(y3);
579
580 use y4 = Qubit[20];
581 ApplyXorInPlace(279, y4);
582 IncByL(7895L,y4); // 279 += 7895
583 let i4 = MeasureInteger(y4);
584 ResetAll(y4);
585
586 return (i0, i1, i2, i3, i4);
587 }"
588 },
589 &Value::Tuple(
590 vec![
591 Value::Int(0),
592 Value::Int(1),
593 Value::Int(1),
594 Value::Int(0),
595 Value::Int(279 + 7_895),
596 ]
597 .into(),
598 None,
599 ),
600 );
601}
602
603//
604// IncByLUsingIncByLE
605//
606
607#[test]
608fn check_ripple_carry_cg_inc_by_l_general() {
609 test_expression(
610 {
611 "{ // Branching cases for IncByLUsingIncByLE
612 import Microsoft.Quantum.Unstable.Arithmetic.*;
613
614 use y0 = Qubit[10];
615 ApplyXorInPlace(172, y0);
616 IncByLUsingIncByLE(RippleCarryCGIncByLE, 128L, y0);
617 let i0 = MeasureInteger(y0);
618 ResetAll(y0);
619
620 use y1 = Qubit[10];
621 ApplyXorInPlace(172, y1);
622 IncByLUsingIncByLE(RippleCarryCGIncByLE, 0L, y1);
623 let i1 = MeasureInteger(y1);
624 ResetAll(y1);
625
626 use y2 = Qubit[10];
627 ApplyXorInPlace(172, y2);
628 IncByLUsingIncByLE(RippleCarryCGIncByLE, 14L, y2);
629 let i2 = MeasureInteger(y2);
630 ResetAll(y2);
631
632 return (i0, i1, i2);
633 }"
634 },
635 &Value::Tuple(
636 vec![Value::Int(300), Value::Int(172), Value::Int(186)].into(),
637 None,
638 ),
639 );
640}
641
642//
643// AddLE
644//
645
646const ADD_LE_TEST_LIB: &str = include_str!("resources/src/add_le.qs");
647
648#[test]
649fn check_add_le_exhaustive_bitwidth_1() {
650 test_expression_with_lib(
651 "Test.TestAddLE(\"Check AddLE\",
652 Microsoft.Quantum.Unstable.Arithmetic.AddLE, 1)",
653 ADD_LE_TEST_LIB,
654 &Value::Tuple(vec![].into(), None),
655 );
656}
657
658#[test]
659fn check_add_le_exhaustive_bitwidth_2() {
660 test_expression_with_lib(
661 "Test.TestAddLE(\"Check AddLE\",
662 Microsoft.Quantum.Unstable.Arithmetic.AddLE, 2)",
663 ADD_LE_TEST_LIB,
664 &Value::Tuple(vec![].into(), None),
665 );
666}
667
668#[test]
669fn check_add_le_exhaustive_bitwidth_3() {
670 test_expression_with_lib(
671 "Test.TestAddLE(\"Check AddLE\",
672 Microsoft.Quantum.Unstable.Arithmetic.AddLE, 3)",
673 ADD_LE_TEST_LIB,
674 &Value::Tuple(vec![].into(), None),
675 );
676}
677
678#[test]
679fn check_add_le_exhaustive_bitwidth_4() {
680 test_expression_with_lib(
681 "Test.TestAddLE(\"Check AddLE\",
682 Microsoft.Quantum.Unstable.Arithmetic.AddLE, 4)",
683 ADD_LE_TEST_LIB,
684 &Value::Tuple(vec![].into(), None),
685 );
686}
687
688#[test]
689fn check_add_le_general() {
690 test_expression(
691 {
692 "{ // General cases for AddLE
693 import Microsoft.Quantum.Unstable.Arithmetic.*;
694
695 use x1 = Qubit[10];
696 use y1 = Qubit[10];
697 use z1 = Qubit[10];
698 ApplyXorInPlace(279, x1);
699 ApplyXorInPlace(383, y1);
700 AddLE(x1,y1,z1); // z = 279 + 383
701 let i1 = MeasureInteger(z1);
702 ResetAll(x1);
703 ResetAll(y1);
704 ResetAll(z1);
705
706 return i1;
707 }"
708 },
709 &Value::Int(279 + 383),
710 );
711}
712
713//
714// RippleCarryCGAddLE
715//
716
717#[test]
718fn check_ripple_carry_cg_add_le_exhaustive_bitwidth_1() {
719 test_expression_with_lib(
720 "Test.TestAddLE(\"Check RippleCarryCGAddLE\",
721 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGAddLE, 1)",
722 ADD_LE_TEST_LIB,
723 &Value::Tuple(vec![].into(), None),
724 );
725}
726
727#[test]
728fn check_ripple_carry_cg_add_le_exhaustive_bitwidth_2() {
729 test_expression_with_lib(
730 "Test.TestAddLE(\"Check RippleCarryCGAddLE\",
731 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGAddLE, 2)",
732 ADD_LE_TEST_LIB,
733 &Value::Tuple(vec![].into(), None),
734 );
735}
736
737#[test]
738fn check_ripple_carry_cg_add_le_exhaustive_bitwidth_3() {
739 test_expression_with_lib(
740 "Test.TestAddLE(\"Check RippleCarryCGAddLE\",
741 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGAddLE, 3)",
742 ADD_LE_TEST_LIB,
743 &Value::Tuple(vec![].into(), None),
744 );
745}
746
747#[test]
748fn check_ripple_carry_cg_add_le_exhaustive_bitwidth_4() {
749 test_expression_with_lib(
750 "Test.TestAddLE(\"Check RippleCarryCGAddLE\",
751 Microsoft.Quantum.Unstable.Arithmetic.RippleCarryCGAddLE, 4)",
752 ADD_LE_TEST_LIB,
753 &Value::Tuple(vec![].into(), None),
754 );
755}
756
757#[test]
758fn check_ripple_carry_cg_add_le_general() {
759 test_expression(
760 {
761 "{ // General cases for RippleCarryAddLE
762 import Microsoft.Quantum.Unstable.Arithmetic.*;
763
764 use x1 = Qubit[10];
765 use y1 = Qubit[10];
766 use z1 = Qubit[15];
767 ApplyXorInPlace(978, x1);
768 ApplyXorInPlace(456, y1);
769 RippleCarryCGAddLE(x1,y1,z1);
770 let i1 = MeasureInteger(z1);
771 ResetAll(x1+y1+z1);
772
773 return i1;
774 }"
775 },
776 &Value::Int(978 + 456),
777 );
778}
779
780//
781// LookAheadDKRSAddLE
782//
783
784#[test]
785fn check_lookahead_dkrs_add_le_exhaustive_bitwidth_1() {
786 test_expression_with_lib(
787 "Test.TestAddLE(\"Check LookAheadDKRSAddLE\",
788 Microsoft.Quantum.Unstable.Arithmetic.LookAheadDKRSAddLE, 1)",
789 ADD_LE_TEST_LIB,
790 &Value::Tuple(vec![].into(), None),
791 );
792}
793
794#[test]
795fn check_lookahead_dkrs_add_le_exhaustive_bitwidth_2() {
796 test_expression_with_lib(
797 "Test.TestAddLE(\"Check LookAheadDKRSAddLE\",
798 Microsoft.Quantum.Unstable.Arithmetic.LookAheadDKRSAddLE, 2)",
799 ADD_LE_TEST_LIB,
800 &Value::Tuple(vec![].into(), None),
801 );
802}
803
804#[test]
805fn check_lookahead_dkrs_add_le_exhaustive_bitwidth_3() {
806 test_expression_with_lib(
807 "Test.TestAddLE(\"Check LookAheadDKRSAddLE\",
808 Microsoft.Quantum.Unstable.Arithmetic.LookAheadDKRSAddLE, 3)",
809 ADD_LE_TEST_LIB,
810 &Value::Tuple(vec![].into(), None),
811 );
812}
813
814#[test]
815fn check_lookahead_dkrs_add_le_exhaustive_bitwidth_4() {
816 test_expression_with_lib(
817 "Test.TestAddLE(\"Check LookAheadDKRSAddLE\",
818 Microsoft.Quantum.Unstable.Arithmetic.LookAheadDKRSAddLE, 4)",
819 ADD_LE_TEST_LIB,
820 &Value::Tuple(vec![].into(), None),
821 );
822}
823
824#[test]
825fn check_lookahead_dkrs_add_le_general() {
826 test_expression(
827 {
828 "{ // General cases for LookAheadDKRSAddLE
829 import Microsoft.Quantum.Unstable.Arithmetic.*;
830
831 use x1 = Qubit[10];
832 use y1 = Qubit[10];
833 use z1 = Qubit[15];
834 ApplyXorInPlace(939, x1);
835 ApplyXorInPlace(578, y1);
836 LookAheadDKRSAddLE(x1,y1,z1);
837 let i1 = MeasureInteger(z1);
838 ResetAll(x1+y1+z1);
839
840 return i1;
841 }"
842 },
843 &Value::Int(939 + 578),
844 );
845}
846
847const COMPARE_TEST_LIB: &str = include_str!("resources/src/compare.qs");
848
849#[test]
850fn check_apply_if_less_l_exhaustive() {
851 test_expression_with_lib(
852 "Test.CompareWithBigInt(\"Check ApplyIfLessL\", 3,
853 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfLessL(X,_,_,_),
854 (a, b) -> {a < b} )",
855 COMPARE_TEST_LIB,
856 &Value::Tuple(vec![].into(), None),
857 );
858}
859
860#[test]
861fn check_apply_if_less_or_equal_l_exhaustive() {
862 test_expression_with_lib(
863 "Test.CompareWithBigInt(\"Check ApplyIfLessOrEqualL\", 3,
864 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfLessOrEqualL(X,_,_,_),
865 (a, b) -> {a <= b} )",
866 COMPARE_TEST_LIB,
867 &Value::Tuple(vec![].into(), None),
868 );
869}
870
871#[test]
872fn check_apply_if_equal_l_exhaustive() {
873 test_expression_with_lib(
874 "Test.CompareWithBigInt(\"Check ApplyIfEqualL\", 3,
875 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfEqualL(X,_,_,_),
876 (a, b) -> {a == b} )",
877 COMPARE_TEST_LIB,
878 &Value::Tuple(vec![].into(), None),
879 );
880}
881
882#[test]
883fn check_apply_if_greater_or_equal_l_exhaustive() {
884 test_expression_with_lib(
885 "Test.CompareWithBigInt(\"Check ApplyIfGreaterOrEqualL\", 3,
886 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfGreaterOrEqualL(X,_,_,_),
887 (a, b) -> {a >= b} )",
888 COMPARE_TEST_LIB,
889 &Value::Tuple(vec![].into(), None),
890 );
891}
892
893#[test]
894fn check_apply_if_greater_l_exhaustive() {
895 test_expression_with_lib(
896 "Test.CompareWithBigInt(\"Check ApplyIfGreaterL\", 3,
897 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfGreaterL(X,_,_,_),
898 (a, b) -> {a > b} )",
899 COMPARE_TEST_LIB,
900 &Value::Tuple(vec![].into(), None),
901 );
902}
903
904#[test]
905fn check_apply_if_less_le_exhaustive() {
906 test_expression_with_lib(
907 "Test.CompareWithLE(\"Check ApplyIfLessLE\", 3,
908 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfLessLE(X,_,_,_),
909 (a, b) -> {a < b} )",
910 COMPARE_TEST_LIB,
911 &Value::Tuple(vec![].into(), None),
912 );
913}
914
915#[test]
916fn check_apply_if_less_or_equal_le_exhaustive() {
917 test_expression_with_lib(
918 "Test.CompareWithLE(\"Check ApplyIfLessOrEqualLE\", 3,
919 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfLessOrEqualLE(X,_,_,_),
920 (a, b) -> {a <= b} )",
921 COMPARE_TEST_LIB,
922 &Value::Tuple(vec![].into(), None),
923 );
924}
925
926#[test]
927fn check_apply_if_equal_le_exhaustive() {
928 test_expression_with_lib(
929 "Test.CompareWithLE(\"Check ApplyIfEqualLE\", 3,
930 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfEqualLE(X,_,_,_),
931 (a, b) -> {a == b} )",
932 COMPARE_TEST_LIB,
933 &Value::Tuple(vec![].into(), None),
934 );
935}
936
937#[test]
938fn check_apply_if_greater_or_equal_le_exhaustive() {
939 test_expression_with_lib(
940 "Test.CompareWithLE(\"Check ApplyIfGreaterOrEqualLE\", 3,
941 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfGreaterOrEqualLE(X,_,_,_),
942 (a, b) -> {a >= b} )",
943 COMPARE_TEST_LIB,
944 &Value::Tuple(vec![].into(), None),
945 );
946}
947
948#[test]
949fn check_apply_if_greater_le_exhaustive() {
950 test_expression_with_lib(
951 "Test.CompareWithLE(\"Check ApplyIfGreaterLE\", 3,
952 Microsoft.Quantum.Unstable.Arithmetic.ApplyIfGreaterLE(X,_,_,_),
953 (a, b) -> {a > b} )",
954 COMPARE_TEST_LIB,
955 &Value::Tuple(vec![].into(), None),
956 );
957}
958
959#[test]
960fn check_apply_if_less_l_non_x_action() {
961 test_expression(
962 "{
963 import Microsoft.Quantum.Unstable.Arithmetic.*;
964 use input = Qubit[10];
965 use output1 = Qubit[10];
966 use output2 = Qubit[10];
967 ApplyXorInPlace(569, input);
968 ApplyXorInPlace(753, output1);
969 ApplyXorInPlace(753, output2);
970 ApplyIfGreaterL(IncByI(5, _), 572L, input, output1);
971 ApplyIfLessL(IncByI(5, _), 572L, input, output2);
972 let result1 = MeasureInteger(output1);
973 let result2 = MeasureInteger(output2);
974 ResetAll(input);
975 ResetAll(output1);
976 ResetAll(output2);
977 (result1, result2)
978 }",
979 &Value::Tuple(vec![Value::Int(758), Value::Int(753)].into(), None),
980 );
981}
982