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 nonlocal games #1596 task1 classical #1710

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
56f2110
Add Migrate Nonlocal Games - 1596 - CHSH classical
ggridin Jul 9, 2024
d622f32
Merge branch 'MigrateNonlocalGames-1596-task1-classical' of https://g…
ggridin Jul 9, 2024
6c41224
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task1-cl…
ggridin Jul 9, 2024
2319d0a
Address code review comments
ggridin Jul 9, 2024
6a1706f
Remove en-us from QDK link
ggridin Jul 10, 2024
e7b6d26
Remove en-us from QDK link
ggridin Jul 10, 2024
62eb555
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task1-cl…
ggridin Jul 10, 2024
e04a76b
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task1-cl…
ggridin Jul 11, 2024
85f59ec
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task1-cl…
ggridin Jul 12, 2024
b8db86a
Update katas/content/nonlocal_games/index.md
ggridin Jul 12, 2024
f37b9b7
Update katas/content/nonlocal_games/index.md
ggridin Jul 12, 2024
54aa6f2
Update katas/content/nonlocal_games/index.md
ggridin Jul 12, 2024
77f83db
Update katas/content/nonlocal_games/index.md
ggridin Jul 12, 2024
b85b080
Update katas/content/nonlocal_games/index.md
ggridin Jul 12, 2024
67cbd3d
Update katas/content/nonlocal_games/chsh_classical_win_condition/Veri…
ggridin Jul 12, 2024
9523937
Update katas/content/nonlocal_games/chsh_classical_win_condition/solu…
ggridin Jul 12, 2024
4ae50af
Update katas/content/nonlocal_games/chsh_classical_strategy/index.md
ggridin Jul 12, 2024
d57ff0e
Update katas/content/nonlocal_games/chsh_classical_strategy/solution.md
ggridin Jul 12, 2024
462856d
Message update for win condition (actual vs expected)
ggridin Jul 12, 2024
b4041df
Update katas/content/nonlocal_games/index.md
ggridin Jul 12, 2024
b42863b
Update katas/content/nonlocal_games/index.md
ggridin Jul 12, 2024
8278742
Update katas/content/nonlocal_games/chsh_classical_strategy/Verificat…
ggridin Jul 13, 2024
4c32452
Update katas/content/nonlocal_games/chsh_classical_win_condition/solu…
tcNickolas Jul 13, 2024
d45d6fd
Removing external link (John Watrous)
ggridin Jul 13, 2024
901b0fd
Update katas/content/nonlocal_games/index.md
ggridin Jul 13, 2024
bfb34a5
Adding trivial context to classical lesson
ggridin Jul 13, 2024
aa00858
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task1-cl…
ggridin Jul 13, 2024
effafc4
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task1-cl…
ggridin Jul 15, 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
1 change: 1 addition & 0 deletions katas/content/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
"marking_oracles",
"deutsch_algo",
"deutsch_jozsa",
"nonlocal_games",
"qec_shor"
]
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,27 @@
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;
}
else {
Message("Correct!");
}
true
}

}
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 actual = WinCondition_Reference(bits[0], bits[1], bits[2], bits[3]);
let expected = Kata.WinCondition(bits[0], bits[1], bits[2], bits[3]);

if actual != expected {
Message("Win condition is wrong 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. [`Microsoft.Quantum.Logical`](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.logical) library offers you logical functions `And` and `Xor` which you can use for this computation. Alternatively, you can compute these values using built-in operators: $x ∧ y$ as `x and y` and $a ⊕ b$ as `a != b`.


@[solution]({
"id": "nonlocal_games__chsh_classical_win_condition_solution",
"codePath": "Solution.qs"
})
68 changes: 68 additions & 0 deletions katas/content/nonlocal_games/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Nonlocal Games

@[section]({
"id": "nonlocal_games__overview",
"title": "Overview"
})

For our context, "nonlocal" means that the playing parties are separated by a great distance,
so they cannot communicate with each other during the game.
In a new kata, we discuss three quantum nonlocal games that display "quantum pseudo-telepathy" -
the use of quantum entanglement to eliminate the need for classical communication.
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 (entanglement) game

**What you should know to start working on this kata:**
- Basic linear algebra
- Single and multi-qubit systems
- Single and multi-qubit gates
- Single-qubit system measurements

@[section]({
"id": "nonlocal_games__chsh_game",
"title": "CHSH game"
})

The **CHSH Game** quantum kata is a series of exercises designed
to get you familiar with the CHSH game.

In it, 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.

* You can read more about CHSH game in the [lecture notes](https://cs.uwaterloo.ca/~watrous/QC-notes/QC-notes.20.pdf) by
John Watrous.
* At the end of the section you can find an implementation of the CHSH game that includes an explanation of the history and theory behind the game.

@[section]({
"id": "nonlocal_games__chsh_game_classical",
"title": "Part I. Classical CHSH"
})

@[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 communicate using quantum entanglement in nonlocal quantum games.