forked from microsoft/qsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate nonlocal games microsoft#1596 task1 classical
- Loading branch information
Showing
14 changed files
with
438 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
katas/content/nonlocal_games/chsh_classical_win_condition/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
katas/content/nonlocal_games/chsh_quantum_alice_strategy/Placeholder.qs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
katas/content/nonlocal_games/chsh_quantum_alice_strategy/Solution.qs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
katas/content/nonlocal_games/chsh_quantum_alice_strategy/Verification.qs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
katas/content/nonlocal_games/chsh_quantum_alice_strategy/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
12 changes: 12 additions & 0 deletions
12
katas/content/nonlocal_games/chsh_quantum_alice_strategy/solution.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}) |
9 changes: 9 additions & 0 deletions
9
katas/content/nonlocal_games/chsh_quantum_bob_strategy/Placeholder.qs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
katas/content/nonlocal_games/chsh_quantum_bob_strategy/Solution.qs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
katas/content/nonlocal_games/chsh_quantum_bob_strategy/Verification.qs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
katas/content/nonlocal_games/chsh_quantum_bob_strategy/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
katas/content/nonlocal_games/chsh_quantum_bob_strategy/solution.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.