This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Based on https://github.com/Microsoft/Quantum/tree/master/Samples/src/CHSHGame Co-authored-by: Justin Sievers <justinmsievers@gmail.com> Co-authored-by: Johnny Wang <aptx.hdc@gmail.com>
- Loading branch information
1 parent
72451ed
commit 9128297
Showing
9 changed files
with
548 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
// See http://go.microsoft.com/fwlink/?LinkId=827846 | ||
// for the documentation about the extensions.json format | ||
"recommendations": [ | ||
"quantum.quantum-devkit-vscode" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"label": "build", | ||
"command": "dotnet", | ||
"args": [ | ||
"build" | ||
], | ||
"type": "process", | ||
"group": "build", | ||
"presentation": { | ||
"reveal": "silent" | ||
}, | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "test", | ||
"command": "dotnet", | ||
"args": [ | ||
"test" | ||
], | ||
"type": "process", | ||
"group": "test", | ||
"presentation": { | ||
"echo": true, | ||
"reveal": "always", | ||
"focus": false, | ||
"panel": "shared" | ||
}, | ||
"problemMatcher": "$msCompile" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
<PlatformTarget>x64</PlatformTarget> | ||
<IsPackable>false</IsPackable> | ||
<RootNamespace>Quantum.Kata.CHSHGame</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Quantum.Canon" Version="0.5.1903.2703" /> | ||
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.5.1903.2703" /> | ||
<PackageReference Include="Microsoft.Quantum.Xunit" Version="0.5.1903.2703" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" /> | ||
<PackageReference Include="xunit" Version="2.3.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" /> | ||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="README.md" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}") = "CHSHGame", "CHSHGame.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Welcome! | ||
|
||
This kata covers the CHSH game, one of the most famous examples of a nonlocal | ||
(entanglement) game. | ||
|
||
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. | ||
|
||
#### Theory | ||
|
||
* [Lecture 20](https://cs.uwaterloo.ca/~watrous/CPSC519/LectureNotes/20.pdf) by | ||
John Watrous. | ||
|
||
#### Q# Materials | ||
|
||
* Q# Samples repository has [an implementation of the CHSH | ||
game](https://github.com/Microsoft/Quantum/tree/master/Samples/src/CHSHGame) | ||
that includes an explanation of the history and theory behind the game. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// 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.CHSHGame { | ||
|
||
open Microsoft.Quantum.Canon; | ||
open Microsoft.Quantum.Extensions.Math; | ||
open Microsoft.Quantum.Extensions.Convert; | ||
open Microsoft.Quantum.Primitive; | ||
|
||
|
||
|
||
////////////////////////////////////////////////////////////////// | ||
// Part I. Classical CHSH | ||
////////////////////////////////////////////////////////////////// | ||
|
||
// Task 1.1. Win condition | ||
function WinCondition_Reference (x : Bool, y : Bool, a : Bool, b : Bool) : Bool { | ||
return (x and y) == (a != b); | ||
} | ||
|
||
|
||
// Task 1.2. Alice and Bob's classical strategy | ||
// (Both players should return the same value, regardless of input) | ||
function AliceClassical_Reference (x : Bool) : Bool { | ||
return false; | ||
} | ||
|
||
function BobClassical_Reference (y : Bool) : Bool { | ||
return false; | ||
} | ||
|
||
|
||
////////////////////////////////////////////////////////////////// | ||
// Part II. Quantum CHSH | ||
////////////////////////////////////////////////////////////////// | ||
|
||
// Task 2.1. Entangled pair | ||
operation CreateEntangledPair_Reference (qs : Qubit[]) : Unit { | ||
body (...) { | ||
H(qs[0]); | ||
CNOT(qs[0], qs[1]); | ||
} | ||
|
||
adjoint invert; | ||
} | ||
|
||
|
||
// Task 2.2. Alice's quantum strategy | ||
operation AliceQuantum_Reference (bit : Bool, qubit : Qubit) : Bool { | ||
if (bit) { | ||
// Measure in sign basis if bit is 1 | ||
return BoolFromResult(Measure([PauliX], [qubit])); | ||
} else { | ||
// Measure in computational basis if bit is 0 | ||
return BoolFromResult(Measure([PauliZ], [qubit])); | ||
} | ||
} | ||
|
||
|
||
// Task 2.3. Rotate Bob's qubit | ||
operation RotateBobQubit_Reference (clockwise : Bool, qubit : Qubit) : Unit { | ||
if (clockwise) { | ||
Ry(-2.0 * PI() / 8.0, qubit); | ||
} else { | ||
Ry(2.0 * PI() / 8.0, qubit); | ||
} | ||
} | ||
|
||
|
||
// Task 2.4. Bob's quantum strategy | ||
operation BobQuantum_Reference (bit : Bool, qubit : Qubit) : Bool { | ||
RotateBobQubit_Reference(not bit, qubit); | ||
return BoolFromResult(M(qubit)); | ||
} | ||
|
||
|
||
// Task 2.5. Play the CHSH game | ||
operation PlayQuantumCHSH_Reference (askAlice : (Qubit => Bool), | ||
askBob : (Qubit => Bool)) : (Bool, Bool) { | ||
mutable aliceResult = false; | ||
mutable bobResult = false; | ||
|
||
using ((aliceQubit, bobQubit) = (Qubit(), Qubit())) { | ||
CreateEntangledPair_Reference([aliceQubit, bobQubit]); | ||
|
||
set aliceResult = askAlice(aliceQubit); | ||
set bobResult = askBob(bobQubit); | ||
|
||
Reset(aliceQubit); | ||
Reset(bobQubit); | ||
} | ||
|
||
return (aliceResult, bobResult); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
namespace Quantum.Kata.CHSHGame { | ||
|
||
open Microsoft.Quantum.Canon; | ||
open Microsoft.Quantum.Extensions.Math; | ||
open Microsoft.Quantum.Primitive; | ||
|
||
|
||
////////////////////////////////////////////////////////////////// | ||
// Welcome! | ||
////////////////////////////////////////////////////////////////// | ||
|
||
// The "CHSH Game" quantum kata is a series of exercises designed | ||
// to get you familiar with the CHSH game. | ||
// In it two players (Alice and Bob) try to win the following game: | ||
// each of them is given a bit (Alice gets X and Bob gets Y), and | ||
// they have to return new bits (Alice returns A and Bob returns B) | ||
// so that X ∧ Y = A ⊕ B. The trick is, they can not communicate during the game. | ||
|
||
// 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 CHSH | ||
////////////////////////////////////////////////////////////////// | ||
|
||
// Task 1.1. Win condition | ||
// Input: | ||
// 1) Alice and Bob's starting bits (X and Y), | ||
// 2) Alice and Bob's output bits (A and B). | ||
// Output: | ||
// True if Alice and Bob won the CHSH game, that is, if X ∧ Y = A ⊕ B, | ||
// and false otherwise. | ||
function WinCondition (x : Bool, y : Bool, a : Bool, b : Bool) : Bool { | ||
// ... | ||
fail "Task 1.1 not implemented yet"; | ||
} | ||
|
||
|
||
// Task 1.2. Alice and Bob's classical strategy | ||
// In this task you have to implement two functions, one for Alice's classical strategy and one for Bob's. | ||
// Note that they are covered by one test, so you have to implement both before attemping the test. | ||
|
||
// Input: Alice's starting bit (X). | ||
// Output: The bit that Alice should output (A) to maximize their chance of winning. | ||
operation AliceClassical (x : Bool) : Bool { | ||
// ... | ||
fail "Alice's strategy in task 1.2 not implemented yet"; | ||
} | ||
|
||
// Input: Bob's starting bit (Y). | ||
// Output: The bit that Bob should output (B) to maximize their chance of winning. | ||
operation BobClassical (y : Bool) : Bool { | ||
// ... | ||
fail "Bob's strategy in task 1.2 not implemented yet"; | ||
} | ||
|
||
|
||
////////////////////////////////////////////////////////////////// | ||
// Part II. Quantum CHSH | ||
////////////////////////////////////////////////////////////////// | ||
|
||
// In the quantum version of the game, the players still can not | ||
// communicate during the game, but they are allowed to share | ||
// qubits from a Bell pair before the start of the game. | ||
|
||
// Task 2.1. Entangled pair | ||
// Input: An array of two qubits in the |00⟩ state. | ||
// Goal: Create a Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2) on these qubits. | ||
operation CreateEntangledPair (qs : Qubit[]) : Unit { | ||
// The following lines enforce the constraints on the input that you are given. | ||
// You don't need to modify them. Feel free to remove them, this won't cause your code to fail. | ||
AssertIntEqual(Length(qs), 2, "The array should have exactly 2 qubits."); | ||
|
||
// ... | ||
fail "Task 2.1 not implemented yet"; | ||
} | ||
|
||
|
||
// Task 2.2. Alice's quantum strategy | ||
// Inputs: | ||
// 1) Alice's starting bit (X), | ||
// 2) Alice's half of Bell pair they share with Bob. | ||
// Goal: Measure Alice's qubit in the Z basis if her bit is 0 (false), | ||
// or the X basis if her bit is 1 (true), and return the result. | ||
// The state of the qubit after the operation does not matter. | ||
operation AliceQuantum (bit : Bool, qubit : Qubit) : Bool { | ||
// ... | ||
fail "Task 2.2 not implemented yet"; | ||
} | ||
|
||
|
||
// Task 2.3. Rotate Bob's qubit | ||
// Inputs: | ||
// 1) The direction to rotate: true for clockwise, false for counterclockwise, | ||
// 2) Bob's qubit. | ||
// Goal: Rotate the qubit π/8 radians around the Y axis in the given direction. | ||
operation RotateBobQubit (clockwise : Bool, qubit : Qubit) : Unit { | ||
// Hint: Ry operation applies a rotation by a given angle in counterclockwise direction. | ||
// ... | ||
fail "Task 2.3 not implemented yet"; | ||
} | ||
|
||
|
||
// Task 2.4. Bob's quantum strategy | ||
// Inputs: | ||
// 1) Bob's starting bit (Y), | ||
// 2) Bob's half of Bell pair they share with Alice. | ||
// Goal: Measure Bob's qubit in the π/8 basis if his bit is 0 (false), | ||
// or the -π/8 basis if his bit is 1 (true), and return the result. | ||
// The state of the qubit after the operation does not matter. | ||
operation BobQuantum (bit : Bool, qubit : Qubit) : Bool { | ||
// ... | ||
fail "Task 2.4 not implemented yet"; | ||
} | ||
|
||
|
||
// Task 2.5. Play the CHSH game using the quantum strategy | ||
// Input: Operations that return Alice and Bob's output bits (A and B) based on their quantum | ||
// strategies and given their respective qubits from the Bell pair. | ||
// Alice and Bob have already been told what their starting bits X and Y are. | ||
// Goal: Return Alice and Bob's output bits (A, B). | ||
// | ||
// Note that this task uses strategies AliceQuantum and BobQuantum | ||
// which you've implemented in tasks 2.2 and 2.4, respectively. | ||
operation PlayQuantumCHSH (askAlice : (Qubit => Bool), askBob : (Qubit => Bool)) | ||
: (Bool, Bool) { | ||
// ... | ||
fail "Task 2.5 not implemented yet"; | ||
} | ||
|
||
} |
Oops, something went wrong.