From b4a9cd9172dbd1a93f78f1d835c403f72ea8c418 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Fri, 16 May 2025 13:25:49 -0700 Subject: [PATCH 1/7] Katas --- katas/content/KatasLibrary.qs | 6 +++--- katas/content/distinguishing_unitaries/Common.qs | 4 ++-- katas/content/key_distribution/examples/BB84Demo.qs | 2 +- katas/content/key_distribution/random_array/Solution.qs | 2 +- .../multi_qubit_measurements/examples/MeasuringOne.qs | 2 +- .../examples/PartialMeasurementsDemo.qs | 2 +- .../phase_estimation/examples/QuantumPhaseEstimationDemo.qs | 2 +- katas/content/random_numbers/Common.qs | 2 +- katas/content/solving_sat/Common.qs | 6 +++--- 9 files changed, 14 insertions(+), 14 deletions(-) diff --git a/katas/content/KatasLibrary.qs b/katas/content/KatasLibrary.qs index ca71aeda83..b3b22b89c1 100644 --- a/katas/content/KatasLibrary.qs +++ b/katas/content/KatasLibrary.qs @@ -210,7 +210,7 @@ namespace KatasUtils { // get the solution's answer and verify if NOT a match, then differentiate what kind of mismatch let ans = testImpl(q); if ans != (state == 1) { - set misclassifications w/= state <- misclassifications[state] + 1; + misclassifications[state] += 1; } // If the final state is to be preserved, check if it was not modified @@ -271,11 +271,11 @@ namespace KatasUtils { if ans >= 0 and ans < nStates { // classification result is a valid state index - check if is it correct if ans != state { - set misclassifications w/= ((state * nStates) + ans) <- (misclassifications[(state * nStates) + ans] + 1); + misclassifications[(state * nStates) + ans] += 1; } } else { // classification result is an invalid state index - file it separately - set unknownClassifications w/= state <- (unknownClassifications[state] + 1); + unknownClassifications[state] += 1; } if preserveState { diff --git a/katas/content/distinguishing_unitaries/Common.qs b/katas/content/distinguishing_unitaries/Common.qs index 60e1909105..12c4a108dc 100644 --- a/katas/content/distinguishing_unitaries/Common.qs +++ b/katas/content/distinguishing_unitaries/Common.qs @@ -28,10 +28,10 @@ namespace Kata.Verification { if returnedIndex != actualIndex { if returnedIndex < 0 or returnedIndex >= nUnitaries { - set unknownClassifications w/= actualIndex <- unknownClassifications[actualIndex] + 1; + unknownClassifications[actualIndex] += 1; } else { let index = actualIndex * nUnitaries + returnedIndex; - set wrongClassifications w/= index <- wrongClassifications[index] + 1; + wrongClassifications[index] += 1; } } } diff --git a/katas/content/key_distribution/examples/BB84Demo.qs b/katas/content/key_distribution/examples/BB84Demo.qs index 33db128dc0..b1e4ca32c6 100644 --- a/katas/content/key_distribution/examples/BB84Demo.qs +++ b/katas/content/key_distribution/examples/BB84Demo.qs @@ -34,7 +34,7 @@ namespace Kata { operation RandomArray(N : Int) : Bool[] { mutable array = [false, size = N]; for i in 0..N - 1 { - set array w/= i <- DrawRandomInt(0, 1) == 0; + array[i] = DrawRandomInt(0, 1) == 0; } return array; } diff --git a/katas/content/key_distribution/random_array/Solution.qs b/katas/content/key_distribution/random_array/Solution.qs index fb5453bc8b..2e981b4491 100644 --- a/katas/content/key_distribution/random_array/Solution.qs +++ b/katas/content/key_distribution/random_array/Solution.qs @@ -4,7 +4,7 @@ namespace Kata { operation RandomArray(N : Int) : Bool[] { mutable array = [false, size = N]; for i in 0..N - 1 { - set array w/= i <- DrawRandomInt(0, 1) == 0; + array[i] = DrawRandomInt(0, 1) == 0; } return array; } diff --git a/katas/content/multi_qubit_measurements/examples/MeasuringOne.qs b/katas/content/multi_qubit_measurements/examples/MeasuringOne.qs index aec873424e..4675cfb24b 100644 --- a/katas/content/multi_qubit_measurements/examples/MeasuringOne.qs +++ b/katas/content/multi_qubit_measurements/examples/MeasuringOne.qs @@ -25,7 +25,7 @@ namespace Kata { // and convert the result to an integer, interpreting it as big endian binary notation. let result = (MResetZ(qs[0]) == One ? 1 | 0) * 2 + (MResetZ(qs[1]) == One ? 1 | 0); - set countArray w/= result <- countArray[result] + 1; + countArray[result] += 1; } // Obtain simulated probability of measurement for each outcome. diff --git a/katas/content/multi_qubit_measurements/examples/PartialMeasurementsDemo.qs b/katas/content/multi_qubit_measurements/examples/PartialMeasurementsDemo.qs index 50b1224998..c1ba794605 100644 --- a/katas/content/multi_qubit_measurements/examples/PartialMeasurementsDemo.qs +++ b/katas/content/multi_qubit_measurements/examples/PartialMeasurementsDemo.qs @@ -23,7 +23,7 @@ namespace Kata { // Measure the first qubit. let outcome = M(qs[0]) == Zero ? 0 | 1; - set countArray w/= outcome <- countArray[outcome] + 1; + countArray[outcome] += 1; if countArray[outcome] == 1 { // The first time the outcome is 0/1, print the system state afterwards. diff --git a/katas/content/phase_estimation/examples/QuantumPhaseEstimationDemo.qs b/katas/content/phase_estimation/examples/QuantumPhaseEstimationDemo.qs index 3d2c971a3a..a132e49a4c 100644 --- a/katas/content/phase_estimation/examples/QuantumPhaseEstimationDemo.qs +++ b/katas/content/phase_estimation/examples/QuantumPhaseEstimationDemo.qs @@ -19,7 +19,7 @@ namespace Kata { mutable counts = [0, size = 2^n]; for _ in 1..100 { let res = PhaseEstimation(U, P, n); - set counts w/= res <- counts[res] + 1; + counts[res] += 1; } for i in 0..2^n - 1 { if counts[i] > 0 { diff --git a/katas/content/random_numbers/Common.qs b/katas/content/random_numbers/Common.qs index 9e8238b1a0..c9321bb261 100644 --- a/katas/content/random_numbers/Common.qs +++ b/katas/content/random_numbers/Common.qs @@ -53,7 +53,7 @@ namespace Kata.Verification { return 0x1; } set average += IntAsDouble(val); - set counts w/= val <- counts[val] + 1; + counts[val] += 1; } set average = average / IntAsDouble(nRuns); diff --git a/katas/content/solving_sat/Common.qs b/katas/content/solving_sat/Common.qs index 863513cf62..9a9ea898a0 100644 --- a/katas/content/solving_sat/Common.qs +++ b/katas/content/solving_sat/Common.qs @@ -40,8 +40,8 @@ namespace Kata.Verification { set nextInd = DrawRandomInt(0, nVar - 1); } until (not usedVariables[nextInd]) fixup {} - set clause w/= k <- (nextInd, DrawRandomBool(0.5)); - set usedVariables w/= nextInd <- true; + clause[k] = (nextInd, DrawRandomBool(0.5)); + usedVariables[nextInd] = true; } return clause; } @@ -50,7 +50,7 @@ namespace Kata.Verification { mutable problem = [[(0, false), size = 0], size = nClause]; for j in 0..nClause - 1 { - set problem w/= j <- Generate_SAT_Clause(nVar, nTerms); + problem[j] = Generate_SAT_Clause(nVar, nTerms); } return problem; } From bf838537ee4df465b8c3514c81ce83a336d00a83 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Fri, 16 May 2025 13:50:52 -0700 Subject: [PATCH 2/7] Libraries --- .../src/JordanWigner/BlockEncoding.qs | 14 +++++----- .../ClusterOperatorEvolutionSet.qs | 8 +++--- .../JordanWigner/OptimizedBlockEncoding.qs | 18 ++++++------- library/chemistry/src/JordanWigner/VQE.qs | 26 +++++++++---------- .../chemistry/src/MixedStatePreparation.qs | 8 +++--- library/fixed_point/src/Convert.qs | 2 +- .../tests/resources/src/state_preparation.qs | 2 +- library/std/src/Std/Arrays.qs | 15 ++++++----- 8 files changed, 47 insertions(+), 46 deletions(-) diff --git a/library/chemistry/src/JordanWigner/BlockEncoding.qs b/library/chemistry/src/JordanWigner/BlockEncoding.qs index c20ced3ec9..be5a47ea74 100644 --- a/library/chemistry/src/JordanWigner/BlockEncoding.qs +++ b/library/chemistry/src/JordanWigner/BlockEncoding.qs @@ -50,14 +50,14 @@ function JWBlockEncodingGeneratorSystem(data : JWOptimizedHTerms) : GeneratorSys for idx in IndexRange(ZData) { // Array of Arrays of Length 1 - genIdxes w/= idx <- (ZTermToPauliGenIdx(HTermToGenIdx(ZData[idx], [0])))[0]; + genIdxes[idx] = (ZTermToPauliGenIdx(HTermToGenIdx(ZData[idx], [0])))[0]; } startIdx = Length(ZData); for idx in IndexRange(ZZData) { // Array of Arrays of Length 1 - genIdxes w/= startIdx + idx <- (ZZTermToPauliGenIdx(HTermToGenIdx(ZZData[idx], [1])))[0]; + genIdxes[startIdx + idx] = (ZZTermToPauliGenIdx(HTermToGenIdx(ZZData[idx], [1])))[0]; } startIdx = startIdx + Length(ZZData); @@ -66,8 +66,8 @@ function JWBlockEncodingGeneratorSystem(data : JWOptimizedHTerms) : GeneratorSys // Array of Arrays of Length 2 let genArr = PQandPQQRTermToPauliGenIdx(HTermToGenIdx(PQandPQQRData[idx], [2])); - genIdxes w/= startIdx + 2 * idx <- genArr[0]; - genIdxes w/= (startIdx + 2 * idx) + 1 <- genArr[1]; + genIdxes[startIdx + 2 * idx] = genArr[0]; + genIdxes[(startIdx + 2 * idx) + 1] = genArr[1]; } startIdx = startIdx + 2 * Length(PQandPQQRData); @@ -78,7 +78,7 @@ function JWBlockEncodingGeneratorSystem(data : JWOptimizedHTerms) : GeneratorSys let genArr = V0123TermToPauliGenIdx(HTermToGenIdx(h0123Data[idx], [3])); for idx0123 in IndexRange(genArr) { - genIdxes w/= finalIdx <- genArr[idx0123]; + genIdxes[finalIdx] = genArr[idx0123]; finalIdx += 1; } } @@ -203,11 +203,11 @@ function V0123TermToPauliGenIdx(term : GeneratorIndex) : GeneratorIndex[] { for idxOp in IndexRange(ops) { if (IsNotZero(v0123[idxOp % 4])) { let newCoeff = [v0123[idxOp % 4]]; - genIdxes w/= nonZero <- new GeneratorIndex { + genIdxes[nonZero] = new GeneratorIndex { Term = (ops[idxOp] + Repeated(3, Length(qubitsPQJW) + Length(qubitsRSJW)), newCoeff), Subsystem = ((qubitsPQ + qubitsRS) + qubitsPQJW) + qubitsRSJW }; - nonZero = nonZero + 1; + nonZero += 1; } } diff --git a/library/chemistry/src/JordanWigner/ClusterOperatorEvolutionSet.qs b/library/chemistry/src/JordanWigner/ClusterOperatorEvolutionSet.qs index a1b7e3b97f..816df9dba2 100644 --- a/library/chemistry/src/JordanWigner/ClusterOperatorEvolutionSet.qs +++ b/library/chemistry/src/JordanWigner/ClusterOperatorEvolutionSet.qs @@ -43,12 +43,12 @@ function ComputeJWBitString(nFermions : Int, idxFermions : Int[]) : Bool[] { } // NOTE: This could be optimized for idx in 0..fermionIdx { - zString w/= idx <- not zString[idx]; + zString[idx] = not zString[idx]; } } for fermionIdx in idxFermions { - zString w/= fermionIdx <- false; + zString[fermionIdx] = false; } return zString; } @@ -60,7 +60,7 @@ function ComputeJWPauliZString(nFermions : Int, idxFermions : Int[]) : Pauli[] { mutable pauliString = Repeated(PauliI, Length(bitString)); for idx in IndexRange(bitString) { if bitString[idx] { - pauliString w/= idx <- PauliZ; + pauliString[idx] = PauliZ; } } return pauliString; @@ -79,7 +79,7 @@ function ComputeJWPauliString( for idx in IndexRange(idxFermions) { let idxFermion = idxFermions[idx]; let op = pauliReplacements[idx]; - pauliString w/= idxFermion <- op; + pauliString[idxFermion] = op; } return pauliString; diff --git a/library/chemistry/src/JordanWigner/OptimizedBlockEncoding.qs b/library/chemistry/src/JordanWigner/OptimizedBlockEncoding.qs index 9c2aa1c449..8af14112dc 100644 --- a/library/chemistry/src/JordanWigner/OptimizedBlockEncoding.qs +++ b/library/chemistry/src/JordanWigner/OptimizedBlockEncoding.qs @@ -275,7 +275,7 @@ function V0123TermToPauliMajIdx(term : GeneratorIndex) : OptimizedBETermIndex[] for idxOp in 0..3 { if IsNotZero(v0123[idxOp]) { let newCoeff = (2.0 * 0.25) * v0123[idxOp]; - majIdxes w/= nonZero <- new OptimizedBETermIndex { + majIdxes[nonZero] = new OptimizedBETermIndex { Coefficient = newCoeff, UseSignQubit = v0123[idxOp] < 0.0, ZControlRegisterMask = selectZControlRegisters, @@ -283,7 +283,7 @@ function V0123TermToPauliMajIdx(term : GeneratorIndex) : OptimizedBETermIndex[] PauliBases = ops[idxOp], RegisterIndices = indexRegisters }; - nonZero = nonZero + 1; + nonZero += 1; } } @@ -322,25 +322,25 @@ function OptimizedBlockEncodingGeneratorSystem(data : JWOptimizedHTerms) : Optim for idx in IndexRange(ZData) { // Array of Arrays of Length 1 - majIdxes w/= idx <- ZTermToPauliMajIdx(HTermToGenIdx(ZData[idx], [0])); + majIdxes[idx] = ZTermToPauliMajIdx(HTermToGenIdx(ZData[idx], [0])); } startIdx = Length(ZData); for idx in IndexRange(ZZData) { // Array of Arrays of Length 1 - majIdxes w/= startIdx + idx <- ZZTermToPauliMajIdx(HTermToGenIdx(ZZData[idx], [1])); + majIdxes[startIdx + idx] = ZZTermToPauliMajIdx(HTermToGenIdx(ZZData[idx], [1])); } - startIdx = startIdx + Length(ZZData); + startIdx += Length(ZZData); for idx in IndexRange(PQandPQQRData) { // Array of Arrays of Length 1 - majIdxes w/= startIdx + idx <- PQandPQQRTermToPauliMajIdx(HTermToGenIdx(PQandPQQRData[idx], [2])); + majIdxes[startIdx + idx] = PQandPQQRTermToPauliMajIdx(HTermToGenIdx(PQandPQQRData[idx], [2])); } - startIdx = startIdx + Length(PQandPQQRData); + startIdx += Length(PQandPQQRData); mutable finalIdx = startIdx; for idx in 0..Length(h0123Data) - 1 { @@ -349,8 +349,8 @@ function OptimizedBlockEncodingGeneratorSystem(data : JWOptimizedHTerms) : Optim let genArr = V0123TermToPauliMajIdx(HTermToGenIdx(h0123Data[idx], [3])); for idx0123 in IndexRange(genArr) { - majIdxes w/= finalIdx <- genArr[idx0123]; - finalIdx = finalIdx + 1; + majIdxes[finalIdx] = genArr[idx0123]; + finalIdx += 1; } } diff --git a/library/chemistry/src/JordanWigner/VQE.qs b/library/chemistry/src/JordanWigner/VQE.qs index e8fda1cb3d..164455b721 100644 --- a/library/chemistry/src/JordanWigner/VQE.qs +++ b/library/chemistry/src/JordanWigner/VQE.qs @@ -244,9 +244,9 @@ function MeasurementOperators( if termType == 0 or termType == 1 { mutable op = Repeated(PauliI, nQubits); for idx in indices { - op w/= idx <- PauliZ; + op[idx] = PauliZ; } - ops w/= 0 <- op; + ops[0] = op; } // PQRS terms set operators between indices P and Q (resp R and S) to PauliZ @@ -267,15 +267,15 @@ function MeasurementOperators( mutable op = Repeated(PauliI, nQubits); for idx in IndexRange(indices) { - op w/= indices[idx] <- compactOp[idx]; + op[indices[idx]] = compactOp[idx]; } for i in indices[0] + 1..indices[1] - 1 { - op w/= i <- PauliZ; + op[i] = PauliZ; } for i in indices[2] + 1..indices[3] - 1 { - op w/= i <- PauliZ; + op[i] = PauliZ; } - ops w/= iOp <- op; + ops[iOp] = op; } } @@ -289,17 +289,17 @@ function MeasurementOperators( mutable op = Repeated(PauliI, nQubits); let nIndices = Length(indices); - op w/= indices[0] <- compactOp[0]; - op w/= indices[nIndices-1] <- compactOp[1]; + op[indices[0]] = compactOp[0]; + op[indices[nIndices-1]] = compactOp[1]; for i in indices[0] + 1..indices[nIndices - 1] - 1 { - op w/= i <- PauliZ; + op[i] = PauliZ; } // Case of PQQR term if nIndices == 4 { - op w/= indices[1] <- (indices[0] < indices[1] and indices[1] < indices[3]) ? PauliI | PauliZ; + op[indices[1]] = (indices[0] < indices[1] and indices[1] < indices[3]) ? PauliI | PauliZ; } - ops w/= iOp <- op; + ops[iOp] = op; } } @@ -333,10 +333,10 @@ function ExpandedCoefficients(coeff : Double[], termType : Int) : Double[] { // Return the expanded array of coefficients if termType == 0 or termType == 1 { - coeffs w/= 0 <- coeff[0]; + coeffs[0] = coeff[0]; } elif termType == 2 or termType == 3 { for i in 0..nCoeffs - 1 { - coeffs w/= i <- coeff[i / 2]; + coeffs[i] = coeff[i / 2]; } } diff --git a/library/chemistry/src/MixedStatePreparation.qs b/library/chemistry/src/MixedStatePreparation.qs index 6e511c8637..975f760e2a 100644 --- a/library/chemistry/src/MixedStatePreparation.qs +++ b/library/chemistry/src/MixedStatePreparation.qs @@ -220,7 +220,7 @@ function QuantumROMDiscretization(bitsPrecision : Int, coefficients : Double[]) // Uniformly distribute excess bars across coefficients. for idx in 0..AbsI(bars) - 1 { - keepCoeff w/= idx <- keepCoeff[idx] + (bars > 0 ? -1 | + 1); + keepCoeff[idx] += (bars > 0 ? -1 | + 1); } mutable barSink = []; @@ -241,8 +241,8 @@ function QuantumROMDiscretization(bitsPrecision : Int, coefficients : Double[]) barSink = Most(barSink); barSource = Most(barSource); - keepCoeff w/= idxSource <- keepCoeff[idxSource] - barHeight + keepCoeff[idxSink]; - altIndex w/= idxSink <- idxSource; + keepCoeff[idxSource] += keepCoeff[idxSink] - barHeight; + altIndex[idxSink] = idxSource; if keepCoeff[idxSource] < barHeight { barSink += [idxSource]; @@ -252,7 +252,7 @@ function QuantumROMDiscretization(bitsPrecision : Int, coefficients : Double[]) } elif Length(barSource) > 0 { let idxSource = Tail(barSource); barSource = Most(barSource); - keepCoeff w/= idxSource <- barHeight; + keepCoeff[idxSource] = barHeight; } else { return (oneNorm, keepCoeff, altIndex); } diff --git a/library/fixed_point/src/Convert.qs b/library/fixed_point/src/Convert.qs index 29fd3cd651..f217078373 100644 --- a/library/fixed_point/src/Convert.qs +++ b/library/fixed_point/src/Convert.qs @@ -40,7 +40,7 @@ function FixedPointAsBoolArray(integerBits : Int, fractionalBits : Int, value : set currentBit = not currentBit; } if currentBit { - set result w/= idx <- true; + result[idx] = true; } } diff --git a/library/src/tests/resources/src/state_preparation.qs b/library/src/tests/resources/src/state_preparation.qs index 3e4ae311f8..43b05e190f 100644 --- a/library/src/tests/resources/src/state_preparation.qs +++ b/library/src/tests/resources/src/state_preparation.qs @@ -99,7 +99,7 @@ namespace Test { let bitsize = 2^n; for i in 0..bitsize - 1 { mutable c = Repeated(0.0, bitsize); - set c w/= i <- 1.0; + c[i] = 1.0; PreparePureStateD(c, qs); DumpMachine(); ResetAll(qs); diff --git a/library/std/src/Std/Arrays.qs b/library/std/src/Std/Arrays.qs index b335926d36..285af24134 100644 --- a/library/std/src/Std/Arrays.qs +++ b/library/std/src/Std/Arrays.qs @@ -344,7 +344,7 @@ function Excluding<'T>(remove : Int[], array : 'T[]) : 'T[] { let arrayLength = Length(array); mutable toKeep = Repeated(true, arrayLength); for indexToRemove in remove { - set toKeep w/= indexToRemove <- false; + toKeep[indexToRemove] = false; } mutable output = []; for index in 0..arrayLength - 1 { @@ -987,10 +987,10 @@ function Partitioned<'T>(partitionSizes : Int[], array : 'T[]) : 'T[][] { if partitionEndIndex >= Length(array) { fail "Partitioned argument out of bounds."; } - set output w/= index <- array[partitionStartIndex..partitionEndIndex]; - set partitionStartIndex = partitionEndIndex + 1; + output[index] = array[partitionStartIndex..partitionEndIndex]; + partitionStartIndex = partitionEndIndex + 1; } - set output w/= Length(partitionSizes) <- array[partitionStartIndex..Length(array) - 1]; + output[Length(partitionSizes)] = array[partitionStartIndex..Length(array) - 1]; output } @@ -1227,9 +1227,10 @@ function Subarray<'T>(locations : Int[], array : 'T[]) : 'T[] { /// Swapped(1, 3, [0, 1, 2, 3, 4]); /// ``` function Swapped<'T>(firstIndex : Int, secondIndex : Int, array : 'T[]) : 'T[] { - array - w/ firstIndex <- array[secondIndex] - w/ secondIndex <- array[firstIndex] + mutable copy = array; + copy[firstIndex] = array[secondIndex]; + copy[secondIndex] = array[firstIndex]; + copy } /// # Summary From 1013aa35fdfa616ebe7ccb41cc68185ff1ef4bc7 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Fri, 16 May 2025 14:18:07 -0700 Subject: [PATCH 3/7] Samples --- .../DotProductViaPhaseEstimation.qs | 2 +- samples/algorithms/SimpleVQE.qs | 6 ++-- samples/estimation/Dynamics.qs | 12 +++---- samples/estimation/EkeraHastadFactoring.qs | 2 +- .../src/DoubleFactorizedChemistry.qs | 4 +-- .../estimation/df-chemistry/src/Prepare.qs | 8 ++--- .../estimation/estimation-heisenberg-2D.ipynb | 16 ++++----- .../estimation/estimation-hubbard-2D.ipynb | 16 ++++----- samples/estimation/estimation-ising-2D.ipynb | 12 +++---- samples/language/CopyAndUpdateOperator.qs | 36 ------------------- .../iterative_phase_estimation.ipynb | 2 +- 11 files changed, 41 insertions(+), 75 deletions(-) delete mode 100644 samples/language/CopyAndUpdateOperator.qs diff --git a/samples/algorithms/DotProductViaPhaseEstimation.qs b/samples/algorithms/DotProductViaPhaseEstimation.qs index 2c10db9382..44257dd41f 100644 --- a/samples/algorithms/DotProductViaPhaseEstimation.qs +++ b/samples/algorithms/DotProductViaPhaseEstimation.qs @@ -114,7 +114,7 @@ operation IterativePhaseEstimation( } H(ControlReg); //Make a measurement mid circuit - set MeasureControlReg w/= (Measurements - 1 - index) <- MResetZ(ControlReg); + MeasureControlReg[Measurements - 1 - index] = MResetZ(ControlReg); if MeasureControlReg[Measurements - 1 - index] == One { //Assign bitValue based on previous measurement bitValue += 2^(index); diff --git a/samples/algorithms/SimpleVQE.qs b/samples/algorithms/SimpleVQE.qs index 08212dd77d..d9959c5749 100644 --- a/samples/algorithms/SimpleVQE.qs +++ b/samples/algorithms/SimpleVQE.qs @@ -171,16 +171,18 @@ operation SimpleDescent( while (currentAttempt < attemptLimit) and (currentStep > minimalStep) { mutable hadImprovement = false; for i in IndexRange(initialPoint) { - let nextPoint = bestPoint w/ i <- bestPoint[i] + currentStep; + mutable nextPoint = bestPoint; // Copy the best point + nextPoint[i] += currentStep; let nextValue = f(nextPoint); // Evaluate quantum part currentAttempt = currentAttempt + 1; if nextValue < bestValue { hadImprovement = true; bestValue = nextValue; bestPoint = nextPoint; + nextPoint[i] -= currentStep; Message($"Value improved to {bestValue}."); } - let nextPoint = bestPoint w/ i <- bestPoint[i] - currentStep; + nextPoint[i] -= currentStep; let nextValue = f(nextPoint); // Evaluate quantum part currentAttempt = currentAttempt + 1; if nextValue < bestValue { diff --git a/samples/estimation/Dynamics.qs b/samples/estimation/Dynamics.qs index e15699b048..c1346d5859 100644 --- a/samples/estimation/Dynamics.qs +++ b/samples/estimation/Dynamics.qs @@ -58,26 +58,26 @@ function SetAngleSequence(p : Double, dt : Double, J : Double, g : Double) : Dou for i in 0..len1 { if (i % 2 == 0) { - set values w/= i <- val1; + values[i] = val1; } else { - set values w/= i <- val2; + values[i] = val2; } } for i in len1 + 1..len1 + len2 { if (i % 2 == 0) { - set values w/= i <- val3; + values[i] = val3; } else { - set values w/= i <- val4; + values[i] = val4; } } for i in len1 + len2 + 1..valLength - 1 { if (i % 2 == 0) { - set values w/= i <- val1; + values[i] = val1; } else { - set values w/= i <- val2; + values[i] = val2; } } return values; diff --git a/samples/estimation/EkeraHastadFactoring.qs b/samples/estimation/EkeraHastadFactoring.qs index e23efb86c9..ff2121d8ab 100644 --- a/samples/estimation/EkeraHastadFactoring.qs +++ b/samples/estimation/EkeraHastadFactoring.qs @@ -187,7 +187,7 @@ internal function LookupData(factor : BigInt, expLength : Int, mulLength : Int, for a in 0..2^expLength - 1 { let idx = b * 2^expLength + a; let value = ModulusL(factor * IntAsBigInt(b) * IntAsBigInt(sign) * (base^a), mod); - set data w/= idx <- BigIntAsBoolArray(value, numBits); + data[idx] = BigIntAsBoolArray(value, numBits); } } diff --git a/samples/estimation/df-chemistry/src/DoubleFactorizedChemistry.qs b/samples/estimation/df-chemistry/src/DoubleFactorizedChemistry.qs index 5d1617aaba..46351caca1 100644 --- a/samples/estimation/df-chemistry/src/DoubleFactorizedChemistry.qs +++ b/samples/estimation/df-chemistry/src/DoubleFactorizedChemistry.qs @@ -286,8 +286,8 @@ internal function ComputeOffsetDataSet(lengths : Int[]) : Bool[][] { mutable offsets = [0, size = Length(lengths)]; for i in IndexRange(lengths) { - set offsets w/= i <- currentOffset; - set currentOffset += lengths[i]; + offsets[i] = currentOffset; + currentOffset += lengths[i]; } let offsetWidth = Ceiling(Lg(IntAsDouble(currentOffset))); diff --git a/samples/estimation/df-chemistry/src/Prepare.qs b/samples/estimation/df-chemistry/src/Prepare.qs index 3ae7de04bb..f7d3b3d05d 100644 --- a/samples/estimation/df-chemistry/src/Prepare.qs +++ b/samples/estimation/df-chemistry/src/Prepare.qs @@ -89,7 +89,7 @@ internal function DiscretizedProbabilityDistribution(bitsPrecision : Int, coeffi // Uniformly distribute excess bars across coefficients. for idx in 0..AbsI(bars) - 1 { - set keepCoeff w/= idx <- keepCoeff[idx] + (bars > 0 ? -1 | + 1); + keepCoeff[idx] += bars > 0 ? -1 | + 1; } mutable barSink = []; @@ -110,8 +110,8 @@ internal function DiscretizedProbabilityDistribution(bitsPrecision : Int, coeffi set barSink = Most(barSink); set barSource = Most(barSource); - set keepCoeff w/= idxSource <- keepCoeff[idxSource] - barHeight + keepCoeff[idxSink]; - set altIndex w/= idxSink <- idxSource; + keepCoeff[idxSource] += keepCoeff[idxSink] - barHeight; + altIndex[idxSink] = idxSource; if keepCoeff[idxSource] < barHeight { set barSink += [idxSource]; @@ -121,7 +121,7 @@ internal function DiscretizedProbabilityDistribution(bitsPrecision : Int, coeffi } elif Length(barSource) > 0 { let idxSource = Tail(barSource); set barSource = Most(barSource); - set keepCoeff w/= idxSource <- barHeight; + keepCoeff[idxSource] = barHeight; } else { return (keepCoeff, altIndex); } diff --git a/samples/estimation/estimation-heisenberg-2D.ipynb b/samples/estimation/estimation-heisenberg-2D.ipynb index 68a63b6b58..f6124af005 100644 --- a/samples/estimation/estimation-heisenberg-2D.ipynb +++ b/samples/estimation/estimation-heisenberg-2D.ipynb @@ -162,36 +162,36 @@ " let val3 = -J*(1.0-4.0*p)*dt/2.0;\n", "\n", " // Angles bookending Term2\n", - " set values w/= len1 <- val2;\n", - " set values w/= len1+len2 <- val2;\n", + " values[len1] = val2;\n", + " values[len1+len2] = val2;\n", "\n", " for i in 0..len1-1 {\n", "\n", " if (i % 2 == 0) {\n", - " set values w/= i <- val1*2.0;\n", + " values[i] = val1*2.0;\n", " }\n", " else {\n", - " set values w/= i <- val1;\n", + " values[i] = val1;\n", " }\n", " }\n", "\n", " for i in len1+1..len1+len2-1 {\n", "\n", " if (i % 2 == 0) {\n", - " set values w/= i <- val3*2.0;\n", + " values[i] = val3*2.0;\n", " }\n", " else {\n", - " set values w/= i <- val3;\n", + " values[i] = val3;\n", " }\n", " }\n", "\n", " for i in len1+len2+1..valLength-1 {\n", "\n", " if (i % 2 == 0) {\n", - " set values w/= i <- val1*2.0;\n", + " values[i] = val1*2.0;\n", " }\n", " else {\n", - " set values w/= i <- val1;\n", + " values[i] = val1;\n", " }\n", " }\n", "\n", diff --git a/samples/estimation/estimation-hubbard-2D.ipynb b/samples/estimation/estimation-hubbard-2D.ipynb index 16b31befb9..8d42bdd665 100644 --- a/samples/estimation/estimation-hubbard-2D.ipynb +++ b/samples/estimation/estimation-hubbard-2D.ipynb @@ -258,33 +258,33 @@ " mutable values = [0.0, size=valLength];\n", "\n", " // Angles bookending Term2\n", - " set values w/= len1 <- valA;\n", - " set values w/= len1+len2 <- valA;\n", + " values[len1] = valA;\n", + " values[len1+len2] = valA;\n", "\n", " for i in 0..len1-1 {\n", " if (i%2 == 0) {\n", - " set values w/= i <- val1;\n", + " values[i] = val1;\n", " }\n", " else {\n", - " set values w/= i <- val2;\n", + " values[i] = val2;\n", " }\n", " }\n", "\n", " for i in len1+1..len1+len2-1 {\n", " if (i%2 == 0) {\n", - " set values w/= i <- valC;\n", + " values[i] = valC;\n", " }\n", " else {\n", - " set values w/= i <- valB;\n", + " values[i] = valB;\n", " }\n", " }\n", "\n", " for i in len1+len2+1..valLength-1 {\n", " if (i%2 == 0) {\n", - " set values w/= i <- val1;\n", + " values[i] = val1;\n", " }\n", " else {\n", - " set values w/= i <- val2;\n", + " values[i] = val2;\n", " }\n", " }\n", "\n", diff --git a/samples/estimation/estimation-ising-2D.ipynb b/samples/estimation/estimation-ising-2D.ipynb index 87f1ad8a58..34221b698e 100644 --- a/samples/estimation/estimation-ising-2D.ipynb +++ b/samples/estimation/estimation-ising-2D.ipynb @@ -106,29 +106,29 @@ " for i in 0..len1 {\n", "\n", " if (i % 2 == 0) {\n", - " set values w/= i <- val1;\n", + " values[i] = val1;\n", " }\n", " else {\n", - " set values w/= i <- val2;\n", + " values[i] = val2;\n", " }\n", "\n", " }\n", "\n", " for i in len1+1..len1+len2 {\n", " if (i % 2 == 0) {\n", - " set values w/= i <- val3;\n", + " values[i] = val3;\n", " }\n", " else {\n", - " set values w/= i <- val4;\n", + " values[i] = val4;\n", " }\n", " }\n", "\n", " for i in len1+len2+1..valLength-1 {\n", " if (i % 2 == 0) {\n", - " set values w/= i <- val1;\n", + " values[i] = val1;\n", " }\n", " else {\n", - " set values w/= i <- val2;\n", + " values[i] = val2;\n", " }\n", " }\n", " return values;\n", diff --git a/samples/language/CopyAndUpdateOperator.qs b/samples/language/CopyAndUpdateOperator.qs deleted file mode 100644 index 35644d2913..0000000000 --- a/samples/language/CopyAndUpdateOperator.qs +++ /dev/null @@ -1,36 +0,0 @@ -// # Sample -// Copy and Update Operator -// -// # Description -// The copy and update operator in Q# is used to make a copy of an -// array and update a single element in the copied version. - -function Main() : Unit { - let array = [10, 11, 12, 13]; - - // `w/` followed by the `<-` copies and updates a single element. - - // `new_array` is an array with values `[10, 11, 100, 13]`. - // `array` is unchanged. - let new_array = array w/ 2 <- 100; - Message($"Updated array: {new_array}"); - - // `new_array` is an array with values `[10, 100, 12, 200]`. - // `array` is unchanged. - let new_array = array - w/ 1 <- 100 - w/ 3 <- 200; - Message($"Updated array: {new_array}"); - - // In addition to arrays, we can also copy-and-update structs. - // First, let's define a struct called `Complex` which represents a - // complex number. - struct Complex { Real : Double, Imaginary : Double } - // Instantiation of the above struct. - let complex = new Complex { Real = 42.0, Imaginary = 0.0 }; - - // `new_complex` is a new instance of the `Complex` struct with the - // `Real` field updated to `100.0`. - // `complex` is unchanged. - let new_complex = new Complex { ...complex, Real = 100.0 }; -} diff --git a/samples/notebooks/iterative_phase_estimation.ipynb b/samples/notebooks/iterative_phase_estimation.ipynb index 5c6cf8aa95..e5afc6ba5f 100644 --- a/samples/notebooks/iterative_phase_estimation.ipynb +++ b/samples/notebooks/iterative_phase_estimation.ipynb @@ -253,7 +253,7 @@ " }\n", " H(ControlReg);\n", " //Make a measurement mid circuit\n", - " set MeasureControlReg w/= (Measurements - 1 - index) <- MResetZ(ControlReg);\n", + " MeasureControlReg[Measurements - 1 - index] = MResetZ(ControlReg);\n", " if MeasureControlReg[Measurements - 1 - index] == One {\n", " //Assign bitValue based on previous measurement\n", " set bitValue += 2^(index);\n", From 8c9e0c64789e672d6a553064911f8547b081eac3 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Fri, 16 May 2025 14:53:25 -0700 Subject: [PATCH 4/7] minor improvement for VQE --- samples/algorithms/SimpleVQE.qs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/samples/algorithms/SimpleVQE.qs b/samples/algorithms/SimpleVQE.qs index d9959c5749..3595c7d9e7 100644 --- a/samples/algorithms/SimpleVQE.qs +++ b/samples/algorithms/SimpleVQE.qs @@ -172,6 +172,8 @@ operation SimpleDescent( mutable hadImprovement = false; for i in IndexRange(initialPoint) { mutable nextPoint = bestPoint; // Copy the best point + + // Test +currentStep nextPoint[i] += currentStep; let nextValue = f(nextPoint); // Evaluate quantum part currentAttempt = currentAttempt + 1; @@ -179,10 +181,11 @@ operation SimpleDescent( hadImprovement = true; bestValue = nextValue; bestPoint = nextPoint; - nextPoint[i] -= currentStep; Message($"Value improved to {bestValue}."); } - nextPoint[i] -= currentStep; + + // Test -currentStep + nextPoint[i] -= 2.0 * currentStep; let nextValue = f(nextPoint); // Evaluate quantum part currentAttempt = currentAttempt + 1; if nextValue < bestValue { From 75bbba442be084d4b811b7fe1bba08087bf72641 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Fri, 16 May 2025 15:09:32 -0700 Subject: [PATCH 5/7] . --- samples/algorithms/SimpleVQE.qs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/algorithms/SimpleVQE.qs b/samples/algorithms/SimpleVQE.qs index 3595c7d9e7..73258d48e5 100644 --- a/samples/algorithms/SimpleVQE.qs +++ b/samples/algorithms/SimpleVQE.qs @@ -173,7 +173,7 @@ operation SimpleDescent( for i in IndexRange(initialPoint) { mutable nextPoint = bestPoint; // Copy the best point - // Test +currentStep + // Test bestPoint[i] + currentStep nextPoint[i] += currentStep; let nextValue = f(nextPoint); // Evaluate quantum part currentAttempt = currentAttempt + 1; @@ -184,7 +184,7 @@ operation SimpleDescent( Message($"Value improved to {bestValue}."); } - // Test -currentStep + // Test bestPoint[i] - currentStep nextPoint[i] -= 2.0 * currentStep; let nextValue = f(nextPoint); // Evaluate quantum part currentAttempt = currentAttempt + 1; From aefc33dd7909d1356944bb1de10fe0f26e900722 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Fri, 16 May 2025 16:05:36 -0700 Subject: [PATCH 6/7] update tests --- compiler/qsc_partial_eval/src/tests/misc.rs | 2 +- compiler/qsc_partial_eval/src/tests/qubits.rs | 2 +- samples_test/src/tests/language.rs | 8 -------- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/compiler/qsc_partial_eval/src/tests/misc.rs b/compiler/qsc_partial_eval/src/tests/misc.rs index a493abed6a..1a026e4f56 100644 --- a/compiler/qsc_partial_eval/src/tests/misc.rs +++ b/compiler/qsc_partial_eval/src/tests/misc.rs @@ -606,5 +606,5 @@ fn evaluation_error_within_stdlib_yield_correct_package_span() { } "#, }); - assert_error(&error, &expect!["UnexpectedDynamicValue(PackageSpan { package: PackageId(1), span: Span { lo: 13910, hi: 13925 } })"]); + assert_error(&error, &expect!["UnexpectedDynamicValue(PackageSpan { package: PackageId(1), span: Span { lo: 13902, hi: 13917 } })"]); } diff --git a/compiler/qsc_partial_eval/src/tests/qubits.rs b/compiler/qsc_partial_eval/src/tests/qubits.rs index f9985a4c82..bb0e3bb9e2 100644 --- a/compiler/qsc_partial_eval/src/tests/qubits.rs +++ b/compiler/qsc_partial_eval/src/tests/qubits.rs @@ -377,6 +377,6 @@ fn qubit_relabel_in_dynamic_block_triggers_capability_error() { assert_error( &error, - &expect!["CapabilityError(UseOfDynamicQubit(Span { lo: 60173, hi: 60186 }))"], + &expect!["CapabilityError(UseOfDynamicQubit(Span { lo: 60168, hi: 60181 }))"], ); } diff --git a/samples_test/src/tests/language.rs b/samples_test/src/tests/language.rs index ac93ccce2c..844842ee41 100644 --- a/samples_test/src/tests/language.rs +++ b/samples_test/src/tests/language.rs @@ -136,14 +136,6 @@ pub const CONDITIONALBRANCHING_EXPECT_DEBUG: Expect = expect![[r#" It is livable Absolute value of -40 is 40 ()"#]]; -pub const COPYANDUPDATEOPERATOR_EXPECT: Expect = expect![[r#" - Updated array: [10, 11, 100, 13] - Updated array: [10, 100, 12, 200] - ()"#]]; -pub const COPYANDUPDATEOPERATOR_EXPECT_DEBUG: Expect = expect![[r#" - Updated array: [10, 11, 100, 13] - Updated array: [10, 100, 12, 200] - ()"#]]; pub const CUSTOMMEASUREMENTS_EXPECT: Expect = expect!["Zero"]; pub const CUSTOMMEASUREMENTS_EXPECT_DEBUG: Expect = expect!["Zero"]; pub const DATATYPES_EXPECT: Expect = expect![[r#" From f75c3140c8573061af20042578757b5bf349f33c Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Fri, 16 May 2025 16:12:50 -0700 Subject: [PATCH 7/7] Add copilot instructions to avoid `w/=` and `w/` operators. --- vscode/resources/chat-instructions/qsharp.instructions.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vscode/resources/chat-instructions/qsharp.instructions.md b/vscode/resources/chat-instructions/qsharp.instructions.md index 81b0e3ff08..eab3f715ed 100644 --- a/vscode/resources/chat-instructions/qsharp.instructions.md +++ b/vscode/resources/chat-instructions/qsharp.instructions.md @@ -16,6 +16,8 @@ While many Q# operators are C-like, it uses `or` instead of `||` and `and` inste To extract values from a tuple, use destructuring via the `let (item0, item1) = tupleValue;` syntax. +Do not use the `w/=` or `w/` syntax for updating arrays. Instead prefer assignment syntax like `array[index] = value`. + ## Project structure ### Single-file projects