Remove parenthesis around the condition in the if block (p3) (#862)

This commit is contained in:
Manvi-Agrawal 2022-12-19 07:29:56 +05:30 коммит произвёл GitHub
Родитель 370b382aa6
Коммит 77cb481ee5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
18 изменённых файлов: 68 добавлений и 68 удалений

Просмотреть файл

@ -266,7 +266,7 @@ namespace Quantum.Kata.Superposition {
for i in 0 .. 3 {
set numbers w/= i <- DrawRandomInt(0, 1 <<< N - 1);
for j in 0 .. i - 1 {
if (numbers[i] == numbers[j]) {
if numbers[i] == numbers[j] {
set ok = false;
}
}

Просмотреть файл

@ -80,7 +80,7 @@ namespace Quantum.Kata.UnitaryPatterns {
let N = Length(qs);
// for N = 1, we need an identity
if (N > 1) {
if N > 1 {
// do the bottom-right quarter
ApplyToEachCA(Controlled H([Tail(qs)], _), Most(qs));
// do the top-left quarter by calling the same operation recursively
@ -151,7 +151,7 @@ namespace Quantum.Kata.UnitaryPatterns {
// Helper function for Embedding_Perm: finds first location where bit strings differ.
function FirstDiff (bits1 : Bool[], bits2 : Bool[]) : Int {
for i in 0 .. Length(bits1)-1 {
if (bits1[i] != bits2[i]) {
if bits1[i] != bits2[i] {
return i;
}
}
@ -171,24 +171,24 @@ namespace Quantum.Kata.UnitaryPatterns {
// we care only about 2 inputs: basis state of bits1 and bits2
// make sure that the state corresponding to bits1 has qs[diff] set to |0⟩
if (bits1[diff]) {
if bits1[diff] {
X(qs[diff]);
}
// iterate through the bit strings again, setting the final state of qubits
for i in 0..n-1 {
if (bits1[i] == bits2[i]) {
if bits1[i] == bits2[i] {
// if two bits are the same, set both to 1 using X or nothing
if (not bits1[i]) {
if not bits1[i] {
X(qs[i]);
}
} else {
// if two bits are different, set both to 1 using CNOT
if (i > diff) {
if (not bits1[i]) {
if i > diff {
if not bits1[i] {
(ControlledOnInt(0,X))([qs[diff]], qs[i]);
}
if (not bits2[i]) {
if not bits2[i] {
CNOT(qs[diff], qs[i]);
}
}
@ -196,7 +196,7 @@ namespace Quantum.Kata.UnitaryPatterns {
}
// move the differing bit to the last qubit
if (diff < n-1) {
if diff < n-1 {
SWAP(qs[n-1], qs[diff]);
}
}

Просмотреть файл

@ -178,10 +178,10 @@ namespace Quantum.Kata.UnitaryPatterns {
function TwoPatterns_Pattern (size : Int, row : Int, col : Int) : Bool {
// top right and bottom left quarters are all 0
let s2 = size / 2;
if (row / s2 != col / s2) {
if row / s2 != col / s2 {
return false;
}
if (row / s2 == 0) {
if row / s2 == 0 {
// top left quarter is an anti-diagonal
return row % s2 + col % s2 == s2 - 1;
}
@ -201,12 +201,12 @@ namespace Quantum.Kata.UnitaryPatterns {
function IncreasingBlocks_Pattern (size : Int, row : Int, col : Int) : Bool {
// top right and bottom left quarters are all 0
let s2 = size / 2;
if (row / s2 != col / s2) {
if row / s2 != col / s2 {
return false;
}
if (row / s2 == 0) {
if row / s2 == 0 {
// top left quarter is the same pattern for s2, except for the start of the recursion
if (s2 == 1) {
if s2 == 1 {
return true;
}
return IncreasingBlocks_Pattern(s2, row, col);

Просмотреть файл

@ -1416,29 +1416,29 @@
" let diff = 0; // The index of the first bit at which the bit strings are different is 0 (successive indexes)\n",
" // we care only about 2 inputs: basis state of bits1 and bits2\n",
" // make sure that the state corresponding to bits1 has qs[diff] set to 0\n",
" if (bits1[diff]){\n",
" if bits1[diff] {\n",
" X(qs[diff]);\n",
" }\n",
" // iterate through the bit strings again, setting the final state of qubits\n",
" for i in 0 .. n-1 {\n",
" if (bits1[i] == bits2[i]) { // if two bits are the same, set both to 1 using X or nothing\n",
" if (not bits1[i]) {\n",
" if bits1[i] == bits2[i] { // if two bits are the same, set both to 1 using X or nothing\n",
" if not bits1[i] {\n",
" X(qs[i]);\n",
" }\n",
" } else { // if two bits are different, set both to 1 using CNOT\n",
" if (i > diff) {\n",
" if (not bits1[i]) {\n",
" if i > diff {\n",
" if not bits1[i] {\n",
" X(qs[diff]);\n",
" CNOT(qs[diff], qs[i]);\n",
" X(qs[diff]);\n",
" }\n",
" if (not bits2[i]){\n",
" if not bits2[i] {\n",
" CNOT(qs[diff], qs[i]);\n",
" }\n",
" }\n",
" }\n",
" }\n",
" if (diff < n-1) {\n",
" if diff < n-1 {\n",
" SWAP(qs[n-1], qs[diff]); // move the differing bit to the last qubit\n",
" }\n",
"}"

Просмотреть файл

@ -40,7 +40,7 @@ namespace Quantum.Kata.DeutschJozsaAlgorithm {
mutable nOnes = 0;
mutable xBits = x;
while (xBits > 0) {
if (xBits % 2 > 0) {
if xBits % 2 > 0 {
set nOnes += 1;
}
set xBits /= 2;

Просмотреть файл

@ -86,7 +86,7 @@
" mutable nOnes = 0;\n",
" mutable xBits = x;\n",
" while (xBits > 0) {\n",
" if (xBits % 2 > 0) {\n",
" if xBits % 2 > 0 {\n",
" set nOnes += 1;\n",
" }\n",
" set xBits /= 2;\n",

Просмотреть файл

@ -34,7 +34,7 @@ namespace Quantum.Kata.DeutschJozsaAlgorithm {
// Try all the following inputs to see if any of the values differ; should be run 2ᴺ⁻¹ times
for input in 1 .. 2 ^ (N - 1) {
let nextValue = f(input);
if (nextValue != firstValue) {
if nextValue != firstValue {
// Got two different values - the function is balanced
return false;
}
@ -83,7 +83,7 @@ namespace Quantum.Kata.DeutschJozsaAlgorithm {
oracle(x);
ApplyToEach(H, x);
for q in x {
if (M(q) == One) {
if M(q) == One {
set isConstantFunction = false;
}
}

Просмотреть файл

@ -48,7 +48,7 @@ namespace Quantum.Kata.DeutschJozsaAlgorithm {
let actual = IsFunctionConstant_Classical(N, f);
// check that the return value is correct
if (actual != expected) {
if actual != expected {
let actualStr = ConstantOrBalanced(actual);
let expectedStr = ConstantOrBalanced(expected);
fail $" identified as {actualStr} but it is {expectedStr}.";
@ -87,14 +87,14 @@ namespace Quantum.Kata.DeutschJozsaAlgorithm {
let actual = DeutschAlgorithm(oracle);
// check that the return value is correct
if (actual != expected) {
if actual != expected {
let actualStr = ConstantOrBalanced(actual);
let expectedStr = ConstantOrBalanced(expected);
fail $" identified as {actualStr} but it is {expectedStr}.";
}
let nu = GetOracleCallsCount(oracle);
if (nu > 1) {
if nu > 1 {
fail $" took {nu} oracle calls to decide; you are only allowed to call the oracle once";
}
@ -137,14 +137,14 @@ namespace Quantum.Kata.DeutschJozsaAlgorithm {
let actual = DeutschJozsaAlgorithm(N, oracle);
// check that the return value is correct
if (actual != expected) {
if actual != expected {
let actualStr = ConstantOrBalanced(actual);
let expectedStr = ConstantOrBalanced(expected);
fail $" identified as {actualStr} but it is {expectedStr}.";
}
let nu = GetOracleCallsCount(oracle);
if (nu > 1) {
if nu > 1 {
fail $" took {nu} oracle calls to decide; you are only allowed to call the oracle once";
}

Просмотреть файл

@ -177,7 +177,7 @@ namespace Quantum.Kata.ExploringGroversAlgorithm
GroversAlgorithm_Loop(register, oracle, iter);
let res = MultiM(register);
oracle(register, answer);
if (MResetZ(answer) == One) {
if MResetZ(answer) == One {
set correct += 1;
}
ResetAll(register);
@ -205,7 +205,7 @@ namespace Quantum.Kata.ExploringGroversAlgorithm
GroversAlgorithm_Loop(register, oracle, iter);
let res = MultiM(register);
oracle(register, answer);
if (MResetZ(answer) == One) {
if MResetZ(answer) == One {
set correct += 1;
}
ResetAll(register);

Просмотреть файл

@ -311,7 +311,7 @@
" // One means the algorithm returned correct answer, Zero - incorrect.\n",
" // You can measure the qubit and reset it immediately using MResetZ operation, \n",
" // and compare the measurement result to the constants Zero or One using \"==\" operator.\n",
" if (...) {\n",
" if ... {\n",
" // Report the correct/incorrect result using Message function.\n",
" Message(...);\n",
" } else {\n",
@ -343,7 +343,7 @@
" <summary><b>Click here for the complete answer validation code</b></summary>\n",
"<code> use y = Qubit() {\n",
" oracle(register, y);\n",
" if (MResetZ(y) == One) {\n",
" if MResetZ(y) == One {\n",
" Message(\"Correct!\");\n",
" } else {\n",
" Message(\"Incorrect\");\n",
@ -552,7 +552,7 @@
" let answer = ResultArrayAsBoolArray(MultiM(register));\n",
" use y = Qubit() {\n",
" oracle(register, y);\n",
" if (MResetZ(y) == One) {\n",
" if MResetZ(y) == One {\n",
" set successCount += 1;\n",
" }\n",
" }\n",

Просмотреть файл

@ -305,7 +305,7 @@
"\n",
"operation MultiControls (controls : Qubit[], target : Qubit, controlBits : Bool[]) : Unit is Adj {\n",
" for (index in 0 .. Length(controls) - 1) {\n",
" if (controlBits[index] == false) {\n",
" if controlBits[index] == false {\n",
" X(controls[index]); \n",
" }\n",
" }\n",
@ -313,7 +313,7 @@
" Controlled X(controls,target);\n",
"\n",
" for (index in 0 .. Length(controls) - 1) {\n",
" if (controlBits[index] == false) {\n",
" if controlBits[index] == false {\n",
" X(controls[index]); \n",
" }\n",
" }\n",
@ -338,7 +338,7 @@
"operation MultiControls (controls : Qubit[], target : Qubit, controlBits : Bool[]) : Unit is Adj {\n",
" within {\n",
" for (index in 0 .. Length(controls) - 1) {\n",
" if (controlBits[index] == false) {\n",
" if controlBits[index] == false {\n",
" X(controls[index]); \n",
" }\n",
" }\n",

Просмотреть файл

@ -146,7 +146,7 @@
" let register = LittleEndian(qs);\n",
" // Prepare the state using PrepareArbitraryStateD library operation:\n",
" PrepareArbitraryStateD(coefficients, register);\n",
" if (i == 1) {\n",
" if i == 1 {\n",
" Message(\"The state |𝜓❭ of the system before measurement is:\");\n",
" DumpMachine();\n",
" } \n",
@ -331,7 +331,7 @@
" PrepareArbitraryStateD(coefficients, LittleEndian(qs));\n",
" \n",
" // Display the state of the qubits.\n",
" if (i == 1) {\n",
" if i == 1 {\n",
" Message(\"The state |𝜓❭ of the system before measurement is:\");\n",
" DumpMachine();\n",
" Message(divider);\n",
@ -341,7 +341,7 @@
" let outcome = M(qs[0]) == Zero ? 0 | 1;\n",
" set countArray w/= outcome <- countArray[outcome] + 1;\n",
" \n",
" if (countArray[outcome] == 1) { \n",
" if countArray[outcome] == 1 { \n",
" // The first time the outcome is 0/1, print the system state afterwards.\n",
" Message(\"For outcome {outcome}, the post-measurement state of the system is:\");\n",
" DumpMachine();\n",

Просмотреть файл

@ -28,13 +28,13 @@ namespace Quantum.Kata.MultiQubitSystemMeasurements {
// Exercise 7. State selection using partial measurements
operation StateSelction_Reference(qs : Qubit[], i : Int) : Unit {
if (i == 0) {
if (M(qs[0]) == One){
if i == 0 {
if M(qs[0]) == One {
// apply the X gate to the second qubit
X(qs[1]);
}
} else {
if (M(qs[0]) == Zero){
if M(qs[0]) == Zero {
// apply the X gate to the second qubit only
X(qs[1]);
}

Просмотреть файл

@ -47,7 +47,7 @@ namespace Quantum.Kata.MultiQubitSystemMeasurements {
let ans = testImpl(qs);
if ((ans >= 0) and (ans < nStates)) {
// classification result is a valid state index - check if is it correct
if (ans != state) {
if ans != state {
set misclassifications w/= ((state * nStates) + ans) <- (misclassifications[(state * nStates) + ans] + 1);
}
}
@ -56,7 +56,7 @@ namespace Quantum.Kata.MultiQubitSystemMeasurements {
set unknownClassifications w/= state <- (unknownClassifications[state] + 1);
}
if (preserveState) {
if preserveState {
// check that the state of the qubit after the operation is unchanged
Adjoint statePrep(qs, state, alpha);
AssertAllZero(qs);
@ -69,12 +69,12 @@ namespace Quantum.Kata.MultiQubitSystemMeasurements {
mutable totalMisclassifications = 0;
for i in 0 .. nStates - 1 {
for j in 0 .. nStates - 1 {
if (misclassifications[(i * nStates) + j] != 0) {
if misclassifications[(i * nStates) + j] != 0 {
set totalMisclassifications += misclassifications[i * nStates + j];
Message($"Misclassified {stateNames[i]} as {stateNames[j]} in {misclassifications[(i * nStates) + j]} test runs.");
}
}
if (unknownClassifications[i] != 0) {
if unknownClassifications[i] != 0 {
set totalMisclassifications += unknownClassifications[i];
Message($"Misclassified {stateNames[i]} as Unknown State in {unknownClassifications[i]} test runs.");
}
@ -87,11 +87,11 @@ namespace Quantum.Kata.MultiQubitSystemMeasurements {
// Exercise 3: Distinguish four basis states
// ------------------------------------------------------
operation StatePrep_BasisStateMeasurement(qs : Qubit[], state : Int, dummyVar : Double) : Unit is Adj {
if (state / 2 == 1) {
if state / 2 == 1 {
// |10⟩ or |11⟩
X(qs[0]);
}
if (state % 2 == 1) {
if state % 2 == 1 {
// |01⟩ or |11⟩
X(qs[1]);
}
@ -107,7 +107,7 @@ namespace Quantum.Kata.MultiQubitSystemMeasurements {
// Exercise 5: Distinguish orthogonal states using partial measurements
// ------------------------------------------------------
operation StatePrep_IsPlusPlusMinus (qs : Qubit[], state : Int, dummyVar : Double) : Unit is Adj{
if (state == 0){
if state == 0 {
// prepare the state |++-⟩
H(qs[0]);
H(qs[1]);
@ -152,7 +152,7 @@ namespace Quantum.Kata.MultiQubitSystemMeasurements {
// set the second qubit in a superposition a |0⟩ + b|1⟩
// with a = cos alpha, b = sin alpha
Ry(2.0 * alpha, qs[1]);
if (Choice == 1) {
if Choice == 1 {
// if the Choice is 1, change the state to b|0⟩ + a|1⟩
X(qs[1]);
}
@ -227,7 +227,7 @@ namespace Quantum.Kata.MultiQubitSystemMeasurements {
CNOT(qs[0], qs[i]);
}
if (state == 1) {
if state == 1 {
// flip the state of the first half of the qubits
for i in 0 .. Length(qs) / 2 - 1 {
X(qs[i]);

Просмотреть файл

@ -98,7 +98,7 @@ namespace Quantum.Kata.Oracles {
operation ArbitraryBitPattern_Oracle_Challenge_Reference (x : Qubit[], pattern : Bool[]) : Unit is Adj + Ctl {
within {
for i in IndexRange(x) {
if (not pattern[i]) {
if not pattern[i] {
X(x[i]);
}
}

Просмотреть файл

@ -135,7 +135,7 @@ namespace Quantum.Kata.RandomNumberGeneration {
}
for i in min..max {
if (counts[i] < Floor(minimumCopiesGenerated)) {
if counts[i] < Floor(minimumCopiesGenerated) {
Message($"Unexpectedly low number of {i}'s generated. Only {counts[i]} out of {nRuns} were {i}");
return false;
}
@ -147,7 +147,7 @@ namespace Quantum.Kata.RandomNumberGeneration {
mutable totalCount = 0;
for i in 0 .. arrSize - 1 {
set totalCount = totalCount + counts[i];
if (totalCount >= sampleSize / 2) {
if totalCount >= sampleSize / 2 {
return i;
}
}
@ -196,15 +196,15 @@ namespace Quantum.Kata.RandomNumberGeneration {
let goalZeroCount = (x == 0.0) ? 0 | (x == 1.0) ? nRuns | Floor(x * IntAsDouble(nRuns));
// We don't have tests with probabilities near 0.0 or 1.0, so for those the matching has to be exact
if (goalZeroCount == 0 or goalZeroCount == nRuns) {
if (zeroCount != goalZeroCount) {
if zeroCount != goalZeroCount {
Message($"Expected {x * 100.0}% 0's, instead got {zeroCount} 0's out of {nRuns}");
return false;
}
} else {
if (zeroCount < goalZeroCount - 4 * nRuns / 100) {
if zeroCount < goalZeroCount - 4 * nRuns / 100 {
Message($"Unexpectedly low number of 0's generated: expected around {x * IntAsDouble(nRuns)} 0's, got {zeroCount} out of {nRuns}");
return false;
} elif (zeroCount > goalZeroCount + 4 * nRuns / 100) {
} elif zeroCount > goalZeroCount + 4 * nRuns / 100 {
Message($"Unexpectedly high number of 0's generated: expected around {x * IntAsDouble(nRuns)} 0's, got {zeroCount} out of {nRuns}");
return false;
}

Просмотреть файл

@ -159,7 +159,7 @@
" Ry(2.0 * ArcTan2(0.8, 0.6), q);\n",
"\n",
" // Measure in the computational basis, and update the counts according to the outcomes\n",
" if (M(q) == Zero) {\n",
" if M(q) == Zero {\n",
" set countZero += 1;\n",
" } \n",
" // Reset the qubit for use in the next iteration\n",

Просмотреть файл

@ -38,12 +38,12 @@ namespace Quantum.Kata.SingleQubitSystemMeasurements {
// 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)) {
if ans != (state == 1) {
set misclassifications w/= state <- misclassifications[state] + 1;
}
// If the final state is to be verified, check if it matches the measurement outcome
if (checkFinalState) {
if checkFinalState {
Adjoint statePrep(q, state);
AssertQubit(Zero, q);
} else {
@ -53,7 +53,7 @@ namespace Quantum.Kata.SingleQubitSystemMeasurements {
mutable totalMisclassifications = 0;
for i in 0 .. nStates - 1 {
if (misclassifications[i] != 0) {
if misclassifications[i] != 0 {
set totalMisclassifications += misclassifications[i];
Message($"Misclassified {stateName[i]} as {stateName[1 - i]} in {misclassifications[i]} test runs.");
}
@ -67,7 +67,7 @@ namespace Quantum.Kata.SingleQubitSystemMeasurements {
// Exercise 2. Distinguish |0❭ and |1❭
// ------------------------------------------------------
operation StatePrep_IsQubitZero (q : Qubit, state : Int) : Unit is Adj {
if (state == 0) {
if state == 0 {
// convert |0⟩ to |1⟩
X(q);
}
@ -84,7 +84,7 @@ namespace Quantum.Kata.SingleQubitSystemMeasurements {
// Exercise 3. Distinguish |+❭ and |-❭ using Measure operation
// ------------------------------------------------------
operation StatePrep_IsQubitMinus (q : Qubit, state : Int) : Unit is Adj {
if (state == 1) {
if state == 1 {
// convert |0⟩ to |-⟩
X(q);
H(q);
@ -106,7 +106,7 @@ namespace Quantum.Kata.SingleQubitSystemMeasurements {
// |ψ₊⟩ = 0.6 * |0⟩ + 0.8 * |1⟩,
// |ψ₋⟩ = -0.8 * |0⟩ + 0.6 * |1⟩.
operation StatePrep_IsQubitPsiPlus (q : Qubit, state : Int) : Unit is Adj {
if (state == 0) {
if state == 0 {
// convert |0⟩ to |ψ₋⟩
X(q);
Ry(2.0 * ArcTan2(0.8, 0.6), q);
@ -130,7 +130,7 @@ namespace Quantum.Kata.SingleQubitSystemMeasurements {
// |A⟩ = cos(alpha) * |0⟩ - i sin(alpha) * |1⟩,
// |B⟩ = - i sin(alpha) * |0⟩ + cos(alpha) * |1⟩.
operation StatePrep_IsQubitA (alpha : Double, q : Qubit, state : Int) : Unit is Adj {
if (state == 0) {
if state == 0 {
// convert |0⟩ to |B⟩
X(q);
Rx(2.0 * alpha, q);