Skip to content

Commit

Permalink
Migrate nonlocal games microsoft#1596 task1 classical
Browse files Browse the repository at this point in the history
  • Loading branch information
ggridin committed Jul 16, 2024
1 parent 1deae14 commit c9d2eda
Show file tree
Hide file tree
Showing 14 changed files with 438 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Kata.Verification {
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 x = DrawRandomBool(0.5);
let y = DrawRandomBool(0.5);
let (a, b) = (Kata.AliceClassical(x), Kata.BobClassical(y));
if ((x and y) == (a != b)) {
set wins = wins + 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
**Input:**
* Alice and Bob's starting bits (X and Y).
* Alice and Bob's output bits (A and B).

- 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Kata {
operation AliceQuantum (bit : Bool, qubit : Qubit) : Bool {
// Implement your solution here...

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Kata {
operation AliceQuantum (bit : Bool, qubit : Qubit) : Bool {

// Measure in sign basis if bit is 1, and
// measure in computational basis if bit is 0
if bit {
let q = MResetX(qubit);
return (q == One);
}
else {
let q = MResetZ(qubit);
return (q == One);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace Kata.Verification {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Random;

@EntryPoint()
operation CheckSolution() : Bool {
use q = Qubit();
for _ in 1 .. 4 {
// repeat 4 times since we are testing a measurement and wrong basis still might get
// the correct answer, reduces probability of false positives
if (Kata.AliceQuantum(false, q) != false) {
Message("|0⟩ not measured as false");
Reset(q);
return false;
}

// apply the Pauli X gate
X(q);
if (Kata.AliceQuantum(false, q) != true) {
Message("|1⟩ not measured as true");
Reset(q);
return false;
}

// apply the Hadamard gate
H(q);
if (Kata.AliceQuantum(true, q) != false) {
Message("|+⟩ not measured as false");
Reset(q);
return false;
}

// apply the Pauli X and then the Hadamard gate
X(q);
H(q);
if (Kata.AliceQuantum(true, q) != true) {
Message("|-⟩ not measured as true");
Reset(q);
return false;
}
Reset(q);
}
Message("Correct!");
true
}
}
11 changes: 11 additions & 0 deletions katas/content/nonlocal_games/chsh_quantum_alice_strategy/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
In the quantum version of the game, the players still can not communicate during the game,
but they are allowed to share qubits from a Bell pair before the start of the game.

**Input:**

- Alice's starting bit (X)
- Alice's half of Bell pair she shares with Bob.

**Goal:**
Measure Alice's qubit in the Z basis if her bit is 0 (false), or the X basis if her bit is 1 (true), and return the result.
The state of the qubit after the operation does not matter.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
In Q#, you can perform measurements in a specific basis using either the
[Measure operation](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.intrinsic.measure)
or convenient shorthands for measure-and-reset-to-$|0\rangle$ sequence of operations
[MResetZ](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.measurement.mresetz) and
[MResetX](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.measurement.mresetx).

(See the the lesson below for details on why Alice should follow this strategy.)

@[solution]({
"id": "nonlocal_games__chsh_quantum_alice_strategy_solution",
"codePath": "Solution.qs"
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Kata {
open Microsoft.Quantum.Math;

operation BobQuantum (bit : Bool, qubit : Qubit) : Bool {
// Implement your solution here...

return false;
}
}
10 changes: 10 additions & 0 deletions katas/content/nonlocal_games/chsh_quantum_bob_strategy/Solution.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Kata {
open Microsoft.Quantum.Math;

operation BobQuantum (bit : Bool, qubit : Qubit) : Bool {

let angle = 2.0 * PI() / 8.0;
Ry(not bit ? -angle | angle, qubit);
return M(qubit) == One;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace Kata.Verification {
open Microsoft.Quantum.Math;

operation RotateBobQubit (clockwise : Bool, qubit : Qubit) : Unit {
if (clockwise) {
Ry(-PI()/4.0, qubit);
} else {
Ry(PI()/4.0, qubit);
}
}

@EntryPoint()
operation CheckSolution() : Bool {
for _ in 1 .. 4 {
// repeat 4 times since we are testing a measurement and wrong basis still might get
// the correct answer, reduces probability of false positives
use q = Qubit();
RotateBobQubit(false, q);
let result = Kata.BobQuantum(false, q);
Reset(q);
if (result != false) {
Message("π/8 from |0⟩ not measured as false");
return false;
}

X(q);
RotateBobQubit(false, q);
let result = Kata.BobQuantum(false, q);
Reset(q);
if (result != true) {
Message("π/8 from |1⟩ not measured as true");
return false;
}

RotateBobQubit(true, q);
let result = Kata.BobQuantum(true, q);
Reset(q);
if (result != false) {
Message("-π/8 from |0⟩ not measured as false");
return false;
}

X(q);
RotateBobQubit(true, q);
let result = Kata.BobQuantum(true, q);
Reset(q);
if (result != true) {
Message("-π/8 from |1⟩ not measured as true");
return false;
}
}
Message("Correct!");
true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
**Input:**

- Bob's starting bit (Y)
- Bob's half of Bell pair he shares with Alice.

**Goal:**
Measure Bob's qubit in the $\frac{\pi}{8}$ basis if his bit is 0 (false), or the $-\frac{\pi}{8}$ basis if his bit is 1 (true), and return the result.
The state of the qubit after the operation does not matter.
11 changes: 11 additions & 0 deletions katas/content/nonlocal_games/chsh_quantum_bob_strategy/solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Measuring a qubit in the $\theta$ basis is the same as rotating the qubit by $\theta$, clockwise, and then making a standard measurement in the Z basis.

To implement the described transformation in Q#, we need to rotate the qubit by $\frac{\pi}{8}$ clockwise if `bit = false` or counterclockwise if `bit = true` and then perform a measurement.
We can do the rotation using the previous task (note the negation of the boolean parameter we need to do).

(See the lesson below for details on why Bob should follow this strategy.)

@[solution]({
"id": "nonlocal_games__chsh_quantum_bob_strategy_solution",
"codePath": "Solution.qs"
})
99 changes: 99 additions & 0 deletions katas/content/nonlocal_games/examples/CHSHGameDemo.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

//////////////////////////////////////////////////////////////////////
// This file contains reference solutions to all above exercises.
// We recommend that you try to solve the exercises yourself first,
// but feel free to look up the solution if you get stuck.
//////////////////////////////////////////////////////////////////////

namespace Quantum.Kata.CHSHGame {

open Microsoft.Quantum.Random;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Intrinsic;

// Win condition for both classical and quantum games
function WinCondition (x : Bool, y : Bool, a : Bool, b : Bool) : Bool {
return (x and y) == (a != b);
}

//////////////////////////////////////////////////////////////////
// Classical CHSH
//////////////////////////////////////////////////////////////////

// Alice and Bob's classical strategy
// (Both players should return the same value, regardless of input)
function AliceClassical (x : Bool) : Bool {
return false;
}

function BobClassical (y : Bool) : Bool {
return false;
}

//////////////////////////////////////////////////////////////////
// Quantum CHSH
//////////////////////////////////////////////////////////////////

// Create entangled pair
operation CreateEntangledPair (aliceQubit : Qubit, bobQubit : Qubit) : Unit is Adj {
H(aliceQubit);
CNOT(aliceQubit, bobQubit);
}

// Alice's quantum strategy
operation AliceQuantum (bit : Bool, qubit : Qubit) : Bool {
// Measure in sign basis if bit is 1, and
// measure in computational basis if bit is 0
if bit {
let q = MResetX(qubit);
return (q == One);
}
else {
let q = MResetZ(qubit);
return (q == One);
}
}

// Bob's quantum strategy
operation BobQuantum (bit : Bool, qubit : Qubit) : Bool {

let angle = 2.0 * PI() / 8.0;
Ry(not bit ? -angle | angle, qubit);
return M(qubit) == One;
}

operation PlayClassicCHSH(x : Bool, y: Bool) : Bool
{
let (a, b) = (AliceClassical(x), BobClassical(y));
Message($"Classical Alice and Bob respond with a={a} and b={b}");
return WinCondition(x, y, a, b);
}

operation PlayQuantumCHSH(x : Bool, y : Bool, aliceQubit: Qubit, bobQubit : Qubit) : Bool
{
let (a, b) = (AliceQuantum(x, aliceQubit), BobQuantum(y, bobQubit));
Message($"Quantum Alice and Bob respond with a={a} and b={b}");
return WinCondition(x, y, a, b);
}

@EntryPoint()
operation CHSH_GameDemo() : (Bool, Bool) {
// create entaingled pair before the game
use aliceQubit = Qubit();
use bobQubit = Qubit();
CreateEntangledPair(aliceQubit, bobQubit);

let (x, y) = (DrawRandomBool(0.5), DrawRandomBool(0.5));
Message($"Referee has bits x={x} and y={y}");
let isAliceBobWinClassic = PlayClassicCHSH(x, y);
Message($"Alice and Bob {isAliceBobWinClassic ? "win" | "lose"} classical CHSG game");
let isAliceBobWinQuantum = PlayQuantumCHSH(x, y, aliceQubit, bobQubit);
Message($"Alice and Bob {isAliceBobWinQuantum ? "win" | "lose"} quantum CHSG game");

ResetAll([aliceQubit, bobQubit]);
return (isAliceBobWinClassic, isAliceBobWinQuantum);
}
}
Loading

0 comments on commit c9d2eda

Please sign in to comment.