Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 2 additions & 89 deletions MachineLearning/src/DataModel/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ public static List<long[]> PartialLocalLayer(long[] indices, char pauli)

/// <summary>
/// Creates a cyclic block of nQubits controlled rotations that starts
/// with contol qubit (nQubits-1), target qubit (cspan-1) % n , followed by the
/// ladder of entanglers with control qubit iq and target qubit (iq+cspan) % n
/// with control qubit (nQubits-1), target qubit (cspan-1) % nQubits , followed by a
/// ladder of entanglers with control qubits iq and target qubit (iq+cspan) % nQubits
/// </summary>
/// <param name="nQubits">Number of qubits to entangle</param>
/// <param name="pauli"></param>
Expand Down Expand Up @@ -220,68 +220,6 @@ public static void reindex(List<long[]> struc)
}
}

public void QcccTrainSequential(IQArray<IQArray<double>> parameterSource, IQArray<IQArray<double>> trainingSet, IQArray<long> trainingLabels, IQArray<IQArray<long>> trainingSchedule,
IQArray<IQArray<long>> validationSchedule, double learningRate, double tolerance, long miniBatchSize, long maxEpochs, long nMeasurements, uint randomizationSeed)
{
var sim = new QuantumSimulator(false, randomizationSeed);
(this._cachedParameters, this._bias) =
TrainQcccSequential.Run(sim, this._nQubits, this._structure, parameterSource, trainingSet, trainingLabels, trainingSchedule, validationSchedule, learningRate, tolerance, miniBatchSize, maxEpochs, nMeasurements).Result;
}
public void QcccTrainSequential(List<double[]> parameterSource, List<double[]> trainingSet, List<long> trainingLabels, List<long[]> trainingSchedule,
List<long[]> validationSchedule, double learningRate, double tolerance, long miniBatchSize, long maxEpochs, long nMeasurements, uint randomizationSeed)
{
QcccTrainSequential(Qonvert.ToQ(parameterSource), Qonvert.ToQ(trainingSet), Qonvert.ToQ(trainingLabels), Qonvert.ToQ(trainingSchedule),
Qonvert.ToQ(validationSchedule), learningRate, tolerance, miniBatchSize, maxEpochs, nMeasurements, randomizationSeed);
}

public void QcccTrainParallel(IQArray<IQArray<double>> parameterSource, IQArray<IQArray<double>> trainingSet, IQArray<long> trainingLabels, IQArray<IQArray<long>> trainingSchedule,
IQArray<IQArray<long>> validationSchedule, double learningRate, double tolerance, long miniBatchSize, long maxEpochs, long nMeasurements, uint randomizationSeed)
{
var simAll = new List<QuantumSimulator>(parameterSource.Count);
var resultsAll = new List<(IQArray<double>, double)>(parameterSource.Count);
var parameterComb = new List<IQArray<IQArray<double>>>(parameterSource.Count);

var indices = new int[parameterSource.Count];
for (int j = 0; j < parameterSource.Count; j++)
{
indices[j] = j;
simAll.Add(new QuantumSimulator(false, randomizationSeed));
resultsAll.Add((new QArray<double>(),0.0));
parameterComb.Add(new QArray<IQArray<double>>(new IQArray<double>[] { parameterSource[j] })); //Isolating parameter starts - one per thread
}
Parallel.ForEach(indices,
(j) =>
{

var rslt =
TrainQcccSequential.Run(simAll[j], this._nQubits, this._structure, parameterComb[j], trainingSet, trainingLabels, trainingSchedule, validationSchedule, learningRate, tolerance, miniBatchSize, maxEpochs, nMeasurements).Result;
resultsAll[j] = rslt;
}
);
//Estimated parameters and biases for each proposed parameter start. Now postprocess
long bestValidation = long.MaxValue;
int bestJ = -1;
var sim = new QuantumSimulator(false, randomizationSeed);
for (int j = 0; j < parameterSource.Count; j++)
{
var (pars, bias) = resultsAll[j];
long misses = CountValidationMisses.Run(sim, tolerance, this._nQubits, trainingSet, trainingLabels, validationSchedule, this._structure, pars, bias, nMeasurements).Result;
if (bestValidation > misses)
{
bestValidation = misses;
bestJ = j;
}
}
(this._cachedParameters, this._bias) = resultsAll[bestJ];
} //QcccTrainParallel

public void QcccTrainParallel(List<double[]> parameterSource, List<double[]> trainingSet, List<long> trainingLabels, List<long[]> trainingSchedule,
List<long[]> validationSchedule, double learningRate, double tolerance, long miniBatchSize, long maxEpochs, long nMeasurements, uint randomizationSeed)
{
QcccTrainParallel(Qonvert.ToQ(parameterSource), Qonvert.ToQ(trainingSet), Qonvert.ToQ(trainingLabels), Qonvert.ToQ(trainingSchedule),
Qonvert.ToQ(validationSchedule), learningRate, tolerance, miniBatchSize, maxEpochs, nMeasurements, randomizationSeed);
}

public long CountMisclassifications(double tolerance, IQArray<IQArray<double>> samples, IQArray<long> knownLabels, IQArray<IQArray<long>> validationSchedule, long nMeasurements, uint randomizationSeed)
{
if (this.isTrained)
Expand All @@ -304,31 +242,6 @@ public long CountMisclassifications(double tolerance, List<double[]> samples, Li
return CountMisclassifications(tolerance, Qonvert.ToQ(samples), Qonvert.ToQ(knownLabels), Qonvert.ToQ(validationSchedule), nMeasurements, randomizationSeed);
}

//EstimateClassificationProbabilitiesClassicalDataAdapter(samples: Double[][], schedule: Int[][], nQubits: Int, gates: Int[][], param: Double[], measCount: Int): Double[]
public double[] EstimateClassificationProbabilities(double tolerance, IQArray<IQArray<double>> samples, IQArray<IQArray<long>> schedule, long nMeasurements, uint randomizationSeed)
{
if (this.isTrained)
{
var sim = new QuantumSimulator(false, randomizationSeed);
IQArray<double> probs = EstimateClassificationProbabilitiesClassicalDataAdapter.Run(sim, tolerance, samples, schedule, this._nQubits, this._structure, this.CachedParameters, nMeasurements).Result;
return probs.ToArray();
}
return new double[] { -1.0 };
}

public double[] EstimateClassificationProbabilities(double tolerance, List<double[]> samples, List<long[]> schedule, long nMeasurements, uint randomizationSeed)
{
return EstimateClassificationProbabilities(tolerance, Qonvert.ToQ(samples), Qonvert.ToQ(schedule), nMeasurements, randomizationSeed);
}

public double[] EstimateClassificationProbabilities(double tolerance, List<double[]> samples, long nMeasurements, uint randomizationSeed)
{
List<long[]> sched = new List<long[]>(1);
sched.Add(new long[] { 0L, 1L, (long)(samples.Count - 1) });
return EstimateClassificationProbabilities(tolerance, Qonvert.ToQ(samples), Qonvert.ToQ(sched), nMeasurements, randomizationSeed);
}


} //class ClassificationModel

}
Loading