forked from microsoft/Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHost.cs
91 lines (83 loc) · 3.2 KB
/
Host.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Samples.CHSHGame
{
using System;
using Microsoft.Quantum.Simulation.Simulators;
/// <summary>
/// The classical driver for the quantum computation.
/// </summary>
public class Driver
{
/// <summary>
/// Main entry point to the program.
/// </summary>
/// <param name="args">Command-line parameters.</param>
static void Main(string[] args)
{
const int trialCount = 10000;
var generator = new Random();
using (QuantumSimulator sim = new QuantumSimulator())
{
var classicalWinCount = 0;
var quantumWinCount = 0;
for (int i = 0; i < trialCount; i++)
{
bool aliceBit = GetRandomBit(generator);
bool bobBit = GetRandomBit(generator);
bool aliceMeasuresFirst = GetRandomBit(generator);
bool classicalXor =
!PlayClassicalStrategy(aliceBit, bobBit);
bool quantumXor =
!PlayQuantumStrategy.Run(
sim,
aliceBit,
bobBit,
aliceMeasuresFirst).Result;
if ((aliceBit && bobBit) == classicalXor)
{
classicalWinCount++;
}
if ((aliceBit && bobBit) == quantumXor)
{
quantumWinCount++;
}
}
Console.WriteLine(
"Classical success rate: "
+ classicalWinCount / (float)trialCount);
Console.WriteLine(
"Quantum success rate: "
+ quantumWinCount / (float)trialCount);
if (quantumWinCount > classicalWinCount)
{
Console.WriteLine("The quantum success rate exceeded the classical success rate!");
}
}
}
/// <summary>
/// Generates a single random bit.
/// </summary>
/// <param name="generator">An initialized RNG.</param>
/// <returns>A single random bit, as a bool.</returns>
private static bool GetRandomBit(Random generator)
{
int next = generator.Next();
return (next & 1) == 1;
}
/// <summary>
/// Plays the optimal classical strategy for the CHSH game:
/// both Alice and Bob both always output 0. This should result
/// in success 75% of the time.
/// </summary>
/// <param name="aliceBit">The bit given to Alice (X).</param>
/// <param name="bobBit">The bit given to Bob (Y).</param>
/// <returns>Whether Alice and Bob's output bits match.</returns>
private static bool PlayClassicalStrategy(bool aliceBit, bool bobBit)
{
bool aliceOutput = false;
bool bobOutput = false;
return aliceOutput == bobOutput;
}
}
}