diff --git a/katas/content/nonlocal_games/chsh_classical_strategy/Placeholder.qs b/katas/content/nonlocal_games/chsh_classical_strategy/Placeholder.qs new file mode 100644 index 0000000000..f368233adf --- /dev/null +++ b/katas/content/nonlocal_games/chsh_classical_strategy/Placeholder.qs @@ -0,0 +1,13 @@ +namespace Kata { + function AliceClassical (x : Bool) : Bool { + // Implement your solution here... + + return false; + } + + function BobClassical (y : Bool) : Bool { + // Implement your solution here... + + return true; + } +} diff --git a/katas/content/nonlocal_games/chsh_classical_strategy/Solution.qs b/katas/content/nonlocal_games/chsh_classical_strategy/Solution.qs new file mode 100644 index 0000000000..21d8c8b4b6 --- /dev/null +++ b/katas/content/nonlocal_games/chsh_classical_strategy/Solution.qs @@ -0,0 +1,9 @@ +namespace Kata { + function AliceClassical (x : Bool) : Bool { + return false; + } + + function BobClassical (y : Bool) : Bool { + return false; + } +} diff --git a/katas/content/nonlocal_games/chsh_classical_strategy/Verification.qs b/katas/content/nonlocal_games/chsh_classical_strategy/Verification.qs new file mode 100644 index 0000000000..d407458d85 --- /dev/null +++ b/katas/content/nonlocal_games/chsh_classical_strategy/Verification.qs @@ -0,0 +1,25 @@ +namespace Kata.Verification { + open Microsoft.Quantum.Convert; + open Microsoft.Quantum.Random; + + @EntryPoint() + operation CheckSolution() : Bool { + mutable wins = 0; + for i in 1..1000 { + let x = DrawRandomInt(0, 1) == 1 ? true | false; + let y = DrawRandomInt(0, 1) == 1 ? true | false; + let (a, b) = (Kata.AliceClassical(x), Kata.BobClassical(y)); + if ((x and y) == (a != b)) { + set wins = wins + 1; + } + } + Message($"Win rate {IntAsDouble(wins) / 1000.}"); + if (wins < 700) { + Message("Alice and Bob's classical strategy is not optimal"); + return false; + } + Message("Correct!"); + true + } + +} diff --git a/katas/content/nonlocal_games/chsh_classical_strategy/index.md b/katas/content/nonlocal_games/chsh_classical_strategy/index.md new file mode 100644 index 0000000000..927a4c32bf --- /dev/null +++ b/katas/content/nonlocal_games/chsh_classical_strategy/index.md @@ -0,0 +1,8 @@ +In this task you have to implement two functions, one for Alice's classical strategy and one for Bob's. +Note that they are covered by one test, so you have to implement both to pass the test. + +**Input:** +Alice and Bob's starting bits (X and Y). + +**Output:** + Alice and Bob's output bits (A and B) to maximize their chance of winning. diff --git a/katas/content/nonlocal_games/chsh_classical_strategy/solution.md b/katas/content/nonlocal_games/chsh_classical_strategy/solution.md new file mode 100644 index 0000000000..fb22c98290 --- /dev/null +++ b/katas/content/nonlocal_games/chsh_classical_strategy/solution.md @@ -0,0 +1,7 @@ +If Alice and Bob always return TRUE, they will have a 75% win rate, since TRUE ⊕ TRUE = FALSE, and the AND operation on their input bits will be FALSE with 75% probability. +Alternatively, Alice and Bob could agree to always return FALSE to achieve the same 75% win probability. A classical strategy cannot achieve a higher success probability. + +@[solution]({ + "id": "nonlocal_games__chsh_classical_strategy_solution", + "codePath": "Solution.qs" +}) diff --git a/katas/content/nonlocal_games/chsh_classical_win_condition/Placeholder.qs b/katas/content/nonlocal_games/chsh_classical_win_condition/Placeholder.qs new file mode 100644 index 0000000000..1ba27f995c --- /dev/null +++ b/katas/content/nonlocal_games/chsh_classical_win_condition/Placeholder.qs @@ -0,0 +1,7 @@ +namespace Kata { + function WinCondition (x : Bool, y : Bool, a : Bool, b : Bool) : Bool { + // Implement your solution here... + + return false; + } +} diff --git a/katas/content/nonlocal_games/chsh_classical_win_condition/Solution.qs b/katas/content/nonlocal_games/chsh_classical_win_condition/Solution.qs new file mode 100644 index 0000000000..b9d1321c8e --- /dev/null +++ b/katas/content/nonlocal_games/chsh_classical_win_condition/Solution.qs @@ -0,0 +1,5 @@ +namespace Kata { + function WinCondition (x : Bool, y : Bool, a : Bool, b : Bool) : Bool { + return (x and y) == (a != b); + } +} diff --git a/katas/content/nonlocal_games/chsh_classical_win_condition/Verification.qs b/katas/content/nonlocal_games/chsh_classical_win_condition/Verification.qs new file mode 100644 index 0000000000..82a16cd2d8 --- /dev/null +++ b/katas/content/nonlocal_games/chsh_classical_win_condition/Verification.qs @@ -0,0 +1,24 @@ +namespace Kata.Verification { + open Microsoft.Quantum.Convert; + + function WinCondition_Reference(x : Bool, y : Bool, a : Bool, b : Bool) : Bool { + return (x and y) == (a != b); + } + + @EntryPoint() + function CheckSolution() : Bool { + for i in 0..1 <<< 4 - 1 { + let bits = IntAsBoolArray(i, 4); + let expected = WinCondition_Reference(bits[0], bits[1], bits[2], bits[3]); + let actual = Kata.WinCondition(bits[0], bits[1], bits[2], bits[3]); + + if actual != expected { + Message($"Win condition '{actual}' isn't as expected for X = {bits[0]}, Y = {bits[1]}, " + + $"A = {bits[2]}, B = {bits[3]}"); + return false; + } + } + Message("Correct!"); + true + } +} diff --git a/katas/content/nonlocal_games/chsh_classical_win_condition/index.md b/katas/content/nonlocal_games/chsh_classical_win_condition/index.md new file mode 100644 index 0000000000..4485f2f549 --- /dev/null +++ b/katas/content/nonlocal_games/chsh_classical_win_condition/index.md @@ -0,0 +1,7 @@ +**Input:** +* Alice and Bob's starting bits (X and Y). +* Alice and Bob's output bits (A and B). + +**Output:** + True if Alice and Bob won the CHSH game, that is, if X ∧ Y = A ⊕ B, and false otherwise. + diff --git a/katas/content/nonlocal_games/chsh_classical_win_condition/solution.md b/katas/content/nonlocal_games/chsh_classical_win_condition/solution.md new file mode 100644 index 0000000000..f80b0552ae --- /dev/null +++ b/katas/content/nonlocal_games/chsh_classical_win_condition/solution.md @@ -0,0 +1,10 @@ +There are four input pairs (X, Y) possible, (0,0), (0,1), (1,0), and (1,1), each with 25% probability. +In order to win, Alice and Bob have to output different bits if the input is (1,1), and same bits otherwise. + +To check whether the win condition holds, you need to compute $x ∧ y$ and $a ⊕ b$ and to compare these values: if they are equal, Alice and Bob won. You can compute these values using [built-in operators](https://learn.microsoft.com/azure/quantum/user-guide/language/expressions/logicalexpressions): $x ∧ y$ as `x and y` and $a ⊕ b$ as `a != b`. + + +@[solution]({ + "id": "nonlocal_games__chsh_classical_win_condition_solution", + "codePath": "Solution.qs" +}) diff --git a/katas/content/nonlocal_games/index.md b/katas/content/nonlocal_games/index.md new file mode 100644 index 0000000000..f81c197037 --- /dev/null +++ b/katas/content/nonlocal_games/index.md @@ -0,0 +1,61 @@ +# Nonlocal Games + +@[section]({ + "id": "nonlocal_games__overview", + "title": "Overview" +}) + +This kata introduces three quantum nonlocal games that display "quantum pseudo-telepathy" - +the use of quantum entanglement to eliminate the need for classical communication. +In this context, "nonlocal" means that the playing parties are separated by a great distance, +so they cannot communicate with each other during the game. +Another characteristics of these games is that they are "refereed", which means the players try to win against the referee. + +**This kata covers the following topics:** + - Clauser, Horne, Shimony, and Hold thought experiment (often abbreviated as CHSH game) + - Greenberger-Horne-Zeilinger game (often abbreviated as GHZ game) + - The Mermin-Peres Magic Square game + +**What you should know to start working on this kata:** + - Basic linear algebra + - Basic knowledge of quantum gates and measurements + +@[section]({ + "id": "nonlocal_games__chsh_game", + "title": "CHSH Game" +}) + +In **CHSH Game**, two players (Alice and Bob) try to win the following game: + +Each of them is given a bit (Alice gets X and Bob gets Y), and +they have to return new bits (Alice returns A and Bob returns B) +so that X ∧ Y = A ⊕ B. The trick is, they can not communicate during the game. + +> * ∧ is the standard bitwise AND operator. +> * ⊕ is the exclusive or, or XOR operator, so (P ⊕ Q) is true if exactly one of P and Q is true. + +@[section]({ + "id": "nonlocal_games__chsh_game_classical", + "title": "Part I. Classical CHSH" +}) + +To start with, let's take a look at how you would play the classical variant of this game without access to any quantum tools. + +@[exercise]({ + "id": "nonlocal_games__chsh_classical_win_condition", + "title": "Win Condition", + "path": "./chsh_classical_win_condition/" +}) + +@[exercise]({ + "id": "nonlocal_games__chsh_classical_strategy", + "title": "Alice and Bob's Classical Strategy", + "path": "./chsh_classical_strategy/" +}) + +@[section]({ + "id": "nonlocal_games__conclusion", + "title": "Conclusion" +}) + +Congratulations! In this kata you learned how to use quantum entanglement in nonlocal quantum games to get results that are better than any classical strategy can offer.