diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000000..9607cfe9aab --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/BasicGates/bin/Debug/netcoreapp3.1/BasicGates.dll", + "args": [], + "cwd": "${workspaceFolder}/BasicGates", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 00000000000..7b568663cd4 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/BasicGates/BasicGates.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/BasicGates/BasicGates.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/BasicGates/BasicGates.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/GHZGame/GHZGame.ipynb b/GHZGame/GHZGame.ipynb index 4878e2ecbcf..67ece8a7812 100644 --- a/GHZGame/GHZGame.ipynb +++ b/GHZGame/GHZGame.ipynb @@ -289,7 +289,7 @@ "file_extension": ".qs", "mimetype": "text/x-qsharp", "name": "qsharp", - "version": "0.4" + "version": "0.12" } }, "nbformat": 4, diff --git a/WGame/README.md b/WGame/README.md new file mode 100644 index 00000000000..ae818189f40 --- /dev/null +++ b/WGame/README.md @@ -0,0 +1,12 @@ +# Welcome! + +This "kata" covers the W game, an original example of a +nonlocal (entanglement) game inspired by the well-known GHZ and CHSH games. + +You can [run the W Game kata as a Jupyter Notebook](https://mybinder.org/v2/gh/Microsoft/QuantumKatas/master?filepath=WGame%2FWGame.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 +used to increase the players' chance of winning beyond what would be possible with a +purely classical strategy. diff --git a/WGame/ReferenceImplementation.qs b/WGame/ReferenceImplementation.qs new file mode 100644 index 00000000000..40574baafb6 --- /dev/null +++ b/WGame/ReferenceImplementation.qs @@ -0,0 +1,115 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +////////////////////////////////////////////////////////////////////// +// This file contains reference solutions to all tasks. +// The tasks themselves can be found in Tasks.qs file. +// We recommend that you try to solve the tasks yourself first, +// but feel free to look up the solution if you get stuck. +////////////////////////////////////////////////////////////////////// + +namespace Quantum.Kata.WGame { + + open Microsoft.Quantum.Arrays; + open Microsoft.Quantum.Convert; + open Microsoft.Quantum.Math; + open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Intrinsic; + open Microsoft.Quantum.Logical; + + + ////////////////////////////////////////////////////////////////// + // Part I. Classical W game + ////////////////////////////////////////////////////////////////// + + // Task 1.1. Win condition + function WinCondition_Reference (rst : Bool[], abc : Bool[]) : Bool { + mutable abcOnes = 0; + for (i in abc) { + if (i) { + set abcOnes = abcOnes + 1; + } + } + if (rst[0] or rst[1] or rst[2]) { + return (abcOnes != 1); + } + return (abcOnes == 1); + } + + + // Task 1.2. Random classical strategy + operation RandomClassicalStrategy_Reference (input : Bool) : Bool { + return RandomInt(2) == 1; + } + + + // Task 1.3. Simple classical strategy + operation SimpleClassicalStrategy_Reference (input : Bool) : Bool { + // One of several simple strategies that reaches classical win probability of 75%. + return true; + } + + + // Task 1.4. Best classical strategy + operation BestClassicalStrategy_Reference (input : Bool) : Bool { + // The optimal classical strategy, under the condition that approaches are common to all players: + // - - - If input is true, return true + // - - - If input is false, return true with probability 1/3. + // This reaches a win rate of 31/36, or roughly 86.1%. + if (input) { + return true; + } + return RandomInt(3) == 0; + } + + + // Task 1.5. Referee classical W game + operation PlayClassicalW_Reference (strategy : (Bool => Bool), inputs : Bool[]) : Bool[] { + return ForEach(strategy, inputs); + } + + + ////////////////////////////////////////////////////////////////// + // Part II. Quantum W game + ////////////////////////////////////////////////////////////////// + + // Task 2.1. Entangled triple + operation CreateEntangledTriple_Reference (qs : Qubit[]) : Unit is Adj { + let theta = ArcSin(1.0 / Sqrt(3.0)); + Ry(2.0 * theta, qs[0]); + // Starting from |000>, this produces {sqrt(2/3) |000> + sqrt(1/3) |100>}. + + (ControlledOnBitString([false], H))([qs[0]], qs[1]); + // Hadamard-transforms second qubit when first is 0, + // so this leads to (|000> + |010> + |100>) / sqrt(3). + + (ControlledOnBitString([false, false], X))([qs[0], qs[1]], qs[2]); + // Applies NOT gate to third qubit when first two are both 0, leading to the W state desired. + } + + + // Task 2.2. Quantum strategy + operation QuantumStrategy_Reference (input : Bool, qubit : Qubit) : Bool { + if (input) { + H(qubit); + } + return ResultAsBool(M(qubit)); + } + + + // Task 2.3. Play the W game using the quantum strategy + operation PlayQuantumW_Reference (strategies : (Qubit => Bool)[]) : Bool[] { + + using (qs = Qubit[3]) { + CreateEntangledTriple_Reference(qs); + + let a = strategies[0](qs[0]); + let b = strategies[1](qs[1]); + let c = strategies[2](qs[2]); + + ResetAll(qs); + return [a, b, c]; + } + } + +} diff --git a/WGame/Tasks.qs b/WGame/Tasks.qs new file mode 100644 index 00000000000..0d9302307fe --- /dev/null +++ b/WGame/Tasks.qs @@ -0,0 +1,165 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +namespace Quantum.Kata.WGame { + + open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Math; + open Microsoft.Quantum.Intrinsic; + open Microsoft.Quantum.Diagnostics; + open Microsoft.Quantum.Logical; + + ////////////////////////////////////////////////////////////////// + // Welcome! + ////////////////////////////////////////////////////////////////// + + // The "W Game" is an original quantum "kata" similar to the GHZ game + // and the CHSH game, but oriented to the properties of the W state. + + // In it three players (Alice, Bob and Charlie) try to win the + // following game: + + // Each of them is given a bit (r, s and t respectively), and + // they have to return new bits (a, b and c respectively) according + // to the following table: + + // +-------------------+------------------+ + // | Number of true | Number of true | + // | bits in input | bits in output | + // | between players | needed to win | + // +-------------------+------------------+ + // | 0 | exactly 1 | + // | 2 | 0, 2, or 3 | + // +-------------------+------------------+ + + // Either two of the input bits will be true, or all will be false; + // thus, these four scenarios are all equally likely: + + // F,F,F T,T,F T,F,T F,T,T + + // Like the GHZ and CHSH games, the players can not communicate during the game. + + // Also, in this form of the game, all the players have to use the same approach, + // if dependent on the input (i.e. in this game, the team may not have Charlie follow + // a different protocol from Alice and Bob, if any of the protocols depend on the input). + // However, this restriction only applies to strategies for which the composition of the + // team's output bits could vary; the rules permit them individual strategies that are + // independent of the input (such as Bob always outputs true while Alice and Charlie output + // false) allowing them to ensure that exactly one true bit gets submitted between them. + + // Each task is wrapped in one operation preceded by the + // description of the task. Each task has a unit test associated + // with it, which initially fails. Your goal is to fill in the + // blank (marked with // ... comment) with some Q# code to make + // the failing test pass. + + + ////////////////////////////////////////////////////////////////// + // Part I. Classical W game + ////////////////////////////////////////////////////////////////// + + // Task 1.1. Win condition + // Input: + // 1) Alice, Bob and Charlie's input bits (r, s and t), stored as an array of length 3, + // 2) Alice, Bob and Charlie's output bits (a, b and c), stored as an array of length 3. + // The input bits will have zero or two bits set to true. + // Output: + // True if Alice, Bob and Charlie won the W game + // (one true bit between them if no input bits were true + // or any other number of true bits between them if two input bits were true), + // and false otherwise. + function WinCondition (rst : Bool[], abc : Bool[]) : Bool { + // ... + fail "Task 1.1 not implemented yet"; + } + + + // Task 1.2. Random classical strategy + // Input: The input bit for one of the players (r, s or t). + // Output: A random bit that this player will output (a, b or c). + // If all players use this strategy, their win odds will be: + // (1/4 x 3/8) (zero input bits true) + // + (3/4 x 5/8) ( two input bits true) = 9/16, or about 56% of the time. + operation RandomClassicalStrategy (input : Bool) : Bool { + // ... + fail "Task 1.2 not implemented yet"; + } + + + // Task 1.3. Simple classical strategy + // Input: The input bit for one of the players (r, s or t). + // Output: A bit that this player will output (a, b or c) for a good chance of winning. + // All players will use the same strategy. + // Any of several possible naive classical strategies that win 3/4 of the time (75%). + operation SimpleClassicalStrategy (input : Bool) : Bool { + // ... + fail "Task 1.3 not implemented yet"; + } + + + // Task 1.4. Best classical strategy + // Input: The input bit for one of the players (r, s or t). + // Output: A bit that this player will output (a, b or c) to maximize their chance of winning. + // By rule, all players will use the same strategy. + // With this symmetry imposed, the optimal classical strategy should win about 86% of the time. + + // Note: Some intermediate probability theory will be involved here. + operation BestClassicalStrategy (input : Bool) : Bool { + // ... + fail "Task 1.4 not implemented yet"; + } + + + // Task 1.5. Referee classical W game + // Inputs: + // 1) an operation which implements a classical strategy + // (i.e., takes an input bit and produces and output bit), + // 2) an array of 3 input bits that should be passed to the players. + // Output: + // An array of 3 bits that will be produced if each player uses this strategy. + operation PlayClassicalW (strategy : (Bool => Bool), inputs : Bool[]) : Bool[] { + // ... + fail "Task 1.5 not implemented yet"; + } + + + ////////////////////////////////////////////////////////////////// + // Part II. Quantum W game + ////////////////////////////////////////////////////////////////// + + // 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. + + // Task 2.1. Entangled triple + // Input: An array of three qubits in the |000⟩ state. + // Goal: Create the entangled state |W⟩ = (|001⟩ + |010⟩ + |100⟩) / sqrt(3) on these qubits. + operation CreateEntangledTriple (qs : Qubit[]) : Unit { + // ... + fail "Task 2.1 not implemented yet"; + } + + + // Task 2.2. Quantum strategy + // Inputs: + // 1) The input bit for one of the players (r, s or t), + // 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. + operation QuantumStrategy (input : Bool, qubit : Qubit) : Bool { + // ... + fail "Task 2.2 not implemented yet"; + } + + + // Task 2.3. Play the W game using the quantum strategy + // Input: Operations that return Alice, Bob and Charlie's output bits (a, b and c) based on + // their quantum strategies and given their respective qubits from the entangled triple. + // The players have already been told what their starting bits (r, s and t) are. + // Goal: Return an array of players' output bits (a, b and c). + operation PlayQuantumW (strategies : (Qubit => Bool)[]) : Bool[] { + // ... + fail "Task 2.3 not implemented yet"; + } +} diff --git a/WGame/TestSuiteRunner.cs b/WGame/TestSuiteRunner.cs new file mode 100644 index 00000000000..7289e3cad70 --- /dev/null +++ b/WGame/TestSuiteRunner.cs @@ -0,0 +1,42 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +////////////////////////////////////////////////////////////////////// +// This file contains parts of the testing harness. +// You should not modify anything in this file. +// The tasks themselves can be found in Tasks.qs file. +////////////////////////////////////////////////////////////////////// + +using Microsoft.Quantum.Simulation.XUnit; +using Microsoft.Quantum.Simulation.Simulators; +using Xunit.Abstractions; +using System.Diagnostics; + +namespace Quantum.Kata.WGame +{ + public class TestSuiteRunner + { + private readonly ITestOutputHelper output; + + public TestSuiteRunner(ITestOutputHelper output) + { + this.output = output; + } + + /// + /// This driver will run all Q# tests (operations named "...Test") + /// that belong to namespace Quantum.Kata.WGame. + /// + [OperationDriver(TestNamespace = "Quantum.Kata.WGame")] + public void TestTarget(TestOperation op) + { + using (var sim = new QuantumSimulator()) + { + // OnLog defines action(s) performed when Q# test calls function Message + sim.OnLog += (msg) => { output.WriteLine(msg); }; + sim.OnLog += (msg) => { Debug.WriteLine(msg); }; + op.TestOperationRunner(sim); + } + } + } +} diff --git a/WGame/Tests.qs b/WGame/Tests.qs new file mode 100644 index 00000000000..1914dbb4f99 --- /dev/null +++ b/WGame/Tests.qs @@ -0,0 +1,148 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +////////////////////////////////////////////////////////////////////// +// This file contains testing harness for all tasks. +// You should not modify anything in this file. +// The tasks themselves can be found in Tasks.qs file. +////////////////////////////////////////////////////////////////////// + +namespace Quantum.Kata.WGame { + + open Microsoft.Quantum.Math; + open Microsoft.Quantum.Intrinsic; + open Microsoft.Quantum.Convert; + open Microsoft.Quantum.Diagnostics; + + // All possible starting bits (r, s and t) that the referee can give + // to Alice, Bob and Charlie. + function RefereeBits () : Bool[][] { + return [[false, false, false], + [true, true, false], + [false, true, true], + [true, false, true]]; + } + + operation T11_WinCondition_Test () : Unit { + for (rst in RefereeBits()) { + for (i in 0..1 <<< 3 - 1) { + let abc = IntAsBoolArray(i, 3); + EqualityFactB( + WinCondition(rst, abc), + WinCondition_Reference(rst, abc), + $"Win condition is wrong for rst={rst}, abc={abc}"); + } + } + } + + + // ------------------------------------------------------ + operation GetClassicalStrategySuccessRate (N : Int, strategy : (Bool => Bool)) : Double { + let inputs = RefereeBits(); + mutable wins = 0; + for (i in 0..N - 1) { + let rst = inputs[RandomInt(Length(inputs))]; + let abc = PlayClassicalW_Reference(strategy, rst); + if (WinCondition_Reference(rst, abc)) { + set wins = wins + 1; + } + } + return IntAsDouble(wins) / IntAsDouble(N); + } + + operation T12_RandomClassical_Test () : Unit { + EqualityWithinToleranceFact(GetClassicalStrategySuccessRate(10000, RandomClassicalStrategy), 0.5625, 0.02); + } + + + // ------------------------------------------------------ + operation T13_SimpleClassical_Test () : Unit { + EqualityWithinToleranceFact(GetClassicalStrategySuccessRate(10000, SimpleClassicalStrategy), 0.75, 0.02); + } + + + // ------------------------------------------------------ + operation T14_BestClassical_Test () : Unit { + EqualityWithinToleranceFact(GetClassicalStrategySuccessRate(10000, BestClassicalStrategy), 0.8611, 0.02); + } + + + // ------------------------------------------------------ + operation TestStrategy (input : Bool, mode : Int) : Bool { + return mode == 0 ? false | mode == 1 ? true | mode == 2 ? input | not input; + } + + + operation T15_PlayClassicalW_Test () : Unit { + // To test the interaction, run it on several deterministic strategies (not necessarily good ones) + let inputs = RefereeBits(); + for (rst in inputs) { + for (mode in 0..3) { + let result = PlayClassicalW(TestStrategy(_, mode), rst); + let expected = PlayClassicalW_Reference(TestStrategy(_, mode), rst); + AllEqualityFactB(result, expected, $"Unexpected result for rst={rst}"); + } + } + } + + ////////////////////////////////////////////////////////////////// + // Part II. Quantum W game + ////////////////////////////////////////////////////////////////// + + operation AssertEqualOnZeroState (N : Int, taskImpl : (Qubit[] => Unit), refImpl : (Qubit[] => Unit is Adj)) : Unit { + using (qs = Qubit[N]) { + // apply operation that needs to be tested + taskImpl(qs); + + // apply adjoint reference operation and check that the result is |0ᴺ⟩ + Adjoint refImpl(qs); + + // assert that all qubits end up in |0⟩ state + AssertAllZero(qs); + } + } + + operation T21_CreateEntangledTriple_Test () : Unit { + AssertEqualOnZeroState(3, CreateEntangledTriple, CreateEntangledTriple_Reference); + } + + + // ------------------------------------------------------ + operation T22_QuantumStrategy_Test () : Unit { + using (q = Qubit()) { + EqualityFactB(QuantumStrategy(false, q), false, "|0⟩ not measured as false"); + Reset(q); + + X(q); + EqualityFactB(QuantumStrategy(false, q), true, "|1⟩ not measured as true"); + Reset(q); + + H(q); + EqualityFactB(QuantumStrategy(true, q), false, "|+⟩ is not measured as false"); + Reset(q); + + X(q); + H(q); + EqualityFactB(QuantumStrategy(true, q), true, "|-⟩ is not measured as true"); + Reset(q); + } + } + + + // ------------------------------------------------------ + operation T23_PlayQuantumW_Test () : Unit { + mutable wins = 0; + for (i in 1..10000) { + let rst = (RefereeBits())[RandomInt(Length(RefereeBits()))]; + let strategies = [QuantumStrategy_Reference(rst[0], _), + QuantumStrategy_Reference(rst[1], _), + QuantumStrategy_Reference(rst[2], _)]; + let abc = PlayQuantumW(strategies); + if (WinCondition_Reference(rst, abc)) { + set wins = wins + 1; + } + } + EqualityWithinToleranceFact(IntAsDouble(wins) / 10000.0, 0.9375, 0.01); + } + +} diff --git a/WGame/WGame.csproj b/WGame/WGame.csproj new file mode 100644 index 00000000000..3914ca4da89 --- /dev/null +++ b/WGame/WGame.csproj @@ -0,0 +1,20 @@ + + + netcoreapp3.1 + x64 + false + Quantum.Kata.WGame + + + + + + + + + + + + + + diff --git a/WGame/WGame.ipynb b/WGame/WGame.ipynb new file mode 100644 index 00000000000..89217bc61e5 --- /dev/null +++ b/WGame/WGame.ipynb @@ -0,0 +1,345 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# W Game\n", + "\n", + "The **W Game** quantum kata is an original series of exercises based on the katas for the GHZ\n", + "and CHSH games, introducing an original game inspired by these and referred to here as the \"W 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 the sum (number of true bits) of their output bits corresponds\n", + "to the sum of the input bits in the following way:\n", + "\n", + "```\n", + "+-------------------------------+---------------------------------------+\n", + "| Number of true bits in | Number of true bits in |\n", + "| input between players | output needed to win |\n", + "+-------------------------------+---------------------------------------+\n", + "| 0 | exactly 1 |\n", + "| 2 | 0, 2, or 3 (i.e. any number but 1) |\n", + "+-------------------------------+---------------------------------------+\n", + "```\n", + "\n", + "The input will have zero or two bits set to true, and one or three bits set to false.\n", + "The trick is, the players can not communicate during the game.\n", + "\n", + "**Also,** in this game, all the players have to use the same approach, **if** dependent on the input (i.e. in this game, the team may not have Charlie follow a different protocol from Alice and Bob, **if** any of the protocols depend on the input). **However,** this restriction only applies to strategies for which the composition of the team's output bits could vary; the rules permit them individual strategies that are independent of the input (such as Bob always outputs true while Alice and Charlie output false) allowing them to ensure that exactly one true bit gets submitted between them.\n", + "\n", + "The W state represents one of the two non-biseparable classes of three-qubit entangled states; the GHZ state represents the other. These two states cannot be transformed into each other by local (one-qubit) quantum operations, and therefore represent two fundamentally distinct types of tripartite entanglement. For further information: https://en.wikipedia.org/wiki/W_state\n", + "\n", + "Each task is wrapped in one operation preceded by the description of the task.\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.12.20072031" + ] + }, + { + "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", + ">
\n", + "> How to install the right IQ# version\n", + "> For example, if the version of `Microsoft.Quantum.Katas` package above is 0.1.2.3, 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.1.2.3\n", + "> 4. Reinstall the kernel:\n", + "> dotnet iqsharp install\n", + "> 5. Restart the Notebook.\n", + ">
\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Part I. Classical W game\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 W game, that is, if:\n", + "\n", + "* All three input bits were false, and there was exactly one true bit among the team's output bits;\n", + "* Two input bits were true, and the number of true bits among the team's output bits was any number other than 1\n", + "\n", + "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 56% 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. Simple 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 achieve a good chance of winning.\n", + "\n", + "All players will use the same strategy.\n", + "Multiple naive classical strategies are possible that attain a theoretical win probability of 3/4, or 75% of the time." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%kata T13_SimpleClassical_Test \n", + "\n", + "operation SimpleClassicalStrategy (input : Bool) : Bool {\n", + " // ...\n", + " fail \"Task 1.3 not implemented yet\";\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Task 1.4. 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", + "By rule, all players will use the same strategy.\n", + "With this symmetry imposed, the optimal classical strategy should win just over 86% of the time.\n", + "\n", + "Some intermediate probability theory is involved here." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%kata T14_BestClassical_Test \n", + "\n", + "operation BestClassicalStrategy (input : Bool) : Bool {\n", + " // ...\n", + " fail \"Task 1.4 not implemented yet\";\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Task 1.5. Referee classical W 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 T15_PlayClassicalW_Test \n", + "\n", + "operation PlayClassicalW (strategy : (Bool => Bool), inputs : Bool[]) : Bool[] {\n", + " // ...\n", + " fail \"Task 1.5 not implemented yet\";\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Part II. Quantum W game\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 $|W\\rangle = \\frac{1}{\\sqrt{3}} \\big(|001\\rangle + |010\\rangle + |100\\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 W 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)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%kata T23_PlayQuantumW_Test \n", + "\n", + "operation PlayQuantumW (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.12" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/WGame/WGame.sln b/WGame/WGame.sln new file mode 100644 index 00000000000..4fdd5de9620 --- /dev/null +++ b/WGame/WGame.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27428.2043 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{AFBF8E91-493B-406D-9FED-5DDA7F21D245}") = "WGame", "WGame.csproj", "{8C8A4A2B-99CC-4669-B2A6-FF75046F30A5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8C8A4A2B-99CC-4669-B2A6-FF75046F30A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8C8A4A2B-99CC-4669-B2A6-FF75046F30A5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8C8A4A2B-99CC-4669-B2A6-FF75046F30A5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8C8A4A2B-99CC-4669-B2A6-FF75046F30A5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {638BD2DA-E2CE-4D23-B636-53501CA5DE05} + EndGlobalSection +EndGlobal diff --git a/WGame/Workbook_WGame.ipynb b/WGame/Workbook_WGame.ipynb new file mode 100644 index 00000000000..4bed7ba045b --- /dev/null +++ b/WGame/Workbook_WGame.ipynb @@ -0,0 +1,613 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# W Game\n", + "\n", + "The **W Game** quantum kata is an original series of exercises based on the katas for the GHZ\n", + "and CHSH games, introducing an original game inspired by these and referred to here as the \"W 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 the sum (number of true bits) of their output bits corresponds\n", + "to the sum of the input bits in the following way:\n", + "\n", + "```\n", + "+-------------------------------+---------------------------------------+\n", + "| Number of true bits in | Number of true bits in |\n", + "| input between players | output needed to win |\n", + "+-------------------------------+---------------------------------------+\n", + "| 0 | exactly 1 |\n", + "| 2 | 0, 2, or 3 (i.e. any number but 1) |\n", + "+-------------------------------+---------------------------------------+\n", + "```\n", + "\n", + "The input will have zero or two bits set to true, and one or three bits set to false.\n", + "The trick is, the players can not communicate during the game.\n", + "\n", + "**Also,** in this game, all the players have to use the same approach, **if** dependent on the input (i.e. in this game, the team may not have Charlie follow a different protocol from Alice and Bob, **if** any of the protocols depend on the input). **However,** this restriction only applies to strategies for which the composition of the team's output bits could vary; the rules permit them individual strategies that are independent of the input (such as Bob always outputs true while Alice and Charlie output false) allowing them to ensure that exactly one true bit gets submitted between them.\n", + "\n", + "The W state represents one of the two non-biseparable classes of three-qubit entangled states; the GHZ state represents the other. These two states cannot be transformed into each other by local (one-qubit) quantum operations, and therefore represent two fundamentally distinct types of tripartite entanglement. For further information: https://en.wikipedia.org/wiki/W_state\n", + "\n", + "Each task is wrapped in one operation preceded by the description of the task.\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": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "application/json": "{\"LastUpdated\":\"2020-07-30T13:43:56.6179927-07:00\",\"IsCompleted\":true,\"Description\":\"Adding package Microsoft.Quantum.Katas::0.12.20072031\",\"Subtask\":\"done\"}", + "text/plain": [ + "Adding package Microsoft.Quantum.Katas::0.12.20072031: done!" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/json": "[\"Microsoft.Quantum.Standard::0.12.20072031\",\"Microsoft.Quantum.Katas::0.12.20072031\"]", + "text/html": [ + "" + ], + "text/plain": [ + "Microsoft.Quantum.Standard::0.12.20072031, Microsoft.Quantum.Katas::0.12.20072031" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%package Microsoft.Quantum.Katas::0.12.20072031" + ] + }, + { + "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", + ">
\n", + "> How to install the right IQ# version\n", + "> For example, if the version of `Microsoft.Quantum.Katas` package above is 0.1.2.3, 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.1.2.3\n", + "> 4. Reinstall the kernel:\n", + "> dotnet iqsharp install\n", + "> 5. Restart the Notebook.\n", + ">
\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Part I. Classical W game\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 W game, that is, if:\n", + "\n", + "* All three input bits were false, and there was exactly one true bit among the team's output bits;\n", + "* Two input bits were true, and the number of true bits among the team's output bits was any number other than 1\n", + "\n", + "and false otherwise." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "application/json": "\"Success!\"", + "text/plain": [ + "Success!" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%kata T11_WinCondition_Test \n", + "\n", + "function WinCondition (rst : Bool[], abc : Bool[]) : Bool {\n", + " mutable abcOnes = 0;\n", + " for (i in abc) {\n", + " if (i) {\n", + " set abcOnes = abcOnes + 1; // Get number of true bits in output\n", + " }\n", + " }\n", + " if (rst[0] or rst[1] or rst[2]) {\n", + " return (abcOnes != 1); // Condition where two input bits are true\n", + " }\n", + " return (abcOnes == 1); // Condition where zero input bits are true\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 56% of the time." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "application/json": "\"Success!\"", + "text/plain": [ + "Success!" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%kata T12_RandomClassical_Test \n", + "\n", + "operation RandomClassicalStrategy (input : Bool) : Bool {\n", + " let coin = Random([0.5, 0.5]);\n", + " return (coin == 1);\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Task 1.3. Simple 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 achieve a good chance of winning.\n", + "\n", + "All players will use the same strategy.\n", + "Multiple naive classical strategies are possible that attain a theoretical win probability of 3/4, or 75% of the time." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "application/json": "\"Success!\"", + "text/plain": [ + "Success!" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%kata T13_SimpleClassical_Test \n", + "\n", + "operation SimpleClassicalStrategy (input : Bool) : Bool {\n", + " return true; // One of several simple classical strategies with 75% win odds.\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Task 1.4. 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", + "By rule, all players will use the same strategy.\n", + "With this symmetry imposed, the optimal classical strategy should win just over 86% of the time.\n", + "\n", + "Some intermediate probability theory is involved here." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "application/json": "\"Success!\"", + "text/plain": [ + "Success!" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%kata T14_BestClassical_Test \n", + "\n", + "open Microsoft.Quantum.Math;\n", + "\n", + "// When input is true, always return true;\n", + "// when input is false, return true with probability 1/3 and false with probability 2/3.\n", + "// This always wins against two true bits.\n", + "// Against zero true bits, this maximizes the probability at 4/9 of producing exactly one true output bit to win.\n", + "// This leads to a victory chance of (1/4 * 4/9) + (3/4 * 1) = 31/36, or about 86.11%\n", + "\n", + "operation BestClassicalStrategy (input : Bool) : Bool {\n", + " if (input) {\n", + " return true;\n", + " }\n", + " return RandomInt(3) == 0;\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Task 1.5. Referee classical W 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": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "application/json": "\"Success!\"", + "text/plain": [ + "Success!" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%kata T15_PlayClassicalW_Test \n", + "\n", + "operation PlayClassicalW (strategy : (Bool => Bool), inputs : Bool[]) : Bool[] {\n", + " // Input bits and strategy are given, so simply convert them to the output bits.\n", + " let r = inputs[0];\n", + " let s = inputs[1];\n", + " let t = inputs[2];\n", + " let a = strategy(r);\n", + " let b = strategy(s);\n", + " let c = strategy(t);\n", + " return [a, b, c];\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Part II. Quantum W game\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 $|W\\rangle = \\frac{1}{\\sqrt{3}} \\big(|001\\rangle + |010\\rangle + |100\\rangle \\big)$ on these qubits." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "application/json": "\"Success!\"", + "text/plain": [ + "Success!" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%kata T21_CreateEntangledTriple_Test \n", + "\n", + "operation CreateEntangledTriple (qs : Qubit[]) : Unit {\n", + " let theta = ArcSin(1.0 / Sqrt(3.0));\n", + " Ry(2.0 * theta, qs[0]);\n", + " // Starting from |000>, this produces {sqrt(2/3) |000> + sqrt(1/3) |100>}.\n", + "\n", + " (ControlledOnBitString([false], H))([qs[0]], qs[1]);\n", + " // Hadamard-transforms second qubit when first is 0,\n", + " // so this leads to (|000> + |010> + |100>) / sqrt(3).\n", + "\n", + " (ControlledOnBitString([false, false], X))([qs[0], qs[1]], qs[2]);\n", + " // Applies NOT gate to third qubit when first two are both 0, leading to the W state desired.\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": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "application/json": "\"Success!\"", + "text/plain": [ + "Success!" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%kata T22_QuantumStrategy_Test \n", + "\n", + "open Microsoft.Quantum.Measurement;\n", + "\n", + "operation QuantumStrategy (input : Bool, qubit : Qubit) : Bool {\n", + " if (input) {\n", + " let q = MResetX(qubit);\n", + " return (q == One);\n", + " }\n", + " else {\n", + " let q = MResetZ(qubit);\n", + " return (q == One);\n", + " }\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Task 2.3. Play the W 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)." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "application/json": "\"Success!\"", + "text/plain": [ + "Success!" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%kata T23_PlayQuantumW_Test \n", + "\n", + "operation PlayQuantumW (strategies : (Qubit => Bool)[]) : Bool[] {\n", + " using (qs = Qubit[3]) {\n", + " CreateEntangledTriple(qs);\n", + " \n", + " let a = strategies[0](qs[0]);\n", + " let b = strategies[1](qs[1]);\n", + " let c = strategies[2](qs[2]);\n", + "\n", + " return [a, b, c];\n", + " }\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Appraisal of quantum strategy\n", + "\n", + "Recall the formula for the win condition:\n", + "\n", + "* The sum of the answer bits must be 1 if the question bits are (0,0,0)\n", + "* The sum of the answer bits must be 0, 2, or 3 (i.e. unequal to 1) if the question bits are (0,1,1), (1,0,1) or (1,1,0).\n", + "\n", + "The standard phase vector for a three-qubit system is:\n", + "\n", + "$\n", + "\\begin{bmatrix}\n", + "\\psi_{000}\\\\\n", + "\\psi_{001}\\\\\n", + "\\psi_{010}\\\\\n", + "\\psi_{011}\\\\\n", + "\\psi_{100}\\\\\n", + "\\psi_{101}\\\\\n", + "\\psi_{110}\\\\\n", + "\\psi_{111}\n", + "\\end{bmatrix}\n", + "$\n", + "\n", + "For the initial W state, this vector has the following values:\n", + "\n", + "$\n", + "\\begin{bmatrix}\n", + "0\\\\\n", + "1/\\sqrt{3}\\\\\n", + "1/\\sqrt{3}\\\\\n", + "0\\\\\n", + "1/\\sqrt{3}\\\\\n", + "0\\\\\n", + "0\\\\\n", + "0\n", + "\\end{bmatrix}\n", + "$\n", + "\n", + "#### Zero input bits true\n", + "\n", + "When the input bits are all false, all three players simply take a Z-basis measurement of their qubit and output the result. One of the qubits will collapse to true and the others to false, so the win condition is met.\n", + "\n", + "#### Two input bits true\n", + "\n", + "When two input bits are true, the players holding the true bits will measure their qubit in the X basis, while the third measures in the Z. This operation is equivalent to the two players holding true input bits performing a Hadamard transform on their qubit and then measuring in the Z. Consider the set of input bits (0,1,1), for which Bob and Charlie perform a Hadamard transform on their qubits. The relevant $8 \\times 8$ operator matrix is:\n", + "\n", + "$\n", + "I \\otimes H \\otimes H =\n", + "\\begin{bmatrix}\n", + "1/2 & 1/2 & 1/2 & 1/2 & 0 & 0 & 0 & 0\\\\\n", + "1/2 & -1/2 & 1/2 & -1/2 & 0 & 0 & 0 & 0\\\\\n", + "1/2 & 1/2 & -1/2 & -1/2 & 0 & 0 & 0 & 0\\\\\n", + "1/2 & -1/2 & -1/2 & 1/2 & 0 & 0 & 0 & 0\\\\\n", + "0 & 0 & 0 & 0 & 1/2 & 1/2 & 1/2 & 1/2\\\\\n", + "0 & 0 & 0 & 0 & 1/2 & -1/2 & 1/2 & -1/2\\\\\n", + "0 & 0 & 0 & 0 & 1/2 & 1/2 & -1/2 & -1/2\\\\\n", + "0 & 0 & 0 & 0 & 1/2 & -1/2 & -1/2 & 1/2\n", + "\\end{bmatrix}\n", + "$\n", + "\n", + "Operating on the W state with this matrix leads to:\n", + "\n", + "$\n", + "\\begin{bmatrix}\n", + "1/\\sqrt{3}\\\\\n", + "0\\\\\n", + "0\\\\\n", + "-1/\\sqrt{3}\\\\\n", + "1/\\sqrt{12}\\\\\n", + "1/\\sqrt{12}\\\\\n", + "1/\\sqrt{12}\\\\\n", + "1/\\sqrt{12}\n", + "\\end{bmatrix}\n", + "$\n", + "\n", + "The players measure this entangled state in the Z basis, with the following outcomes possible:\n", + "\n", + "```\n", + "+-----------------------+--------------------------------+\n", + "| No. of |1> Qubits | Probability of Observation |\n", + "+-----------------------+--------------------------------+\n", + "| 0 | 1/3 |\n", + "| 1 | 1/12 |\n", + "| 2 | 1/2 |\n", + "| 3 | 1/12 |\n", + "+-----------------------+--------------------------------+\n", + "```\n", + "\n", + "The team only loses in the event of one qubit measured at $|1\\rangle$ and the others at $|0\\rangle$. This occurs $1/12$ of the time, so the team's total winning probability is $1/4 + (3/4)(11/12) = 1/4 + 11/16 = 15/16$, or a 93.75% chance. This is a substantial improvement on the 86.11% chance offered by the optimum symmetrical classical strategy in Task 1.4 above." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Q#", + "language": "qsharp", + "name": "iqsharp" + }, + "language_info": { + "file_extension": ".qs", + "mimetype": "text/x-qsharp", + "name": "qsharp", + "version": "0.12" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}