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 task2 ghz quantum #1840

Open
wants to merge 36 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f1c08d7
Migrate nonlocal games #1596 task2 GHZ quantum
ggridin Aug 13, 2024
6900258
Migrate nonlocal games #1596 task2 GHZ demo and discussion
ggridin Aug 13, 2024
f47ca5c
Bug fixes and wording corrections
ggridin Aug 14, 2024
1d1b2a3
Copy-paste error fix
ggridin Aug 14, 2024
ce6eef7
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Aug 14, 2024
1a6c786
Fix title capitalization
ggridin Aug 14, 2024
6a93bba
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Aug 15, 2024
9f9405c
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Aug 15, 2024
96692c0
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Aug 16, 2024
67504e6
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Aug 20, 2024
db3db99
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Aug 20, 2024
2e43f1f
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Aug 21, 2024
3b5c1ef
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Aug 22, 2024
4c472f2
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Aug 26, 2024
efceb5a
Update katas/content/nonlocal_games/ghz_create_entangled_triple/index.md
ggridin Aug 27, 2024
2e7d518
Update katas/content/nonlocal_games/ghz_create_entangled_triple/index.md
ggridin Aug 27, 2024
57f2990
Update katas/content/nonlocal_games/ghz_create_entangled_triple/index.md
ggridin Aug 27, 2024
2483972
Update katas/content/nonlocal_games/ghz_create_entangled_triple/solut…
ggridin Aug 27, 2024
33b5897
Update katas/content/nonlocal_games/ghz_quantum_strategy/Verification.qs
ggridin Aug 27, 2024
e0d1b9d
Update katas/content/nonlocal_games/ghz_quantum_strategy/Verification.qs
ggridin Aug 27, 2024
3ed29cb
Update katas/content/nonlocal_games/ghz_create_entangled_triple/index.md
ggridin Aug 27, 2024
da30632
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Aug 27, 2024
60b736f
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Aug 29, 2024
16e6e68
Addressing PR comments, step 1
ggridin Aug 29, 2024
2273a6d
Re-phrase quantum strategy assignment
ggridin Aug 29, 2024
13dde36
Warning fix: classic strategies should be functions
ggridin Aug 29, 2024
88e263f
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Aug 29, 2024
fd1e365
Add reference to GHZ and triple states equivalence discussion
ggridin Sep 4, 2024
a5b495b
Merge branch 'MigrateNonlocalGames-1596-task2-GHZ-quantum' of https:/…
ggridin Sep 4, 2024
468e8c3
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Sep 4, 2024
1eff1e5
Apply better formatting
ggridin Sep 4, 2024
c7fb90a
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Sep 8, 2024
584a18e
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Sep 19, 2024
420acbe
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Sep 28, 2024
6a9a8c4
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Oct 7, 2024
b9aa3d3
Update katas/content/nonlocal_games/index.md
ggridin Oct 7, 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
82 changes: 82 additions & 0 deletions katas/content/nonlocal_games/examples/GHZGameDemo.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
namespace Quantum.Kata.GHZGame {
open Microsoft.Quantum.Random;
open Microsoft.Quantum.Convert;

function WinCondition (rst : Bool[], abc : Bool[]) : Bool {
return (rst[0] or rst[1] or rst[2]) == (abc[0] != abc[1] != abc[2]);
}

function AliceClassical (r : Bool) : Bool {
return true;
}

function BobClassical (s : Bool) : Bool {
return true;
}

function CharlieClassical (t : Bool) : Bool {
return true;
}

operation CreateEntangledTriple (qs : Qubit[]) : Unit is Adj {
X(qs[0]);
X(qs[1]);
H(qs[0]);
H(qs[1]);
Controlled Z([qs[0]], qs[1]);
ApplyControlledOnBitString([false, true], X, [qs[0], qs[1]], qs[2]);
ApplyControlledOnBitString([true, false], X, [qs[0], qs[1]], qs[2]);
}

operation AliceQuantum (bit : Bool, qubit : Qubit) : Bool {
if bit {
return MResetX(qubit) == One;
}
return MResetZ(qubit) == One;
}

operation BobQuantum (bit : Bool, qubit : Qubit) : Bool {
if bit {
return MResetX(qubit) == One;
}
return MResetZ(qubit) == One;
}

operation CharlieQuantum (bit : Bool, qubit : Qubit) : Bool {
if bit {
return MResetX(qubit) == One;
}
return MResetZ(qubit) == One;
}

operation getRandomRefereeBits () : Bool[] {
let bits = [[false, false, false],
[true, true, false],
[false, true, true],
[true, false, true]];
return bits[DrawRandomInt(0, 3)];
}

@EntryPoint()
operation GHZ_GameDemo () : Unit {
use (aliceQubit, bobQubit, charlieQubit) = (Qubit(), Qubit(), Qubit());
mutable classicalWins = 0;
mutable quantumWins = 0;
let iterations = 1000;
for _ in 1 .. iterations {
CreateEntangledTriple([aliceQubit, bobQubit, charlieQubit]);
let inputs = getRandomRefereeBits();
let coutputs = [AliceClassical(inputs[0]), BobClassical(inputs[1]), CharlieClassical(inputs[2])];
if WinCondition(inputs, coutputs) {
set classicalWins += 1;
}
let qoutputs = [AliceQuantum(inputs[0], aliceQubit), BobQuantum(inputs[1], bobQubit), CharlieQuantum(inputs[2], charlieQubit)];
if WinCondition(inputs, qoutputs) {
set quantumWins += 1;
}
ResetAll([aliceQubit, bobQubit, charlieQubit]);
}
Message($"Percentage of classical wins is {100.0*IntAsDouble(classicalWins)/IntAsDouble(iterations)}%");
Message($"Percentage of quantum wins is {100.0*IntAsDouble(quantumWins)/IntAsDouble(iterations)}%");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Kata {
operation CreateEntangledTriple (qs : Qubit[]) : Unit {
// Implement your solution here...
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Kata {
operation CreateEntangledTriple (qs : Qubit[]) : Unit is Adj {
X(qs[0]);
X(qs[1]);
H(qs[0]);
H(qs[1]);
Controlled Z([qs[0]], qs[1]);
ApplyControlledOnBitString([false, true], X, [qs[0], qs[1]], qs[2]);
ApplyControlledOnBitString([true, false], X, [qs[0], qs[1]], qs[2]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Kata.Verification {
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Katas;

operation CreateEntangledTriple_Reference (qs : Qubit[]) : Unit is Adj {
X(qs[0]);
X(qs[1]);
H(qs[0]);
H(qs[1]);
Controlled Z([qs[0]], qs[1]);
ApplyControlledOnBitString([false, true], X, [qs[0], qs[1]], qs[2]);
ApplyControlledOnBitString([true, false], X, [qs[0], qs[1]], qs[2]);
}

@EntryPoint()
operation CheckSolution() : Bool {
CheckOperationsEquivalenceOnZeroStateWithFeedback(Kata.CreateEntangledTriple, CreateEntangledTriple_Reference, 3)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
In the quantum version of the game, the players still can not communicate during the game, but they are allowed to share
qubits from an entangled triple before the start of the game.

**Input:**
An array of three qubits in the $\ket{000}$ state.

**Goal:**
Create the entangled state $\ket{\Phi} = \frac{1}{2} \big(\ket{000} - \ket{011} - \ket{101} - \ket{110} \big)$ on these qubits.

>This state is equivalent to the three-qubit [GHZ state](https://en.wikipedia.org/wiki/Greenberger%E2%80%93Horne%E2%80%93Zeilinger_state)
>$$\frac{1}{\sqrt{2}} \big(\ket{000} + \ket{111} \big)$$
>up to local unitary operations. Please refer to the follow-up GHZ Quantum Strategy discussion for details.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
1. Apply an X gate to the first and the second qubits to get the $\ket{110}$ state.
2. Appy an H gate to the first and the second qubits to get the following state:
$$\frac12 \big( \ket{000} - \ket{010} - \ket{100} + \ket{110} \big)$$
3. Flip the sign of the last term using a controlled Z gate with the first qubit as control and the second qubit as target (or vice versa):
$$\frac12 \big( \ket{000} - \ket{010} - \ket{100} -{\color{blue}\ket{110}} \big)$$
4. Now we have the right signs for each term, and the first and the last terms match those of the state we're preparing, so we just need to adjust the two middle terms.
To do this, we can use [ControlledOnBitString](https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.canon/applycontrolledonbitstring) operation to flip the state of the last qubit if the first two qubits are in $\ket{01}$ or in $\ket{10}$ states, which gives us:
$$\frac{1}{2} \big(\ket{000} - {\color{blue}\ket{011}} - {\color{blue}\ket{101}} - \ket{110} \big)$$

@[solution]({
"id": "nonlocal_games__ghz_create_entangled_triple_solution",
"codePath": "Solution.qs"
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Kata {
operation AliceQuantum (bit : Bool, qubit : Qubit) : Bool {
// Implement your solution here...

return false;
}

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

return false;
}

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

return false;
}
}
23 changes: 23 additions & 0 deletions katas/content/nonlocal_games/ghz_quantum_strategy/Solution.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Kata {
operation AliceQuantum (bit : Bool, qubit : Qubit) : Bool {
if bit {
return MResetX(qubit) == One;
}
return MResetZ(qubit) == One;
}

operation BobQuantum (bit : Bool, qubit : Qubit) : Bool {
if bit {
return MResetX(qubit) == One;
}
return MResetZ(qubit) == One;
}

// alternative implementation
operation CharlieQuantum (bit : Bool, qubit : Qubit) : Bool {
if bit {
ggridin marked this conversation as resolved.
Show resolved Hide resolved
H(qubit);
}
return M(qubit) == One;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace Kata.Verification {

function WinCondition_Reference (rst : Bool[], abc : Bool[]) : Bool {
return (rst[0] or rst[1] or rst[2]) == (abc[0] != abc[1] != abc[2]);
}

function RefereeBits () : Bool[][] {
return [[false, false, false],
[true, true, false],
[false, true, true],
[true, false, true]];
}

operation CreateEntangledTriple_Reference (qs : Qubit[]) : Unit is Adj {
X(qs[0]);
X(qs[1]);
H(qs[0]);
H(qs[1]);
Controlled Z([qs[0]], qs[1]);
ApplyControlledOnBitString([false, true], X, [qs[0], qs[1]], qs[2]);
ApplyControlledOnBitString([true, false], X, [qs[0], qs[1]], qs[2]);
}

operation PlayQuantumGHZ_Reference (strategies : ((Bool, Qubit) => Bool)[], inputs : Bool[], qubits : Qubit[]) : Bool[] {
let r = inputs[0];
let s = inputs[1];
let t = inputs[2];
let a = strategies[0](r, qubits[0]);
let b = strategies[1](s, qubits[1]);
let c = strategies[2](t, qubits[2]);
return [a, b, c];
}

@EntryPoint()
operation CheckSolution () : Bool {
use qs = Qubit[3];
let inputs = RefereeBits();
let strategies = [Kata.AliceQuantum, Kata.BobQuantum, Kata.CharlieQuantum];

let iterations = 1000;
mutable wins = 0;
for _ in 1 .. iterations {
for bits in inputs {
CreateEntangledTriple_Reference(qs);
let abc = PlayQuantumGHZ_Reference(strategies, bits, qs);
if WinCondition_Reference(bits, abc) {
set wins = wins + 1;
}
ResetAll(qs);
}
}
if wins < iterations*Length(inputs) {
Message($"Players' quantum strategies get {wins} wins out of {iterations*Length(inputs)} runs, which is not optimal");
return false;
}
Message("Correct!");
true
}
}
11 changes: 11 additions & 0 deletions katas/content/nonlocal_games/ghz_quantum_strategy/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
In this task, you should implement three functions, one for each player's quantum strategy.
Note that they are covered by one test, so you must implement all of them to pass the test.

**Inputs:**
ggridin marked this conversation as resolved.
Show resolved Hide resolved

1. The input bit for one of each of the players (R, S and T respectively),
2. That player's qubit of the entangled triple shared between the players.

**Goal:**
Measure the qubit in the Z basis if the bit is 0 (FALSE), or the X basis if the 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 katas/content/nonlocal_games/ghz_quantum_strategy/solution.md
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://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.intrinsic/measure)
or convenient shorthands for measure-and-reset-to-$\ket{0}$ sequence of operations
[MResetZ](https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.measurement/mresetz) and
[MResetX](https://learn.microsoft.com/qsharp/api/qsharp-lang/microsoft.quantum.measurement/mresetx).

Alternatively, you can recall that measuring the qubit in the X basis is equivalent to applying an H gate to it and measuring it in the Z basis.

@[solution]({
"id": "nonlocal_games__ghz_quantum_strategy_solution",
"codePath": "Solution.qs"
})
Loading