Adapting to new array syntax. (#529)
This commit is contained in:
Родитель
fed0fb5224
Коммит
74ef872165
|
@ -215,7 +215,7 @@ namespace Microsoft.Quantum.Arrays {
|
|||
//Would be better with sort function
|
||||
//Or way to add elements to array
|
||||
mutable arrayKeep = [0, size = nElements];
|
||||
mutable sliced = new 'T[nElements - nSliced];
|
||||
mutable sliced = [Default<'T>(), size = nElements - nSliced];
|
||||
mutable counter = 0;
|
||||
|
||||
for idx in 0 .. nElements - 1 {
|
||||
|
@ -333,7 +333,7 @@ namespace Microsoft.Quantum.Arrays {
|
|||
/// let split = Partitioned([2,2], [1,5,3,7]);
|
||||
/// ```
|
||||
function Partitioned<'T>(nElements: Int[], arr: 'T[]) : 'T[][] {
|
||||
mutable output = new 'T[][Length(nElements) + 1];
|
||||
mutable output = [Default<'T[]>(), size = Length(nElements) + 1];
|
||||
mutable currIdx = 0;
|
||||
for idx in IndexRange(nElements) {
|
||||
if(currIdx + nElements[idx] > Length(arr)) {
|
||||
|
@ -482,7 +482,7 @@ namespace Microsoft.Quantum.Arrays {
|
|||
/// TupleArrayAsNestedArray([(2, 3), (4, 5)]);
|
||||
/// ```
|
||||
function TupleArrayAsNestedArray<'T>(tupleList : ('T, 'T)[]) : 'T[][] {
|
||||
mutable newArray = new 'T[][Length(tupleList)];
|
||||
mutable newArray = [Default<'T[]>(), size = Length(tupleList)];
|
||||
for idx in IndexRange(tupleList) {
|
||||
let (tupleLeft, tupleRight) = tupleList[idx];
|
||||
set newArray w/= idx <- [tupleLeft, tupleRight];
|
||||
|
|
|
@ -32,7 +32,7 @@ namespace Microsoft.Quantum.Arrays {
|
|||
/// - Microsoft.Quantum.Canon.Repeat
|
||||
operation DrawMany<'TInput, 'TOutput>(op : ('TInput => 'TOutput), nSamples : Int, input : 'TInput)
|
||||
: 'TOutput[] {
|
||||
mutable outputs = new 'TOutput[nSamples];
|
||||
mutable outputs = [Default<'TOutput>(), size = nSamples];
|
||||
for idx in 0..nSamples - 1 {
|
||||
set outputs w/= idx <- op(input);
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ 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 new 'T[lFirst + lSecond]
|
||||
return [Default<'T>(), size = lFirst + lSecond]
|
||||
w/ 0..2..(lFirst + lSecond - 1) <- first
|
||||
w/ 1..2..(lFirst + lSecond - 1) <- second;
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ namespace Microsoft.Quantum.Arrays {
|
|||
/// # See Also
|
||||
/// - Microsoft.Quantum.Arrays.ForEach
|
||||
function Mapped<'T, 'U> (mapper : ('T -> 'U), array : 'T[]) : 'U[] {
|
||||
mutable resultArray = new 'U[Length(array)];
|
||||
mutable resultArray = [Default<'U>(), size = Length(array)];
|
||||
|
||||
for idxElement in IndexRange(array) {
|
||||
set resultArray w/= idxElement <- mapper(array[idxElement]);
|
||||
|
@ -81,7 +81,7 @@ namespace Microsoft.Quantum.Arrays {
|
|||
/// # See Also
|
||||
/// - Microsoft.Quantum.Arrays.Mapped
|
||||
function MappedByIndex<'T, 'U> (mapper : ((Int, 'T) -> 'U), array : 'T[]) : 'U[] {
|
||||
mutable resultArray = new 'U[Length(array)];
|
||||
mutable resultArray = [Default<'U>(), size = Length(array)];
|
||||
|
||||
for idxElement in IndexRange(array) {
|
||||
set resultArray w/= idxElement <- mapper(idxElement, array[idxElement]);
|
||||
|
@ -128,7 +128,7 @@ namespace Microsoft.Quantum.Arrays {
|
|||
let end = RangeEnd(range);
|
||||
if ((end - start) / step >= 0) {
|
||||
let nTerms = (end - start) / step + 1;
|
||||
mutable resultArray = new 'T[nTerms];
|
||||
mutable resultArray = [Default<'T>(), size = nTerms];
|
||||
mutable idxElement = 0;
|
||||
for elem in range {
|
||||
set resultArray w/= idxElement <- mapper(elem);
|
||||
|
@ -221,7 +221,7 @@ namespace Microsoft.Quantum.Arrays {
|
|||
/// # See Also
|
||||
/// - Microsoft.Quantum.Arrays.Mapped
|
||||
operation ForEach<'T, 'U> (action : ('T => 'U), array : 'T[]) : 'U[] {
|
||||
mutable resultArray = new 'U[Length(array)];
|
||||
mutable resultArray = [Default<'U>(), size = Length(array)];
|
||||
|
||||
for idxElement in IndexRange(array) {
|
||||
set resultArray w/= idxElement <- action(array[idxElement]);
|
||||
|
|
|
@ -43,7 +43,7 @@ namespace Microsoft.Quantum.Arrays {
|
|||
/// ```
|
||||
function CumulativeFolded<'State, 'T>(fn : (('State, 'T) -> 'State), state : 'State, array : 'T[]) : 'State[] {
|
||||
mutable current = state;
|
||||
mutable result = new 'State[Length(array)];
|
||||
mutable result = [Default<'State>(), size = Length(array)];
|
||||
|
||||
for (i, elem) in Enumerated(array) {
|
||||
set current = fn(current, elem);
|
||||
|
|
|
@ -47,7 +47,7 @@ namespace Microsoft.Quantum.Arrays {
|
|||
/// ```
|
||||
function Subarray<'T> (indices : Int[], array : 'T[]) : 'T[] {
|
||||
let nSliced = Length(indices);
|
||||
mutable sliced = new 'T[nSliced];
|
||||
mutable sliced = [Default<'T>(), size = nSliced];
|
||||
|
||||
for idx in 0 .. nSliced - 1 {
|
||||
set sliced w/= idx <- array[indices[idx]];
|
||||
|
|
|
@ -37,7 +37,7 @@ namespace Microsoft.Quantum.Arrays {
|
|||
return [];
|
||||
}
|
||||
|
||||
mutable result = new 'T[][n + 1 - size];
|
||||
mutable result = [Default<'T[]>(), size = n + 1 - size];
|
||||
|
||||
for i in 0..n - size {
|
||||
set result w/= i <- array[i..i + size - 1];
|
||||
|
|
|
@ -41,7 +41,7 @@ namespace Microsoft.Quantum.Arrays {
|
|||
let nElements = Length(left) < Length(right)
|
||||
? Length(left)
|
||||
| Length(right);
|
||||
mutable output = new ('T, 'U)[nElements];
|
||||
mutable output = [Default<('T, 'U)>(), size = nElements];
|
||||
|
||||
for idxElement in 0 .. nElements - 1 {
|
||||
set output w/= idxElement <- (left[idxElement], right[idxElement]);
|
||||
|
@ -80,7 +80,7 @@ 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 = new ('T1, 'T2, 'T3)[nElements];
|
||||
mutable output = [Default<('T1, 'T2, 'T3)>(), size = nElements];
|
||||
|
||||
for idxElement in 0 .. nElements - 1 {
|
||||
set output w/= idxElement <- (first[idxElement], second[idxElement], third[idxElement]);
|
||||
|
@ -123,7 +123,7 @@ 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 = new ('T1, 'T2, 'T3, 'T4)[nElements];
|
||||
mutable output = [Default<('T1, 'T2, 'T3, 'T4)>(), size = nElements];
|
||||
|
||||
for idxElement in 0 .. nElements - 1 {
|
||||
set output w/= idxElement <- (first[idxElement], second[idxElement], third[idxElement], fourth[idxElement]);
|
||||
|
@ -163,8 +163,8 @@ namespace Microsoft.Quantum.Arrays {
|
|||
/// - Microsoft.Quantum.Arrays.Zipped
|
||||
function Unzipped<'T, 'U>(arr : ('T, 'U)[]) : ('T[], 'U[]) {
|
||||
let nElements = Length(arr);
|
||||
mutable first = new 'T[nElements];
|
||||
mutable second = new 'U[nElements];
|
||||
mutable first = [Default<'T>(), size = nElements];
|
||||
mutable second = [Default<'U>(), size = nElements];
|
||||
for idxElement in 0 .. nElements - 1 {
|
||||
let (left, right) = arr[idxElement];
|
||||
set first w/= idxElement <- left;
|
||||
|
|
|
@ -163,7 +163,7 @@ namespace Microsoft.Quantum.Canon {
|
|||
mutable current = IntAsBoolArray(0, n);
|
||||
|
||||
for i in 0..N - 1 {
|
||||
if (i % 2 == 0) {
|
||||
if i % 2 == 0 {
|
||||
set j = 0;
|
||||
} else {
|
||||
let e = Zipped(current, RangeAsIntArray(0..N - 1));
|
||||
|
@ -172,7 +172,7 @@ namespace Microsoft.Quantum.Canon {
|
|||
|
||||
set j = MaxI(0, Min([j, n - 1]));
|
||||
set res w/= i <- (BoolArrayAsInt(current), j);
|
||||
if (j < n) {
|
||||
if j < n {
|
||||
set current w/= j <- not current[j];
|
||||
}
|
||||
}
|
||||
|
@ -248,14 +248,14 @@ namespace Microsoft.Quantum.Canon {
|
|||
|
||||
H(target);
|
||||
AssertMeasurementProbability([PauliZ], [target], One, 0.5, "Probability of the measurement must be 0.5", 1e-10);
|
||||
if (IsResultOne(MResetZ(target))) {
|
||||
if IsResultOne(MResetZ(target)) {
|
||||
for i in 0..vars - 1 {
|
||||
let start = 1 <<< i;
|
||||
let code = GrayCode(i);
|
||||
for j in 0..Length(code) - 1 {
|
||||
let (offset, ctrl) = code[j];
|
||||
RFrac(PauliZ, -Angle(start + offset), vars, controls[i]);
|
||||
if (i != 0) {
|
||||
if i != 0 {
|
||||
CNOT(controls[ctrl], controls[i]);
|
||||
}
|
||||
}
|
||||
|
@ -273,7 +273,7 @@ namespace Microsoft.Quantum.Canon {
|
|||
/// array.
|
||||
internal function ArrangedQubits(controls : Qubit[], target : Qubit, helper : Qubit[]) : Qubit[] {
|
||||
let numControls = Length(controls);
|
||||
mutable qs = new Qubit[2^numControls] w/ 0 <- target;
|
||||
mutable qs = [target, size = 2^numControls];
|
||||
mutable cntC = 0;
|
||||
mutable cntH = 0;
|
||||
for i in 1..2^numControls - 1 {
|
||||
|
@ -312,7 +312,7 @@ namespace Microsoft.Quantum.Canon {
|
|||
// initialize helper lines with control lines based on LSB
|
||||
for i in 3..2^vars - 1 {
|
||||
let lsb = i &&& -i;
|
||||
if (i != lsb) { // i is power of 2
|
||||
if i != lsb { // i is power of 2
|
||||
CNOT(qs[lsb], qs[i]);
|
||||
}
|
||||
}
|
||||
|
@ -321,7 +321,7 @@ namespace Microsoft.Quantum.Canon {
|
|||
// copy remainder (without LSB)
|
||||
for i in 3..2^vars - 1 {
|
||||
let lsb = i &&& -i;
|
||||
if (i != lsb) {
|
||||
if i != lsb {
|
||||
CNOT(qs[i - lsb], qs[i]);
|
||||
}
|
||||
}
|
||||
|
@ -338,7 +338,7 @@ namespace Microsoft.Quantum.Canon {
|
|||
|
||||
H(target);
|
||||
AssertMeasurementProbability([PauliZ], [target], One, 0.5, "Probability of the measurement must be 0.5", 1e-10);
|
||||
if (IsResultOne(MResetZ(target))) {
|
||||
if IsResultOne(MResetZ(target)) {
|
||||
use helper = Qubit[2^vars - vars - 1];
|
||||
let qs = ArrangedQubits(controls, target, helper);
|
||||
within {
|
||||
|
@ -348,7 +348,7 @@ namespace Microsoft.Quantum.Canon {
|
|||
// can be merged into a single loop.
|
||||
for i in 3..2^vars - 1 {
|
||||
let lsb = i &&& -i;
|
||||
if (i != lsb) {
|
||||
if i != lsb {
|
||||
CNOT(qs[lsb], qs[i]);
|
||||
CNOT(qs[i - lsb], qs[i]);
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче