forked from microsoft/Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepeatUntilSuccessCircuitsTests.qs
72 lines (59 loc) · 2.9 KB
/
RepeatUntilSuccessCircuitsTests.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Samples.UnitTesting {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Diagnostics;
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.
@Test("QuantumSimulator")
operation CheckRepeatUntilSuccessRotatesByCorrectAngle() : 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 (actual, expected) in testList {
// 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(1, ApplyToFirstQubit(actual, _), expected);
}
}
}
/// # Summary
/// Here we check the correctness of Repeat Until Success state preparation circuit
/// defined in RepeatUntilSuccessCircuits.qs.
@Test("QuantumSimulator")
operation CheckRepeatUntilSuccessPreparesCorrectState() : Unit {
for i in 0 .. 100 {
use target = Qubit();
// 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(1.0 / Sqrt(3.0), 0.0);
AssertQubitIsInStateWithinTolerance((zeroAmp, oneAmp), target, 1E-10);
// Reset target back to |0⟩
Reset(target);
}
}
}