Skip to content

Commit

Permalink
Migrate nonlocal games #1596 task1 classical (#1710)
Browse files Browse the repository at this point in the history
The link to the issue: #1596

This pull requests is covering 
1. inital folder structure for nonlocal_games
2. CHSH classical tasks (1.1 and 1.2/3)

Other PRs will follow after this commit.

Details of this PR:

Context is (mostly) copied from previous version QuantumKatas.
1. index.md for nonlocal_games is added.
   - Brief overview for nonlocal games is new.
2. chsh_classical_win_condition (task 1.1) is added.
- index.md: Reference to Q# samples is rephrased to point to this the
kata (reference implementation will be added in next quantum PR).
3. chsh_classical_strategy (combined tasks 1.2 1.3) is added.
4. Conclusion section sentence is new.

Testing done:
0. Build "python ./build.py --npm" is successful
1. Placeholder cases are syntactically correct but failing
verifications.
2. Solutions are successful.
3. Some other failure scenarios were tested manually.
4. Content screenshots are attached. 
![CHSH game Classical
-1](https://github.com/microsoft/qsharp/assets/51379812/1c1b36e1-fddc-4f54-b843-fab9b1e06046)
![CHSH game Classical Win Condition -
2](https://github.com/microsoft/qsharp/assets/51379812/8aaed771-6ba8-4b4c-8e1a-31798785fb32)
![CHSH game Classical Strategy
-3](https://github.com/microsoft/qsharp/assets/51379812/b8c1b3c3-d345-40ac-8b83-e6c216bfb80b)

---------

Co-authored-by: Mariia Mykhailova <[email protected]>
  • Loading branch information
ggridin and tcNickolas authored Jul 16, 2024
1 parent fc6ec5b commit 1deae14
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Kata {
function AliceClassical (x : Bool) : Bool {
return false;
}

function BobClassical (y : Bool) : Bool {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -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
}

}
8 changes: 8 additions & 0 deletions katas/content/nonlocal_games/chsh_classical_strategy/index.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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"
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Kata {
function WinCondition (x : Bool, y : Bool, a : Bool, b : Bool) : Bool {
// Implement your solution here...

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Kata {
function WinCondition (x : Bool, y : Bool, a : Bool, b : Bool) : Bool {
return (x and y) == (a != b);
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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.

Original file line number Diff line number Diff line change
@@ -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"
})
61 changes: 61 additions & 0 deletions katas/content/nonlocal_games/index.md
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 1deae14

Please sign in to comment.