Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate tasks 6-10 from MarkingOracles Kata #1626

Merged
merged 26 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b0e8b68
Merge branch 'main' of github.com:microsoft/qsharp
Mar 26, 2024
f2ec252
Merge branch 'main' of github.com:microsoft/qsharp
Apr 12, 2024
43580e7
Merge branch 'main' of github.com:microsoft/qsharp
Apr 23, 2024
ca6f894
Merge branch 'main' of github.com:microsoft/qsharp
May 17, 2024
a008822
Merge branch 'main' of github.com:microsoft/qsharp
May 24, 2024
fb5c762
Merge branch 'main' of github.com:microsoft/qsharp
May 31, 2024
9c6ef31
Merge branch 'main' of github.com:microsoft/qsharp
Jun 5, 2024
27f90b9
Merge branch 'main' of github.com:microsoft/qsharp
Jun 10, 2024
aafada8
add katas 6, 7, 8
Jun 10, 2024
4246e95
add katas 9 and 10
Jun 12, 2024
ac3f967
adjust majority solution for N=5
Jun 12, 2024
737de30
Update katas/content/marking_oracles/index.md
jkingdon-ms Jun 12, 2024
17a2e71
Update katas/content/marking_oracles/index.md
jkingdon-ms Jun 12, 2024
73e5007
Update katas/content/marking_oracles/index.md
jkingdon-ms Jun 12, 2024
2bcb350
Update katas/content/marking_oracles/index.md
jkingdon-ms Jun 12, 2024
d2f607c
Update katas/content/marking_oracles/index.md
jkingdon-ms Jun 12, 2024
a1896c9
Update katas/content/marking_oracles/majority/index.md
jkingdon-ms Jun 12, 2024
f10bfb0
Update katas/content/marking_oracles/balanced/index.md
jkingdon-ms Jun 12, 2024
b71f525
Update katas/content/marking_oracles/bit_sum_div_3/index.md
jkingdon-ms Jun 12, 2024
1023fb0
Update katas/content/marking_oracles/majority/index.md
jkingdon-ms Jun 12, 2024
e76579b
Update katas/content/marking_oracles/num_div_3/index.md
jkingdon-ms Jun 12, 2024
cfb9919
Merge branch 'katas/marking-oracles-6-10' of github.com:jkingdon-ms/q…
Jun 12, 2024
623f491
PR suggestions
Jun 12, 2024
1c0f435
Merge branch 'main' into katas/marking-oracles-6-10
jkingdon-ms Jun 12, 2024
863331a
Merge branch 'katas/marking-oracles-6-10' of github.com:jkingdon-ms/q…
Jun 12, 2024
2a69634
add Common.qs to compilation list
Jun 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions katas/content/KatasLibrary.qs
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,5 @@ namespace Microsoft.Quantum.Katas {
}
}
return true;
}

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;
}
}
}
20 changes: 20 additions & 0 deletions katas/content/marking_oracles/Common.qs
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 6 additions & 0 deletions katas/content/marking_oracles/balanced/Placeholder.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Kata {
operation Oracle_Balanced (x : Qubit[], y : Qubit) : Unit is Adj + Ctl {
// Implement your solution here...

}
}
23 changes: 23 additions & 0 deletions katas/content/marking_oracles/balanced/Solution.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Kata {
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);
}
}
jkingdon-ms marked this conversation as resolved.
Show resolved Hide resolved

operation IncrementBE (register : Qubit[]) : Unit is Adj + Ctl {
if Length(register) > 1 {
Controlled IncrementBE([register[0]], register[1 ...]);
}
X(register[0]);
}
}
21 changes: 21 additions & 0 deletions katas/content/marking_oracles/balanced/Verification.qs
Original file line number Diff line number Diff line change
@@ -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
}
}
12 changes: 12 additions & 0 deletions katas/content/marking_oracles/balanced/index.md
Original file line number Diff line number Diff line change
@@ -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 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.
6 changes: 6 additions & 0 deletions katas/content/marking_oracles/balanced/solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[placeholder for solution text]

@[solution]({
"id": "marking_oracles__balanced_solution",
"codePath": "./Solution.qs"
})
6 changes: 6 additions & 0 deletions katas/content/marking_oracles/bit_sum_div_3/Placeholder.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Kata {
operation Oracle_BitSumDivisibleBy3 (x : Qubit[], y : Qubit) : Unit is Adj + Ctl {
// Implement your solution here...

}
}
19 changes: 19 additions & 0 deletions katas/content/marking_oracles/bit_sum_div_3/Solution.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Kata {
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);
}
}

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);
}
}
21 changes: 21 additions & 0 deletions katas/content/marking_oracles/bit_sum_div_3/Verification.qs
Original file line number Diff line number Diff line change
@@ -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
}
}
12 changes: 12 additions & 0 deletions katas/content/marking_oracles/bit_sum_div_3/index.md
Original file line number Diff line number Diff line change
@@ -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 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.
6 changes: 6 additions & 0 deletions katas/content/marking_oracles/bit_sum_div_3/solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[placeholder for solution text]

@[solution]({
"id": "marking_oracles__bit_sum_div_3_solution",
"codePath": "./Solution.qs"
})
11 changes: 11 additions & 0 deletions katas/content/marking_oracles/contains_substring/Placeholder.qs
Original file line number Diff line number Diff line change
@@ -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);
}
}
19 changes: 19 additions & 0 deletions katas/content/marking_oracles/contains_substring/Solution.qs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
13 changes: 13 additions & 0 deletions katas/content/marking_oracles/contains_substring/index.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[placeholder for solution text]

@[solution]({
"id": "marking_oracles__contains_substring_solution",
"codePath": "./Solution.qs"
})
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
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;
}

@EntryPoint()
operation CheckSolution() : Bool {
for (n, p, r) in [
Expand All @@ -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;
}
Expand Down
Loading
Loading