Uh oh!
There was an error while loading. Please reload this page.
forked from microsoft/Quantum
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
197 lines (170 loc) · 7.29 KB
/
Copy pathProgram.cs
File metadata and controls
197 lines (170 loc) · 7.29 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
usingSystem;
usingSystem.IO;
usingSystem.Net;
usingSystem.Net.Sockets;
usingSystem.Text;
usingSystem.Threading;
usingSystem.Collections.Generic;
usingNewtonsoft.Json;
usingMicrosoft.Quantum.Simulation.Simulators;
usingMicrosoft.Quantum.Simulation.Core;
usingSystem.Runtime.InteropServices;
usingSystem.Linq;
namespaceMicrosoft.Quantum.Samples.H2Simulation
{
classServerThread
{
privatestaticreadonlydouble[]theoryData={
0.14421,-0.323939,-0.612975,-0.80051,-0.92526,
-1.00901,-1.06539,-1.10233,-1.12559,-1.13894,
-1.14496,-1.1456,-1.14268,-1.13663,-1.12856,
-1.1193,-1.10892,-1.09802,-1.08684,-1.07537,
-1.06424,-1.05344,-1.043,-1.03293,-1.02358,
-1.01482,-1.00665,-0.999025,-0.992226,-0.985805,
-0.980147,-0.975156,-0.970807,-0.966831,-0.963298,
-0.960356,-0.957615,-0.95529,-0.953451,-0.951604,
-0.950183,-0.949016,-0.947872,-0.946982,-0.946219,
-0.945464,-0.944887,-0.944566,-0.94415,-0.943861,
-0.943664,-0.943238,-0.943172,-0.942973
};
privatestaticbyte[]SerializeResponse(stringrespType,objectresponse)=>
System.Text.Encoding.UTF8.GetBytes(
JsonConvert.SerializeObject(
newDictionary<string,object>
{
{"type",respType},
{"data",response}
}
)+"\f"
);
privatestaticvoidSendPlotPoints(NetworkStreamstream)
{
using(varqsim=newQuantumSimulator())
{
// To call a Q# operation that takes unit `()` as its input, we need to grab
// the QVoid.Instance value.
varbondLengths=H2BondLengths.Run(qsim).Result;
// In Q#, we defined the operation that performs the actual estimation; since the Q# operation
// has type (idxBondLength : Int, nBitsPrecision : Int, trotterStepSize : Double) => (Double),
// we pass the index along with that we want six bits of precision and
// step size of 1.
//
// The result of calling H2EstimateEnergyRPE is a Double, so we can minimize over
// that to deal with the possibility that we accidently entered into the excited
// state instead of the ground state of interest.
Func<int,Double>estAtBondLength=(idx)=>Enumerable.Min(
fromidxRepinEnumerable.Range(0,3)
selectH2EstimateEnergyRPE.Run(qsim,idx,6,1.0).Result
);
// We are now equipped to run the Q# simulation at each bond length
// and print the answers out to the console.
foreach(varidxBondinEnumerable.Range(0,54))
{
System.Console.WriteLine($"Estimating at bond length {bondLengths[idxBond]}:");
varest=estAtBondLength(idxBond);
varresponse=SerializeResponse(
"plotPoint",
newDictionary<string,object>
{
{"bondLength",bondLengths[idxBond]},
{"theoreticalEnergy",theoryData[idxBond]},
{"estEnergy",est}
}
);
stream.Write(response,0,response.Length);
}
}
}
publicstaticvoidStart()
{
TcpListenerserver=null;
try
{
varport=8001;
varlocalAddress=IPAddress.Parse("127.0.0.1");
server=newTcpListener(localAddress,port);
server.Start();
while(true)
{
varclient=server.AcceptTcpClient();
Console.WriteLine("@@ Connected to client. @@");
// Allocate a buffer.
varbuffer=newByte[256];
varstream=client.GetStream();
varnBytesRead=-1;
while((nBytesRead=stream.Read(buffer,0,buffer.Length))!=0)
{
varrawMessage=System.Text.Encoding.UTF8.GetString(buffer,0,nBytesRead);
Console.WriteLine($"@@ Received from client: {rawMessage} @@");
varmessage=JsonConvert.DeserializeObject<Dictionary<string,object>>(rawMessage);
message.TryGetValue("type",outvarmessageType);
message.TryGetValue("data",outvarmessageData);
if((string)messageType=="event"&&(string)messageData=="readyToPlot")
{
Console.WriteLine("@@ Got request for plotting data, running simulator. @@");
SendPlotPoints(stream);
}
}
}
}
finally
{
server.Stop();
}
}
}
classProgram
{
staticstringFindOnPath(stringfileName)
{
foreach(varcandidateRootin(
Environment.GetEnvironmentVariable("PATH").Split(
Path.PathSeparator
))
)
{
varpath=Path.Combine(candidateRoot.Trim(),fileName);
if(File.Exists(path))
{
returnpath;
}
}
thrownewFileNotFoundException($"Did not find {fileName} on $Env:PATH.");
}
staticvoidMain(string[]args)
{
Console.WriteLine("Starting Simulation Server...");
varserverThread=newThread(ServerThread.Start);
serverThread.Start();
Console.WriteLine("Starting GUI...");
varprocess=newSystem.Diagnostics.Process
{
StartInfo=newSystem.Diagnostics.ProcessStartInfo
{
FileName=FindOnPath(
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
?"npm.cmd":"npm"
),
UseShellExecute=false,
Arguments="start",
CreateNoWindow=true
}
};
process.Start();
process.WaitForExit();
// Check the npm process' exit code to make sure that npm
// start actually ran correctly.
while(!process.HasExited){
System.Threading.Thread.Sleep(500);
}
if(process.ExitCode!=0){
System.Console.WriteLine($"GUI returned exit code {process.ExitCode}; did you run npm install?");
}
// If we got this far, go on and call Environment's exit method,
// killing the server thread.
Environment.Exit(process.ExitCode);
}
}
}