-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathrun_qsim.h
More file actions
317 lines (267 loc) · 10.7 KB
/
run_qsim.h
File metadata and controls
317 lines (267 loc) · 10.7 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
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
315
316
317
// Copyright 2019 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef RUN_QSIM_H_
#define RUN_QSIM_H_
#include <random>
#include <string>
#include <vector>
#include "circuit.h"
#include "gate.h"
#include "gate_appl.h"
#include "util.h"
namespace qsim {
/**
* Helper struct for running qsim.
*/
template <typename IO, typename Fuser, typename Factory,
typename RGen = std::mt19937>
struct QSimRunner final {
public:
using Simulator = typename Factory::Simulator;
using StateSpace = typename Simulator::StateSpace;
using State = typename StateSpace::State;
using MeasurementResult = typename StateSpace::MeasurementResult;
/**
* User-specified parameters for gate fusion and simulation.
*/
struct Parameter : public Fuser::Parameter {
/**
* Random number generator seed to apply measurement gates.
*/
uint64_t seed;
};
/**
* Runs the given circuit, only measuring at the end.
* @param param Options for gate fusion, parallelism and logging.
* @param factory Object to create simulators and state spaces.
* @param circuit The circuit to be simulated.
* @param measure Function that performs measurements (in the sense of
* computing expectation values, etc).
* @return True if the simulation completed successfully; false otherwise.
*/
template <typename Circuit, typename MeasurementFunc>
static bool Run(const Parameter& param, const Factory& factory,
const Circuit& circuit, MeasurementFunc measure) {
return Run(param, factory, {circuit.gates.back().time}, circuit, measure);
}
/**
* Runs the given circuit, measuring at user-specified times.
* @param param Options for gate fusion, parallelism and logging.
* @param factory Object to create simulators and state spaces.
* @param times_to_measure_at Time steps at which to perform measurements.
* @param circuit The circuit to be simulated.
* @param measure Function that performs measurements (in the sense of
* computing expectation values, etc).
* @return True if the simulation completed successfully; false otherwise.
*/
template <typename Circuit, typename MeasurementFunc>
static bool Run(const Parameter& param, const Factory& factory,
const std::vector<unsigned>& times_to_measure_at,
const Circuit& circuit, MeasurementFunc measure) {
double t0 = 0.0;
double t1 = 0.0;
if (param.verbosity > 1) {
t0 = GetTime();
}
RGen rgen(param.seed);
StateSpace state_space = factory.CreateStateSpace();
auto state = state_space.Create(circuit.num_qubits);
if (state_space.IsNull(state)) {
IO::errorf("not enough memory: is the number of qubits too large?\n");
return false;
}
state_space.SetStateZero(state);
Simulator simulator = factory.CreateSimulator();
if (param.verbosity > 1) {
t1 = GetTime();
IO::messagef("init time is %g seconds.\n", t1 - t0);
t0 = GetTime();
}
auto fused_gates = Fuser::FuseGates(param, circuit.num_qubits,
circuit.gates, times_to_measure_at);
if (fused_gates.size() == 0 && circuit.gates.size() > 0) {
return false;
}
if (param.verbosity > 1) {
t1 = GetTime();
IO::messagef("fuse time is %g seconds.\n", t1 - t0);
}
if (param.verbosity > 0) {
t0 = GetTime();
}
unsigned cur_time_index = 0;
// Apply fused gates.
for (std::size_t i = 0; i < fused_gates.size(); ++i) {
if (param.verbosity > 3) {
t1 = GetTime();
}
if (!ApplyFusedGate(state_space, simulator, fused_gates[i], rgen,
state)) {
IO::errorf("measurement failed.\n");
return false;
}
if (param.verbosity > 3) {
state_space.DeviceSync();
double t2 = GetTime();
IO::messagef("gate %lu done in %g seconds.\n", i, t2 - t1);
}
unsigned t = times_to_measure_at[cur_time_index];
if (i == fused_gates.size() - 1 || t < fused_gates[i + 1].time) {
// Call back to perform measurements.
measure(cur_time_index, state_space, state);
++cur_time_index;
}
}
if (param.verbosity > 0) {
state_space.DeviceSync();
double t2 = GetTime();
IO::messagef("time is %g seconds.\n", t2 - t0);
}
return true;
}
/**
* Runs the given circuit and make the final state available to the caller,
* recording the result of any intermediate measurements in the circuit.
* @param param Options for gate fusion, parallelism and logging.
* @param factory Object to create simulators and state spaces.
* @param circuit The circuit to be simulated.
* @param state As an input parameter, this should contain the initial state
* of the system. After a successful run, it will be populated with the
* final state of the system.
* @param measure_results As an input parameter, this should be empty.
* After a successful run, this will contain all measurements results from
* the run, ordered by time and qubit index.
* @return True if the simulation completed successfully; false otherwise.
*/
template <typename Circuit>
static bool Run(const Parameter& param, const Factory& factory,
const Circuit& circuit, State& state,
std::vector<MeasurementResult>& measure_results) {
StateSpace state_space = factory.CreateStateSpace();
Simulator simulator = factory.CreateSimulator();
return Run(param, circuit, state_space, simulator, state, measure_results);
}
/**
* Runs the given circuit and make the final state available to the caller,
* discarding the result of any intermediate measurements in the circuit.
* @param param Options for gate fusion, parallelism and logging.
* @param factory Object to create simulators and state spaces.
* @param circuit The circuit to be simulated.
* @param state As an input parameter, this should contain the initial state
* of the system. After a successful run, it will be populated with the
* final state of the system.
* @return True if the simulation completed successfully; false otherwise.
*/
template <typename Circuit>
static bool Run(const Parameter& param, const Factory& factory,
const Circuit& circuit, State& state) {
StateSpace state_space = factory.CreateStateSpace();
Simulator simulator = factory.CreateSimulator();
std::vector<MeasurementResult> discarded_results;
return Run(
param, circuit, state_space, simulator, state, discarded_results);
}
/**
* Runs the given circuit and make the final state available to the caller,
* recording the result of any intermediate measurements in the circuit.
* @param param Options for gate fusion, parallelism and logging.
* @param circuit The circuit to be simulated.
* @param state_space StateSpace object required to perform measurements.
* @param simulator Simulator object. Provides specific implementations for
* applying gates.
* @param state As an input parameter, this should contain the initial state
* of the system. After a successful run, it will be populated with the
* final state of the system.
* @param measure_results As an input parameter, this should be empty.
* After a successful run, this will contain all measurements results from
* the run, ordered by time and qubit index.
* @return True if the simulation completed successfully; false otherwise.
*/
template <typename Circuit>
static bool Run(const Parameter& param, const Circuit& circuit,
const StateSpace& state_space, const Simulator& simulator,
State& state,
std::vector<MeasurementResult>& measure_results) {
double t0 = 0.0;
double t1 = 0.0;
if (param.verbosity > 1) {
t0 = GetTime();
}
RGen rgen(param.seed);
if (param.verbosity > 1) {
t1 = GetTime();
IO::messagef("init time is %g seconds.\n", t1 - t0);
t0 = GetTime();
}
using Gates = detail::Gates<Circuit>;
const auto& gates = Gates::get(circuit);
auto fused_gates = Fuser::FuseGates(param, state.num_qubits(), gates);
if (fused_gates.size() == 0 && gates.size() > 0) {
return false;
}
measure_results.reserve(fused_gates.size());
if (param.verbosity > 1) {
t1 = GetTime();
IO::messagef("fuse time is %g seconds.\n", t1 - t0);
}
if (param.verbosity > 0) {
t0 = GetTime();
}
// Apply fused gates.
for (std::size_t i = 0; i < fused_gates.size(); ++i) {
if (param.verbosity > 3) {
t1 = GetTime();
}
if (!ApplyFusedGate(state_space, simulator, fused_gates[i], rgen, state,
measure_results)) {
IO::errorf("measurement failed.\n");
return false;
}
if (param.verbosity > 3) {
state_space.DeviceSync();
double t2 = GetTime();
IO::messagef("gate %lu done in %g seconds.\n", i, t2 - t1);
}
}
if (param.verbosity > 0) {
state_space.DeviceSync();
double t2 = GetTime();
IO::messagef("simu time is %g seconds.\n", t2 - t0);
}
return true;
}
/**
* Runs the given circuit and make the final state available to the caller,
* discarding the result of any intermediate measurements in the circuit.
* @param param Options for gate fusion, parallelism and logging.
* @param circuit The circuit to be simulated.
* @param state_space StateSpace object required to perform measurements.
* @param simulator Simulator object. Provides specific implementations for
* applying gates.
* @param state As an input parameter, this should contain the initial state
* of the system. After a successful run, it will be populated with the
* final state of the system.
* @return True if the simulation completed successfully; false otherwise.
*/
template <typename Circuit>
static bool Run(const Parameter& param, const Circuit& circuit,
const StateSpace& state_space, const Simulator& simulator,
State& state) {
std::vector<MeasurementResult> discarded_results;
return Run(
param, circuit, state_space, simulator, state, discarded_results);
}
};
} // namespace qsim
#endif // RUN_QSIM_H_