diff --git a/Chemistry/tests/SystemTests/Molecules/MoleculeTestHelpers.cs b/Chemistry/tests/SystemTests/Molecules/MoleculeTestHelpers.cs index 4773c2f4b08..a408474bfad 100644 --- a/Chemistry/tests/SystemTests/Molecules/MoleculeTestHelpers.cs +++ b/Chemistry/tests/SystemTests/Molecules/MoleculeTestHelpers.cs @@ -32,7 +32,7 @@ namespace SystemTests.Molecules public static class Helper { - private static readonly SHA256Managed hashMethod = new SHA256Managed(); + private static readonly SHA256 hashMethod = SHA256.Create(); /// /// Returns a seed to use for the test run based on the class diff --git a/MachineLearning/src/Training.qs b/MachineLearning/src/Training.qs index 807f27f4d62..56d6df0953c 100644 --- a/MachineLearning/src/Training.qs +++ b/MachineLearning/src/Training.qs @@ -87,8 +87,7 @@ namespace Microsoft.Quantum.MachineLearning { trainingSchedule : SamplingSchedule, validationSchedule : SamplingSchedule ) : (SequentialModel, Int) { - mutable bestSoFar = Default() - w/ Structure <- (Head(models))::Structure; + mutable bestSoFar = SequentialModel((Head(models))::Structure, [], 0.0); mutable bestValidation = Length(samples) + 1; let features = Mapped(_Features, samples); diff --git a/MachineLearning/tests/StructureTests.qs b/MachineLearning/tests/StructureTests.qs index f62c2c2726f..57f7298bf00 100644 --- a/MachineLearning/tests/StructureTests.qs +++ b/MachineLearning/tests/StructureTests.qs @@ -10,33 +10,20 @@ namespace Microsoft.Quantum.MachineLearning.Tests { @Test("QuantumSimulator") function NQubitsRequiredFact() : Unit { - let model = Default() - w/ Structure <- [ - ML.ControlledRotation((3, [7, 9]), PauliX, 0), - ML.ControlledRotation((8, []), PauliY, 1) - ]; + let model = ML.SequentialModel([ + ML.ControlledRotation((3, [7, 9]), PauliX, 0), + ML.ControlledRotation((8, []), PauliY, 1) + ], [], 0.); let actual = ML.NQubitsRequired(model); EqualityFactI(actual, 10, "Wrong output from NQubitsRequired."); } 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 - ]; + return ML.SequentialModel([ + ML.ControlledRotation((2, [0]), PauliX, 0), + ML.ControlledRotation((0, [1, 2]), PauliZ, 1)], + [1.234, 2.345], + 0.0); } operation ApplyExampleModelManually(register : Qubit[]) : Unit is Adj + Ctl { @@ -64,23 +51,9 @@ namespace Microsoft.Quantum.MachineLearning.Tests { Fact(All(EqualCR, Zipped( ML.LocalRotationsLayer(3, PauliY), [ - Default() - w/ TargetIndex <- 0 - w/ ControlIndices <- [] - w/ Axis <- PauliY - w/ ParameterIndex <- 0, - - Default() - w/ TargetIndex <- 1 - w/ ControlIndices <- [] - w/ Axis <- PauliY - w/ ParameterIndex <- 1, - - Default() - w/ TargetIndex <- 2 - w/ ControlIndices <- [] - w/ Axis <- PauliY - w/ ParameterIndex <- 2 + ML.ControlledRotation((0, []), PauliY, 0), + ML.ControlledRotation((1, []), PauliY, 1), + ML.ControlledRotation((2, []), PauliY, 2) ] )), "LocalRotationsLayer returned wrong output."); } @@ -90,23 +63,9 @@ namespace Microsoft.Quantum.MachineLearning.Tests { Fact(All(EqualCR, Zipped( ML.PartialRotationsLayer([4, 5, 6], PauliY), [ - Default() - w/ TargetIndex <- 4 - w/ ControlIndices <- [] - w/ Axis <- PauliY - w/ ParameterIndex <- 0, - - Default() - w/ TargetIndex <- 5 - w/ ControlIndices <- [] - w/ Axis <- PauliY - w/ ParameterIndex <- 1, - - Default() - w/ TargetIndex <- 6 - w/ ControlIndices <- [] - w/ Axis <- PauliY - w/ ParameterIndex <- 2 + ML.ControlledRotation((4, []), PauliY, 0), + ML.ControlledRotation((5, []), PauliY, 1), + ML.ControlledRotation((6, []), PauliY, 2) ] )), "PartialRotationsLayer returned wrong output."); } @@ -116,23 +75,9 @@ namespace Microsoft.Quantum.MachineLearning.Tests { Fact(All(EqualCR, Zipped( 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 + ML.ControlledRotation((0, [2]), PauliX, 0), + ML.ControlledRotation((1, [0]), PauliX, 1), + ML.ControlledRotation((2, [1]), PauliX, 2) ] )), "CyclicEntanglingLayer returned wrong output."); } @@ -141,46 +86,19 @@ namespace Microsoft.Quantum.MachineLearning.Tests { 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 + ML.ControlledRotation((0, [2]), PauliX, 0), + ML.ControlledRotation((1, [0]), PauliX, 1) ], [ - Default() - w/ TargetIndex <- 2 - w/ ControlIndices <- [1] - w/ Axis <- PauliZ - w/ ParameterIndex <- 0 + ML.ControlledRotation((2, [1]), PauliZ, 0) ] ]); Fact(All(EqualCR, Zipped( combined, [ - 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 + ML.ControlledRotation((0, [2]), PauliX, 0), + ML.ControlledRotation((1, [0]), PauliX, 1), + ML.ControlledRotation((2, [1]), PauliZ, 2) ] )), "CombinedStructure returned wrong output."); } diff --git a/Standard/src/Arrays/Arrays.qs b/Standard/src/Arrays/Arrays.qs index 7506b9efcbd..96dca783ce0 100644 --- a/Standard/src/Arrays/Arrays.qs +++ b/Standard/src/Arrays/Arrays.qs @@ -211,23 +211,23 @@ namespace Microsoft.Quantum.Arrays { let nSliced = Length(remove); let nElements = Length(array); + if nElements - nSliced <= 0 { + return []; + } + //Would be better with sort function //Or way to add elements to array - mutable arrayKeep = [0, size = nElements]; - mutable sliced = [Default<'T>(), size = nElements - nSliced]; + mutable arrayKeep = SequenceI(0, nElements - 1); + mutable sliced = [array[0], size = nElements - nSliced]; mutable counter = 0; - for idx in 0 .. nElements - 1 { - set arrayKeep w/= idx <- idx; - } - - for idx in 0 .. nSliced - 1 { - set arrayKeep w/= remove[idx] <- -1; + for idx in remove { + set arrayKeep w/= idx <- -1; } - for idx in 0 .. nElements - 1 { - if (arrayKeep[idx] >= 0) { - set sliced w/= counter <- array[arrayKeep[idx]]; + for idx in arrayKeep { + if idx >= 0 { + set sliced w/= counter <- array[idx]; set counter += 1; } } @@ -331,16 +331,16 @@ namespace Microsoft.Quantum.Arrays { /// let split = Partitioned([2,2], [1,5,3,7]); /// ``` function Partitioned<'T>(nElements: Int[], arr: 'T[]) : 'T[][] { - mutable output = [Default<'T[]>(), size = Length(nElements) + 1]; + mutable output = [[], size = Length(nElements) + 1]; mutable currIdx = 0; for idx in IndexRange(nElements) { - if(currIdx + nElements[idx] > Length(arr)) { + if currIdx + nElements[idx] > Length(arr) { fail "Partitioned argument out of bounds."; } - set output w/= idx <- arr[currIdx..currIdx + nElements[idx]-1]; + set output w/= idx <- arr[currIdx..currIdx + nElements[idx] - 1]; set currIdx = currIdx + nElements[idx]; } - set output w/= Length(nElements) <- arr[currIdx..Length(arr)-1]; + set output w/= Length(nElements) <- arr[currIdx..Length(arr) - 1]; return output; } @@ -483,7 +483,7 @@ namespace Microsoft.Quantum.Arrays { /// TupleArrayAsNestedArray([(2, 3), (4, 5)]); /// ``` function TupleArrayAsNestedArray<'T>(tupleList : ('T, 'T)[]) : 'T[][] { - mutable newArray = [Default<'T[]>(), size = Length(tupleList)]; + mutable newArray = [[], size = Length(tupleList)]; for idx in IndexRange(tupleList) { let (tupleLeft, tupleRight) = tupleList[idx]; set newArray w/= idx <- [tupleLeft, tupleRight]; diff --git a/Standard/src/Arrays/DrawMany.qs b/Standard/src/Arrays/DrawMany.qs index 68eece54e0c..295c2dd4727 100644 --- a/Standard/src/Arrays/DrawMany.qs +++ b/Standard/src/Arrays/DrawMany.qs @@ -32,8 +32,13 @@ namespace Microsoft.Quantum.Arrays { /// - Microsoft.Quantum.Canon.Repeat operation DrawMany<'TInput, 'TOutput>(op : ('TInput => 'TOutput), nSamples : Int, input : 'TInput) : 'TOutput[] { - mutable outputs = [Default<'TOutput>(), size = nSamples]; - for idx in 0..nSamples - 1 { + if nSamples == 0 { + return []; + } + + let first = op(input); + mutable outputs = [first, size = nSamples]; + for idx in 1..nSamples - 1 { set outputs w/= idx <- op(input); } return outputs; diff --git a/Standard/src/Arrays/Interleaved.qs b/Standard/src/Arrays/Interleaved.qs index f944bbc919c..540e722d1f3 100644 --- a/Standard/src/Arrays/Interleaved.qs +++ b/Standard/src/Arrays/Interleaved.qs @@ -43,8 +43,12 @@ namespace Microsoft.Quantum.Arrays { Fact(lFirst >= lSecond and lFirst - lSecond <= 1, "Array `first` is either of same size as `second`, or has one more element"); - return [Default<'T>(), size = lFirst + lSecond] - w/ 0..2..(lFirst + lSecond - 1) <- first - w/ 1..2..(lFirst + lSecond - 1) <- second; + if lFirst == 0 { + return []; + } else { + return [first[0], size = lFirst + lSecond] + w/ 0..2..(lFirst + lSecond - 1) <- first + w/ 1..2..(lFirst + lSecond - 1) <- second; + } } } diff --git a/Standard/src/Arrays/Map.qs b/Standard/src/Arrays/Map.qs index 989aeb679a6..f3ff61f0ce0 100644 --- a/Standard/src/Arrays/Map.qs +++ b/Standard/src/Arrays/Map.qs @@ -32,13 +32,16 @@ namespace Microsoft.Quantum.Arrays { /// # See Also /// - Microsoft.Quantum.Arrays.ForEach function Mapped<'T, 'U> (mapper : ('T -> 'U), array : 'T[]) : 'U[] { - mutable resultArray = [Default<'U>(), size = Length(array)]; - - for idxElement in IndexRange(array) { - set resultArray w/= idxElement <- mapper(array[idxElement]); + let length = Length(array); + if length == 0 { + return []; } - - return resultArray; + let first = mapper(array[0]); + mutable retval = [first, size = length]; + for idx in 1..length - 1 { + set retval w/= idx <- mapper(array[idx]); + } + return retval; } /// # Summary @@ -80,13 +83,16 @@ namespace Microsoft.Quantum.Arrays { /// # See Also /// - Microsoft.Quantum.Arrays.Mapped function MappedByIndex<'T, 'U> (mapper : ((Int, 'T) -> 'U), array : 'T[]) : 'U[] { - mutable resultArray = [Default<'U>(), size = Length(array)]; - - for idxElement in IndexRange(array) { - set resultArray w/= idxElement <- mapper(idxElement, array[idxElement]); + let length = Length(array); + if length == 0 { + return []; } - - return resultArray; + let first = mapper(0, array[0]); + mutable retval = [first, size = length]; + for idx in 1..length - 1 { + set retval w/= idx <- mapper(idx, array[idx]); + } + return retval; } /// # Summary @@ -127,10 +133,13 @@ namespace Microsoft.Quantum.Arrays { let end = RangeEnd(range); if ((end - start) / step >= 0) { let nTerms = (end - start) / step + 1; - mutable resultArray = [Default<'T>(), size = nTerms]; + let first = mapper(start); + mutable resultArray = [first, size = nTerms]; mutable idxElement = 0; for elem in range { - set resultArray w/= idxElement <- mapper(elem); + if idxElement != 0 { + set resultArray w/= idxElement <- mapper(elem); + } set idxElement += 1; } return resultArray; @@ -220,13 +229,16 @@ namespace Microsoft.Quantum.Arrays { /// # See Also /// - Microsoft.Quantum.Arrays.Mapped operation ForEach<'T, 'U> (action : ('T => 'U), array : 'T[]) : 'U[] { - mutable resultArray = [Default<'U>(), size = Length(array)]; - - for idxElement in IndexRange(array) { - set resultArray w/= idxElement <- action(array[idxElement]); + let length = Length(array); + if length == 0 { + return []; } - - return resultArray; + let first = action(array[0]); + mutable retval = [first, size = length]; + for idx in 1..length - 1 { + set retval w/= idx <- action(array[idx]); + } + return retval; } } diff --git a/Standard/src/Arrays/Reductions.qs b/Standard/src/Arrays/Reductions.qs index 4603bc9d141..34e43624ede 100644 --- a/Standard/src/Arrays/Reductions.qs +++ b/Standard/src/Arrays/Reductions.qs @@ -43,7 +43,8 @@ namespace Microsoft.Quantum.Arrays { /// ``` function CumulativeFolded<'State, 'T>(fn : (('State, 'T) -> 'State), state : 'State, array : 'T[]) : 'State[] { mutable current = state; - mutable result = [Default<'State>(), size = Length(array)]; + // initialize with current, and then overwrite in loop + mutable result = [current, size = Length(array)]; for (i, elem) in Enumerated(array) { set current = fn(current, elem); diff --git a/Standard/src/Arrays/Subarray.qs b/Standard/src/Arrays/Subarray.qs index 1071dd4a104..f0a0dad9645 100644 --- a/Standard/src/Arrays/Subarray.qs +++ b/Standard/src/Arrays/Subarray.qs @@ -47,9 +47,14 @@ namespace Microsoft.Quantum.Arrays { /// ``` function Subarray<'T> (indices : Int[], array : 'T[]) : 'T[] { let nSliced = Length(indices); - mutable sliced = [Default<'T>(), size = nSliced]; - for idx in 0 .. nSliced - 1 { + if nSliced == 0 { + return []; + } + + mutable sliced = [array[indices[0]], size = nSliced]; + + for idx in 1 .. nSliced - 1 { set sliced w/= idx <- array[indices[idx]]; } diff --git a/Standard/src/Arrays/Windows.qs b/Standard/src/Arrays/Windows.qs index cf8f9551c6d..34837baae6f 100644 --- a/Standard/src/Arrays/Windows.qs +++ b/Standard/src/Arrays/Windows.qs @@ -37,7 +37,7 @@ namespace Microsoft.Quantum.Arrays { return []; } - mutable result = [Default<'T[]>(), size = n + 1 - size]; + mutable result = [[], size = n + 1 - size]; for i in 0..n - size { set result w/= i <- array[i..i + size - 1]; diff --git a/Standard/src/Arrays/Zip.qs b/Standard/src/Arrays/Zip.qs index 10965085658..b6634c14b52 100644 --- a/Standard/src/Arrays/Zip.qs +++ b/Standard/src/Arrays/Zip.qs @@ -2,6 +2,7 @@ // Licensed under the MIT License. namespace Microsoft.Quantum.Arrays { + open Microsoft.Quantum.Canon; open Microsoft.Quantum.Math; /// # Summary @@ -40,9 +41,14 @@ namespace Microsoft.Quantum.Arrays { let nElements = Length(left) < Length(right) ? Length(left) | Length(right); - mutable output = [Default<('T, 'U)>(), size = nElements]; - for idxElement in 0 .. nElements - 1 { + if nElements == 0 { + return []; + } + + mutable output = [(left[0], right[0]), size = nElements]; + + for idxElement in 1 .. nElements - 1 { set output w/= idxElement <- (left[idxElement], right[idxElement]); } @@ -79,9 +85,14 @@ namespace Microsoft.Quantum.Arrays { /// - Microsoft.Quantum.Arrays.Zipped4 function Zipped3<'T1, 'T2, 'T3> (first : 'T1[], second : 'T2[], third : 'T3[]) : ('T1, 'T2, 'T3)[] { let nElements = Min([Length(first), Length(second), Length(third)]); - mutable output = [Default<('T1, 'T2, 'T3)>(), size = nElements]; - for idxElement in 0 .. nElements - 1 { + if nElements == 0 { + return []; + } + + mutable output = [(first[0], second[0], third[0]), size = nElements]; + + for idxElement in 1 .. nElements - 1 { set output w/= idxElement <- (first[idxElement], second[idxElement], third[idxElement]); } @@ -122,9 +133,14 @@ namespace Microsoft.Quantum.Arrays { /// - Microsoft.Quantum.Arrays.Zipped3 function Zipped4<'T1, 'T2, 'T3, 'T4> (first : 'T1[], second : 'T2[], third : 'T3[], fourth : 'T4[]) : ('T1, 'T2, 'T3, 'T4)[] { let nElements = Min([Length(first), Length(second), Length(third), Length(fourth)]); - mutable output = [Default<('T1, 'T2, 'T3, 'T4)>(), size = nElements]; - for idxElement in 0 .. nElements - 1 { + if nElements == 0 { + return []; + } + + mutable output = [(first[0], second[0], third[0], fourth[0]), size = nElements]; + + for idxElement in 1 .. nElements - 1 { set output w/= idxElement <- (first[idxElement], second[idxElement], third[idxElement], fourth[idxElement]); } @@ -162,9 +178,14 @@ namespace Microsoft.Quantum.Arrays { /// - Microsoft.Quantum.Arrays.Zipped function Unzipped<'T, 'U>(arr : ('T, 'U)[]) : ('T[], 'U[]) { let nElements = Length(arr); - mutable first = [Default<'T>(), size = nElements]; - mutable second = [Default<'U>(), size = nElements]; - for idxElement in 0 .. nElements - 1 { + + if nElements == 0 { + return ([], []); + } + + mutable first = [Fst(arr[0]), size = nElements]; + mutable second = [Snd(arr[0]), size = nElements]; + for idxElement in 1 .. nElements - 1 { let (left, right) = arr[idxElement]; set first w/= idxElement <- left; set second w/= idxElement <- right; diff --git a/Standard/tests/SimulatorTestTargets.cs b/Standard/tests/SimulatorTestTargets.cs index c49bea1de67..12f2d1dce12 100644 --- a/Standard/tests/SimulatorTestTargets.cs +++ b/Standard/tests/SimulatorTestTargets.cs @@ -214,6 +214,6 @@ private void LogSimulatorSeed(TestOperation opData, QuantumSimulator sim) return seed; } - private static readonly SHA256Managed hashMethod = new SHA256Managed(); + private static readonly SHA256 hashMethod = SHA256.Create(); } }