Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

new folder for W game #444

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 27 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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}"
}
]
}
42 changes: 42 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
2 changes: 1 addition & 1 deletion GHZGame/GHZGame.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@
"file_extension": ".qs",
"mimetype": "text/x-qsharp",
"name": "qsharp",
"version": "0.4"
"version": "0.12"
}
},
"nbformat": 4,
Expand Down
12 changes: 12 additions & 0 deletions WGame/README.md
Original file line number Diff line number Diff line change
@@ -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.
115 changes: 115 additions & 0 deletions WGame/ReferenceImplementation.qs
Original file line number Diff line number Diff line change
@@ -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];
}
}

}
165 changes: 165 additions & 0 deletions WGame/Tasks.qs
Original file line number Diff line number Diff line change
@@ -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";
}
}
Loading