forked from microsoft/Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQcvvTests.qs
More file actions
104 lines (88 loc) · 3.77 KB
/
QcvvTests.qs
File metadata and controls
104 lines (88 loc) · 3.77 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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Tests {
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Extensions.Convert;
open Microsoft.Quantum.Extensions.Math;
operation ChoiStateTest() : () {
body {
using (register = Qubit[2]) {
PrepareChoiStateCA(NoOp, [register[0]], [register[1]]);
// As usual, the same confusion about {+1, -1} and {0, 1}
// labeling bites us here.
Assert([PauliX; PauliX], register, Zero, "XX");
Assert([PauliZ; PauliZ], register, Zero, "ZZ");
ResetAll(register);
}
}
}
operation EstimateFrequencyTest () : () {
body {
let freq = EstimateFrequency(
ApplyToEach(H, _),
MeasureAllZ,
1,
1000
);
AssertAlmostEqualTol(freq, 0.5, 0.1);
}
}
operation _RobustPhaseEstimationTestOp(phase: Double, power: Int, qubits : Qubit[]) : (){
body {
//Rz(- 2.0* phase * ToDouble(power), qubits[0])
Exp([PauliZ], phase * ToDouble(power), qubits);
//Exp([PauliI], phase * ToDouble(power), qubits);
}
adjoint auto
controlled auto
controlled adjoint auto
}
operation RobustPhaseEstimationDemoImpl(phaseSet : Double, bitsPrecision: Int) : Double{
body {
let op = DiscreteOracle(_RobustPhaseEstimationTestOp(phaseSet, _, _));
mutable phaseEst = ToDouble(0);
using (q = Qubit[1]) {
set phaseEst = RobustPhaseEstimation(bitsPrecision, op, q);
ResetAll(q);
}
return phaseEst;
}
}
// Probabilistic test. Might fail occasionally
operation RobustPhaseEstimationTest() : () {
body {
let bitsPrecision = 10;
for (idxTest in 0..9) {
let phaseSet = 2.0 * PI() * ToDouble(idxTest - 5) / 12.0;
let phaseEst = RobustPhaseEstimationDemoImpl(phaseSet, bitsPrecision);
AssertAlmostEqualTol(phaseEst, phaseSet, 1e-2);
}
}
}
operation PrepareQubitTest() : () {
body {
using (register = Qubit[1]) {
let qubit = register[0];
let bases = [PauliI; PauliX; PauliY; PauliZ];
for (idxBasis in 0..Length(bases) - 1) {
let basis = bases[idxBasis];
PrepareQubit(basis, qubit);
Assert([basis], register, Zero, $"Did not prepare in {basis} correctly.");
Reset(qubit);
}
}
}
}
operation SingleQubitProcessTomographyMeasurementTest() : () {
body {
AssertResultEqual(SingleQubitProcessTomographyMeasurement(PauliI, PauliI, H), Zero, "Failed at ⟪I | H | I⟫.");
AssertResultEqual(SingleQubitProcessTomographyMeasurement(PauliX, PauliI, H), Zero, "Failed at ⟪I | H | X⟫.");
AssertResultEqual(SingleQubitProcessTomographyMeasurement(PauliY, PauliI, H), Zero, "Failed at ⟪I | H | Y⟫.");
AssertResultEqual(SingleQubitProcessTomographyMeasurement(PauliZ, PauliI, H), Zero, "Failed at ⟪I | H | Z⟫.");
AssertResultEqual(SingleQubitProcessTomographyMeasurement(PauliX, PauliZ, H), Zero, "Failed at ⟪Z | H | X⟫.");
AssertResultEqual(SingleQubitProcessTomographyMeasurement(PauliY, PauliY, H), One, "Failed at -⟪Y | H | Y⟫.");
AssertResultEqual(SingleQubitProcessTomographyMeasurement(PauliX, PauliZ, H), Zero, "Failed at ⟪Z | H | X⟫.");
}
}
}