-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathLightningSimulator.cpp
526 lines (429 loc) · 20.4 KB
/
LightningSimulator.cpp
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Copyright 2022-2023 Xanadu Quantum Technologies Inc.
// 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
// http://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.
#include "LightningSimulator.hpp"
#include "AdjointJacobianLQubit.hpp"
#include "JacobianData.hpp"
#include "LinearAlgebra.hpp"
#include "MeasurementsLQubit.hpp"
namespace Catalyst::Runtime::Simulator {
auto LightningSimulator::AllocateQubit() -> QubitIdType
{
size_t sv_id = this->device_sv->allocateWire();
return this->qubit_manager.Allocate(sv_id);
}
auto LightningSimulator::AllocateQubits(size_t num_qubits) -> std::vector<QubitIdType>
{
if (!num_qubits) {
return {};
}
// at the first call when num_qubits == 0
if (!this->GetNumQubits()) {
this->device_sv = std::make_unique<StateVectorT>(num_qubits);
return this->qubit_manager.AllocateRange(0, num_qubits);
}
std::vector<QubitIdType> result(num_qubits);
std::generate_n(result.begin(), num_qubits, [this]() { return AllocateQubit(); });
return result;
}
void LightningSimulator::ReleaseAllQubits()
{
this->device_sv->clearData();
this->qubit_manager.ReleaseAll();
}
void LightningSimulator::ReleaseQubit(QubitIdType q)
{
if (this->qubit_manager.isValidQubitId(q)) {
this->device_sv->releaseWire(this->qubit_manager.getDeviceId(q));
}
this->qubit_manager.Release(q);
}
auto LightningSimulator::GetNumQubits() const -> size_t { return this->device_sv->getNumQubits(); }
void LightningSimulator::StartTapeRecording()
{
RT_FAIL_IF(this->tape_recording, "Cannot re-activate the cache manager");
this->tape_recording = true;
this->cache_manager.Reset();
}
void LightningSimulator::StopTapeRecording()
{
RT_FAIL_IF(!this->tape_recording, "Cannot stop an already stopped cache manager");
this->tape_recording = false;
}
auto LightningSimulator::CacheManagerInfo()
-> std::tuple<size_t, size_t, size_t, std::vector<std::string>, std::vector<ObsIdType>>
{
return {this->cache_manager.getNumOperations(), this->cache_manager.getNumObservables(),
this->cache_manager.getNumParams(), this->cache_manager.getOperationsNames(),
this->cache_manager.getObservablesKeys()};
}
void LightningSimulator::SetDeviceShots(size_t shots) { this->device_shots = shots; }
auto LightningSimulator::GetDeviceShots() const -> size_t { return this->device_shots; }
void LightningSimulator::SetDevicePRNG(std::mt19937 *gen) { this->gen = gen; }
void LightningSimulator::PrintState()
{
using std::cout;
using std::endl;
const size_t num_qubits = this->device_sv->getNumQubits();
const size_t size = Pennylane::Util::exp2(num_qubits);
size_t idx = 0;
cout << "*** State-Vector of Size " << size << " ***" << endl;
cout << "[";
auto &&state = this->device_sv->getDataVector();
for (; idx < size - 1; idx++) {
cout << state[idx] << ", ";
}
cout << state[idx] << "]" << endl;
}
auto LightningSimulator::Zero() const -> Result
{
return const_cast<Result>(&GLOBAL_RESULT_FALSE_CONST);
}
auto LightningSimulator::One() const -> Result
{
return const_cast<Result>(&GLOBAL_RESULT_TRUE_CONST);
}
void LightningSimulator::NamedOperation(const std::string &name, const std::vector<double> ¶ms,
const std::vector<QubitIdType> &wires, bool inverse,
const std::vector<QubitIdType> &controlled_wires,
const std::vector<bool> &controlled_values)
{
// Check the validity of number of qubits and parameters
RT_FAIL_IF(controlled_wires.size() != controlled_values.size(),
"Controlled wires/values size mismatch");
RT_FAIL_IF(!isValidQubits(wires), "Given wires do not refer to qubits");
RT_FAIL_IF(!isValidQubits(controlled_wires), "Given controlled wires do not refer to qubits");
// Convert wires to device wires
auto &&dev_wires = getDeviceWires(wires);
auto &&dev_controlled_wires = getDeviceWires(controlled_wires);
// Update the state-vector
if (controlled_wires.empty()) {
this->device_sv->applyOperation(name, dev_wires, inverse, params);
}
else {
this->device_sv->applyOperation(name, dev_controlled_wires, controlled_values, dev_wires,
inverse, params);
}
// Update tape caching if required
if (this->tape_recording) {
this->cache_manager.addOperation(name, params, dev_wires, inverse, {}, dev_controlled_wires,
controlled_values);
}
}
void LightningSimulator::MatrixOperation(const std::vector<std::complex<double>> &matrix,
const std::vector<QubitIdType> &wires, bool inverse,
const std::vector<QubitIdType> &controlled_wires,
const std::vector<bool> &controlled_values)
{
RT_FAIL_IF(controlled_wires.size() != controlled_values.size(),
"Controlled wires/values size mismatch");
RT_FAIL_IF(!isValidQubits(wires), "Given wires do not refer to qubits");
RT_FAIL_IF(!isValidQubits(controlled_wires), "Given controlled wires do not refer to qubits");
// Convert wires to device wires
// with checking validity of wires
auto &&dev_wires = getDeviceWires(wires);
auto &&dev_controlled_wires = getDeviceWires(controlled_wires);
// Update the state-vector
if (controlled_wires.empty()) {
this->device_sv->applyMatrix(matrix.data(), dev_wires, inverse);
}
else {
this->device_sv->applyControlledMatrix(matrix.data(), dev_controlled_wires,
controlled_values, dev_wires, inverse);
}
// Update tape caching if required
if (this->tape_recording) {
this->cache_manager.addOperation("QubitUnitary", {}, dev_wires, inverse, matrix,
dev_controlled_wires, controlled_values);
}
}
auto LightningSimulator::Observable(ObsId id, const std::vector<std::complex<double>> &matrix,
const std::vector<QubitIdType> &wires) -> ObsIdType
{
RT_FAIL_IF(wires.size() > this->GetNumQubits(), "Invalid number of wires");
RT_FAIL_IF(!isValidQubits(wires), "Invalid given wires");
auto &&dev_wires = getDeviceWires(wires);
if (id == ObsId::Hermitian) {
return this->obs_manager.createHermitianObs(matrix, dev_wires);
}
return this->obs_manager.createNamedObs(id, dev_wires);
}
auto LightningSimulator::TensorObservable(const std::vector<ObsIdType> &obs) -> ObsIdType
{
return this->obs_manager.createTensorProdObs(obs);
}
auto LightningSimulator::HamiltonianObservable(const std::vector<double> &coeffs,
const std::vector<ObsIdType> &obs) -> ObsIdType
{
return this->obs_manager.createHamiltonianObs(coeffs, obs);
}
auto LightningSimulator::Expval(ObsIdType obsKey) -> double
{
RT_FAIL_IF(!this->obs_manager.isValidObservables({obsKey}),
"Invalid key for cached observables");
auto &&obs = this->obs_manager.getObservable(obsKey);
// update tape caching
if (this->tape_recording) {
this->cache_manager.addObservable(obsKey, MeasurementsT::Expval);
}
Pennylane::LightningQubit::Measures::Measurements<StateVectorT> m{*(this->device_sv)};
return device_shots ? m.expval(*obs, device_shots, {}) : m.expval(*obs);
}
auto LightningSimulator::Var(ObsIdType obsKey) -> double
{
RT_FAIL_IF(!this->obs_manager.isValidObservables({obsKey}),
"Invalid key for cached observables");
auto &&obs = this->obs_manager.getObservable(obsKey);
// update tape caching
if (this->tape_recording) {
this->cache_manager.addObservable(obsKey, MeasurementsT::Var);
}
Pennylane::LightningQubit::Measures::Measurements<StateVectorT> m{*(this->device_sv)};
return device_shots ? m.var(*obs, device_shots) : m.var(*obs);
}
void LightningSimulator::State(DataView<std::complex<double>, 1> &state)
{
auto &&dv_state = this->device_sv->getDataVector();
RT_FAIL_IF(state.size() != dv_state.size(), "Invalid size for the pre-allocated state vector");
std::move(dv_state.begin(), dv_state.end(), state.begin());
}
void LightningSimulator::Probs(DataView<double, 1> &probs)
{
Pennylane::LightningQubit::Measures::Measurements<StateVectorT> m{*(this->device_sv)};
auto &&dv_probs = device_shots ? m.probs(device_shots) : m.probs();
RT_FAIL_IF(probs.size() != dv_probs.size(), "Invalid size for the pre-allocated probabilities");
std::move(dv_probs.begin(), dv_probs.end(), probs.begin());
}
void LightningSimulator::PartialProbs(DataView<double, 1> &probs,
const std::vector<QubitIdType> &wires)
{
const size_t numWires = wires.size();
const size_t numQubits = this->GetNumQubits();
RT_FAIL_IF(numWires > numQubits, "Invalid number of wires");
RT_FAIL_IF(!isValidQubits(wires), "Invalid given wires to measure");
auto dev_wires = getDeviceWires(wires);
Pennylane::LightningQubit::Measures::Measurements<StateVectorT> m{*(this->device_sv)};
auto &&dv_probs = device_shots ? m.probs(dev_wires, device_shots) : m.probs(dev_wires);
RT_FAIL_IF(probs.size() != dv_probs.size(),
"Invalid size for the pre-allocated partial-probabilities");
std::move(dv_probs.begin(), dv_probs.end(), probs.begin());
}
std::vector<size_t> LightningSimulator::GenerateSamplesMetropolis(size_t shots)
{
// generate_samples_metropolis is a member function of the Measures class.
Pennylane::LightningQubit::Measures::Measurements<StateVectorT> m{*(this->device_sv)};
// PL-Lightning generates samples using the alias method.
// Reference: https://en.wikipedia.org/wiki/Alias_method
// Given the number of samples, returns 1-D vector of samples
// in binary, each sample is separated by a stride equal to
// the number of qubits.
//
// Return Value Optimization (RVO)
return m.generate_samples_metropolis(this->kernel_name, this->num_burnin, shots);
}
std::vector<size_t> LightningSimulator::GenerateSamples(size_t shots)
{
if (this->mcmc) {
return this->GenerateSamplesMetropolis(shots);
}
// generate_samples is a member function of the Measures class.
Pennylane::LightningQubit::Measures::Measurements<StateVectorT> m{*(this->device_sv)};
// PL-Lightning generates samples using the alias method.
// Reference: https://en.wikipedia.org/wiki/Alias_method
// Given the number of samples, returns 1-D vector of samples
// in binary, each sample is separated by a stride equal to
// the number of qubits.
//
// Return Value Optimization (RVO)
return m.generate_samples(shots);
}
void LightningSimulator::Sample(DataView<double, 2> &samples, size_t shots)
{
auto li_samples = this->GenerateSamples(shots);
RT_FAIL_IF(samples.size() != li_samples.size(), "Invalid size for the pre-allocated samples");
const size_t numQubits = this->GetNumQubits();
// The lightning samples are layed out as a single vector of size
// shots*qubits, where each element represents a single bit. The
// corresponding shape is (shots, qubits). Gather the desired bits
// corresponding to the input wires into a bitstring.
auto samplesIter = samples.begin();
for (size_t shot = 0; shot < shots; shot++) {
for (size_t wire = 0; wire < numQubits; wire++) {
*(samplesIter++) = static_cast<double>(li_samples[shot * numQubits + wire]);
}
}
}
void LightningSimulator::PartialSample(DataView<double, 2> &samples,
const std::vector<QubitIdType> &wires, size_t shots)
{
const size_t numWires = wires.size();
const size_t numQubits = this->GetNumQubits();
RT_FAIL_IF(numWires > numQubits, "Invalid number of wires");
RT_FAIL_IF(!isValidQubits(wires), "Invalid given wires to measure");
RT_FAIL_IF(samples.size() != shots * numWires,
"Invalid size for the pre-allocated partial-samples");
// get device wires
auto &&dev_wires = getDeviceWires(wires);
auto li_samples = this->GenerateSamples(shots);
// The lightning samples are layed out as a single vector of size
// shots*qubits, where each element represents a single bit. The
// corresponding shape is (shots, qubits). Gather the desired bits
// corresponding to the input wires into a bitstring.
auto samplesIter = samples.begin();
for (size_t shot = 0; shot < shots; shot++) {
for (auto wire : dev_wires) {
*(samplesIter++) = static_cast<double>(li_samples[shot * numQubits + wire]);
}
}
}
void LightningSimulator::Counts(DataView<double, 1> &eigvals, DataView<int64_t, 1> &counts,
size_t shots)
{
const size_t numQubits = this->GetNumQubits();
const size_t numElements = 1U << numQubits;
RT_FAIL_IF(eigvals.size() != numElements || counts.size() != numElements,
"Invalid size for the pre-allocated counts");
auto li_samples = this->GenerateSamples(shots);
// Fill the eigenvalues with the integer representation of the corresponding
// computational basis bitstring. In the future, eigenvalues can also be
// obtained from an observable, hence the bitstring integer is stored as a
// double.
std::iota(eigvals.begin(), eigvals.end(), 0);
std::fill(counts.begin(), counts.end(), 0);
// The lightning samples are layed out as a single vector of size
// shots*qubits, where each element represents a single bit. The
// corresponding shape is (shots, qubits). Gather the bits of all qubits
// into a bitstring.
for (size_t shot = 0; shot < shots; shot++) {
std::bitset<CHAR_BIT * sizeof(double)> basisState;
size_t idx = numQubits;
for (size_t wire = 0; wire < numQubits; wire++) {
basisState[--idx] = li_samples[shot * numQubits + wire];
}
counts(static_cast<size_t>(basisState.to_ulong())) += 1;
}
}
void LightningSimulator::PartialCounts(DataView<double, 1> &eigvals, DataView<int64_t, 1> &counts,
const std::vector<QubitIdType> &wires, size_t shots)
{
const size_t numWires = wires.size();
const size_t numQubits = this->GetNumQubits();
const size_t numElements = 1U << numWires;
RT_FAIL_IF(numWires > numQubits, "Invalid number of wires");
RT_FAIL_IF(!isValidQubits(wires), "Invalid given wires to measure");
RT_FAIL_IF((eigvals.size() != numElements || counts.size() != numElements),
"Invalid size for the pre-allocated partial-counts");
// get device wires
auto &&dev_wires = getDeviceWires(wires);
auto li_samples = this->GenerateSamples(shots);
// Fill the eigenvalues with the integer representation of the corresponding
// computational basis bitstring. In the future, eigenvalues can also be
// obtained from an observable, hence the bitstring integer is stored as a
// double.
std::iota(eigvals.begin(), eigvals.end(), 0);
std::fill(counts.begin(), counts.end(), 0);
// The lightning samples are layed out as a single vector of size
// shots*qubits, where each element represents a single bit. The
// corresponding shape is (shots, qubits). Gather the desired bits
// corresponding to the input wires into a bitstring.
for (size_t shot = 0; shot < shots; shot++) {
std::bitset<CHAR_BIT * sizeof(double)> basisState;
size_t idx = dev_wires.size();
for (auto wire : dev_wires) {
basisState[--idx] = li_samples[shot * numQubits + wire];
}
counts(static_cast<size_t>(basisState.to_ulong())) += 1;
}
}
auto LightningSimulator::Measure(QubitIdType wire, std::optional<int32_t> postselect) -> Result
{
// get a measurement
std::vector<QubitIdType> wires = {reinterpret_cast<QubitIdType>(wire)};
std::vector<double> probs(1U << wires.size());
DataView<double, 1> buffer_view(probs);
auto device_shots = GetDeviceShots();
SetDeviceShots(0);
PartialProbs(buffer_view, wires);
SetDeviceShots(device_shots);
// It represents the measured result, true for 1, false for 0
bool mres = Lightning::simulateDraw(probs, postselect, this->gen);
auto dev_wires = getDeviceWires(wires);
this->device_sv->collapse(dev_wires[0], mres ? 1 : 0);
return mres ? this->One() : this->Zero();
}
// Gradient
void LightningSimulator::Gradient(std::vector<DataView<double, 1>> &gradients,
const std::vector<size_t> &trainParams)
{
const bool tp_empty = trainParams.empty();
const size_t num_observables = this->cache_manager.getNumObservables();
const size_t num_params = this->cache_manager.getNumParams();
const size_t num_train_params = tp_empty ? num_params : trainParams.size();
const size_t jac_size = num_train_params * this->cache_manager.getNumObservables();
if (!jac_size) {
return;
}
RT_FAIL_IF(gradients.size() != num_observables, "Invalid number of pre-allocated gradients");
auto &&obs_callees = this->cache_manager.getObservablesCallees();
bool is_valid_measurements =
std::all_of(obs_callees.begin(), obs_callees.end(),
[](const auto &m) { return m == MeasurementsT::Expval; });
RT_FAIL_IF(!is_valid_measurements,
"Unsupported measurements to compute gradient; "
"Adjoint differentiation method only supports expectation return type");
auto &&state = this->device_sv->getDataVector();
// create OpsData
auto &&ops_names = this->cache_manager.getOperationsNames();
auto &&ops_params = this->cache_manager.getOperationsParameters();
auto &&ops_wires = this->cache_manager.getOperationsWires();
auto &&ops_inverses = this->cache_manager.getOperationsInverses();
auto &&ops_matrices = this->cache_manager.getOperationsMatrices();
auto &&ops_controlled_wires = this->cache_manager.getOperationsControlledWires();
auto &&ops_controlled_values = this->cache_manager.getOperationsControlledValues();
const auto &&ops = Pennylane::Algorithms::OpsData<StateVectorT>(
ops_names, ops_params, ops_wires, ops_inverses, ops_matrices, ops_controlled_wires,
ops_controlled_values);
// create the vector of observables
auto &&obs_keys = this->cache_manager.getObservablesKeys();
std::vector<std::shared_ptr<Pennylane::Observables::Observable<StateVectorT>>> obs_vec;
obs_vec.reserve(obs_keys.size());
for (auto idx : obs_keys) {
obs_vec.emplace_back(this->obs_manager.getObservable(idx));
}
std::vector<size_t> all_params;
if (tp_empty) {
all_params.reserve(num_params);
for (size_t i = 0; i < num_params; i++) {
all_params.push_back(i);
}
}
// construct the Jacobian data
Pennylane::Algorithms::JacobianData<StateVectorT> tape{
num_params, state.size(), state.data(), obs_vec, ops, tp_empty ? all_params : trainParams};
Pennylane::LightningQubit::Algorithms::AdjointJacobian<StateVectorT> adj;
std::vector<double> jacobian(jac_size, 0);
adj.adjointJacobian(std::span{jacobian}, tape,
/* ref_data */ *this->device_sv,
/* apply_operations */ false);
// convert jacobians to a list of lists for each observable
std::vector<double> jacobian_t =
Pennylane::LightningQubit::Util::Transpose(jacobian, num_train_params, num_observables);
std::vector<double> cur_buffer(num_train_params);
auto begin_loc_iter = jacobian_t.begin();
for (size_t obs_idx = 0; obs_idx < num_observables; obs_idx++) {
RT_ASSERT(begin_loc_iter != jacobian_t.end());
RT_ASSERT(num_train_params <= gradients[obs_idx].size());
std::move(begin_loc_iter, begin_loc_iter + num_train_params, cur_buffer.begin());
std::move(cur_buffer.begin(), cur_buffer.end(), gradients[obs_idx].begin());
begin_loc_iter += num_train_params;
}
}
} // namespace Catalyst::Runtime::Simulator
GENERATE_DEVICE_FACTORY(LightningSimulator, Catalyst::Runtime::Simulator::LightningSimulator);