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
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
40 changes: 35 additions & 5 deletions Standard/src/Arrays/Arrays.qs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ namespace Microsoft.Quantum.Arrays {
return array[0 .. Length(array) - 2];
}

internal function Lookup<'T> (array : 'T[], index : Int) : 'T {
return array[index];
}

/// # Summary
/// Given an array, returns a function which returns elements of that
/// array.
Expand All @@ -90,7 +86,7 @@ namespace Microsoft.Quantum.Arrays {
/// where functions are used to avoid the need to record an entire array
/// in memory.
function LookupFunction<'T> (array : 'T[]) : (Int -> 'T) {
return Lookup(array, _);
return ElementAt(_, array);
}

/// # Summary
Expand Down Expand Up @@ -129,6 +125,40 @@ namespace Microsoft.Quantum.Arrays {
return array[0];
}

/// # Summary
/// Returns a tuple of first and all remaining elements of the array.
///
/// # Type Parameters
/// ## 'A
/// The type of the array elements.
///
/// # Input
/// ## array
/// An array with at least one element.
///
/// # Output
/// A tuple of first and all remaining elements of the array.
function HeadAndRest<'A>(array : 'A[]) : ('A, 'A[]) {
return (Head(array), Rest(array));
}

/// # Summary
/// Returns a tuple of all but one and the last element of the array.
///
/// # Type Parameters
/// ## 'A
/// The type of the array elements.
///
/// # Input
/// ## array
/// An array with at least one element.
///
/// # Output
/// A tuple of all but one and the last element of the array.
function MostAndTail<'A>(array : 'A[]) : ('A[], 'A) {
return (Most(array), Tail(array));
}

/// # Summary
/// Creates an array of given length with all elements equal to given value.
///
Expand Down
45 changes: 43 additions & 2 deletions Standard/src/Arrays/Filter.qs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.Quantum.Arrays {
Expand Down Expand Up @@ -40,7 +40,7 @@ namespace Microsoft.Quantum.Arrays {
/// }
/// ```
/// The outcome one should expect from this example will be an array of numbers greater than 5.
function Filtered<'T> (predicate : ('T -> Bool), array : 'T[]) : 'T[] {
function Filtered<'T>(predicate : ('T -> Bool), array : 'T[]) : 'T[] {
mutable totalFound = 0;
mutable idxArray = new Int[Length(array)];

Expand Down Expand Up @@ -80,4 +80,45 @@ namespace Microsoft.Quantum.Arrays {
);
}

/// # Summary
/// Given an array and a predicate that is defined
/// for the elements of the array, returns the number of elements
/// an array that consists of those elements that satisfy the predicate.
///
/// # Remarks
/// The function is defined for generic types, i.e., whenever we have
/// an array `'T[]` and a predicate `'T -> Bool` we can filter elements.
///
/// # Type Parameters
/// ## 'T
/// The type of `array` elements.
///
/// # Input
/// ## predicate
/// A function from `'T` to Boolean that is used to filter elements.
/// ## array
/// An array of elements over `'T`.
///
/// # Output
/// The number of elements in `array` that satisfy the predicate.
///
/// # Example
/// The following code demonstrates the "Count" function.
/// A predicate is defined using the @"microsoft.quantum.logical.greaterthani" function:
/// ```Q#
/// let predicate = GreaterThanI(_, 5);
/// let count = Count(predicate, [2, 5, 9, 1, 8]);
/// // count = 2
/// ```
function Count<'T>(predicate : ('T -> Bool), array : 'T[]) : Int {
mutable totalFound = 0;

for (element in array) {
if (predicate(element)) {
set totalFound += 1;
}
}

return totalFound;
}
}
50 changes: 50 additions & 0 deletions Standard/src/Arrays/Interleaved.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.Quantum.Arrays {
open Microsoft.Quantum.Diagnostics;

/// # Summary
/// Interleaves two arrays of (almost) same size.
///
/// # Description
/// This function returns the interleaving of two arrays, starting
/// with the first element from the first array, then the first
/// element from the second array, and so on.
///
/// The first array must either be
/// of the same length as the second one, or can have one more element.
///
/// # Type Parameters
/// ## 'T
/// The type of each element of `first` and `second`.
///
/// # Input
/// ## first
/// The first array to be interleaved.
///
/// ## second
/// The second array to be interleaved.
///
/// # Output
/// Interleaved array
///
/// # Example
/// ```Q#
/// // same as int1 = [1, -1, 2, -2, 3, -3]
/// let int1 = Interleaved([1, 2, 3], [-1, -2, -3])
///
/// // same as int2 = [false, true, false, true, false]
/// let int2 = Interleaved(ConstantArray(3, false), ConstantArray(2, true));
/// ```
function Interleaved<'T>(first : 'T[], second : 'T[]) : 'T[] {
let lFirst = Length(first);
let lSecond = Length(second);

Fact(lFirst >= lSecond and lFirst - lSecond <= 1, "Array `first` is either of same size as `second`, or has one more element");

return new 'T[lFirst + lSecond]
w/ 0..2..(lFirst + lSecond - 1) <- first
w/ 1..2..(lFirst + lSecond - 1) <- second;
}
}
104 changes: 104 additions & 0 deletions Standard/src/Arrays/Map.qs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

namespace Microsoft.Quantum.Arrays {
open Microsoft.Quantum.Math;

/// # Summary
/// Given an array and a function that is defined
Expand Down Expand Up @@ -86,6 +87,109 @@ namespace Microsoft.Quantum.Arrays {
return resultArray;
}

/// # Summary
/// Given a range and a function that takes an integer as input,
/// returns a new array that consists
/// of the images of the range values under the function.
///
/// # Remarks
/// The function is defined for generic types, i.e., whenever we have
/// a function `mapper: Int -> 'T` we can map the values
/// of the range and produce an array of type `'T[]`.
///
/// # Type Parameters
/// ## 'T
/// The result type of the `mapper` function.
///
/// # Input
/// ## mapper
/// A function from `Int` to `'T` that is used to map range values.
/// ## range
/// A range of integers.
///
/// # Output
/// An array `'T[]` of elements that are mapped by the `mapper` function.
///
/// # Example
/// This example adds 1 to a range of even numbers:
/// ```Q#
/// let numbers = MappedOverRange(PlusI(1, _), 0..2..10);
/// // numbers = [1, 3, 5, 7, 9, 11]
/// ```
///
/// # See Also
/// - Microsoft.Quantum.Arrays.Mapped
function MappedOverRange<'T> (mapper : (Int -> 'T), range : Range) : 'T[] {
let start = RangeStart(range);
let step = RangeStep(range);
let end = RangeEnd(range);
if ((end - start) / step >= 0) {
let nTerms = (end - start) / step + 1;
mutable resultArray = new 'T[nTerms];
mutable idxElement = 0;
for (elem in range) {
set resultArray w/= idxElement <- mapper(elem);
set idxElement += 1;
}
return resultArray;
} else {
return new 'T[0];
}
}

/// # Summary
/// Given an array and a function that maps an array element to some output
/// array, returns the concatenated output arrays for each array element.
///
/// # Type Parameters
/// ## 'TInput
/// The type of `array` elements.
/// ## 'TOutput
/// The `mapper` function returns arrays of this type.
///
/// # Input
/// ## mapper
/// A function from `'TInput` to `'TOutput[]` that is used to map array elements.
/// ## array
/// An array of elements.
///
/// # Output
/// An array of `'TOutput[]` which is the concatenation of all arrays generated by
/// the mapping function.
///
/// # Example
/// ```Q#
/// let Numbers = SequenceI(1, _); // generates numbers starting from 1
/// let values = FlatMapped(Numbers, [1, 2, 3]);
/// // values = [1, 1, 2, 1, 2, 3]
/// ```
function FlatMapped<'TInput, 'TOutput>(mapper : ('TInput -> 'TOutput[]), array : 'TInput[]) : 'TOutput[] {
return Fold(PlusA<'TOutput>, new 'TOutput[0], Mapped(mapper, array));
}

/// # Summary
/// Given an array of arrays, returns the concatenation of all arrays.
///
/// # Type Parameters
/// ## 'T
/// The type of `array` elements.
///
/// # Input
/// ## arrays
/// Array of arrays.
///
/// # Output
/// Concatenation of all arrays.
///
/// # Example
/// ```Q#
/// let flattened = Flattened([[1, 2], [3], [4, 5, 6]]);
/// // flattened = [1, 2, 3, 4, 5, 6]
/// ```
function Flattened<'T>(arrays : 'T[][]): 'T[] {
return Fold(PlusA<'T>, new 'T[0], arrays);
}

/// # Summary
/// Given an array and an operation that is defined
/// for the elements of the array, returns a new array that consists
Expand Down
Loading