This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathSNIPPETS.cs
540 lines (475 loc) · 11.7 KB
/
SNIPPETS.cs
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Tests.IQSharp
{
public static class SNIPPETS
{
public static string HelloQ =
@"
/// # Summary
/// The simplest program. Just generate a debug Message on the console.
operation HelloQ() : Unit
{
Message($""Hello from quantum world!"");
}
";
public static string HelloQ_2 =
@"
/// # Summary
/// This to show that you can override the definition:
operation HelloQ() : Unit
{
Message($""msg0"");
Message($""msg1"");
}
";
public static string DependsOnHelloQ =
@"
/// # Summary
/// This to show that you can depend on other snippets:
operation DependsOnHelloQ() : Unit
{
HelloQ();
}
";
public static string Op1_Op2 =
@"
/// # Summary
/// This to show that you can define two operations in the same snippet:
operation Op1() : Unit
{
HelloQ();
}
operation Op2() : Unit
{
Op1();
}
";
public static string Op3_Op4_Op5_EntryPoints =
@"
/// # Summary
/// This to show that @EntryPoint() is ignored when compiling from snippets:
@EntryPoint()
operation Op3() : Unit
{
HelloQ();
}
@EntryPoint()
operation Op4() : Unit
{
Op3();
}
@ EntryPoint ( )
operation Op5() : Unit
{
Op4();
}
";
public static string Op6b_Op6a =
@"
/// # Summary
/// This to show that the order of snippet operations returned
/// from compilation is the same as the order in which they
/// are defined in the code:
operation Op6b() : Unit
{
HelloQ();
}
operation Op6a() : Unit
{
HelloQ();
}
";
public static string Op7_EndsWithComment =
@"
operation Op7() : Unit
{
HelloQ();
} // This snippet ends with a comment";
public static string CommentOnly = @"// This snippet contains only a comment";
public static string OpenNamespaces1 =
@"
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Diagnostics;
";
public static string OpenNamespaces2 =
@"
open Tests.qss;
open Microsoft.Quantum.Diagnostics;
";
public static string DependsOnNamespace =
@"
operation DependsOnNamespace() : Unit
{
using (qubits = Qubit[3])
{
Message(""Hello from DependsOnNamespace"");
HelloQ();
DumpMachine();
}
}
";
public static string OpenAliasedNamespace =
@"
open Microsoft.Quantum.Diagnostics as Diag;
";
public static string DependsOnAliasedNamespace =
@"
operation DependsOnAliasedNamespace() : Unit
{
using (qubits = Qubit[3])
{
Message(""Hello from DependsOnAliasedNamespace"");
Diag.DumpMachine();
}
}
";
public static string SimpleDebugOperation =
@"
operation SimpleDebugOperation() : (Result, Result)
{
using (qubits = Qubit[2])
{
H(qubits[0]);
H(qubits[1]);
return (M(qubits[0]), M(qubits[1]));
}
}
";
public static string FailIfOne =
@"
operation FailIfOne() : Unit {
use q = Qubit();
if M(q) == One {
fail ""Expected measuring a freshly allocated qubit to return Zero, but returned One."";
}
}
";
public static string ApplyWithinBlock =
@"
/// # Summary
/// Checks that within/apply block is properly compiled.
/// See https://github.com/microsoft/iqsharp/issues/266.
@EntryPoint()
operation ApplyWithinBlock() : Unit
{
using (q = Qubit())
{
within {
H(q);
Message(""Within"");
}
apply {
X(q);
Message(""Apply"");
}
}
}
";
public static string DependsOnWorkspace =
@"
/// # Summary
/// This to check that you can call operations from the workspace:
operation DependsOnWorkspace() : Result[]
{
return Tests.qss.HelloAgain(5, ""Foo"");
}
";
public static string DumpToFile =
@"
/// # Summary
/// This checks the ability to dump simulator state to a file.
operation DumpToFile() : Unit
{
using (qs = Qubit[2])
{
Microsoft.Quantum.Diagnostics.DumpMachine(""DumpMachine.txt"");
Microsoft.Quantum.Diagnostics.DumpRegister(""DumpRegister.txt"", qs);
Message(""Dumped to file!"");
}
}
";
public static string OneWarning =
@"
/// # Summary
/// This script has one warning for using `()`
operation OneWarning() : ()
{
Message($""msg0"");
}
";
public static string ThreeWarnings =
@"
/// # Summary
/// This script has two warnings. One for using `()`, and two
/// for missing `(...)`
operation ThreeWarnings() : ()
{
body {
Message($""msg0"");
Message($""msg1"");
Message($""msg2"");
}
adjoint {}
}
";
public static string TwoErrors =
@"
/// # Summary
/// This script has errors:
/// * Unknown UDT (header)
/// * Missing semicolon
operation TwoErrors(foo: Bar) : ()
{
body {
Message($""msg0"");
Message(""msg1"") // Missing semicolon
Message($""msg2"");
}
}
";
public static string DependsOnChemistryWorkspace =
@"
/// # Summary
/// This scripts depend on the Chemistry.Workspace correctly loaded.
///
open Tests.IQSharp.Chemistry.Samples;
open Mock.Chemistry;
operation DependsOnChemistryWorkspace() : ((JordanWignerEncodingData, Int, Double) => (Double, Double))
{
return UseJordanWignerEncodingData;
}
";
public static string UseJordanWignerEncodingData =
@"
open Mock.Chemistry;
operation UseJordanWignerEncodingData (qSharpData: JordanWignerEncodingData, nBitsPrecision : Int, trotterStepSize : Double) : (Double, Double) {
let (nSpinOrbitals, data, statePrepData, energyShift) = qSharpData!;
// Prepare ProductState
let estPhase = 2.0;
let estEnergy = 3.0;
return (estPhase, estEnergy);
}
";
public static string InvalidFunctor =
@"
/// # Summary
/// This script has an operation can't have adjoint since it has a measurement inside.
operation InvalidFunctor(q: Qubit) : Unit {
body(...) {
let m = M(q);
if (m) { X(q); }
}
adjoint auto;
}
";
public static string InvalidEntryPoint =
@"
/// # Summary
/// This script has an operation that is not valid to be marked as an entry point.
operation InvalidEntryPoint(q : Qubit) : Unit {
H(q);
}
";
public static string Reverse =
@"
/// # Summary
/// This script returns the same array in reverse order.
/// Used to make sure we can pass arguments to snippets simulation.
operation Reverse(name : String, array: Int[]) : Int[] {
Message($""Hello {name}"");
let n = Length(array);
mutable m = new Int[n];
for(i in n-1..-1..0) {
set m w/= i <- array[n - 1 - i];
}
return m;
}
";
public static string CompareMeasurementResult =
@"
operation CompareMeasurementResult() : Result {
using (qubits = Qubit[2]) {
let r = M(qubits[0]);
if (r == One) {
H(qubits[1]);
Reset(qubits[1]);
}
return r;
}
}
";
public static string UnusedClassicallyControlledOperation =
@"
operation ValidEntryPoint() : Result {
use q = Qubit();
H(q);
return M(q);
}
operation ClassicalControl() : Result {
use q = Qubit[2];
H(q[0]);
if (M(q[0])== One) {
X(q[1]);
}
return M(q[1]);
}
";
public static string ValidParameterTypes =
@"
operation UseValidParameterTypes(
myBool: Bool,
myDouble: Double,
myInt: Int,
myStr: String,
myPauli: Pauli,
myResult: Result,
(innerInt: Int, innerDouble: Double)
) : Result {
use q = Qubit();
H(q);
return M(q);
}
";
public static string ValidBoolParameter =
@"
operation UseBoolType(
myBool: Bool
) : Result {
use q = Qubit();
H(q);
return M(q);
}
";
public static string ValidDoubleParameter =
@"
operation UseDoubleType(
myDouble: Double
) : Result {
use q = Qubit();
H(q);
return M(q);
}
";
public static string ValidIntParameter =
@"
operation UseIntType(
myInt: Int
) : Result {
use q = Qubit();
H(q);
return M(q);
}
";
public static string ValidStringParameter =
@"
operation UseStringType(
myStr: String
) : Result {
use q = Qubit();
H(q);
return M(q);
}
";
public static string ValidPauliParameter =
@"
operation UsePauliType(
myPauli: Pauli
) : Result {
use q = Qubit();
H(q);
return M(q);
}
";
public static string ValidResultParameter =
@"
operation UseResultType(
myResult: Result
) : Result {
use q = Qubit();
H(q);
return M(q);
}
";
public static string ValidTupleParameters =
@"
operation UseTupleType(
(innerInt: Int, innerDouble: Double),
(innerString: String, innerResult: Result)
) : Result {
use q = Qubit();
H(q);
return M(q);
}
";
public static string InvalidUnitParameters =
@"
operation UseUnitType(myUnit: Unit) : Result {
use q = Qubit();
H(q);
return M(q);
}
operation UseUnitTypeFirst(myUnit: Unit, myInt: Int, myBool: Bool) : Result {
use q = Qubit();
H(q);
return M(q);
}
operation UseUnitTypeMiddle(myInt: Int, myUnit: Unit, myBool: Bool) : Result {
use q = Qubit();
H(q);
return M(q);
}
operation UseUnitTypeLast(myInt: Int, myBool: Bool, myUnit: Unit) : Result {
use q = Qubit();
H(q);
return M(q);
}
";
public static string InvalidArrayParameters =
@"
operation UseArrayType(myArray: Int[]) : Result {
use q = Qubit();
H(q);
return M(q);
}
operation UseArrayTypeFirst(myArray: Int[], myInt: Int, myBool: Bool) : Result {
use q = Qubit();
H(q);
return M(q);
}
operation UseArrayTypeMiddle(myInt: Int, myArray: Int[], myBool: Bool) : Result {
use q = Qubit();
H(q);
return M(q);
}
operation UseArrayTypeLast(myInt: Int, myBool: Bool, myArray: Int[]) : Result {
use q = Qubit();
H(q);
return M(q);
}
";
public static string InvalidRangeParameters =
@"
operation UseRangeType(myRange: Range) : Result {
use q = Qubit();
H(q);
return M(q);
}
operation UseRangeTypeFirst(myRange: Range, myInt: Int, myBool: Bool) : Result {
use q = Qubit();
H(q);
return M(q);
}
operation UseRangeTypeMiddle(myInt: Int, myRange: Range, myBool: Bool) : Result {
use q = Qubit();
H(q);
return M(q);
}
operation UseRangeTypeLast(myInt: Int, myBool: Bool, myRange: Range) : Result {
use q = Qubit();
H(q);
return M(q);
}
";
}
}