Skip to content

Commit

Permalink
Updates to new array creation syntax, part 3 (microsoft#762)
Browse files Browse the repository at this point in the history
Replaces ConstantArray and deprecated syntax in Q# Jupyter Notebooks
  • Loading branch information
tcNickolas authored Mar 14, 2022
1 parent 7c4b47e commit d349630
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 34 deletions.
4 changes: 2 additions & 2 deletions BoundedKnapsack/BoundedKnapsack.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"\n",
"operation MeasureCombination01 (selectedItems : Qubit[]) : Bool[] {\n",
" // ...\n",
" return new Bool[0];\n",
" return [];\n",
"}"
]
},
Expand Down Expand Up @@ -317,7 +317,7 @@
"\n",
"operation MeasureCombination (selectedItemCounts : Qubit[][]) : Int[] {\n",
" // ...\n",
" return new Int[0];\n",
" return [];\n",
"}"
]
},
Expand Down
6 changes: 3 additions & 3 deletions DeutschJozsaAlgorithm/Tests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ namespace Quantum.Kata.DeutschJozsaAlgorithm {
operation T15_Oracle_ProductFunction () : Unit {
// cross-tests
// the mask for all 1's corresponds to Oracle_OddNumberOfOnes
mutable r = ConstantArray(10, 1);
mutable r = [1, size = 10];
let L = Length(r);

for i in 2 .. L {
AssertTwoOraclesAreEqual(i .. i, Oracle_ProductFunction(_, _, r[0 .. i - 1]), Oracle_OddNumberOfOnes_Reference);
}

// the mask with all 0's corresponds to Oracle_Zero
set r = ConstantArray(10, 0);
set r = [0, size = 10];

for i in 2 .. L {
AssertTwoOraclesAreEqual(i .. i, Oracle_ProductFunction(_, _, r[0 .. i - 1]), Oracle_Zero_Reference);
Expand All @@ -124,7 +124,7 @@ namespace Quantum.Kata.DeutschJozsaAlgorithm {
operation T16_Oracle_ProductWithNegationFunction () : Unit {
// cross-tests
// the mask for all 1's corresponds to Oracle_OddNumberOfOnes
mutable r = ConstantArray(10, 1);
mutable r = [1, size = 10];
let L = Length(r);

for i in 2 .. L {
Expand Down
4 changes: 2 additions & 2 deletions GraphColoring/GraphColoring.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
"\n",
"operation MeasureColoring (K : Int, register : Qubit[]) : Int[] {\n",
" // ...\n",
" return new Int[0];\n",
" return [];\n",
"}"
]
},
Expand Down Expand Up @@ -363,7 +363,7 @@
"\n",
"operation GroversAlgorithm (V : Int, oracle : ((Qubit[], Qubit) => Unit is Adj)) : Int[] {\n",
" // ...\n",
" return new Int[V];\n",
" return [0, size = V];\n",
"}"
]
},
Expand Down
2 changes: 1 addition & 1 deletion GraphColoring/Workbook_GraphColoring.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@
"open Microsoft.Quantum.Measurement;\n",
"\n",
"operation GroversAlgorithm (V : Int, oracle : ((Qubit[], Qubit) => Unit is Adj)) : Int[] {\n",
" mutable coloring = new Int[V];\n",
" mutable coloring = [0, size = V];\n",
" use (register, output) = (Qubit[2 * V], Qubit());\n",
" mutable correct = false;\n",
" mutable iterations = 1;\n",
Expand Down
6 changes: 3 additions & 3 deletions KeyDistribution_BB84/KeyDistribution_BB84.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
"\n",
"operation RandomArray (N : Int) : Bool[] {\n",
" // ...\n",
" return new Bool[N];\n",
" return [false, size = N];\n",
"}"
]
},
Expand Down Expand Up @@ -252,7 +252,7 @@
"\n",
"operation MeasureBobsQubits (qs : Qubit[], bases : Bool[]) : Bool[] {\n",
" // ...\n",
" return new Bool[0];\n",
" return [];\n",
"}"
]
},
Expand Down Expand Up @@ -292,7 +292,7 @@
"\n",
"function GenerateSharedKey (basesAlice : Bool[], basesBob : Bool[], measurementsBob : Bool[]) : Bool[] {\n",
" // ...\n",
" return new Bool[0];\n",
" return [];\n",
"}"
]
},
Expand Down
2 changes: 1 addition & 1 deletion KeyDistribution_BB84/Tests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ namespace Quantum.Kata.KeyDistribution {
@Test("QuantumSimulator")
operation T25_CheckKeysMatch () : Unit {
// Hard-coded test to validate that the solution checks the right relation with error rate
mutable key1 = ConstantArray(10, false);
mutable key1 = [false, size = 10];
mutable key2 = key1 w/ 3 <- true;
mutable errorRate = 15;
mutable result = CheckKeysMatch(key1, key2, errorRate);
Expand Down
6 changes: 2 additions & 4 deletions SimonsAlgorithm/ReferenceImplementation.qs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ namespace Quantum.Kata.SimonsAlgorithm {

// measure all qubits of the input register;
// the result of each measurement is converted to an Int
mutable j = new Int[N];
mutable j = [];
for i in 0 .. N - 1 {
if (M(x[i]) == One) {
set j w/= i <- 1;
}
set j += [M(x[i]) == One ? 1 | 0];
}

// since y has not been measured, we reset y qubits
Expand Down
2 changes: 1 addition & 1 deletion SimonsAlgorithm/Tasks.qs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ namespace Quantum.Kata.SimonsAlgorithm {

// Declare an Int array in which the result will be stored;
// the variable has to be mutable to allow updating it.
mutable b = new Int[N];
mutable b = [];

// ...

Expand Down
6 changes: 3 additions & 3 deletions SimonsAlgorithm/Tests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace Quantum.Kata.SimonsAlgorithm {
operation Q13_Oracle_OperatorOutput () : Unit {
// cross-tests
// the mask for all 1's should behave the same as Oracle_CountBits
mutable A = ConstantArray(11, 1);
mutable A = [1, size = 11];
let L = Length(A);

for i in 2 .. L {
Expand All @@ -85,7 +85,7 @@ namespace Quantum.Kata.SimonsAlgorithm {
set A = [1, 1, 0, 0];
AssertTwoOraclesWithIntArrAreEqual(A, Oracle_OperatorOutput, Oracle_OperatorOutput_Reference);

set A = ConstantArray(5, 0);
set A = [0, size = 5];
AssertTwoOraclesWithIntArrAreEqual(A, Oracle_OperatorOutput, Oracle_OperatorOutput_Reference);

set A = [1, 0, 1, 1, 1];
Expand Down Expand Up @@ -137,7 +137,7 @@ namespace Quantum.Kata.SimonsAlgorithm {
AssertTwoOraclesWithDifferentOutputsAreEqual(5, Oracle_MultidimensionalOperatorOutput(_, _, [B]), Oracle_OperatorOutput_Reference(_, _, B));

// cross-test for bit counting oracle
set B = ConstantArray(5, 1);
set B = [1, size = 5];
AssertTwoOraclesWithDifferentOutputsAreEqual(5, Oracle_MultidimensionalOperatorOutput(_, _, [B]), Oracle_CountBits_Reference);
}

Expand Down
4 changes: 2 additions & 2 deletions SolveSATWithGrover/SolveSATWithGrover.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@
"\n",
"operation UniversalGroversAlgorithm (N : Int, oracle : ((Qubit[], Qubit) => Unit is Adj)) : Bool[] {\n",
" // ...\n",
" return new Bool[N];\n",
" return [false, size = N];\n",
"}"
]
},
Expand All @@ -570,7 +570,7 @@
"file_extension": ".qs",
"mimetype": "text/x-qsharp",
"name": "qsharp",
"version": "0.12"
"version": "0.14"
}
},
"nbformat": 4,
Expand Down
6 changes: 3 additions & 3 deletions SolveSATWithGrover/Workbook_SolveSATWithGrover.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@
"source": [
"// Find all qubits which are variables in clause\n",
"function GetClauseQubits (queryRegister : Qubit[], clause: (Int,Bool)[]) : Qubit[] {\n",
" mutable clauseQubits = new Qubit[0];\n",
" mutable clauseQubits = [];\n",
" for (index, _) in clause {\n",
" set clauseQubits += [queryRegister[index]];\n",
" }\n",
Expand Down Expand Up @@ -1028,7 +1028,7 @@
"\n",
"operation UniversalGroversAlgorithm (N : Int, oracle : ((Qubit[], Qubit) => Unit is Adj)) : Bool[] {\n",
" // Since we are unaware of the optimal number of iterations, we try different numbers of iterations.\n",
" mutable answer = new Bool[N];\n",
" mutable answer = [false, size = N];\n",
" use (register, output) = (Qubit[N], Qubit());\n",
" mutable correct = false;\n",
" mutable iter = 1;\n",
Expand Down Expand Up @@ -1073,7 +1073,7 @@
"file_extension": ".qs",
"mimetype": "text/x-qsharp",
"name": "qsharp",
"version": "0.12"
"version": "0.14"
}
},
"nbformat": 4,
Expand Down
6 changes: 3 additions & 3 deletions Superposition/Workbook_Superposition_Part2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -554,14 +554,14 @@
"open Microsoft.Quantum.Math;\n",
"\n",
"operation FourBitstringSuperposition (qs : Qubit[], bits : Bool[][]) : Unit {\n",
" FourBitstringSuperposition_Recursive(new Bool[0], qs, bits);\n",
" FourBitstringSuperposition_Recursive([], qs, bits);\n",
"}\n",
"\n",
"operation FourBitstringSuperposition_Recursive (currentBitString : Bool[], qs : Qubit[], bits : Bool[][]) : Unit {\n",
" // an array of bit strings whose columns we are considering begin with |0⟩\n",
" mutable zeroLeads = new Bool[][0];\n",
" mutable zeroLeads = [];\n",
" // an array of bit strings whose columns we are considering begin with |1⟩\n",
" mutable oneLeads = new Bool[][0];\n",
" mutable oneLeads = [];\n",
" // the number of bit strings we're considering\n",
" let rows = Length(bits);\n",
" // the current position we're considering\n",
Expand Down
10 changes: 5 additions & 5 deletions tutorials/QuantumClassification/InsideQuantumClassifiers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@
"open Microsoft.Quantum.Random;\n",
"\n",
"operation SampleData (samplesNumber : Int, separationAngles : Double[]) : (Double[][], Int[]) {\n",
" mutable features = new Double[][samplesNumber];\n",
" mutable labels = new Int[samplesNumber];\n",
" mutable features = [];\n",
" mutable labels = [];\n",
" for i in 0 .. samplesNumber - 1 {\n",
" let sample = [DrawRandomDouble(0.0, 1.0), DrawRandomDouble(0.0, 1.0)];\n",
" let angle = ArcTan2(sample[1], sample[0]);\n",
" set features w/= i <- sample;\n",
" set labels w/= i <- (angle < separationAngles[0] or angle > separationAngles[1]) ? 0 | 1;\n",
" set features += [sample];\n",
" set labels += [(angle < separationAngles[0] or angle > separationAngles[1]) ? 0 | 1];\n",
" }\n",
" return (features, labels);\n",
"}\n",
Expand Down Expand Up @@ -426,7 +426,7 @@
"\n",
"function ClassifierStructure () : ControlledRotation[] {\n",
" return [\n",
" ControlledRotation((0, new Int[0]), PauliY, 0)\n",
" ControlledRotation((0, []), PauliY, 0)\n",
" ];\n",
"}"
]
Expand Down
2 changes: 1 addition & 1 deletion tutorials/RandomNumberGeneration/Tests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ namespace Quantum.Kata.RandomNumberGeneration {
let idealCopiesGenerated = IntAsDouble(nRuns) / IntAsDouble(max-min+1);
let minimumCopiesGenerated = (0.8 * idealCopiesGenerated > 40.0) ? 0.8 * idealCopiesGenerated | 0.0;

mutable counts = ConstantArray(max + 1, 0);
mutable counts = [0, size = max + 1];
mutable average = 0.0;

ResetOracleCallsCount();
Expand Down

0 comments on commit d349630

Please sign in to comment.