-
Notifications
You must be signed in to change notification settings - Fork 5
/
OpenQasmDriver.cs
314 lines (289 loc) · 9.85 KB
/
OpenQasmDriver.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.Quantum.Primitive;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.Simulators;
using Microsoft.Quantum.Simulation.Common;
namespace Qiskit
{
/// <summary>
/// Quick and dirty Simulatorbase to write OpenQASM 2.0
/// Just enough to show that it would work
/// Please don't put this in production until its fully engineerd.
/// This code could eat your cat. So imagine what schrodinger has to say about that one.
/// </summary>
public abstract class OpenQasmDriver : SimulatorBase
{
public OpenQasmDriver(IQubitManager qubitManager = null) : base(qubitManager)
{
QasmLog.AppendLine("include \"qelib1.inc\";");
QasmLog.AppendLine($"qreg q[{QBitCount}];");
QasmLog.AppendLine($"creg c[{QBitCount}];");
}
protected abstract IEnumerable<Result> RunOpenQasm(StringBuilder qasm, int runs);
public abstract int QBitCount { get; }
/// <summary>
/// Processes Hadamard gate
/// </summary>
public class QSimH : H
{
public QSimH(IOperationFactory m) : base(m)
{
}
public override Func<Qubit, QVoid> Body
{
get
{
return delegate (Qubit q)
{
if (q == null)
{
return QVoid.Instance;
}
var driver = (Factory as OpenQasmDriver);
driver.QasmLog.AppendLine($"h q[{q.Id}];");
return QVoid.Instance;
};
}
}
}
/// <summary>
/// Ingnored becuase this machine doesn't have it. And we recycle qubits anyway
/// </summary>
public class QSimReset : Reset
{
public QSimReset(IOperationFactory m) : base(m)
{
}
public override Func<Qubit, QVoid> Body
{
get
{
return delegate (Qubit q)
{
return QVoid.Instance;
};
}
}
}
/// <summary>
/// Processes Measurement gate
/// Every measurement will currently trigger a schedule of a job
/// </summary>
public class QSimM : M
{
public QSimM(IOperationFactory m) : base(m)
{
}
public override Func<Qubit, Result> Body
{
get
{
return delegate (Qubit q)
{
if (q == null)
{
return Result.Zero;
}
var driver = (Factory as OpenQasmDriver);
driver.QasmLog.AppendLine($"measure q[{(uint)q.Id}] -> c[{(uint)q.Id}];");
var result = driver.RunOpenQasm(driver.QasmLog,1).ToArray();
return result[q.Id];
};
}
}
}
/// <summary>
/// Processes Pauli-X gate
/// </summary>
public class QSimX : X
{
public QSimX(IOperationFactory m) : base(m)
{
}
public override Func<Qubit, QVoid> Body
{
get
{
return delegate (Qubit q)
{
if (q == null)
{
return QVoid.Instance;
}
var driver = (Factory as OpenQasmDriver);
driver.QasmLog.AppendLine($"x q[{q.Id}];");
return QVoid.Instance;
};
}
}
}
/// <summary>
/// Processes Pauli-Y gate
/// </summary>
public class QSimY : Y
{
public QSimY(IOperationFactory m) : base(m)
{
}
public override Func<Qubit, QVoid> Body
{
get
{
return delegate (Qubit q)
{
if (q == null)
{
return QVoid.Instance;
}
var driver = (Factory as OpenQasmDriver);
driver.QasmLog.AppendLine($"y q[{q.Id}];");
return QVoid.Instance;
};
}
}
}
/// <summary>
/// Processes Pauli-Y gate
/// </summary>
public class QSimZ : Z
{
public QSimZ(IOperationFactory m) : base(m)
{
}
public override Func<Qubit, QVoid> Body
{
get
{
return delegate (Qubit q)
{
if (q == null)
{
return QVoid.Instance;
}
var driver = (Factory as OpenQasmDriver);
driver.QasmLog.AppendLine($"z q[{q.Id}];");
return QVoid.Instance;
};
}
}
}
/// <summary>
/// Ignoring Asserts, because OpenQasm doesn't have them and don't add behavior unless hit.
/// </summary>
public class QSimAssert : Assert
{
public QSimAssert(IOperationFactory m) : base(m)
{
}
public override Func<(QArray<Pauli>, QArray<Qubit>, Result, string), QVoid> Body
{
get {
return delegate ((QArray<Pauli>, QArray<Qubit>, Result, string) assert)
{
return QVoid.Instance;
};
}
}
}
/// <summary>
/// Ignoring AssertProbes, because OpenQasm doesn't have them and don't add behavior unless hit.
/// </summary>
public class QSimAssertProb : AssertProb
{
public QSimAssertProb(IOperationFactory m) : base(m)
{
}
public override Func<(QArray<Pauli>, QArray<Qubit>, Result, double, string, double), QVoid> Body
{
get
{
return delegate ((QArray<Pauli>, QArray<Qubit>, Result, double, string, double) assert)
{
return QVoid.Instance;
};
}
}
}
/// <summary>
/// Processes CNOT gate
/// </summary>
public class QSimCNOT : CNOT
{
public QSimCNOT(IOperationFactory m) : base(m)
{
}
public override Func<(Qubit,Qubit), QVoid> Body
{
get
{
return delegate ((Qubit, Qubit) cnot)
{
if (cnot.Item1 == null || cnot.Item2 == null)
{
return QVoid.Instance;
}
var driver = (Factory as OpenQasmDriver);
driver.QasmLog.AppendLine($"cx q[{cnot.Item1.Id}],q[{cnot.Item2.Id}];");
return QVoid.Instance;
};
}
}
}
/// <summary>
/// Process R Gate
/// </summary>
public class QSimR : R
{
public QSimR(IOperationFactory m) : base(m)
{
}
public override Func<(Pauli, double, Qubit), QVoid> Body
{
get
{
return delegate ((Pauli, double, Qubit) rGate)
{
if (rGate.Item3 == null)
{
return QVoid.Instance;
}
var driver = (Factory as OpenQasmDriver);
switch (rGate.Item1)
{
case Pauli.PauliI:
driver.QasmLog.AppendLine($"U({rGate.Item2},{rGate.Item2},{rGate.Item2}) q[{rGate.Item3.Id}];");
break;
case Pauli.PauliX:
driver.QasmLog.AppendLine($"rx({rGate.Item2}) q[{rGate.Item3.Id}];");
break;
case Pauli.PauliY:
driver.QasmLog.AppendLine($"ry({rGate.Item2}) q[{rGate.Item3.Id}];");
break;
case Pauli.PauliZ:
driver.QasmLog.AppendLine($"rz({rGate.Item2}) q[{rGate.Item3.Id}];");
break;
}
return QVoid.Instance;
};
}
}
}
//Log of the current to be executing Qasm
public readonly StringBuilder QasmLog = new StringBuilder();
/// <summary>
/// Implemented as stub, to work around current reflection bug of simulator base
/// </summary>
public class QSimMeasure : Measure
{
public QSimMeasure(IOperationFactory m) : base(m)
{
}
public override Func<(QArray<Pauli>, QArray<Qubit>), Result> Body => throw new NotImplementedException();
}
}
}