From 7997c9a9ffe9325586585e089114725b97d1933a Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Wed, 5 Feb 2020 11:34:10 -0800 Subject: [PATCH 1/9] Began adding more tests. --- MachineLearning/tests/TypesTests.qs | 20 +++++++++++++++++++ MachineLearning/tests/ValidationTests.qs | 19 ++++++++++++++++++ Standard/src/Diagnostics/Facts.qs | 25 ++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 MachineLearning/tests/TypesTests.qs create mode 100644 MachineLearning/tests/ValidationTests.qs diff --git a/MachineLearning/tests/TypesTests.qs b/MachineLearning/tests/TypesTests.qs new file mode 100644 index 00000000000..4a70e80f30e --- /dev/null +++ b/MachineLearning/tests/TypesTests.qs @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.Quantum.MachineLearning.Tests { + open Microsoft.Quantum.Diagnostics; + open Microsoft.Quantum.MachineLearning as ML; + + @Test("QuantumSimulator") + function ScheduleLengthFact() : Unit { + let actualLength = ML.ScheduleLength(ML.SamplingSchedule([0..4, 1..2..5])); + EqualityFactI(actualLength, 5 + 3, "Wrong output from ScheduleLength."); + } + + @Test("QuantumSimulator") + function SampledFact() : Unit { + let actuallySampled = ML.Sampled(ML.SamplingSchedule([0..4, 1..2..5]), [0, 10, 20, 30, 40, 50, 60, 70]); + AllEqualityFactI(actuallySampled, [0, 10, 20, 30, 40, 10, 30, 50], "Wrong output from Sampled."); + } + +} diff --git a/MachineLearning/tests/ValidationTests.qs b/MachineLearning/tests/ValidationTests.qs new file mode 100644 index 00000000000..9fc54cf74aa --- /dev/null +++ b/MachineLearning/tests/ValidationTests.qs @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.Quantum.MachineLearning.Tests { + open Microsoft.Quantum.Diagnostics; + open Microsoft.Quantum.MachineLearning as ML; + + @Test("QuantumSimulator") + function MisclassificationsFact() : Unit { + let misclassifications = ML.Misclassifications([0, 1, 0, 0], [0, 1, 1, 0]); + AllEqualityFactI(misclassifications, [2], "Wrong output from Misclassifications."); + } + + @Test("QuantumSimulator") + function NMisclassificationsFact() : Unit { + let nMisclassifications = ML.NMisclassifications([0, 1, 0, 0, 1], [0, 1, 1, 0, 0]); + EqualityFactI(nMisclassifications, 2, "Wrong output from NMisclassifications."); + } +} diff --git a/Standard/src/Diagnostics/Facts.qs b/Standard/src/Diagnostics/Facts.qs index 33224a98d85..eaa4622c58a 100644 --- a/Standard/src/Diagnostics/Facts.qs +++ b/Standard/src/Diagnostics/Facts.qs @@ -200,6 +200,9 @@ namespace Microsoft.Quantum.Diagnostics { /// The array that is expected from a test case of interest. /// ## message /// A message to be printed if the arrays are not equal. + /// + /// # See Also + /// Microsoft.Quantum.Diagnostics.AllEqualityFactI function AllEqualityFactB(actual : Bool[], expected : Bool[], message : String) : Unit { let n = Length(actual); if (n != Length(expected)) { @@ -209,4 +212,26 @@ namespace Microsoft.Quantum.Diagnostics { Ignore(Mapped(EqualityFactB(_, _, message), Zip(actual, expected))); } + /// # Summary + /// Asserts that two arrays of integer values are equal. + /// + /// # Input + /// ## actual + /// The array that is produced by a test case of interest. + /// ## expected + /// The array that is expected from a test case of interest. + /// ## message + /// A message to be printed if the arrays are not equal. + /// + /// # See Also + /// Microsoft.Quantum.Diagnostics.AllEqualityFactB + function AllEqualityFactI(actual : Int[], expected : Int[], message : String) : Unit { + let n = Length(actual); + if (n != Length(expected)) { + fail message; + } + + Ignore(Mapped(EqualityFactI(_, _, message), Zip(actual, expected))); + } + } From 2076e8c10be6fd44615e8832f33b05da637a62c7 Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Wed, 5 Feb 2020 12:54:18 -0800 Subject: [PATCH 2/9] New test for applysequentialclassifier. --- MachineLearning/tests/StructureTests.qs | 52 +++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 MachineLearning/tests/StructureTests.qs diff --git a/MachineLearning/tests/StructureTests.qs b/MachineLearning/tests/StructureTests.qs new file mode 100644 index 00000000000..6ffeafc2ed5 --- /dev/null +++ b/MachineLearning/tests/StructureTests.qs @@ -0,0 +1,52 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.Quantum.MachineLearning.Tests { + open Microsoft.Quantum.Intrinsic; + open Microsoft.Quantum.Diagnostics; + open Microsoft.Quantum.MachineLearning as ML; + + @Test("QuantumSimulator") + function NQubitsRequiredFact() : Unit { + let model = Default() + w/ Structure <- [ + ML.ControlledRotation((3, [7, 9]), PauliX, 0) + ]; + let actual = ML.NQubitsRequired(model); + EqualityFactI(actual, 10, "Wrong output from ScheduleLength."); + } + + function ExampleModel() : ML.SequentialModel { + return Default() + w/ Structure <- [ + Default() + w/ TargetIndex <- 2 + w/ ControlIndices <- [0] + w/ Axis <- PauliX + w/ ParameterIndex <- 0, + Default() + w/ TargetIndex <- 0 + w/ ControlIndices <- [1, 2] + w/ Axis <- PauliZ + w/ ParameterIndex <- 1 + ] + w/ Parameters <- [ + 1.234, + 2.345 + ]; + } + + operation ApplyExampleModelManually(register : Qubit[]) : Unit is Adj + Ctl { + Controlled R([register[0]], (PauliX, 1.234, register[2])); + Controlled R([register[1], register[2]], (PauliZ, 2.345, register[0])); + } + + @Test("QuantumSimulator") + operation TestApplySequentialClassifier() : Unit { + AssertOperationsEqualReferenced(ML.NQubitsRequired(ExampleModel()), + ML.ApplySequentialClassifier(ExampleModel(), _), + ApplyExampleModelManually + ); + } + +} From cfb10d3ebe3becc15bdea26af38dcc81712baa6d Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Wed, 5 Feb 2020 13:54:47 -0800 Subject: [PATCH 3/9] Reduce warning count. --- Standard/src/Arithmetic/Integer.qs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Standard/src/Arithmetic/Integer.qs b/Standard/src/Arithmetic/Integer.qs index 07c9d9a306a..df9209fc4f6 100644 --- a/Standard/src/Arithmetic/Integer.qs +++ b/Standard/src/Arithmetic/Integer.qs @@ -583,16 +583,18 @@ namespace Microsoft.Quantum.Arithmetic { X(ys![0]); } else { - ApplyToEachCA(X, ys!); - ApplyToEachCA(CNOT, Zip(Rest(xs!),Rest(ys!))); - (Adjoint CascadeCNOT) (Rest(xs!)); - CascadeCCNOT (Most(ys!), xs!); - (Controlled CCNOT) (controls, (xs![nQubits-1], ys![nQubits-1], result)); - (Adjoint CascadeCCNOT) (Most(ys!), xs!); - CascadeCNOT(Rest(xs!)); - (Controlled CNOT) (controls, (xs![nQubits-1], result)); - ApplyToEachCA(CNOT, Zip(Rest(xs!), Rest(ys!))); - ApplyToEachCA(X, ys!); + within { + ApplyToEachCA(X, ys!); + ApplyToEachCA(CNOT, Zip(Rest(xs!), Rest(ys!))); + } apply { + within { + (Adjoint ApplyCNOTChain) (Rest(xs!)); + CascadeCCNOT (Most(ys!), xs!); + } apply { + (Controlled CCNOT) (controls, (xs![nQubits-1], ys![nQubits-1], result)); + } + (Controlled CNOT) (controls, (xs![nQubits-1], result)); + } } } } From 1c85c75992ff06fad4ab4e895a975ba2d0aeca2c Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Wed, 5 Feb 2020 14:53:43 -0800 Subject: [PATCH 4/9] More tests of structure functions. --- MachineLearning/src/Structure.qs | 3 +- MachineLearning/tests/StructureTests.qs | 136 +++++++++++++++++++++++- 2 files changed, 137 insertions(+), 2 deletions(-) diff --git a/MachineLearning/src/Structure.qs b/MachineLearning/src/Structure.qs index 7b8f191a3c8..c1c8a0810f6 100644 --- a/MachineLearning/src/Structure.qs +++ b/MachineLearning/src/Structure.qs @@ -22,7 +22,7 @@ namespace Microsoft.Quantum.MachineLearning { : Int { mutable nQubitsRequired = 0; for (gate in model::Structure) { - set nQubitsRequired = Fold( + set nQubitsRequired = 1 + Fold( MaxI, 0, gate::ControlIndices + [ gate::TargetIndex, @@ -51,6 +51,7 @@ namespace Microsoft.Quantum.MachineLearning { : (Unit) is Adj + Ctl { for (gate in model::Structure) { if (gate::ParameterIndex < Length(model::Parameters)) { + Message($"axis {gate::Axis} parameter {model::Parameters}[{gate::ParameterIndex}] target {gate::TargetIndex}"); let input = (gate::Axis, model::Parameters[gate::ParameterIndex], qubits[gate::TargetIndex]); if (IsEmpty(gate::ControlIndices)) { // Uncontrolled rotation of target diff --git a/MachineLearning/tests/StructureTests.qs b/MachineLearning/tests/StructureTests.qs index 6ffeafc2ed5..76da542f15f 100644 --- a/MachineLearning/tests/StructureTests.qs +++ b/MachineLearning/tests/StructureTests.qs @@ -2,6 +2,8 @@ // Licensed under the MIT License. namespace Microsoft.Quantum.MachineLearning.Tests { + open Microsoft.Quantum.Logical; + open Microsoft.Quantum.Arrays; open Microsoft.Quantum.Intrinsic; open Microsoft.Quantum.Diagnostics; open Microsoft.Quantum.MachineLearning as ML; @@ -13,7 +15,7 @@ namespace Microsoft.Quantum.MachineLearning.Tests { ML.ControlledRotation((3, [7, 9]), PauliX, 0) ]; let actual = ML.NQubitsRequired(model); - EqualityFactI(actual, 10, "Wrong output from ScheduleLength."); + EqualityFactI(actual, 10, "Wrong output from NQubitsRequired."); } function ExampleModel() : ML.SequentialModel { @@ -49,4 +51,136 @@ namespace Microsoft.Quantum.MachineLearning.Tests { ); } + function EqualCR(x : ML.ControlledRotation, y : ML.ControlledRotation) : Bool { + return x::Axis == y::Axis and + All(EqualI, Zip(x::ControlIndices, y::ControlIndices)) and + x::TargetIndex == y::TargetIndex and + x::ParameterIndex == y::ParameterIndex; + } + + @Test("QuantumSimulator") + function LocalRotationsLayerFact() : Unit { + Fact(All(EqualCR, Zip( + ML.LocalRotationsLayer(3, PauliY), + [ + Default() + w/ TargetIndex <- 0 + w/ ControlIndices <- new Int[0] + w/ Axis <- PauliY + w/ ParameterIndex <- 0, + + Default() + w/ TargetIndex <- 1 + w/ ControlIndices <- new Int[0] + w/ Axis <- PauliY + w/ ParameterIndex <- 1, + + Default() + w/ TargetIndex <- 2 + w/ ControlIndices <- new Int[0] + w/ Axis <- PauliY + w/ ParameterIndex <- 2 + ] + )), "LocalRotationsLayer returned wrong output."); + } + + @Test("QuantumSimulator") + function PartialRotationsLayerFact() : Unit { + Fact(All(EqualCR, Zip( + ML.PartialRotationsLayer([4, 5, 6], PauliY), + [ + Default() + w/ TargetIndex <- 4 + w/ ControlIndices <- new Int[0] + w/ Axis <- PauliY + w/ ParameterIndex <- 0, + + Default() + w/ TargetIndex <- 5 + w/ ControlIndices <- new Int[0] + w/ Axis <- PauliY + w/ ParameterIndex <- 1, + + Default() + w/ TargetIndex <- 6 + w/ ControlIndices <- new Int[0] + w/ Axis <- PauliY + w/ ParameterIndex <- 2 + ] + )), "PartialRotationsLayer returned wrong output."); + } + + @Test("QuantumSimulator") + function CyclicEntanglingLayerFact() : Unit { + Fact(All(EqualCR, Zip( + ML.CyclicEntanglingLayer(3, PauliX, 2), + [ + Default() + w/ TargetIndex <- 0 + w/ ControlIndices <- [2] + w/ Axis <- PauliX + w/ ParameterIndex <- 0, + + Default() + w/ TargetIndex <- 1 + w/ ControlIndices <- [0] + w/ Axis <- PauliX + w/ ParameterIndex <- 1, + + Default() + w/ TargetIndex <- 2 + w/ ControlIndices <- [1] + w/ Axis <- PauliX + w/ ParameterIndex <- 2 + ] + )), "CyclicEntanglingLayer returned wrong output."); + } + + @Test("QuantumSimulator") + function CombinedStructureFact() : Unit { + Fact(All(EqualCR, Zip( + ML.CombinedStructure([ + [ + Default() + w/ TargetIndex <- 0 + w/ ControlIndices <- [2] + w/ Axis <- PauliX + w/ ParameterIndex <- 0, + + Default() + w/ TargetIndex <- 1 + w/ ControlIndices <- [0] + w/ Axis <- PauliX + w/ ParameterIndex <- 1, + ], + [ + Default() + w/ TargetIndex <- 0 + w/ ControlIndices <- [2] + w/ Axis <- PauliZ + w/ ParameterIndex <- 0, + ] + ]), + [ + Default() + w/ TargetIndex <- 0 + w/ ControlIndices <- [2] + w/ Axis <- PauliX + w/ ParameterIndex <- 0, + + Default() + w/ TargetIndex <- 1 + w/ ControlIndices <- [0] + w/ Axis <- PauliX + w/ ParameterIndex <- 1, + + Default() + w/ TargetIndex <- 2 + w/ ControlIndices <- [1] + w/ Axis <- PauliZ + w/ ParameterIndex <- 2 + ] + )), "CombinedStructure returned wrong output."); + } + } From fd0b40ae42a3f4cbe19a72bb5a86d18e5d6a80ec Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Wed, 5 Feb 2020 15:08:03 -0800 Subject: [PATCH 5/9] Fixed test and eliminated some warnings. --- MachineLearning/tests/StructureTests.qs | 45 +++++++++++++------------ Standard/src/Arithmetic/Comparators.qs | 18 ++++------ Standard/src/Arithmetic/Integer.qs | 2 +- 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/MachineLearning/tests/StructureTests.qs b/MachineLearning/tests/StructureTests.qs index 76da542f15f..49ee4503189 100644 --- a/MachineLearning/tests/StructureTests.qs +++ b/MachineLearning/tests/StructureTests.qs @@ -138,29 +138,30 @@ namespace Microsoft.Quantum.MachineLearning.Tests { @Test("QuantumSimulator") function CombinedStructureFact() : Unit { + let combined = ML.CombinedStructure([ + [ + Default() + w/ TargetIndex <- 0 + w/ ControlIndices <- [2] + w/ Axis <- PauliX + w/ ParameterIndex <- 0, + + Default() + w/ TargetIndex <- 1 + w/ ControlIndices <- [0] + w/ Axis <- PauliX + w/ ParameterIndex <- 1, + ], + [ + Default() + w/ TargetIndex <- 2 + w/ ControlIndices <- [1] + w/ Axis <- PauliZ + w/ ParameterIndex <- 0, + ] + ]); Fact(All(EqualCR, Zip( - ML.CombinedStructure([ - [ - Default() - w/ TargetIndex <- 0 - w/ ControlIndices <- [2] - w/ Axis <- PauliX - w/ ParameterIndex <- 0, - - Default() - w/ TargetIndex <- 1 - w/ ControlIndices <- [0] - w/ Axis <- PauliX - w/ ParameterIndex <- 1, - ], - [ - Default() - w/ TargetIndex <- 0 - w/ ControlIndices <- [2] - w/ Axis <- PauliZ - w/ ParameterIndex <- 0, - ] - ]), + combined, [ Default() w/ TargetIndex <- 0 diff --git a/Standard/src/Arithmetic/Comparators.qs b/Standard/src/Arithmetic/Comparators.qs index c6898baa6f6..217ab59c479 100644 --- a/Standard/src/Arithmetic/Comparators.qs +++ b/Standard/src/Arithmetic/Comparators.qs @@ -54,19 +54,15 @@ namespace Microsoft.Quantum.Arithmetic { } // Implementation step of `ApplyRippleCarryComparatorLE`. - operation _ApplyRippleCarryComparatorLE(x: LittleEndian, y: LittleEndian, auxiliary: Qubit[], output: Qubit) : Unit { - body (...) { - let nQubitsX = Length(x!); + operation _ApplyRippleCarryComparatorLE(x: LittleEndian, y: LittleEndian, auxiliary: Qubit[], output: Qubit) + : Unit is Adj + Ctl { + let nQubitsX = Length(x!); - // Take 2's complement - ApplyToEachCA(X, x! + auxiliary); + // Take 2's complement + ApplyToEachCA(X, x! + auxiliary); - InPlaceMajority(x![0], [y![0], auxiliary[0]]); - ApplyToEachCA(MAJ, Zip3(Most(x!), Rest(y!), Rest(x!))); - } - adjoint auto; - controlled auto; - adjoint controlled auto; + ApplyMajorityInPlace(x![0], [y![0], auxiliary[0]]); + ApplyToEachCA(MAJ, Zip3(Most(x!), Rest(y!), Rest(x!))); } } diff --git a/Standard/src/Arithmetic/Integer.qs b/Standard/src/Arithmetic/Integer.qs index df9209fc4f6..4c59819a1b2 100644 --- a/Standard/src/Arithmetic/Integer.qs +++ b/Standard/src/Arithmetic/Integer.qs @@ -404,7 +404,7 @@ namespace Microsoft.Quantum.Arithmetic { "Input registers must have the same number of qubits." ); ApplyToEachCA(CNOT, Zip(Rest(xs!), Rest(ys!))); - (Adjoint CascadeCNOT) (Rest(xs!)); + Adjoint ApplyCNOTChain(Rest(xs!)); } /// # Summary From 748d129b422a6466d55b5ccc693a3e7e55af1f4a Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Wed, 5 Feb 2020 15:10:39 -0800 Subject: [PATCH 6/9] Further warning elimination. --- MachineLearning/tests/StructureTests.qs | 4 +-- Standard/src/Simulation/Algorithms.qs | 40 ++++++++++++------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/MachineLearning/tests/StructureTests.qs b/MachineLearning/tests/StructureTests.qs index 49ee4503189..e9ee25a29ae 100644 --- a/MachineLearning/tests/StructureTests.qs +++ b/MachineLearning/tests/StructureTests.qs @@ -150,14 +150,14 @@ namespace Microsoft.Quantum.MachineLearning.Tests { w/ TargetIndex <- 1 w/ ControlIndices <- [0] w/ Axis <- PauliX - w/ ParameterIndex <- 1, + w/ ParameterIndex <- 1 ], [ Default() w/ TargetIndex <- 2 w/ ControlIndices <- [1] w/ Axis <- PauliZ - w/ ParameterIndex <- 0, + w/ ParameterIndex <- 0 ] ]); Fact(All(EqualCR, Zip( diff --git a/Standard/src/Simulation/Algorithms.qs b/Standard/src/Simulation/Algorithms.qs index 3ecc5d7f19f..ef33f64567b 100644 --- a/Standard/src/Simulation/Algorithms.qs +++ b/Standard/src/Simulation/Algorithms.qs @@ -9,7 +9,7 @@ namespace Microsoft.Quantum.Simulation { // A simulation technique converts an EvolutionGenerator to time evolution // by the encoded system for some time step // Here is an example of a simulation technique. - + /// # Summary /// Implements time-evolution by a term contained in a `GeneratorSystem`. /// @@ -28,8 +28,8 @@ namespace Microsoft.Quantum.Simulation { let generatorIndex = generatorSystemFunction(idx); (evolutionSet!(generatorIndex))!(stepsize, qubits); } - - + + /// # Summary /// Implements a single time-step of time-evolution by the system /// described in an `EvolutionGenerator` using a Trotter–Suzuki @@ -54,19 +54,19 @@ namespace Microsoft.Quantum.Simulation { { let (evolutionSet, generatorSystem) = evolutionGenerator!; let (nTerms, generatorSystemFunction) = generatorSystem!; - + // The input to DecomposeIntoTimeStepsCA has signature // (Int, ((Int, Double, Qubit[]) => () is Adj + Ctl)) let trotterForm = (nTerms, TrotterStepImpl(evolutionGenerator, _, _, _)); - return (DecomposeIntoTimeStepsCA(trotterForm, trotterOrder))(trotterStepSize, _); + return (DecomposedIntoTimeStepsCA(trotterForm, trotterOrder))(trotterStepSize, _); } - - + + // This simulation algorithm takes (timeMax, EvolutionGenerator, // register) and other algorithm-specific parameters (trotterStepSize, // trotterOrder), and performs evolution under the EvolutionGenerator // for time = timeMax. - + /// # Summary /// Makes repeated calls to `TrotterStep` to approximate the /// time-evolution operator exp(_-iHt_). @@ -88,19 +88,19 @@ namespace Microsoft.Quantum.Simulation { { let nTimeSlices = Ceiling(maxTime / trotterStepSize); let resizedTrotterStepSize = maxTime / IntAsDouble(nTimeSlices); - + for (idxTimeSlice in 0 .. nTimeSlices - 1) { (TrotterStep(evolutionGenerator, trotterOrder, resizedTrotterStepSize))(qubits); } } - + adjoint invert; controlled distribute; controlled adjoint distribute; } - - + + /// # Summary /// `SimulationAlgorithm` function that uses a Trotter–Suzuki /// decomposition to approximate the time-evolution operator _exp(-iHt)_. @@ -117,11 +117,11 @@ namespace Microsoft.Quantum.Simulation { { return SimulationAlgorithm(TrotterSimulationAlgorithmImpl(trotterStepSize, trotterOrder, _, _, _)); } - - + + // This simple time-dependent simulation algorithm implements a // sequence of uniformly-sized trotter steps - + /// # Summary /// Implementation of multiple Trotter steps to approximate a unitary /// operator that solves the time-dependent Schrödinger equation. @@ -143,7 +143,7 @@ namespace Microsoft.Quantum.Simulation { { let nTimeSlices = Ceiling(maxTime / trotterStepSize); let resizedTrotterStepSize = maxTime / IntAsDouble(nTimeSlices); - + for (idxTimeSlice in 0 .. nTimeSlices - 1) { let schedule = IntAsDouble(idxTimeSlice) / IntAsDouble(nTimeSlices); @@ -153,13 +153,13 @@ namespace Microsoft.Quantum.Simulation { (TrotterSimulationAlgorithm(resizedTrotterStepSize, trotterOrder))!(resizedTrotterStepSize, evolutionGenerator, qubits); } } - + adjoint invert; controlled distribute; controlled adjoint distribute; } - - + + /// # Summary /// `TimeDependentSimulationAlgorithm` function that uses a Trotter–Suzuki /// decomposition to approximate a unitary operator that solves the @@ -177,7 +177,7 @@ namespace Microsoft.Quantum.Simulation { { return TimeDependentSimulationAlgorithm(TimeDependentTrotterSimulationAlgorithmImpl(trotterStepSize, trotterOrder, _, _, _)); } - + } From a387017c95452fb2ca67a29d89c1dd2ae52dd67d Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Wed, 5 Feb 2020 15:29:50 -0800 Subject: [PATCH 7/9] Tests for InferredLabel and InferredLabels. --- MachineLearning/tests/ClassificationTests.qs | 25 ++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 MachineLearning/tests/ClassificationTests.qs diff --git a/MachineLearning/tests/ClassificationTests.qs b/MachineLearning/tests/ClassificationTests.qs new file mode 100644 index 00000000000..2b41e0cbc5c --- /dev/null +++ b/MachineLearning/tests/ClassificationTests.qs @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.Quantum.MachineLearning.Tests { + open Microsoft.Quantum.Logical; + open Microsoft.Quantum.Arrays; + open Microsoft.Quantum.Intrinsic; + open Microsoft.Quantum.Diagnostics; + open Microsoft.Quantum.MachineLearning as ML; + + @Test("QuantumSimulator") + function InferredLabelFact() : Unit { + EqualityFactI(ML.InferredLabel(0.25, 0.26), 1, "InferredLabel returned wrong class."); + } + + @Test("QuantumSimulator") + function InferredLabelsFact() : Unit { + AllEqualityFactI( + ML.InferredLabels(0.25, [0.23, 0.26, 0.1]), + [0, 1, 0], + "InferredLabels returned at least wrong class." + ); + } + +} From 1039a0d13a5c717b9df6b12ee9617e4434b7a3b2 Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Wed, 5 Feb 2020 15:36:11 -0800 Subject: [PATCH 8/9] Undo printf-style debugging. --- MachineLearning/src/Structure.qs | 1 - 1 file changed, 1 deletion(-) diff --git a/MachineLearning/src/Structure.qs b/MachineLearning/src/Structure.qs index c1c8a0810f6..80f31432225 100644 --- a/MachineLearning/src/Structure.qs +++ b/MachineLearning/src/Structure.qs @@ -51,7 +51,6 @@ namespace Microsoft.Quantum.MachineLearning { : (Unit) is Adj + Ctl { for (gate in model::Structure) { if (gate::ParameterIndex < Length(model::Parameters)) { - Message($"axis {gate::Axis} parameter {model::Parameters}[{gate::ParameterIndex}] target {gate::TargetIndex}"); let input = (gate::Axis, model::Parameters[gate::ParameterIndex], qubits[gate::TargetIndex]); if (IsEmpty(gate::ControlIndices)) { // Uncontrolled rotation of target From d9ff81615f2a2c46d07afc75693a90abf24b23e6 Mon Sep 17 00:00:00 2001 From: Chris Granade Date: Tue, 11 Feb 2020 14:05:19 -0800 Subject: [PATCH 9/9] Update MachineLearning/tests/ClassificationTests.qs Co-Authored-By: Alan Geller --- MachineLearning/tests/ClassificationTests.qs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MachineLearning/tests/ClassificationTests.qs b/MachineLearning/tests/ClassificationTests.qs index 2b41e0cbc5c..96e9c4ce309 100644 --- a/MachineLearning/tests/ClassificationTests.qs +++ b/MachineLearning/tests/ClassificationTests.qs @@ -18,7 +18,7 @@ namespace Microsoft.Quantum.MachineLearning.Tests { AllEqualityFactI( ML.InferredLabels(0.25, [0.23, 0.26, 0.1]), [0, 1, 0], - "InferredLabels returned at least wrong class." + "InferredLabels returned at least one wrong class." ); }