From d34963054bd8cfc2ddbc4f12ffec6536648afa52 Mon Sep 17 00:00:00 2001 From: Mariia Mykhailova Date: Mon, 14 Mar 2022 10:31:37 -0700 Subject: [PATCH] Updates to new array creation syntax, part 3 (#762) Replaces ConstantArray and deprecated syntax in Q# Jupyter Notebooks --- BoundedKnapsack/BoundedKnapsack.ipynb | 4 ++-- DeutschJozsaAlgorithm/Tests.qs | 6 +++--- GraphColoring/GraphColoring.ipynb | 4 ++-- GraphColoring/Workbook_GraphColoring.ipynb | 2 +- KeyDistribution_BB84/KeyDistribution_BB84.ipynb | 6 +++--- KeyDistribution_BB84/Tests.qs | 2 +- SimonsAlgorithm/ReferenceImplementation.qs | 6 ++---- SimonsAlgorithm/Tasks.qs | 2 +- SimonsAlgorithm/Tests.qs | 6 +++--- SolveSATWithGrover/SolveSATWithGrover.ipynb | 4 ++-- SolveSATWithGrover/Workbook_SolveSATWithGrover.ipynb | 6 +++--- Superposition/Workbook_Superposition_Part2.ipynb | 6 +++--- .../InsideQuantumClassifiers.ipynb | 10 +++++----- tutorials/RandomNumberGeneration/Tests.qs | 2 +- 14 files changed, 32 insertions(+), 34 deletions(-) diff --git a/BoundedKnapsack/BoundedKnapsack.ipynb b/BoundedKnapsack/BoundedKnapsack.ipynb index 6f453df0c2a..270a1623d75 100644 --- a/BoundedKnapsack/BoundedKnapsack.ipynb +++ b/BoundedKnapsack/BoundedKnapsack.ipynb @@ -62,7 +62,7 @@ "\n", "operation MeasureCombination01 (selectedItems : Qubit[]) : Bool[] {\n", " // ...\n", - " return new Bool[0];\n", + " return [];\n", "}" ] }, @@ -317,7 +317,7 @@ "\n", "operation MeasureCombination (selectedItemCounts : Qubit[][]) : Int[] {\n", " // ...\n", - " return new Int[0];\n", + " return [];\n", "}" ] }, diff --git a/DeutschJozsaAlgorithm/Tests.qs b/DeutschJozsaAlgorithm/Tests.qs index 7ec755655f0..f77194cb93f 100644 --- a/DeutschJozsaAlgorithm/Tests.qs +++ b/DeutschJozsaAlgorithm/Tests.qs @@ -91,7 +91,7 @@ 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 { @@ -99,7 +99,7 @@ namespace Quantum.Kata.DeutschJozsaAlgorithm { } // 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); @@ -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 { diff --git a/GraphColoring/GraphColoring.ipynb b/GraphColoring/GraphColoring.ipynb index a1aa7ea5107..a501ab50fbf 100644 --- a/GraphColoring/GraphColoring.ipynb +++ b/GraphColoring/GraphColoring.ipynb @@ -140,7 +140,7 @@ "\n", "operation MeasureColoring (K : Int, register : Qubit[]) : Int[] {\n", " // ...\n", - " return new Int[0];\n", + " return [];\n", "}" ] }, @@ -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", "}" ] }, diff --git a/GraphColoring/Workbook_GraphColoring.ipynb b/GraphColoring/Workbook_GraphColoring.ipynb index 8d6e1caf5ee..847c74ff691 100644 --- a/GraphColoring/Workbook_GraphColoring.ipynb +++ b/GraphColoring/Workbook_GraphColoring.ipynb @@ -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", diff --git a/KeyDistribution_BB84/KeyDistribution_BB84.ipynb b/KeyDistribution_BB84/KeyDistribution_BB84.ipynb index 7ee3b6ea40c..2fdf790fc33 100644 --- a/KeyDistribution_BB84/KeyDistribution_BB84.ipynb +++ b/KeyDistribution_BB84/KeyDistribution_BB84.ipynb @@ -169,7 +169,7 @@ "\n", "operation RandomArray (N : Int) : Bool[] {\n", " // ...\n", - " return new Bool[N];\n", + " return [false, size = N];\n", "}" ] }, @@ -252,7 +252,7 @@ "\n", "operation MeasureBobsQubits (qs : Qubit[], bases : Bool[]) : Bool[] {\n", " // ...\n", - " return new Bool[0];\n", + " return [];\n", "}" ] }, @@ -292,7 +292,7 @@ "\n", "function GenerateSharedKey (basesAlice : Bool[], basesBob : Bool[], measurementsBob : Bool[]) : Bool[] {\n", " // ...\n", - " return new Bool[0];\n", + " return [];\n", "}" ] }, diff --git a/KeyDistribution_BB84/Tests.qs b/KeyDistribution_BB84/Tests.qs index 319a4b3218f..e6de17bf5a8 100644 --- a/KeyDistribution_BB84/Tests.qs +++ b/KeyDistribution_BB84/Tests.qs @@ -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); diff --git a/SimonsAlgorithm/ReferenceImplementation.qs b/SimonsAlgorithm/ReferenceImplementation.qs index 57183051bf0..24f2d143186 100644 --- a/SimonsAlgorithm/ReferenceImplementation.qs +++ b/SimonsAlgorithm/ReferenceImplementation.qs @@ -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 diff --git a/SimonsAlgorithm/Tasks.qs b/SimonsAlgorithm/Tasks.qs index 2158158a6ef..56c62e3622f 100644 --- a/SimonsAlgorithm/Tasks.qs +++ b/SimonsAlgorithm/Tasks.qs @@ -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 = []; // ... diff --git a/SimonsAlgorithm/Tests.qs b/SimonsAlgorithm/Tests.qs index 9e65865c037..11d57e28207 100644 --- a/SimonsAlgorithm/Tests.qs +++ b/SimonsAlgorithm/Tests.qs @@ -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 { @@ -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]; @@ -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); } diff --git a/SolveSATWithGrover/SolveSATWithGrover.ipynb b/SolveSATWithGrover/SolveSATWithGrover.ipynb index 2c7013b5ddd..214c7b7d6de 100644 --- a/SolveSATWithGrover/SolveSATWithGrover.ipynb +++ b/SolveSATWithGrover/SolveSATWithGrover.ipynb @@ -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", "}" ] }, @@ -570,7 +570,7 @@ "file_extension": ".qs", "mimetype": "text/x-qsharp", "name": "qsharp", - "version": "0.12" + "version": "0.14" } }, "nbformat": 4, diff --git a/SolveSATWithGrover/Workbook_SolveSATWithGrover.ipynb b/SolveSATWithGrover/Workbook_SolveSATWithGrover.ipynb index 1d9c0b92a69..8e82aeff86f 100644 --- a/SolveSATWithGrover/Workbook_SolveSATWithGrover.ipynb +++ b/SolveSATWithGrover/Workbook_SolveSATWithGrover.ipynb @@ -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", @@ -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", @@ -1073,7 +1073,7 @@ "file_extension": ".qs", "mimetype": "text/x-qsharp", "name": "qsharp", - "version": "0.12" + "version": "0.14" } }, "nbformat": 4, diff --git a/Superposition/Workbook_Superposition_Part2.ipynb b/Superposition/Workbook_Superposition_Part2.ipynb index 7379ed265f0..8af21c1a793 100644 --- a/Superposition/Workbook_Superposition_Part2.ipynb +++ b/Superposition/Workbook_Superposition_Part2.ipynb @@ -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", diff --git a/tutorials/QuantumClassification/InsideQuantumClassifiers.ipynb b/tutorials/QuantumClassification/InsideQuantumClassifiers.ipynb index 983c678d621..f284b62502c 100644 --- a/tutorials/QuantumClassification/InsideQuantumClassifiers.ipynb +++ b/tutorials/QuantumClassification/InsideQuantumClassifiers.ipynb @@ -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", @@ -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", "}" ] diff --git a/tutorials/RandomNumberGeneration/Tests.qs b/tutorials/RandomNumberGeneration/Tests.qs index 6b0fbadee47..15ee19ffd6b 100644 --- a/tutorials/RandomNumberGeneration/Tests.qs +++ b/tutorials/RandomNumberGeneration/Tests.qs @@ -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();