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 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
37 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
ace43c7
Merge branch 'microsoft:main' into MigrateNonlocalGames-1596-task2-GH…
ggridin Nov 19, 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
88 changes: 88 additions & 0 deletions katas/content/nonlocal_games/examples/GHZGameDemo.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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]);
}

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

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

operation 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 {
let res = MResetX(qubit);
return res == One;
}
let res = MResetZ(qubit);
return res == One;
}

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

operation CharlieQuantum (bit : Bool, qubit : Qubit) : Bool {
if bit {
let res = MResetX(qubit);
return res == One;
}
let res = MResetZ(qubit);
return res == 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,17 @@
namespace Kata {
operation CreateEntangledTriple (qs : Qubit[]) : Unit is Adj {
X(qs[0]);
X(qs[1]);

H(qs[0]);
H(qs[1]);
// At this point we have (|000⟩ - |010⟩ - |100⟩ + |110⟩) / 2
ggridin marked this conversation as resolved.
Show resolved Hide resolved

// Flip the sign of the last term
Controlled Z([qs[0]], qs[1]);

// Flip the state of the last qubit for the two middle terms
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,40 @@
namespace Kata.Verification {
open Microsoft.Quantum.Diagnostics;

operation CreateEntangledTriple_Reference (qs : Qubit[]) : Unit is Adj {
X(qs[0]);
X(qs[1]);

H(qs[0]);
H(qs[1]);
// At this point we have (|000⟩ - |010⟩ - |100⟩ + |110⟩) / 2

// Flip the sign of the last term
Controlled Z([qs[0]], qs[1]);

// Flip the state of the last qubit for the two middle terms
ApplyControlledOnBitString([false, true], X, [qs[0], qs[1]], qs[2]);
ApplyControlledOnBitString([true, false], X, [qs[0], qs[1]], qs[2]);
}

@EntryPoint()
operation CheckSolution() : Bool {
use qs = Qubit[3];
ggridin marked this conversation as resolved.
Show resolved Hide resolved
// apply operation that needs to be tested
Kata.CreateEntangledTriple(qs);

// apply adjoint reference operation and check that the result is |0ᴺ⟩
Adjoint CreateEntangledTriple_Reference(qs);

// check that all qubits end up in |0⟩ state
let result = CheckAllZero(qs);
ResetAll(qs);
if result {
Message("Correct!");
}
else {
Message("Entangled triple is not implemented correctly");
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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.
ggridin marked this conversation as resolved.
Show resolved Hide resolved

**Goal:**
- Create the entangled state $\ket{\Phi} = \frac{1}{2} \big(\ket{000} - \ket{011} - \ket{101} - \ket{110} \big)$ on these qubits.
ggridin marked this conversation as resolved.
Show resolved Hide resolved
This state is equivalent to the [GHZ state]([GHZ state](https://en.wikipedia.org/wiki/Greenberger%E2%80%93Horne%E2%80%93Zeilinger_state))
ggridin marked this conversation as resolved.
Show resolved Hide resolved
$\frac{1}{\sqrt{2}} (\big(\ket{000} + \ket{111} \big)$ up to local unitary operation.
ggridin marked this conversation as resolved.
Show resolved Hide resolved
ggridin marked this conversation as resolved.
Show resolved Hide resolved
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)$
ggridin marked this conversation as resolved.
Show resolved Hide resolved
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://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.canon.controlledonbitstring) 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:
ggridin marked this conversation as resolved.
Show resolved Hide resolved
$\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;
}
}
27 changes: 27 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,27 @@
namespace Kata {
operation AliceQuantum (bit : Bool, qubit : Qubit) : Bool {
if bit {
let res = MResetX(qubit);
return res == One;
ggridin marked this conversation as resolved.
Show resolved Hide resolved
}
let res = MResetZ(qubit);
return res == One;
}

operation BobQuantum (bit : Bool, qubit : Qubit) : Bool {
if bit {
let res = MResetX(qubit);
return res == One;
}
let res = MResetZ(qubit);
return res == 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 0 .. iterations - 1 {
ggridin marked this conversation as resolved.
Show resolved Hide resolved
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($"Player's quantum strategies get {wins} wins out of {iterations*Length(inputs)} possible inputs, which is not optimal");
ggridin marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
Message("Correct!");
true
}
}
8 changes: 8 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,8 @@
**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://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.intrinsic.measure)
ggridin marked this conversation as resolved.
Show resolved Hide resolved
or convenient shorthands for measure-and-reset-to-$\ket{0}$ 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).

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