forked from microsoft/Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArithmeticTests.qs
180 lines (161 loc) · 6.8 KB
/
ArithmeticTests.qs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Tests {
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Primitive;
operation InPlaceXorTestHelper( testValue : Int, numberOfQubits : Int ) : () {
body {
using (register = Qubit[numberOfQubits]) {
let registerLE = LittleEndian(register);
InPlaceXorLE(testValue, registerLE);
let measuredValue = MeasureInteger(registerLE);
AssertIntEqual(testValue, measuredValue, "Did not measure the integer we expected.");
}
}
}
operation InPlaceXorLETest() : () {
body {
ApplyToEach( InPlaceXorTestHelper, [(63,6);(42,6)] );
}
}
operation IntegerIncrementLETestHelper( summand1 : Int, summand2 : Int, numberOfQubits : Int ) : () {
body {
using (register = Qubit[numberOfQubits]) {
let registerLE = LittleEndian(register);
InPlaceXorLE(summand1, registerLE);
IntegerIncrementLE( summand2, registerLE );
let expected = Modulus(summand1 + summand2, 2 ^ numberOfQubits );
let actual = MeasureInteger(registerLE);
AssertIntEqual(expected, actual, $"Expected {expected}, got {actual}");
}
}
}
/// # Summary
/// Exhaustively tests Microsoft.Quantum.Canon.IntegerIncrementLE
/// on 4 qubits
operation IntegerIncrementLETest() : () {
body {
let numberOfQubits = 4;
for( summand1 in 0 .. 2^numberOfQubits - 1 ) {
for( summand2 in -2^numberOfQubits .. 2^numberOfQubits ) {
IntegerIncrementLETestHelper( summand1, summand2, numberOfQubits );
}
}
}
}
operation ModularIncrementLETestHelper(
summand1 : Int,
summand2 : Int,
modulus : Int,
numberOfQubits : Int ) : ()
{
body {
using (register = Qubit[numberOfQubits]) {
let registerLE = LittleEndian(register);
InPlaceXorLE(summand1, registerLE);
ModularIncrementLE( summand2, modulus, registerLE );
let expected = Modulus(summand1 + summand2, modulus );
let actual = MeasureInteger(registerLE);
AssertIntEqual(expected, actual, $"Expected {expected}, got {actual}");
using( controls = Qubit[2] )
{
InPlaceXorLE(summand1, registerLE);
(Controlled ModularIncrementLE)(controls,(summand2, modulus, registerLE));
let actual2 = MeasureInteger(registerLE);
AssertIntEqual(summand1, actual2, $"Expected {summand1}, got {actual2}");
// now set all controls to 1
InPlaceXorLE(summand1, registerLE);
(ControlledOnInt(0,ModularIncrementLE(summand2, modulus,_)))(controls, registerLE);
let actual3 = MeasureInteger(registerLE);
AssertIntEqual(expected, actual3, $"Expected {expected}, got {actual3}");
// restore controls back to |0⟩
}
}
}
}
/// # Summary
/// Exhaustively tests Microsoft.Quantum.Canon.ModularIncrementLE
/// on 4 qubits with modulus 13
operation ModularIncrementLETest() : () {
body {
let numberOfQubits = 4;
let modulus = 13;
for( summand1 in 0 .. modulus - 1 ) {
for( summand2 in 0 .. modulus - 1 ) {
ModularIncrementLETestHelper( summand1, summand2, modulus, numberOfQubits );
}
}
}
}
operation ModularAddProductLETestHelper(
summand : Int,
multiplier1 : Int,
multiplier2 : Int,
modulus : Int,
numberOfQubits : Int ) : ()
{
body {
using (register = Qubit[numberOfQubits * 2]) {
let summandLE = LittleEndian(register[ 0 .. numberOfQubits - 1 ]);
let multiplierLE = LittleEndian(register[ numberOfQubits .. 2*numberOfQubits - 1 ]);
InPlaceXorLE(summand, summandLE);
InPlaceXorLE(multiplier1, multiplierLE);
ModularAddProductLE( multiplier2, modulus, multiplierLE, summandLE );
let expected = Modulus(summand + multiplier1 * multiplier2, modulus );
let actual = MeasureInteger(summandLE);
let actualMult = MeasureInteger(multiplierLE);
AssertIntEqual(expected, actual, $"Expected {expected}, got {actual}");
AssertIntEqual(multiplier1, actualMult, $"Expected {multiplier1}, got {actualMult}");
}
}
}
/// # Summary
/// Exhaustively tests Microsoft.Quantum.Canon.ModularAddProductLE
/// on 4 qubits with modulus 13
operation ModularAddProductLETest() : () {
body {
let numberOfQubits = 4;
let modulus = 13;
for( summand in 0 .. modulus - 1 ) {
for( multiplier1 in 0 .. modulus - 1 ) {
for( multiplier2 in 0 .. modulus - 1) {
ModularAddProductLETestHelper( summand, multiplier1, multiplier2, modulus, numberOfQubits );
}
}
}
}
}
operation ModularMultiplyByConstantLETestHelper(
multiplier1 : Int,
multiplier2 : Int,
modulus : Int,
numberOfQubits : Int ) : ()
{
body {
using (register = Qubit[numberOfQubits]) {
if( IsCoprime(multiplier2,modulus) ) {
let multiplierLE = LittleEndian(register);
InPlaceXorLE(multiplier1, multiplierLE);
ModularMultiplyByConstantLE( multiplier2, modulus, multiplierLE );
let expected = Modulus( multiplier1 * multiplier2, modulus );
let actualMult = MeasureInteger(multiplierLE);
AssertIntEqual(expected, actualMult, $"Expected {expected}, got {actualMult}");
}
}
}
}
/// # Summary
/// Exhaustively tests Microsoft.Quantum.Canon.ModularMultiplyByConstantLE
/// on 4 qubits with modulus 13
operation ModularMultiplyByConstantLETest() : () {
body {
let numberOfQubits = 4;
let modulus = 13;
for( multiplier1 in 0 .. modulus - 1 ) {
for( multiplier2 in 0 .. modulus - 1) {
ModularMultiplyByConstantLETestHelper( multiplier1, multiplier2, modulus, numberOfQubits );
}
}
}
}
}