forked from microsoft/Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatUntilSuccessCircuitsTests.qs
More file actions
75 lines (55 loc) · 3.14 KB
/
RepeatUntilSuccessCircuitsTests.qs
File metadata and controls
75 lines (55 loc) · 3.14 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Samples.UnitTesting {
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Extensions.Math;
open Microsoft.Quantum.Extensions.Convert;
open Microsoft.Quantum.Extensions.Testing;
open Microsoft.Quantum.Canon;
///////////////////////////////////////////////////////////////////////////////////////////////
// Unit test to circuits implementing for exp(±i⋅ArcTan(2)⋅Z) with using Repeat-Until-Success
// (RUS) protocols. Note that all test operations names end with `Test`
///////////////////////////////////////////////////////////////////////////////////////////////
/// # Summary
/// Here we check the correctness of Repeat Until Success unitary circuits
/// defined in RepeatUntilSuccessCircuits.qs.
operation RepeatUntilSuccessCircuitsTest () : Unit {
// List of operations to test (actual,expected)
// we first test ExpIZArcTan2NC and its Adjoint
// next test ExpIZArcTan2PS and its Adjoint
let testList = [(ExpIZArcTan2NC, Exp([PauliZ], ArcTan(2.0), _)), (Adjoint ExpIZArcTan2NC, Exp([PauliZ], -ArcTan(2.0), _)), (ExpIZArcTan2PS, Exp([PauliZ], ArcTan(2.0), _)), (Adjoint ExpIZArcTan2PS, Exp([PauliZ], -ArcTan(2.0), _))];
// As the circuits are probabilistic we repeat tests multiple times
for (i in 0 .. 400) {
// next go over everything in testList
for (j in 0 .. Length(testList) - 1) {
let (actual, expected) = testList[j];
// This will log the names and parameters of operations being tested
Message($"Testing {actual} against {expected}. Attempt: {i}");
// Using Referenced testing, as it uses only one call to the operation
// Note that in QCTraceSimulator call graph
// ExpIZArcTan2NC and ExpIZArcTan2PS will be called from ApplyToFirstQubit
AssertOperationsEqualReferenced(ApplyToFirstQubit(actual, _), expected, 1);
}
}
}
/// # Summary
/// Here we check the correctness of Repeat Until Success state preparation circuit
/// defined in RepeatUntilSuccessCircuits.qs.
operation RepeatUntilSuccessStatePreparationTest () : Unit {
for (i in 0 .. 100) {
using (qubits = Qubit[1]) {
let target = qubits[0];
// Prepare input in |+⟩ state
H(target);
// Apply function being tested
RepeatUntilSuccessStatePreparation(target);
// Assert that prepared state is (√2/√3,1/√3)
let zeroAmp = Complex(Sqrt(2.0) / Sqrt(3.0), 0.0);
let oneAmp = Complex(ToDouble(1) / Sqrt(3.0), 0.0);
AssertQubitState((zeroAmp, oneAmp), target, 1E-10);
// Reset target back to |0⟩
Reset(target);
}
}
}
}