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 180
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Improvements to Microsoft.Quantum.Arrays #313
Copy link
Copy link
Closed
Labels
Kind-EnhancementNew feature or requestNew feature or requestPkg-StandardIssue relates to the Microsoft.Quantum.Standard package.Issue relates to the Microsoft.Quantum.Standard package.Status-NeedsApiReviewThis PR requires an API review before merging in.This PR requires an API review before merging in.
Description
Abstract
There is a close connection between the iteration over classical values, and generalizing "circuit"-like patterns into high-level code. In Q#, we use exploit this connection with the Microsoft.Quantum.Arrays namespace to make it easier express high-level patterns. We have noted from user feedback, however, that some common patterns are difficult to express using existing array functions, or that users may not be able to discover needed functionality.
The improvements proposed here empower users to make better use of high-level quantum development in Q# by making it easier to iterate over collections of values, such as Qubit[], and to use them to write out complex algorithmic patterns efficiently.
Proposed deprecations and renamings
- namespace Microsoft.Quantum.Arrays
function Exclude<'T>(remove : Int[], array : 'T[]) : 'T[];
function Excluding<'T>(remove : Int[], array : 'T[]) : 'T[];function Zip<'T, 'U>(left : 'T[], right : 'U[]) : ('T, 'U)[];
function Zipped<'T, 'U>(left : 'T[], right : 'U[]) : ('T, 'U)[];
Proposed additions
- namespace Microsoft.Quantum.Arrays
- Accessor functions
- Reductions and combinators
function CumulativeFolded<'State, 'TInput>(fn : ('State, 'TInput -> 'State), initialState : 'State, arr : 'TInput[]) : 'State[];function FlatMapped<'TInput, 'TOutput>(mapper : 'TInput -> 'TOutput[], arr : 'TInput[]) : 'TOutput[];
Returns (mapper(arr[0]) + mapper(arr[1]) + ... + mapper(arr[n - 1])).function MappedOverRange<'T>(fn : Int -> 'T, range : Range) : 'T[];
Returns the value offnat each element ofrange.function Flattened<'T>(arr : 'T[][]) : 'T[];
Returns the sum over array concatenation ofarr; i.e.:arr[0] + arr[1] + ... + arr[Length(arr) - 1]. See comment by @msoeken below.
- Multidimensional array functions
- Facts
- Conversion
- Other functions
function Count<'T>(predicate : ('T -> Bool), array : 'T[]) : Intfunction EmptyArray<'T>() : 'T[];(see Default values for qubits and callables in uninitialized arrays cause runtime errors qsharp-compiler#589)function Interleaved<'T>(first : 'T[], second : 'T[]) : 'T[];
Given two arrays, returns a new array[first[0], second[0], first[1], second[1], ...]function Prefixes<'T>(arr : 'T[]) : 'T[][];
Given an array, returns a new array of all prefixes; i.e.:[[arr[0]], [arr[0], arr[1]], ... arr].function Windows<'T>(size : Int, arr : 'T[]) : 'T[][];
Returns[[arr[0], arr[1], ..., arr[size - 1]], [arr[1], arr[2], ..., arr[size]], ..., [arr[n - size + 1], arr[n - size + 2], ..., arr[n - 1]]].function Unique<'T>(arr : 'T[]) : 'T[];function Unzipped<'T, 'U>(arr : ('T, 'U)[]) : ('T[], 'U[]);- Unzipped3, Unzipped4, etc.
- namespace Microsoft.Quantum.Canon
operation ApplyTo{Head, Tail, Most, Rest}{,A,C,CA}
Future considerations and language feature dependencies
- Functions like Transposed or ColumnAt may need to be redesigned to work well with 𝑛-d arrays.
Other comments
- @guenp has suggested that Fold is a reasonable name if there's a way to find it in docs from searching for "reduce." The input name "folder" is dubious, though, regardless; duplicate meaning that can be confused.
Metadata
Metadata
Assignees
Labels
Kind-EnhancementNew feature or requestNew feature or requestPkg-StandardIssue relates to the Microsoft.Quantum.Standard package.Issue relates to the Microsoft.Quantum.Standard package.Status-NeedsApiReviewThis PR requires an API review before merging in.This PR requires an API review before merging in.