Convert the GHZ game kata to Jupyter Notebook format (#120)

This commit is contained in:
Jack Hyder 2019-05-29 14:20:07 -07:00 коммит произвёл Mariia Mykhailova
Родитель 789ad28a6d
Коммит 42f04ccb07
6 изменённых файлов: 311 добавлений и 10 удалений

Просмотреть файл

@ -63,6 +63,9 @@ RUN jupyter nbconvert CHSHGame/CHSHGame.ipynb --execute --stdout --to markdown
RUN dotnet build DeutschJozsaAlgorithm
RUN jupyter nbconvert DeutschJozsaAlgorithm/DeutschJozsaAlgorithm.ipynb --execute --stdout --to markdown --allow-errors --ExecutePreprocessor.timeout=120
RUN dotnet build GHZGame
RUN jupyter nbconvert GHZGame/GHZGame.ipynb --execute --stdout --to markdown --allow-errors --ExecutePreprocessor.timeout=120
RUN dotnet build Measurements
RUN jupyter nbconvert Measurements/Measurements.ipynb --execute --stdout --to markdown --allow-errors --ExecutePreprocessor.timeout=120

Просмотреть файл

@ -19,8 +19,4 @@
<ItemGroup>
<None Include="README.md" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>

302
GHZGame/GHZGame.ipynb Normal file
Просмотреть файл

@ -0,0 +1,302 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# GHZ Game\n",
"\n",
"The **GHZ Game** quantum kata is a series of exercises designed\n",
"to get you familiar with the GHZ game.\n",
"\n",
"In it three players (Alice, Bob and Charlie) try to win the following game:\n",
"\n",
"Each of them is given a bit (r, s and t respectively), and\n",
"they have to return new bits (a, b and c respectively) so\n",
"that r s t = a ⊕ b ⊕ c. The input bits will have\n",
"zero or two bits set to true and three or one bits set to false.\n",
"The trick is, the players can not communicate during the game.\n",
"\n",
"* You can read more about the GHZ game in the [lecture notes](https://cs.uwaterloo.ca/~watrous/CPSC519/LectureNotes/20.pdf) by John Watrous. \n",
"* Another description can be found in the [lecture notes](https://staff.fnwi.uva.nl/m.walter/physics491/lecture1.pdf) by Michael Walter.\n",
"\n",
"Each task is wrapped in one operation preceded by the description of the task.\n",
"Each task has a unit test associated with it, which initially fails.\n",
"Your goal is to fill in the blank (marked with the `// ...` comments)\n",
"with some Q# code that solves the task. To verify your answer, run the cell using Ctrl/⌘+Enter.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To begin, first prepare this notebook for execution (if you skip this step, you'll get \"Syntax does not match any known patterns\" error when you try to execute Q# code in the next cells):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%package Microsoft.Quantum.Katas::0.6.1905.301"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> The package versions in the output of the cell above should always match. If you are running the Notebooks locally and the versions do not match, please install the IQ# version that matches the version of the `Microsoft.Quantum.Katas` package.\n",
"> <details>\n",
"> <summary><u>How to install the right IQ# version</u></summary>\n",
"> For example, if the version of `Microsoft.Quantum.Katas` package above is 0.6.1905.301, the installation steps are as follows:\n",
">\n",
"> 1. Stop the kernel.\n",
"> 2. Uninstall the existing version of IQ#:\n",
"> dotnet tool uninstall microsoft.quantum.iqsharp -g\n",
"> 3. Install the matching version:\n",
"> dotnet tool install microsoft.quantum.iqsharp -g --version 0.6.1905.301\n",
"> 4. Reinstall the kernel:\n",
"> dotnet iqsharp install\n",
"> 5. Restart the Notebook.\n",
"> </details>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part I. Classical GHZ\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 1.1. Win Condition\n",
"**Inputs:** \n",
"\n",
" 1. Alice, Bob and Charlie's input bits (r, s and t), stored as an array of length 3,\n",
"\n",
" 2. Alice, Bob and Charlie's output bits (a, b and c), stored as an array of length 3.\n",
"\n",
"The input bits will have zero or two bits set to true.\n",
"\n",
"**Output:** \n",
"True if Alice, Bob and Charlie won the GHZ game, that is, if r s t = a ⊕ b ⊕ c, and false otherwise."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T11_WinCondition_Test \n",
"\n",
"function WinCondition (rst : Bool[], abc : Bool[]) : Bool {\n",
" // ...\n",
" fail \"Task 1.1 not implemented yet\";\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 1.2. Random classical strategy\n",
"\n",
"**Input:** The input bit for one of the players (r, s or t).\n",
"\n",
"**Output:** A random bit that this player will output (a, b or c).\n",
"\n",
"If all players use this strategy, they will win about 50% of the time."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T12_RandomClassical_Test \n",
"\n",
"operation RandomClassicalStrategy (input : Bool) : Bool {\n",
" // ...\n",
" fail \"Task 1.2 not implemented yet\";\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 1.3. Best classical strategy\n",
"\n",
"**Input:** The input bit for one of the players (r, s or t).\n",
"\n",
"**Output:** A bit that this player will output (a, b or c) to maximize their chance of winning.\n",
"\n",
"All players will use the same strategy.\n",
"The best classical strategy should win about 75% of the time."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T13_BestClassical_Test \n",
"\n",
"operation BestClassicalStrategy (input : Bool) : Bool {\n",
" // ...\n",
" fail \"Task 1.3 not implemented yet\";\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 1.4. Referee classical GHZ game\n",
"\n",
"**Inputs:** \n",
"\n",
" 1. An operation which implements a classical strategy (i.e., takes an input bit and produces an output bit),\n",
"\n",
" 2. An array of 3 input bits that should be passed to the players.\n",
"\n",
"**Output:** \n",
"An array of 3 bits that will be produced if each player uses this strategy."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T14_PlayClassicalGHZ_Test \n",
"\n",
"operation PlayClassicalGHZ (strategy : (Bool => Bool), inputs : Bool[]) : Bool[] {\n",
" // ...\n",
" fail \"Task 1.4 not implemented yet\";\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part II. Quantum GHZ\n",
"\n",
"In the quantum version of the game, the players still can not\n",
"communicate during the game, but they are allowed to share \n",
"qubits from an entangled triple before the start of the game.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 2.1. Entangled triple\n",
"\n",
"**Input:** An array of three qubits in the $|000\\rangle$ state.\n",
"\n",
"**Goal:** Create the entangled state $|\\Phi\\rangle = \\frac{1}{2} \\big(|000\\rangle - |011\\rangle - |101\\rangle - |110\\rangle \\big)$ on these qubits."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T21_CreateEntangledTriple_Test \n",
"\n",
"operation CreateEntangledTriple (qs : Qubit[]) : Unit {\n",
" // ...\n",
" fail \"Task 2.1 not implemented yet\";\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 2.2. Quantum strategy\n",
"\n",
"**Inputs:**\n",
"\n",
" 1. The input bit for one of the players (r, s or t),\n",
"\n",
" 2. That player's qubit of the entangled triple shared between the players.\n",
"\n",
"**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.\n",
"\n",
"The state of the qubit after the operation does not matter."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T22_QuantumStrategy_Test \n",
"\n",
"operation QuantumStrategy (input : Bool, qubit : Qubit) : Bool {\n",
" // ...\n",
" fail \"Task 2.2 not implemented yet\";\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 2.3. Play the GHZ game using the quantum strategy\n",
"\n",
"**Input:** Operations that return Alice, Bob and Charlie's output bits (a, b and c) based on\n",
"their quantum strategies and given their respective qubits from the entangled triple.\n",
"The players have already been told what their starting bits (r, s and t) are.\n",
"\n",
"**Goal:** Return an array of players' output bits (a, b and c).\n",
"\n",
"Note that this task uses QuantumStrategy which you've implemented in task 2.2.\n",
"\n",
"<span style=\"color:red\">*This task is not evaluated properly with version 0.6 of the packages; please return to it after 0.7 release.*</span>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T23_PlayQuantumGHZ_Test \n",
"\n",
"operation PlayQuantumGHZ (strategies : (Qubit => Bool)[]) : Bool[] {\n",
" // ...\n",
" fail \"Task 2.3 not implemented yet\";\n",
"}"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Q#",
"language": "qsharp",
"name": "iqsharp"
},
"language_info": {
"file_extension": ".qs",
"mimetype": "text/x-qsharp",
"name": "qsharp",
"version": "0.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

Просмотреть файл

@ -3,6 +3,8 @@
This kata covers the Greenberger-Horne-Zeilinger game (often abbreviated as GHZ game),
a well-known example of a nonlocal (entanglement) game.
You can [run the GHZ Game kata as a Jupyter Notebook](https://mybinder.org/v2/gh/Microsoft/QuantumKatas/master?filepath=GHZGame%2FGHZGame.ipynb)!
In a nonlocal game, several cooperating players play a game against a referee answering the referee's questions. The players are free to share information
(and even qubits!) before the game starts, but are forbidden from communicating
with each other afterwards. Nonlocal games show that quantum entanglement can be
@ -12,4 +14,4 @@ purely classical strategy.
#### Theory
* [Lecture 1](https://staff.fnwi.uva.nl/m.walter/physics491/lecture1.pdf) by Michael Walter.
* [Lecture 20](https://cs.uwaterloo.ca/~watrous/CPSC519/LectureNotes/20.pdf) by John Watrous.
* [Lecture 20](https://cs.uwaterloo.ca/~watrous/CPSC519/LectureNotes/20.pdf) by John Watrous.

Просмотреть файл

@ -10,6 +10,7 @@
namespace Quantum.Kata.GHZGame {
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Canon;
@ -41,11 +42,7 @@ namespace Quantum.Kata.GHZGame {
// Task 1.4. Referee classical GHZ game
operation PlayClassicalGHZ_Reference (strategy : (Bool => Bool), inputs : Bool[]) : Bool[] {
mutable results = new Bool[Length(inputs)];
for (i in 0..Length(inputs) - 1) {
set results w/= i <- strategy(inputs[i]);
}
return results;
return ForEach(strategy, inputs);
}

Просмотреть файл

@ -19,6 +19,7 @@
"* **[Deutsch–Jozsa algorithm](./DeutschJozsaAlgorithm/DeutschJozsaAlgorithm.ipynb)**.\n",
" This kata starts with writing quantum oracles which implement classical functions, and continues to introduce the Bernstein–Vazirani and Deutsch–Jozsa algorithms.\n",
"* **[CHSH game](./CHSHGame/CHSHGame.ipynb)**.\n",
"* **[GHZ Game](./GHZGame/GHZGame.ipynb)**.\n",
"\n",
"For a full list of Quantum Katas, available as Q# projects, see the [QuantumKatas repository](https://github.com/Microsoft/quantumkatas/).\n",
"\n",