microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
brlackey/ising-model-sample

Branches

Tags

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

Clone

HTTPS

Download ZIP

library/signed/src/Tests.qs

114lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4import Std.Diagnostics.Fact;
5import Operations.Invert2sSI;
6import Measurement.MeasureSignedInteger;
7
8@Test()
9operation MeasureSignedIntTests() : Unit {
10 let testCases = [
11 ("0b0001 == 1", 4, (qs) => X(qs[0]), (qs) => MeasureSignedInteger(qs, 6), 1),
12 ("0b1111 == -1", 4, (qs) => { X(qs[0]); X(qs[1]); X(qs[2]); X(qs[3]); }, (qs) => MeasureSignedInteger(qs, 4), -1),
13 ("0b01000 == 8", 5, (qs) => X(qs[3]), (qs) => MeasureSignedInteger(qs, 5), 8),
14 ("0b11110 == -2", 5, (qs) => {
15 X(qs[1]);
16 X(qs[2]);
17 X(qs[3]);
18 X(qs[4]);
19 }, (qs) => MeasureSignedInteger(qs, 5), -2),
20 ("0b11000 == -8", 5, (qs) => { X(qs[3]); X(qs[4]); }, (qs) => MeasureSignedInteger(qs, 5), -8)
21 ];
22 Fact(Qtest.Operations.CheckAllTestCases(testCases), "SignedInt tests failed");
23}
24
25@Test()
26operation SignedOpTests() : Unit {
27 use a = Qubit[32];
28 use b = Qubit[32];
29 use c = Qubit[64];
30
31 // 0b11111110 (-2 in twos complement) * 0b00000001 == 0b11111110 (-2)
32 X(a[1]);
33 Operations.Invert2sSI(a);
34 X(b[0]);
35 TestSignedIntOp(Operations.MultiplySI, a, b, c, -2);
36
37 // 0b11111110 (-2 in twos complement) * 0b11111111 (-1 in twos complement) == 0b00000010 (2)
38 X(a[1]);
39 Operations.Invert2sSI(a);
40 X(b[0]);
41 Operations.Invert2sSI(b);
42 TestSignedIntOp(Operations.MultiplySI, a, b, c, 2);
43
44
45 // 0b11111110 (-2 in twos complement) squared is 0b00000100 (4)
46 X(a[1]);
47 Operations.Invert2sSI(a);
48 TestSignedIntOp((a, b, _) => Operations.SquareSI(a, c), a, b, c, 4);
49
50}
51
52@Test()
53operation UnsignedOpTests() : Unit {
54 use a = Qubit[2];
55 use b = Qubit[2];
56 use c = Qubit[4];
57
58 // 0b10 * 0b01 == 0b10 (1 * 2 = 2)
59 X(a[0]);
60 X(b[1]);
61 TestIntOp(Operations.MultiplyI, a, b, c, 2);
62
63 // 0b01 * 0b10 == 0b10 (1 * 2 = 2)
64 X(a[1]);
65 X(b[0]);
66 TestIntOp(Operations.MultiplyI, a, b, c, 2);
67
68 // 0b11 * 0b11 == 0b1001 (3 * 3 = 9)
69 X(a[0]);
70 X(b[0]);
71 X(a[1]);
72 X(b[1]);
73 TestIntOp(Operations.MultiplyI, a, b, c, 9);
74
75
76 use a = Qubit[8];
77 use b = Qubit[8];
78 use c = Qubit[16];
79
80 // 0b00001010 * 0b00001011 == 0b01100100 (10 * 11 = 110)
81 X(a[1]);
82 X(a[3]);
83 X(b[1]);
84 X(b[3]);
85 X(b[0]);
86 TestIntOp(Operations.MultiplyI, a, b, c, 110);
87
88 // 0b00001010 ^ 2 = 0b01100100 (10 ^ 2 = 100)
89 X(a[1]);
90 X(a[3]);
91 TestIntOp((a, b, _) => Operations.SquareI(a, c), a, b, c, 100);
92
93 // 0b00001010 / 0b00000010 == 0b00000101 (10 / 2 = 5)
94 X(a[1]);
95 X(a[3]);
96 X(b[1]);
97 // need smaller result register for div, mod, etc
98 use d = Qubit[8];
99 TestIntOp(Operations.DivideI, a, b, d, 5);
100}
101
102operation TestIntOp(op : (Qubit[], Qubit[], Qubit[]) => Unit, a : Qubit[], b : Qubit[], c : Qubit[], expect : Int) : Unit {
103 op(a, b, c);
104 let res = MeasureInteger(c);
105 Fact(res == expect, $"Expected {expect}, got {res}");
106 ResetAll(a + b + c);
107}
108
109operation TestSignedIntOp(op : (Qubit[], Qubit[], Qubit[]) => Unit, a : Qubit[], b : Qubit[], c : Qubit[], expect : Int) : Unit {
110 op(a, b, c);
111 let res = MeasureSignedInteger(c, 64);
112 Fact(res == expect, $"Expected {expect}, got {res}");
113 ResetAll(a + b + c);
114}
115