This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 936
Expand file tree
/
Copy pathTraining.qs
More file actions
87 lines (77 loc) · 3.11 KB
/
Copy pathTraining.qs
File metadata and controls
87 lines (77 loc) · 3.11 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Samples {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Random;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.MachineLearning;
open Microsoft.Quantum.MachineLearning.Datasets as Datasets;
open Microsoft.Quantum.Math;
function DefaultSchedule(samples : LabeledSample[]) : SamplingSchedule {
return SamplingSchedule([
0..Length(samples) - 1
]);
}
function ClassifierStructure() : ControlledRotation[] {
return CombinedStructure([
LocalRotationsLayer(4, PauliZ),
LocalRotationsLayer(4, PauliX),
CyclicEntanglingLayer(4, PauliX, 1),
PartialRotationsLayer([3], PauliX)
]);
}
operation SampleSingleParameter() : Double {
return PI() * (DrawRandomDouble(0.0, 1.0) - 1.0);
}
operation SampleParametersForSequence(structure : ControlledRotation[]) : Double[] {
return ForEach(SampleSingleParameter, ConstantArray(Length(structure), ()));
}
operation SampleInitialParameters(nInitialParameterSets : Int, structure : ControlledRotation[]) : Double[][] {
return ForEach(SampleParametersForSequence, ConstantArray(nInitialParameterSets, structure));
}
operation TrainWineModel() : (Double[], Double) {
// Get the first 143 samples to use as training data.
let samples = (Datasets.WineData())[...142];
let structure = ClassifierStructure();
// Sample a random set of parameters.
let initialParameters = SampleInitialParameters(16, structure);
Message("Ready to train.");
let (optimizedModel, nMisses) = TrainSequentialClassifier(
Mapped(
SequentialModel(structure, _, 0.0),
initialParameters
),
samples,
DefaultTrainingOptions()
w/ LearningRate <- 0.4
w/ MinibatchSize <- 2
w/ Tolerance <- 0.01
w/ NMeasurements <- 10000
w/ MaxEpochs <- 16
w/ VerboseMessage <- Message,
DefaultSchedule(samples),
DefaultSchedule(samples)
);
Message($"Training complete, found optimal parameters and bias: {optimizedModel::Parameters}, {optimizedModel::Bias}");
return (optimizedModel::Parameters, optimizedModel::Bias);
}
operation ValidateWineModel(
parameters : Double[],
bias : Double
) : Double {
// Get the remaining samples to use as validation data.
let samples = (Datasets.WineData())[143...];
let tolerance = 0.005;
let nMeasurements = 10000;
let results = ValidateSequentialClassifier(
SequentialModel(ClassifierStructure(), parameters, bias),
samples,
tolerance,
nMeasurements,
DefaultSchedule(samples)
);
return IntAsDouble(results::NMisclassifications) / IntAsDouble(Length(samples));
}
}