From aafada8f744254b6be06b03349e3ed72bf9c1597 Mon Sep 17 00:00:00 2001 From: James Kingdon Date: Mon, 10 Jun 2024 18:37:27 -0400 Subject: [PATCH 01/15] add katas 6, 7, 8 --- katas/content/KatasLibrary.qs | 18 ++++++++++- .../marking_oracles/balanced/Placeholder.qs | 14 ++++++++ .../marking_oracles/balanced/Solution.qs | 17 ++++++++++ .../marking_oracles/balanced/Verification.qs | 21 ++++++++++++ .../content/marking_oracles/balanced/index.md | 11 +++++++ .../marking_oracles/balanced/solution.md | 6 ++++ .../contains_substring/Placeholder.qs | 11 +++++++ .../contains_substring/Solution.qs | 19 +++++++++++ .../contains_substring/Verification.qs | 32 +++++++++++++++++++ .../contains_substring/index.md | 13 ++++++++ .../contains_substring/solution.md | 6 ++++ .../contains_substring_p/Verification.qs | 13 ++------ katas/content/marking_oracles/index.md | 27 ++++++++++++++++ .../marking_oracles/majority/Placeholder.qs | 14 ++++++++ .../marking_oracles/majority/Solution.qs | 17 ++++++++++ .../marking_oracles/majority/Verification.qs | 22 +++++++++++++ .../content/marking_oracles/majority/index.md | 11 +++++++ .../marking_oracles/majority/solution.md | 6 ++++ 18 files changed, 266 insertions(+), 12 deletions(-) create mode 100644 katas/content/marking_oracles/balanced/Placeholder.qs create mode 100644 katas/content/marking_oracles/balanced/Solution.qs create mode 100644 katas/content/marking_oracles/balanced/Verification.qs create mode 100644 katas/content/marking_oracles/balanced/index.md create mode 100644 katas/content/marking_oracles/balanced/solution.md create mode 100644 katas/content/marking_oracles/contains_substring/Placeholder.qs create mode 100644 katas/content/marking_oracles/contains_substring/Solution.qs create mode 100644 katas/content/marking_oracles/contains_substring/Verification.qs create mode 100644 katas/content/marking_oracles/contains_substring/index.md create mode 100644 katas/content/marking_oracles/contains_substring/solution.md create mode 100644 katas/content/marking_oracles/majority/Placeholder.qs create mode 100644 katas/content/marking_oracles/majority/Solution.qs create mode 100644 katas/content/marking_oracles/majority/Verification.qs create mode 100644 katas/content/marking_oracles/majority/index.md create mode 100644 katas/content/marking_oracles/majority/solution.md diff --git a/katas/content/KatasLibrary.qs b/katas/content/KatasLibrary.qs index 7b244384ae..39d4cc8d8d 100644 --- a/katas/content/KatasLibrary.qs +++ b/katas/content/KatasLibrary.qs @@ -334,6 +334,13 @@ namespace Microsoft.Quantum.Katas { return true; } + operation IncrementBE (register : Qubit[]) : Unit is Adj + Ctl { + if Length(register) > 1 { + Controlled IncrementBE([register[0]], register[1 ...]); + } + X(register[0]); + } + function F_PeriodicGivenPeriod(args : Bool[], P : Int) : Bool { let N = Length(args); for i in 0 .. N - P - 1 { @@ -342,5 +349,14 @@ namespace Microsoft.Quantum.Katas { } } return true; - } + } + + function F_ContainsSubstringAtPosition(args : Bool[], r : Bool[], p : Int) : Bool { + for i in 0 .. Length(r) - 1 { + if r[i] != args[i + p] { + return false; + } + } + return true; + } } diff --git a/katas/content/marking_oracles/balanced/Placeholder.qs b/katas/content/marking_oracles/balanced/Placeholder.qs new file mode 100644 index 0000000000..1cb23c1ac6 --- /dev/null +++ b/katas/content/marking_oracles/balanced/Placeholder.qs @@ -0,0 +1,14 @@ +namespace Kata { + operation Oracle_Balanced (x : Qubit[], y : Qubit) : Unit is Adj + Ctl { + // Implement your solution here... + + } + + // Helper operation that implements increment for a qubit register + operation IncrementBE (register : Qubit[]) : Unit is Adj + Ctl { + if Length(register) > 1 { + Controlled IncrementBE([register[0]], register[1 ...]); + } + X(register[0]); + } +} diff --git a/katas/content/marking_oracles/balanced/Solution.qs b/katas/content/marking_oracles/balanced/Solution.qs new file mode 100644 index 0000000000..b241ffeb35 --- /dev/null +++ b/katas/content/marking_oracles/balanced/Solution.qs @@ -0,0 +1,17 @@ +namespace Kata { + open Microsoft.Quantum.Katas; + open Microsoft.Quantum.Math; + + operation Oracle_Balanced (x : Qubit[], y : Qubit) : Unit is Adj + Ctl { + let N = Length(x); + let log = BitSizeI(N); + use inc = Qubit[log]; + within { + for q in x { + Controlled IncrementBE([q], inc); + } + } apply { + ApplyControlledOnInt(N / 2, X, inc, y); + } + } +} diff --git a/katas/content/marking_oracles/balanced/Verification.qs b/katas/content/marking_oracles/balanced/Verification.qs new file mode 100644 index 0000000000..5e70562036 --- /dev/null +++ b/katas/content/marking_oracles/balanced/Verification.qs @@ -0,0 +1,21 @@ +namespace Kata.Verification { + open Microsoft.Quantum.Katas; + open Microsoft.Quantum.Arrays; + + function F_Balanced(args : Bool[]) : Bool { + return Count(x -> x, args) == Length(args) / 2; + } + + @EntryPoint() + operation CheckSolution() : Bool { + for n in 2 .. 2 .. 6 { + if not CheckOracleImplementsFunction(n, Kata.Oracle_Balanced, F_Balanced) { + Message($"Test failed for n = {n}"); + return false; + } + } + + Message("Correct!"); + true + } +} diff --git a/katas/content/marking_oracles/balanced/index.md b/katas/content/marking_oracles/balanced/index.md new file mode 100644 index 0000000000..ad084dcaaa --- /dev/null +++ b/katas/content/marking_oracles/balanced/index.md @@ -0,0 +1,11 @@ +**Inputs:** + +1. $N$ qubits in an arbitrary state $\ket{x}$ (input/query register). +2. A qubit in an arbitrary state $\ket{y}$ (output/target qubit). + +**Goal:** +Implement a quantum oracle which checks whether the input register is a balanced bit string, i.e., whether it contains exactly $N/2 0$s and $N/2 1$s. $N$ will be an even number. + +For example, for $N = 4$ basis states $\ket{0011}$ and $\ket{0101}$ are balanced, and $\ket{0010}$ and $\ket{1111}$ are not. +Leave the qubits in the input register in the same state they started in. +Your solution should work on inputs in superposition, and not use any measurements. \ No newline at end of file diff --git a/katas/content/marking_oracles/balanced/solution.md b/katas/content/marking_oracles/balanced/solution.md new file mode 100644 index 0000000000..dac8b8095c --- /dev/null +++ b/katas/content/marking_oracles/balanced/solution.md @@ -0,0 +1,6 @@ +[placeholder for solution text] + +@[solution]({ + "id": "marking_oracles__balanced_solution", + "codePath": "./Solution.qs" +}) diff --git a/katas/content/marking_oracles/contains_substring/Placeholder.qs b/katas/content/marking_oracles/contains_substring/Placeholder.qs new file mode 100644 index 0000000000..e1a7b18f93 --- /dev/null +++ b/katas/content/marking_oracles/contains_substring/Placeholder.qs @@ -0,0 +1,11 @@ +namespace Kata { + operation Oracle_ContainsSubstring (x : Qubit[], y : Qubit, r : Bool[]) : Unit is Adj + Ctl { + // Implement your solution here... + + } + + // You might find this helper operation from an earlier task useful. + operation Oracle_ContainsSubstringAtPosition (x : Qubit[], y : Qubit, r : Bool[], p : Int) : Unit is Adj + Ctl { + ApplyControlledOnBitString(r, X, x[p .. p + Length(r) - 1], y); + } +} diff --git a/katas/content/marking_oracles/contains_substring/Solution.qs b/katas/content/marking_oracles/contains_substring/Solution.qs new file mode 100644 index 0000000000..950872ddf8 --- /dev/null +++ b/katas/content/marking_oracles/contains_substring/Solution.qs @@ -0,0 +1,19 @@ +namespace Kata { + operation Oracle_ContainsSubstring (x : Qubit[], y : Qubit, r : Bool[]) : Unit is Adj + Ctl { + let N = Length(x); + let K = Length(r); + use aux = Qubit[N - K + 1]; + within { + for P in 0 .. N - K { + Oracle_ContainsSubstringAtPosition(x, aux[P], r, P); + } + } apply { + ApplyControlledOnInt(0, X, aux, y); + X(y); + } + } + + operation Oracle_ContainsSubstringAtPosition (x : Qubit[], y : Qubit, r : Bool[], p : Int) : Unit is Adj + Ctl { + ApplyControlledOnBitString(r, X, x[p .. p + Length(r) - 1], y); + } +} diff --git a/katas/content/marking_oracles/contains_substring/Verification.qs b/katas/content/marking_oracles/contains_substring/Verification.qs new file mode 100644 index 0000000000..f397fbe482 --- /dev/null +++ b/katas/content/marking_oracles/contains_substring/Verification.qs @@ -0,0 +1,32 @@ +namespace Kata.Verification { + open Microsoft.Quantum.Katas; + + function F_ContainsSubstring(args : Bool[], r : Bool[]) : Bool { + let N = Length(args); + let K = Length(r); + for P in 0 .. N - K { + if F_ContainsSubstringAtPosition(args, r, P) { + return true; + } + } + return false; + } + + @EntryPoint() + operation CheckSolution() : Bool { + for (n, r) in [ + (2, [true]), + (3, [false, true]), + (4, [true, true, false]), + (5, [false, false]) + ] { + if not CheckOracleImplementsFunction(n, Kata.Oracle_ContainsSubstring(_, _, r), F_ContainsSubstring(_, r)) { + Message($"Test failed for n = {n}, r = {r}"); + return false; + } + } + + Message("Correct!"); + true + } +} diff --git a/katas/content/marking_oracles/contains_substring/index.md b/katas/content/marking_oracles/contains_substring/index.md new file mode 100644 index 0000000000..5aac236ba8 --- /dev/null +++ b/katas/content/marking_oracles/contains_substring/index.md @@ -0,0 +1,13 @@ +**Inputs:** + +1. $N$ qubits in an arbitrary state $\ket{x}$ (input/query register). +2. A qubit in an arbitrary state $\ket{y}$ (output/target qubit). +3. A bit pattern $r$ of length $K$ represented as a `Bool[]` ($1 ≤ K ≤ N$). + +**Goal:** +Implement a quantum oracle which checks whether the input register contains the given pattern, i.e., whether there exists a position $P$ such that for all $j$ between $0$ and $K - 1$, inclusive, $r_j = x_{j+P}$. + +For example, for $N = 3$ a bit string `[false, true, false]` contains a pattern `[true, false]` (starting at index 1). + +Leave the qubits in the input register in the same state they started in. +Your solution should work on inputs in superposition, and not use any measurements. \ No newline at end of file diff --git a/katas/content/marking_oracles/contains_substring/solution.md b/katas/content/marking_oracles/contains_substring/solution.md new file mode 100644 index 0000000000..564a9b713c --- /dev/null +++ b/katas/content/marking_oracles/contains_substring/solution.md @@ -0,0 +1,6 @@ +[placeholder for solution text] + +@[solution]({ + "id": "marking_oracles__contains_substring_solution", + "codePath": "./Solution.qs" +}) diff --git a/katas/content/marking_oracles/contains_substring_p/Verification.qs b/katas/content/marking_oracles/contains_substring_p/Verification.qs index 900b71ad75..abcca37b57 100644 --- a/katas/content/marking_oracles/contains_substring_p/Verification.qs +++ b/katas/content/marking_oracles/contains_substring_p/Verification.qs @@ -1,14 +1,5 @@ namespace Kata.Verification { - open Microsoft.Quantum.Katas; - - function ContainsSubstringAtPositionF(args : Bool[], r : Bool[], p : Int) : Bool { - for i in 0 .. Length(r) - 1 { - if r[i] != args[i + p] { - return false; - } - } - return true; - } + open Microsoft.Quantum.Katas; @EntryPoint() operation CheckSolution() : Bool { @@ -18,7 +9,7 @@ namespace Kata.Verification { (4, 1, [true, true, false]), (5, 3, [false]) ] { - if not CheckOracleImplementsFunction(n, Kata.Oracle_ContainsSubstringAtPosition(_, _, r, p), ContainsSubstringAtPositionF(_, r, p)) { + if not CheckOracleImplementsFunction(n, Kata.Oracle_ContainsSubstringAtPosition(_, _, r, p), F_ContainsSubstringAtPosition(_, r, p)) { Message($"Test failed for n = {n}, p = {p}, r = {r}"); return false; } diff --git a/katas/content/marking_oracles/index.md b/katas/content/marking_oracles/index.md index 8207d2b447..25d107deed 100644 --- a/katas/content/marking_oracles/index.md +++ b/katas/content/marking_oracles/index.md @@ -101,6 +101,33 @@ This kata continues the exploration of quantum oracles started in the Oracles ka ] }) +@[exercise]({ + "id": "marking_oracles__contains_substring", + "title": "Contains Substring", + "path": "./contains_substring/", + "qsDependencies": [ + "../KatasLibrary.qs" + ] +}) + +@[exercise]({ + "id": "marking_oracles__balanced", + "title": "0s and 1s Balanced", + "path": "./balanced/", + "qsDependencies": [ + "../KatasLibrary.qs" + ] +}) + +@[exercise]({ + "id": "marking_oracles__majority", + "title": "0s or 1s Majority", + "path": "./majority/", + "qsDependencies": [ + "../KatasLibrary.qs" + ] +}) + @[section]({ "id": "marking_oracles__conclusion", "title": "Conclusion" diff --git a/katas/content/marking_oracles/majority/Placeholder.qs b/katas/content/marking_oracles/majority/Placeholder.qs new file mode 100644 index 0000000000..43a3a4fb3c --- /dev/null +++ b/katas/content/marking_oracles/majority/Placeholder.qs @@ -0,0 +1,14 @@ +namespace Kata { + operation Oracle_Majority (x : Qubit[], y : Qubit) : Unit is Adj + Ctl { + // Implement your solution here... + + } + + // Helper operation that implements increment for a qubit register + operation IncrementBE (register : Qubit[]) : Unit is Adj + Ctl { + if Length(register) > 1 { + Controlled IncrementBE([register[0]], register[1 ...]); + } + X(register[0]); + } +} diff --git a/katas/content/marking_oracles/majority/Solution.qs b/katas/content/marking_oracles/majority/Solution.qs new file mode 100644 index 0000000000..d90ade36e7 --- /dev/null +++ b/katas/content/marking_oracles/majority/Solution.qs @@ -0,0 +1,17 @@ +namespace Kata { + open Microsoft.Quantum.Katas; + open Microsoft.Quantum.Math; + + operation Oracle_Majority (x : Qubit[], y : Qubit) : Unit is Adj + Ctl { + let N = Length(x); + let log = BitSizeI(N); + use inc = Qubit[log]; + within { + for q in x { + Controlled IncrementBE([q], inc); + } + } apply { + CNOT(inc[log-1], y); + } + } +} diff --git a/katas/content/marking_oracles/majority/Verification.qs b/katas/content/marking_oracles/majority/Verification.qs new file mode 100644 index 0000000000..d17f051f61 --- /dev/null +++ b/katas/content/marking_oracles/majority/Verification.qs @@ -0,0 +1,22 @@ +namespace Kata.Verification { + open Microsoft.Quantum.Katas; + open Microsoft.Quantum.Arrays; + + function F_Majority(args : Bool[]) : Bool { + let N = Length(args); + return Count(x -> x, args) > (N - 1) / 2; + } + + @EntryPoint() + operation CheckSolution() : Bool { + for n in [3, 7] { + if not CheckOracleImplementsFunction(n, Kata.Oracle_Majority, F_Majority) { + Message($"Test failed for n = {n}"); + return false; + } + } + + Message("Correct!"); + true + } +} diff --git a/katas/content/marking_oracles/majority/index.md b/katas/content/marking_oracles/majority/index.md new file mode 100644 index 0000000000..78da54fc77 --- /dev/null +++ b/katas/content/marking_oracles/majority/index.md @@ -0,0 +1,11 @@ +**Inputs:** + +1. $N$ qubits in an arbitrary state $\ket{x}$ (input/query register). +2. A qubit in an arbitrary state $\ket{y}$ (output/target qubit). + +**Goal:** +Implement a quantum oracle which calculates the majority function, i.e., $f(x) = 1$ if most of the bits in the bit string are $1$'s, and $0$ otherwise. + +For example, for $N = 3$ majority function for basis states $\ket{001}$ and $\ket{000}$ is $0$, and for $\ket{101}$ and $\ket{111}$ is $1$. +Leave the qubits in the input register in the same state they started in. +Your solution should work on inputs in superposition, and not use any measurements. \ No newline at end of file diff --git a/katas/content/marking_oracles/majority/solution.md b/katas/content/marking_oracles/majority/solution.md new file mode 100644 index 0000000000..29417946da --- /dev/null +++ b/katas/content/marking_oracles/majority/solution.md @@ -0,0 +1,6 @@ +[placeholder for solution text] + +@[solution]({ + "id": "marking_oracles__majority_solution", + "codePath": "./Solution.qs" +}) From 4246e9509c2b1035fec7b93ef972dab62d3c52fb Mon Sep 17 00:00:00 2001 From: James Kingdon Date: Tue, 11 Jun 2024 21:01:23 -0400 Subject: [PATCH 02/15] add katas 9 and 10 --- katas/content/KatasLibrary.qs | 7 +++++++ .../bit_sum_div_3/Placeholder.qs | 14 +++++++++++++ .../marking_oracles/bit_sum_div_3/Solution.qs | 13 ++++++++++++ .../bit_sum_div_3/Verification.qs | 21 +++++++++++++++++++ .../marking_oracles/bit_sum_div_3/index.md | 11 ++++++++++ .../marking_oracles/bit_sum_div_3/solution.md | 6 ++++++ katas/content/marking_oracles/index.md | 18 ++++++++++++++++ .../marking_oracles/num_div_3/Placeholder.qs | 14 +++++++++++++ .../marking_oracles/num_div_3/Solution.qs | 20 ++++++++++++++++++ .../marking_oracles/num_div_3/Verification.qs | 21 +++++++++++++++++++ .../marking_oracles/num_div_3/index.md | 12 +++++++++++ .../marking_oracles/num_div_3/solution.md | 6 ++++++ 12 files changed, 163 insertions(+) create mode 100644 katas/content/marking_oracles/bit_sum_div_3/Placeholder.qs create mode 100644 katas/content/marking_oracles/bit_sum_div_3/Solution.qs create mode 100644 katas/content/marking_oracles/bit_sum_div_3/Verification.qs create mode 100644 katas/content/marking_oracles/bit_sum_div_3/index.md create mode 100644 katas/content/marking_oracles/bit_sum_div_3/solution.md create mode 100644 katas/content/marking_oracles/num_div_3/Placeholder.qs create mode 100644 katas/content/marking_oracles/num_div_3/Solution.qs create mode 100644 katas/content/marking_oracles/num_div_3/Verification.qs create mode 100644 katas/content/marking_oracles/num_div_3/index.md create mode 100644 katas/content/marking_oracles/num_div_3/solution.md diff --git a/katas/content/KatasLibrary.qs b/katas/content/KatasLibrary.qs index 39d4cc8d8d..11aa5aa337 100644 --- a/katas/content/KatasLibrary.qs +++ b/katas/content/KatasLibrary.qs @@ -341,6 +341,13 @@ namespace Microsoft.Quantum.Katas { X(register[0]); } + operation IncrementMod3 (counterRegister : Qubit[]) : Unit is Adj + Ctl { + let sum = counterRegister[0]; + let carry = counterRegister[1]; + ApplyControlledOnInt(0, X, [carry], sum); + ApplyControlledOnInt(0, X, [sum], carry); + } + function F_PeriodicGivenPeriod(args : Bool[], P : Int) : Bool { let N = Length(args); for i in 0 .. N - P - 1 { diff --git a/katas/content/marking_oracles/bit_sum_div_3/Placeholder.qs b/katas/content/marking_oracles/bit_sum_div_3/Placeholder.qs new file mode 100644 index 0000000000..674492fd25 --- /dev/null +++ b/katas/content/marking_oracles/bit_sum_div_3/Placeholder.qs @@ -0,0 +1,14 @@ +namespace Kata { + operation Oracle_BitSumDivisibleBy3 (x : Qubit[], y : Qubit) : Unit is Adj + Ctl { + // Implement your solution here... + + } + + // Helper operation that implements counting modulo 3 + operation IncrementMod3 (counterRegister : Qubit[]) : Unit is Adj + Ctl { + let sum = counterRegister[0]; + let carry = counterRegister[1]; + ApplyControlledOnInt(0, X, [carry], sum); + ApplyControlledOnInt(0, X, [sum], carry); + } +} diff --git a/katas/content/marking_oracles/bit_sum_div_3/Solution.qs b/katas/content/marking_oracles/bit_sum_div_3/Solution.qs new file mode 100644 index 0000000000..b4fe99baa1 --- /dev/null +++ b/katas/content/marking_oracles/bit_sum_div_3/Solution.qs @@ -0,0 +1,13 @@ +namespace Kata { + open Microsoft.Quantum.Katas; + operation Oracle_BitSumDivisibleBy3 (x : Qubit[], y : Qubit) : Unit is Adj + Ctl { + use counter = Qubit[2]; + within { + for q in x { + Controlled IncrementMod3([q], counter); + } + } apply { + ApplyControlledOnInt(0, X, counter, y); + } + } +} diff --git a/katas/content/marking_oracles/bit_sum_div_3/Verification.qs b/katas/content/marking_oracles/bit_sum_div_3/Verification.qs new file mode 100644 index 0000000000..f40328c622 --- /dev/null +++ b/katas/content/marking_oracles/bit_sum_div_3/Verification.qs @@ -0,0 +1,21 @@ +namespace Kata.Verification { + open Microsoft.Quantum.Katas; + open Microsoft.Quantum.Arrays; + + function F_BitSumDivisibleBy3(args : Bool[]) : Bool { + return Count(x -> x, args) % 3 == 0; + } + + @EntryPoint() + operation CheckSolution() : Bool { + for n in 3 .. 6 { + if not CheckOracleImplementsFunction(n, Kata.Oracle_BitSumDivisibleBy3, F_BitSumDivisibleBy3) { + Message($"Test failed for n = {n}"); + return false; + } + } + + Message("Correct!"); + true + } +} diff --git a/katas/content/marking_oracles/bit_sum_div_3/index.md b/katas/content/marking_oracles/bit_sum_div_3/index.md new file mode 100644 index 0000000000..460f5310ef --- /dev/null +++ b/katas/content/marking_oracles/bit_sum_div_3/index.md @@ -0,0 +1,11 @@ +**Inputs:** + +1. $N$ qubits in an arbitrary state $\ket{x}$ (input/query register). +2. A qubit in an arbitrary state $\ket{y}$ (output/target qubit). + +**Goal:** +Implement a quantum oracle which checks whether the sum of bits in the bit string is divisible by $3$. + +For example, for $N = 3$ the only basis states that should be marked are $\ket{000}$ and $\ket{111}$. +Leave the qubits in the input register in the same state they started in. +Your solution should work on inputs in superposition, and not use any measurements. \ No newline at end of file diff --git a/katas/content/marking_oracles/bit_sum_div_3/solution.md b/katas/content/marking_oracles/bit_sum_div_3/solution.md new file mode 100644 index 0000000000..cbeba4d0b0 --- /dev/null +++ b/katas/content/marking_oracles/bit_sum_div_3/solution.md @@ -0,0 +1,6 @@ +[placeholder for solution text] + +@[solution]({ + "id": "marking_oracles__bit_sum_div_3_solution", + "codePath": "./Solution.qs" +}) diff --git a/katas/content/marking_oracles/index.md b/katas/content/marking_oracles/index.md index 25d107deed..5479c37584 100644 --- a/katas/content/marking_oracles/index.md +++ b/katas/content/marking_oracles/index.md @@ -128,6 +128,24 @@ This kata continues the exploration of quantum oracles started in the Oracles ka ] }) +@[exercise]({ + "id": "marking_oracles__bit_sum_div_3", + "title": "Bit Sum Divisible By 3", + "path": "./bit_sum_div_3/", + "qsDependencies": [ + "../KatasLibrary.qs" + ] +}) + +@[exercise]({ + "id": "marking_oracles__num_div_3", + "title": "Number Divisible By 3", + "path": "./num_div_3/", + "qsDependencies": [ + "../KatasLibrary.qs" + ] +}) + @[section]({ "id": "marking_oracles__conclusion", "title": "Conclusion" diff --git a/katas/content/marking_oracles/num_div_3/Placeholder.qs b/katas/content/marking_oracles/num_div_3/Placeholder.qs new file mode 100644 index 0000000000..30d997bd5a --- /dev/null +++ b/katas/content/marking_oracles/num_div_3/Placeholder.qs @@ -0,0 +1,14 @@ +namespace Kata { + operation Oracle_DivisibleBy3 (x : Qubit[], y : Qubit) : Unit is Adj + Ctl { + // Implement your solution here... + + } + + // Helper operation that implements counting modulo 3 + operation IncrementMod3 (counterRegister : Qubit[]) : Unit is Adj + Ctl { + let sum = counterRegister[0]; + let carry = counterRegister[1]; + ApplyControlledOnInt(0, X, [carry], sum); + ApplyControlledOnInt(0, X, [sum], carry); + } +} diff --git a/katas/content/marking_oracles/num_div_3/Solution.qs b/katas/content/marking_oracles/num_div_3/Solution.qs new file mode 100644 index 0000000000..b2a082a8ad --- /dev/null +++ b/katas/content/marking_oracles/num_div_3/Solution.qs @@ -0,0 +1,20 @@ +namespace Kata { + open Microsoft.Quantum.Katas; + operation Oracle_DivisibleBy3 (x : Qubit[], y : Qubit) : Unit is Adj + Ctl { + use counter = Qubit[2]; + within { + for i in 0 .. Length(x) - 1 { // Iterate starting from the least significant bit + if i % 2 == 0 { + // i-th power of 2 is 1 mod 3 + Controlled IncrementMod3([x[i]], counter); + } else { + // i-th power of 2 is 2 mod 3 - same as -1, which is Adjoint of +1 + Controlled Adjoint IncrementMod3([x[i]], counter); + } + } + } apply { + // divisible by 3 only if the result is divisible by 3 + ApplyControlledOnInt(0, X, counter, y); + } + } +} diff --git a/katas/content/marking_oracles/num_div_3/Verification.qs b/katas/content/marking_oracles/num_div_3/Verification.qs new file mode 100644 index 0000000000..658b238d76 --- /dev/null +++ b/katas/content/marking_oracles/num_div_3/Verification.qs @@ -0,0 +1,21 @@ +namespace Kata.Verification { + open Microsoft.Quantum.Katas; + open Microsoft.Quantum.Convert; + + function F_DivisibleBy3(args : Bool[]) : Bool { + return BoolArrayAsInt(args) % 3 == 0; + } + + @EntryPoint() + operation CheckSolution() : Bool { + for n in 2 .. 7 { + if not CheckOracleImplementsFunction(n, Kata.Oracle_DivisibleBy3, F_DivisibleBy3) { + Message($"Test failed for n = {n}"); + return false; + } + } + + Message("Correct!"); + true + } +} diff --git a/katas/content/marking_oracles/num_div_3/index.md b/katas/content/marking_oracles/num_div_3/index.md new file mode 100644 index 0000000000..f7246e38e9 --- /dev/null +++ b/katas/content/marking_oracles/num_div_3/index.md @@ -0,0 +1,12 @@ +**Inputs:** + +1. $N$ qubits in an arbitrary state $\ket{x}$ (input/query register). +2. A qubit in an arbitrary state $\ket{y}$ (output/target qubit). + +**Goal:** +Implement a quantum oracle which checks whether the number represented by the bit string is divisible by $3$. +Use little endian notation to convert the bit string to an integer, i.e., the least significant bit is stored in `x[0]`. + +For example, for $N = 3$ the basis states that should be marked are $\ket{000}$, $\ket{110}$, and $\ket{011}$. +Leave the qubits in the input register in the same state they started in. +Your solution should work on inputs in superposition, and not use any measurements. \ No newline at end of file diff --git a/katas/content/marking_oracles/num_div_3/solution.md b/katas/content/marking_oracles/num_div_3/solution.md new file mode 100644 index 0000000000..42b72c9b65 --- /dev/null +++ b/katas/content/marking_oracles/num_div_3/solution.md @@ -0,0 +1,6 @@ +[placeholder for solution text] + +@[solution]({ + "id": "marking_oracles__num_div_3_solution", + "codePath": "./Solution.qs" +}) From ac3f9675cf1ee4c4edef6360f7daf2354722d1d9 Mon Sep 17 00:00:00 2001 From: James Kingdon Date: Tue, 11 Jun 2024 21:30:39 -0400 Subject: [PATCH 03/15] adjust majority solution for N=5 --- katas/content/marking_oracles/balanced/index.md | 2 +- katas/content/marking_oracles/majority/Solution.qs | 3 +++ katas/content/marking_oracles/majority/Verification.qs | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/katas/content/marking_oracles/balanced/index.md b/katas/content/marking_oracles/balanced/index.md index ad084dcaaa..126feefa45 100644 --- a/katas/content/marking_oracles/balanced/index.md +++ b/katas/content/marking_oracles/balanced/index.md @@ -4,7 +4,7 @@ 2. A qubit in an arbitrary state $\ket{y}$ (output/target qubit). **Goal:** -Implement a quantum oracle which checks whether the input register is a balanced bit string, i.e., whether it contains exactly $N/2 0$s and $N/2 1$s. $N$ will be an even number. +Implement a quantum oracle which checks whether the input register is a balanced bit string, i.e., whether it contains exactly $N/2$ $0$'s and $N/2$ $1$'s. $N$ will be an even number. For example, for $N = 4$ basis states $\ket{0011}$ and $\ket{0101}$ are balanced, and $\ket{0010}$ and $\ket{1111}$ are not. Leave the qubits in the input register in the same state they started in. diff --git a/katas/content/marking_oracles/majority/Solution.qs b/katas/content/marking_oracles/majority/Solution.qs index d90ade36e7..872690c95b 100644 --- a/katas/content/marking_oracles/majority/Solution.qs +++ b/katas/content/marking_oracles/majority/Solution.qs @@ -12,6 +12,9 @@ namespace Kata { } } apply { CNOT(inc[log-1], y); + if log > 2 { + CCNOT(inc[0], inc[1], y); + } } } } diff --git a/katas/content/marking_oracles/majority/Verification.qs b/katas/content/marking_oracles/majority/Verification.qs index d17f051f61..d076ebef0e 100644 --- a/katas/content/marking_oracles/majority/Verification.qs +++ b/katas/content/marking_oracles/majority/Verification.qs @@ -9,7 +9,7 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - for n in [3, 7] { + for n in [3, 5] { if not CheckOracleImplementsFunction(n, Kata.Oracle_Majority, F_Majority) { Message($"Test failed for n = {n}"); return false; From 737de30540e96b11bf217fefcbefd7981c6e4150 Mon Sep 17 00:00:00 2001 From: James Kingdon <96249042+jkingdon-ms@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:06:24 -0400 Subject: [PATCH 04/15] Update katas/content/marking_oracles/index.md Co-authored-by: Mariia Mykhailova --- katas/content/marking_oracles/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/katas/content/marking_oracles/index.md b/katas/content/marking_oracles/index.md index 5479c37584..b9ecfe237d 100644 --- a/katas/content/marking_oracles/index.md +++ b/katas/content/marking_oracles/index.md @@ -103,7 +103,7 @@ This kata continues the exploration of quantum oracles started in the Oracles ka @[exercise]({ "id": "marking_oracles__contains_substring", - "title": "Contains Substring", + "title": "Does Bit String Contain Substring?", "path": "./contains_substring/", "qsDependencies": [ "../KatasLibrary.qs" From 17a2e718bc328256352a732a6f1a399cbb36644c Mon Sep 17 00:00:00 2001 From: James Kingdon <96249042+jkingdon-ms@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:06:31 -0400 Subject: [PATCH 05/15] Update katas/content/marking_oracles/index.md Co-authored-by: Mariia Mykhailova --- katas/content/marking_oracles/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/katas/content/marking_oracles/index.md b/katas/content/marking_oracles/index.md index b9ecfe237d..69d0aeccfa 100644 --- a/katas/content/marking_oracles/index.md +++ b/katas/content/marking_oracles/index.md @@ -112,7 +112,7 @@ This kata continues the exploration of quantum oracles started in the Oracles ka @[exercise]({ "id": "marking_oracles__balanced", - "title": "0s and 1s Balanced", + "title": "Is Bit String Balanced?", "path": "./balanced/", "qsDependencies": [ "../KatasLibrary.qs" From 73e50074add857b44db11e61034c0529325b55f5 Mon Sep 17 00:00:00 2001 From: James Kingdon <96249042+jkingdon-ms@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:06:41 -0400 Subject: [PATCH 06/15] Update katas/content/marking_oracles/index.md Co-authored-by: Mariia Mykhailova --- katas/content/marking_oracles/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/katas/content/marking_oracles/index.md b/katas/content/marking_oracles/index.md index 69d0aeccfa..9bbb3fc2ad 100644 --- a/katas/content/marking_oracles/index.md +++ b/katas/content/marking_oracles/index.md @@ -121,7 +121,7 @@ This kata continues the exploration of quantum oracles started in the Oracles ka @[exercise]({ "id": "marking_oracles__majority", - "title": "0s or 1s Majority", + "title": "Majority Function", "path": "./majority/", "qsDependencies": [ "../KatasLibrary.qs" From 2bcb35099f0728ac2990d63ebc84094cf73d9d57 Mon Sep 17 00:00:00 2001 From: James Kingdon <96249042+jkingdon-ms@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:06:52 -0400 Subject: [PATCH 07/15] Update katas/content/marking_oracles/index.md Co-authored-by: Mariia Mykhailova --- katas/content/marking_oracles/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/katas/content/marking_oracles/index.md b/katas/content/marking_oracles/index.md index 9bbb3fc2ad..f26a0625dd 100644 --- a/katas/content/marking_oracles/index.md +++ b/katas/content/marking_oracles/index.md @@ -130,7 +130,7 @@ This kata continues the exploration of quantum oracles started in the Oracles ka @[exercise]({ "id": "marking_oracles__bit_sum_div_3", - "title": "Bit Sum Divisible By 3", + "title": "Is Bit Sum Divisible by 3?", "path": "./bit_sum_div_3/", "qsDependencies": [ "../KatasLibrary.qs" From d2f607c4765cb83c03f54aefec2cabe1f98132dd Mon Sep 17 00:00:00 2001 From: James Kingdon <96249042+jkingdon-ms@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:07:02 -0400 Subject: [PATCH 08/15] Update katas/content/marking_oracles/index.md Co-authored-by: Mariia Mykhailova --- katas/content/marking_oracles/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/katas/content/marking_oracles/index.md b/katas/content/marking_oracles/index.md index f26a0625dd..a05c4eda8d 100644 --- a/katas/content/marking_oracles/index.md +++ b/katas/content/marking_oracles/index.md @@ -139,7 +139,7 @@ This kata continues the exploration of quantum oracles started in the Oracles ka @[exercise]({ "id": "marking_oracles__num_div_3", - "title": "Number Divisible By 3", + "title": "Is Number Divisible by 3?", "path": "./num_div_3/", "qsDependencies": [ "../KatasLibrary.qs" From a1896c94ef0bd2d5413f4a19d99e7bf65715d0ca Mon Sep 17 00:00:00 2001 From: James Kingdon <96249042+jkingdon-ms@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:07:31 -0400 Subject: [PATCH 09/15] Update katas/content/marking_oracles/majority/index.md Co-authored-by: Mariia Mykhailova --- katas/content/marking_oracles/majority/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/katas/content/marking_oracles/majority/index.md b/katas/content/marking_oracles/majority/index.md index 78da54fc77..42c2ba03f3 100644 --- a/katas/content/marking_oracles/majority/index.md +++ b/katas/content/marking_oracles/majority/index.md @@ -1,6 +1,6 @@ **Inputs:** -1. $N$ qubits in an arbitrary state $\ket{x}$ (input/query register). +1. $N$ qubits in an arbitrary state $\ket{x}$ (input/query register). $N$ will be one of the values $3, 5, 7$. 2. A qubit in an arbitrary state $\ket{y}$ (output/target qubit). **Goal:** From f10bfb06cbde1cac677a2ff7e6e6087bfc607cd1 Mon Sep 17 00:00:00 2001 From: James Kingdon <96249042+jkingdon-ms@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:07:59 -0400 Subject: [PATCH 10/15] Update katas/content/marking_oracles/balanced/index.md Co-authored-by: Mariia Mykhailova --- katas/content/marking_oracles/balanced/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/katas/content/marking_oracles/balanced/index.md b/katas/content/marking_oracles/balanced/index.md index 126feefa45..3ea6fa1755 100644 --- a/katas/content/marking_oracles/balanced/index.md +++ b/katas/content/marking_oracles/balanced/index.md @@ -7,5 +7,6 @@ Implement a quantum oracle which checks whether the input register is a balanced bit string, i.e., whether it contains exactly $N/2$ $0$'s and $N/2$ $1$'s. $N$ will be an even number. For example, for $N = 4$ basis states $\ket{0011}$ and $\ket{0101}$ are balanced, and $\ket{0010}$ and $\ket{1111}$ are not. + Leave the qubits in the input register in the same state they started in. Your solution should work on inputs in superposition, and not use any measurements. \ No newline at end of file From b71f525baa254bb9fdf1de3f903a9b316b0c79b2 Mon Sep 17 00:00:00 2001 From: James Kingdon <96249042+jkingdon-ms@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:08:15 -0400 Subject: [PATCH 11/15] Update katas/content/marking_oracles/bit_sum_div_3/index.md Co-authored-by: Mariia Mykhailova --- katas/content/marking_oracles/bit_sum_div_3/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/katas/content/marking_oracles/bit_sum_div_3/index.md b/katas/content/marking_oracles/bit_sum_div_3/index.md index 460f5310ef..8f77adb4b8 100644 --- a/katas/content/marking_oracles/bit_sum_div_3/index.md +++ b/katas/content/marking_oracles/bit_sum_div_3/index.md @@ -7,5 +7,6 @@ Implement a quantum oracle which checks whether the sum of bits in the bit string is divisible by $3$. For example, for $N = 3$ the only basis states that should be marked are $\ket{000}$ and $\ket{111}$. + Leave the qubits in the input register in the same state they started in. Your solution should work on inputs in superposition, and not use any measurements. \ No newline at end of file From 1023fb0a41d61ca61ee6b576669b0dc84c6b34bc Mon Sep 17 00:00:00 2001 From: James Kingdon <96249042+jkingdon-ms@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:08:49 -0400 Subject: [PATCH 12/15] Update katas/content/marking_oracles/majority/index.md Co-authored-by: Mariia Mykhailova --- katas/content/marking_oracles/majority/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/katas/content/marking_oracles/majority/index.md b/katas/content/marking_oracles/majority/index.md index 42c2ba03f3..770207c4ef 100644 --- a/katas/content/marking_oracles/majority/index.md +++ b/katas/content/marking_oracles/majority/index.md @@ -7,5 +7,6 @@ Implement a quantum oracle which calculates the majority function, i.e., $f(x) = 1$ if most of the bits in the bit string are $1$'s, and $0$ otherwise. For example, for $N = 3$ majority function for basis states $\ket{001}$ and $\ket{000}$ is $0$, and for $\ket{101}$ and $\ket{111}$ is $1$. + Leave the qubits in the input register in the same state they started in. Your solution should work on inputs in superposition, and not use any measurements. \ No newline at end of file From e76579b2b106ac5c3b5a0558dfee1ff73bc0e4db Mon Sep 17 00:00:00 2001 From: James Kingdon <96249042+jkingdon-ms@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:08:58 -0400 Subject: [PATCH 13/15] Update katas/content/marking_oracles/num_div_3/index.md Co-authored-by: Mariia Mykhailova --- katas/content/marking_oracles/num_div_3/index.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/katas/content/marking_oracles/num_div_3/index.md b/katas/content/marking_oracles/num_div_3/index.md index f7246e38e9..691b967d01 100644 --- a/katas/content/marking_oracles/num_div_3/index.md +++ b/katas/content/marking_oracles/num_div_3/index.md @@ -7,6 +7,7 @@ Implement a quantum oracle which checks whether the number represented by the bit string is divisible by $3$. Use little endian notation to convert the bit string to an integer, i.e., the least significant bit is stored in `x[0]`. -For example, for $N = 3$ the basis states that should be marked are $\ket{000}$, $\ket{110}$, and $\ket{011}$. +For example, for $N = 3$ the basis states that should be marked are $\ket{000} = \ket{0}$, $\ket{110} = \ket{3}$, and $\ket{011} = \ket{6}$. + Leave the qubits in the input register in the same state they started in. Your solution should work on inputs in superposition, and not use any measurements. \ No newline at end of file From 623f491c0ba7b188444f6024c8e457fd89ecce56 Mon Sep 17 00:00:00 2001 From: James Kingdon Date: Wed, 12 Jun 2024 16:31:04 -0400 Subject: [PATCH 14/15] PR suggestions --- katas/content/KatasLibrary.qs | 35 +------------------ katas/content/marking_oracles/Common.qs | 20 +++++++++++ .../marking_oracles/balanced/Placeholder.qs | 8 ----- .../marking_oracles/balanced/Solution.qs | 8 ++++- .../bit_sum_div_3/Placeholder.qs | 8 ----- .../marking_oracles/bit_sum_div_3/Solution.qs | 8 ++++- .../contains_substring_p/Verification.qs | 2 +- .../marking_oracles/majority/Placeholder.qs | 8 ----- .../marking_oracles/majority/Solution.qs | 12 +++++-- .../marking_oracles/majority/Verification.qs | 2 +- .../marking_oracles/num_div_3/Placeholder.qs | 8 ----- .../marking_oracles/num_div_3/Solution.qs | 13 ++++--- 12 files changed, 54 insertions(+), 78 deletions(-) create mode 100644 katas/content/marking_oracles/Common.qs diff --git a/katas/content/KatasLibrary.qs b/katas/content/KatasLibrary.qs index 11aa5aa337..2a48951828 100644 --- a/katas/content/KatasLibrary.qs +++ b/katas/content/KatasLibrary.qs @@ -332,38 +332,5 @@ namespace Microsoft.Quantum.Katas { } } return true; - } - - operation IncrementBE (register : Qubit[]) : Unit is Adj + Ctl { - if Length(register) > 1 { - Controlled IncrementBE([register[0]], register[1 ...]); - } - X(register[0]); - } - - operation IncrementMod3 (counterRegister : Qubit[]) : Unit is Adj + Ctl { - let sum = counterRegister[0]; - let carry = counterRegister[1]; - ApplyControlledOnInt(0, X, [carry], sum); - ApplyControlledOnInt(0, X, [sum], carry); - } - - function F_PeriodicGivenPeriod(args : Bool[], P : Int) : Bool { - let N = Length(args); - for i in 0 .. N - P - 1 { - if args[i] != args[i + P] { - return false; - } - } - return true; - } - - function F_ContainsSubstringAtPosition(args : Bool[], r : Bool[], p : Int) : Bool { - for i in 0 .. Length(r) - 1 { - if r[i] != args[i + p] { - return false; - } - } - return true; - } + } } diff --git a/katas/content/marking_oracles/Common.qs b/katas/content/marking_oracles/Common.qs new file mode 100644 index 0000000000..3e0098d6de --- /dev/null +++ b/katas/content/marking_oracles/Common.qs @@ -0,0 +1,20 @@ +namespace Kata.Verification { + function F_PeriodicGivenPeriod(args : Bool[], P : Int) : Bool { + let N = Length(args); + for i in 0 .. N - P - 1 { + if args[i] != args[i + P] { + return false; + } + } + return true; + } + + function F_ContainsSubstringAtPosition(args : Bool[], r : Bool[], p : Int) : Bool { + for i in 0 .. Length(r) - 1 { + if r[i] != args[i + p] { + return false; + } + } + return true; + } +} diff --git a/katas/content/marking_oracles/balanced/Placeholder.qs b/katas/content/marking_oracles/balanced/Placeholder.qs index 1cb23c1ac6..8c27260e91 100644 --- a/katas/content/marking_oracles/balanced/Placeholder.qs +++ b/katas/content/marking_oracles/balanced/Placeholder.qs @@ -3,12 +3,4 @@ namespace Kata { // Implement your solution here... } - - // Helper operation that implements increment for a qubit register - operation IncrementBE (register : Qubit[]) : Unit is Adj + Ctl { - if Length(register) > 1 { - Controlled IncrementBE([register[0]], register[1 ...]); - } - X(register[0]); - } } diff --git a/katas/content/marking_oracles/balanced/Solution.qs b/katas/content/marking_oracles/balanced/Solution.qs index b241ffeb35..12175028af 100644 --- a/katas/content/marking_oracles/balanced/Solution.qs +++ b/katas/content/marking_oracles/balanced/Solution.qs @@ -1,5 +1,4 @@ namespace Kata { - open Microsoft.Quantum.Katas; open Microsoft.Quantum.Math; operation Oracle_Balanced (x : Qubit[], y : Qubit) : Unit is Adj + Ctl { @@ -14,4 +13,11 @@ namespace Kata { ApplyControlledOnInt(N / 2, X, inc, y); } } + + operation IncrementBE (register : Qubit[]) : Unit is Adj + Ctl { + if Length(register) > 1 { + Controlled IncrementBE([register[0]], register[1 ...]); + } + X(register[0]); + } } diff --git a/katas/content/marking_oracles/bit_sum_div_3/Placeholder.qs b/katas/content/marking_oracles/bit_sum_div_3/Placeholder.qs index 674492fd25..a0951e824b 100644 --- a/katas/content/marking_oracles/bit_sum_div_3/Placeholder.qs +++ b/katas/content/marking_oracles/bit_sum_div_3/Placeholder.qs @@ -3,12 +3,4 @@ namespace Kata { // Implement your solution here... } - - // Helper operation that implements counting modulo 3 - operation IncrementMod3 (counterRegister : Qubit[]) : Unit is Adj + Ctl { - let sum = counterRegister[0]; - let carry = counterRegister[1]; - ApplyControlledOnInt(0, X, [carry], sum); - ApplyControlledOnInt(0, X, [sum], carry); - } } diff --git a/katas/content/marking_oracles/bit_sum_div_3/Solution.qs b/katas/content/marking_oracles/bit_sum_div_3/Solution.qs index b4fe99baa1..db3c54f5bd 100644 --- a/katas/content/marking_oracles/bit_sum_div_3/Solution.qs +++ b/katas/content/marking_oracles/bit_sum_div_3/Solution.qs @@ -1,5 +1,4 @@ namespace Kata { - open Microsoft.Quantum.Katas; operation Oracle_BitSumDivisibleBy3 (x : Qubit[], y : Qubit) : Unit is Adj + Ctl { use counter = Qubit[2]; within { @@ -10,4 +9,11 @@ namespace Kata { ApplyControlledOnInt(0, X, counter, y); } } + + operation IncrementMod3 (counterRegister : Qubit[]) : Unit is Adj + Ctl { + let sum = counterRegister[0]; + let carry = counterRegister[1]; + ApplyControlledOnInt(0, X, [carry], sum); + ApplyControlledOnInt(0, X, [sum], carry); + } } diff --git a/katas/content/marking_oracles/contains_substring_p/Verification.qs b/katas/content/marking_oracles/contains_substring_p/Verification.qs index abcca37b57..e930a0e09e 100644 --- a/katas/content/marking_oracles/contains_substring_p/Verification.qs +++ b/katas/content/marking_oracles/contains_substring_p/Verification.qs @@ -1,5 +1,5 @@ namespace Kata.Verification { - open Microsoft.Quantum.Katas; + open Microsoft.Quantum.Katas; @EntryPoint() operation CheckSolution() : Bool { diff --git a/katas/content/marking_oracles/majority/Placeholder.qs b/katas/content/marking_oracles/majority/Placeholder.qs index 43a3a4fb3c..c67092ed1b 100644 --- a/katas/content/marking_oracles/majority/Placeholder.qs +++ b/katas/content/marking_oracles/majority/Placeholder.qs @@ -3,12 +3,4 @@ namespace Kata { // Implement your solution here... } - - // Helper operation that implements increment for a qubit register - operation IncrementBE (register : Qubit[]) : Unit is Adj + Ctl { - if Length(register) > 1 { - Controlled IncrementBE([register[0]], register[1 ...]); - } - X(register[0]); - } } diff --git a/katas/content/marking_oracles/majority/Solution.qs b/katas/content/marking_oracles/majority/Solution.qs index 872690c95b..cbac9c0956 100644 --- a/katas/content/marking_oracles/majority/Solution.qs +++ b/katas/content/marking_oracles/majority/Solution.qs @@ -1,5 +1,4 @@ namespace Kata { - open Microsoft.Quantum.Katas; open Microsoft.Quantum.Math; operation Oracle_Majority (x : Qubit[], y : Qubit) : Unit is Adj + Ctl { @@ -12,9 +11,16 @@ namespace Kata { } } apply { CNOT(inc[log-1], y); - if log > 2 { + if N == 5 { CCNOT(inc[0], inc[1], y); } } - } + } + + operation IncrementBE (register : Qubit[]) : Unit is Adj + Ctl { + if Length(register) > 1 { + Controlled IncrementBE([register[0]], register[1 ...]); + } + X(register[0]); + } } diff --git a/katas/content/marking_oracles/majority/Verification.qs b/katas/content/marking_oracles/majority/Verification.qs index d076ebef0e..39e702f25f 100644 --- a/katas/content/marking_oracles/majority/Verification.qs +++ b/katas/content/marking_oracles/majority/Verification.qs @@ -9,7 +9,7 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - for n in [3, 5] { + for n in [3, 5, 7] { if not CheckOracleImplementsFunction(n, Kata.Oracle_Majority, F_Majority) { Message($"Test failed for n = {n}"); return false; diff --git a/katas/content/marking_oracles/num_div_3/Placeholder.qs b/katas/content/marking_oracles/num_div_3/Placeholder.qs index 30d997bd5a..1525697594 100644 --- a/katas/content/marking_oracles/num_div_3/Placeholder.qs +++ b/katas/content/marking_oracles/num_div_3/Placeholder.qs @@ -3,12 +3,4 @@ namespace Kata { // Implement your solution here... } - - // Helper operation that implements counting modulo 3 - operation IncrementMod3 (counterRegister : Qubit[]) : Unit is Adj + Ctl { - let sum = counterRegister[0]; - let carry = counterRegister[1]; - ApplyControlledOnInt(0, X, [carry], sum); - ApplyControlledOnInt(0, X, [sum], carry); - } } diff --git a/katas/content/marking_oracles/num_div_3/Solution.qs b/katas/content/marking_oracles/num_div_3/Solution.qs index b2a082a8ad..7a437f2d21 100644 --- a/katas/content/marking_oracles/num_div_3/Solution.qs +++ b/katas/content/marking_oracles/num_div_3/Solution.qs @@ -1,20 +1,23 @@ namespace Kata { - open Microsoft.Quantum.Katas; operation Oracle_DivisibleBy3 (x : Qubit[], y : Qubit) : Unit is Adj + Ctl { use counter = Qubit[2]; within { - for i in 0 .. Length(x) - 1 { // Iterate starting from the least significant bit + for i in 0 .. Length(x) - 1 { if i % 2 == 0 { - // i-th power of 2 is 1 mod 3 Controlled IncrementMod3([x[i]], counter); } else { - // i-th power of 2 is 2 mod 3 - same as -1, which is Adjoint of +1 Controlled Adjoint IncrementMod3([x[i]], counter); } } } apply { - // divisible by 3 only if the result is divisible by 3 ApplyControlledOnInt(0, X, counter, y); } } + + operation IncrementMod3 (counterRegister : Qubit[]) : Unit is Adj + Ctl { + let sum = counterRegister[0]; + let carry = counterRegister[1]; + ApplyControlledOnInt(0, X, [carry], sum); + ApplyControlledOnInt(0, X, [sum], carry); + } } From 2a69634536a0de276c9e7b9e722cde1da01df8ba Mon Sep 17 00:00:00 2001 From: James Kingdon Date: Wed, 12 Jun 2024 16:41:58 -0400 Subject: [PATCH 15/15] add Common.qs to compilation list --- katas/content/marking_oracles/index.md | 42 +++++++++++++++++--------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/katas/content/marking_oracles/index.md b/katas/content/marking_oracles/index.md index a05c4eda8d..2acacc5e06 100644 --- a/katas/content/marking_oracles/index.md +++ b/katas/content/marking_oracles/index.md @@ -25,7 +25,8 @@ This kata continues the exploration of quantum oracles started in the Oracles ka "title": "K-th Bit", "path": "./kth_bit/", "qsDependencies": [ - "../KatasLibrary.qs" + "../KatasLibrary.qs", + "./Common.qs" ] }) @@ -34,7 +35,8 @@ This kata continues the exploration of quantum oracles started in the Oracles ka "title": "Parity Function", "path": "./parity/", "qsDependencies": [ - "../KatasLibrary.qs" + "../KatasLibrary.qs", + "./Common.qs" ] }) @@ -43,7 +45,8 @@ This kata continues the exploration of quantum oracles started in the Oracles ka "title": "Product Function", "path": "./product/", "qsDependencies": [ - "../KatasLibrary.qs" + "../KatasLibrary.qs", + "./Common.qs" ] }) @@ -52,7 +55,8 @@ This kata continues the exploration of quantum oracles started in the Oracles ka "title": "Product Function with Negation", "path": "./product_negation/", "qsDependencies": [ - "../KatasLibrary.qs" + "../KatasLibrary.qs", + "./Common.qs" ] }) @@ -61,7 +65,8 @@ This kata continues the exploration of quantum oracles started in the Oracles ka "title": "Palindrome Checker", "path": "./palindrome/", "qsDependencies": [ - "../KatasLibrary.qs" + "../KatasLibrary.qs", + "./Common.qs" ] }) @@ -70,7 +75,8 @@ This kata continues the exploration of quantum oracles started in the Oracles ka "title": "Is Bit String Periodic with Period P?", "path": "./periodic_p/", "qsDependencies": [ - "../KatasLibrary.qs" + "../KatasLibrary.qs", + "./Common.qs" ] }) @@ -79,7 +85,8 @@ This kata continues the exploration of quantum oracles started in the Oracles ka "title": "Is Bit String Periodic?", "path": "./periodic/", "qsDependencies": [ - "../KatasLibrary.qs" + "../KatasLibrary.qs", + "./Common.qs" ] }) @@ -88,7 +95,8 @@ This kata continues the exploration of quantum oracles started in the Oracles ka "title": "Does Bit String Contain Substring At Position?", "path": "./contains_substring_p/", "qsDependencies": [ - "../KatasLibrary.qs" + "../KatasLibrary.qs", + "./Common.qs" ] }) @@ -97,7 +105,8 @@ This kata continues the exploration of quantum oracles started in the Oracles ka "title": "Pattern Matching", "path": "./pattern_match/", "qsDependencies": [ - "../KatasLibrary.qs" + "../KatasLibrary.qs", + "./Common.qs" ] }) @@ -106,7 +115,8 @@ This kata continues the exploration of quantum oracles started in the Oracles ka "title": "Does Bit String Contain Substring?", "path": "./contains_substring/", "qsDependencies": [ - "../KatasLibrary.qs" + "../KatasLibrary.qs", + "./Common.qs" ] }) @@ -115,7 +125,8 @@ This kata continues the exploration of quantum oracles started in the Oracles ka "title": "Is Bit String Balanced?", "path": "./balanced/", "qsDependencies": [ - "../KatasLibrary.qs" + "../KatasLibrary.qs", + "./Common.qs" ] }) @@ -124,7 +135,8 @@ This kata continues the exploration of quantum oracles started in the Oracles ka "title": "Majority Function", "path": "./majority/", "qsDependencies": [ - "../KatasLibrary.qs" + "../KatasLibrary.qs", + "./Common.qs" ] }) @@ -133,7 +145,8 @@ This kata continues the exploration of quantum oracles started in the Oracles ka "title": "Is Bit Sum Divisible by 3?", "path": "./bit_sum_div_3/", "qsDependencies": [ - "../KatasLibrary.qs" + "../KatasLibrary.qs", + "./Common.qs" ] }) @@ -142,7 +155,8 @@ This kata continues the exploration of quantum oracles started in the Oracles ka "title": "Is Number Divisible by 3?", "path": "./num_div_3/", "qsDependencies": [ - "../KatasLibrary.qs" + "../KatasLibrary.qs", + "./Common.qs" ] })