forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQppCircuitSimulator.cpp
More file actions
423 lines (361 loc) · 14.8 KB
/
QppCircuitSimulator.cpp
File metadata and controls
423 lines (361 loc) · 14.8 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
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
/*******************************************************************************
* Copyright (c) 2022 - 2026 NVIDIA Corporation & Affiliates. *
* All rights reserved. *
* *
* This source code and the accompanying materials are made available under *
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/
#include "common/FmtCore.h"
#include "nvqir/CircuitSimulator.h"
#include "nvqir/Gates.h"
#include <bit>
#include <iostream>
#include <qpp.h>
#include <set>
#include <span>
using namespace cudaq;
namespace nvqir {
/// @brief QppState provides an implementation of `SimulationState` that
/// encapsulates the state data for the Qpp Circuit Simulator.
struct QppState : public cudaq::SimulationState {
/// @brief The state. This class takes ownership move semantics.
qpp::ket state;
QppState(qpp::ket &&data) : state(std::move(data)) {}
QppState(const std::vector<std::size_t> &shape,
const std::vector<std::complex<double>> &data) {
if (shape.size() != 1)
throw std::runtime_error(
"QppState must be created from data with 1D shape.");
state = Eigen::Map<qpp::ket>(
const_cast<std::complex<double> *>(data.data()), shape[0]);
}
std::size_t getNumQubits() const override { return std::log2(state.size()); }
std::complex<double> overlap(const cudaq::SimulationState &other) override {
if (other.getNumTensors() != 1 ||
(other.getTensor().extents != getTensor().extents))
throw std::runtime_error("[qpp-state] overlap error - other state "
"dimension not equal to this state dimension.");
std::span<std::complex<double>> otherState(
reinterpret_cast<std::complex<double> *>(other.getTensor().data),
other.getTensor().extents[0]);
return std::abs(std::inner_product(
state.begin(), state.end(), otherState.begin(), complex{0., 0.},
[](auto a, auto b) { return a + b; },
[](auto a, auto b) { return a * std::conj(b); }));
}
std::complex<double>
getAmplitude(const std::vector<int> &basisState) override {
if (getNumQubits() != basisState.size())
throw std::runtime_error(fmt::format(
"[qpp-state] getAmplitude with an invalid number of bits in the "
"basis state: expected {}, provided {}.",
getNumQubits(), basisState.size()));
if (std::any_of(basisState.begin(), basisState.end(),
[](int x) { return x != 0 && x != 1; }))
throw std::runtime_error(
"[qpp-state] getAmplitude with an invalid basis state: only "
"qubit state (0 or 1) is supported.");
// Convert the basis state to an index value
const std::size_t idx = std::accumulate(
std::make_reverse_iterator(basisState.end()),
std::make_reverse_iterator(basisState.begin()), 0ull,
[](std::size_t acc, int bit) { return (acc << 1) + bit; });
return state[idx];
}
Tensor getTensor(std::size_t tensorIdx = 0) const override {
if (tensorIdx != 0)
throw std::runtime_error("[qpp-state] invalid tensor requested.");
return Tensor{
reinterpret_cast<void *>(
const_cast<std::complex<double> *>(state.data())),
std::vector<std::size_t>{static_cast<std::size_t>(state.size())},
getPrecision()};
}
// /// @brief Return all tensors that represent this state
std::vector<Tensor> getTensors() const override { return {getTensor()}; }
// /// @brief Return the number of tensors that represent this state.
std::size_t getNumTensors() const override { return 1; }
std::complex<double>
operator()(std::size_t tensorIdx,
const std::vector<std::size_t> &indices) override {
if (tensorIdx != 0)
throw std::runtime_error("[qpp-state] invalid tensor requested.");
if (indices.size() != 1)
throw std::runtime_error("[qpp-state] invalid element extraction.");
return state[indices[0]];
}
std::unique_ptr<SimulationState>
createFromSizeAndPtr(std::size_t size, void *ptr, std::size_t) override {
if (!ptr || size == 0)
throw std::runtime_error(
"[createFromSizeAndPtr] invalid null pointer or zero size");
return std::make_unique<QppState>(Eigen::Map<qpp::ket>(
reinterpret_cast<std::complex<double> *>(ptr), size));
}
void dump(std::ostream &os) const override { os << state << "\n"; }
precision getPrecision() const override {
return cudaq::SimulationState::precision::fp64;
}
void destroyState() override {
qpp::ket k;
state = k;
}
};
/// @brief The QppCircuitSimulator implements the CircuitSimulator
/// base class to provide a simulator delegating to the Q++ library from
/// https://github.com/softwareqinc/qpp.
template <typename StateType>
class QppCircuitSimulator : public nvqir::CircuitSimulatorBase<double> {
protected:
/// The QPP state representation (qpp::ket or qpp::cmat)
StateType state;
/// @brief Convert internal qubit index to Q++ qubit index.
///
/// In Q++, qubits are indexed from left to right, and thus q0 is the leftmost
/// qubit. Internally, in CUDA-Q, qubits are index from right to left,
/// hence q0 is the rightmost qubit. Example:
/// ```
/// Q++ indices: 0 1 2 3
/// |0>|0>|0>|0>
/// 3 2 1 0 : CUDA-Q indices
/// ```
std::size_t convertQubitIndex(std::size_t qubitIndex) {
assert(stateDimension > 0 && "The state is empty, and thus has no qubits");
return std::log2(stateDimension) - qubitIndex - 1;
}
/// @brief Compute the expectation value <Z...Z> over the given qubit indices.
double calculateExpectationValue(const std::vector<std::size_t> &qubits) {
std::size_t bitmask = 0;
for (auto q : qubits)
bitmask |= (1ULL << q);
const auto hasEvenParity = [&bitmask](std::size_t x) -> bool {
return std::popcount(x & bitmask) % 2 == 0;
};
std::vector<double> result;
if constexpr (std::is_same_v<StateType, qpp::ket>) {
result.resize(stateDimension);
#if defined(_OPENMP)
#pragma omp parallel for
#endif
for (std::size_t i = 0; i < stateDimension; ++i)
result[i] = (hasEvenParity(i) ? 1.0 : -1.0) * std::norm(state[i]);
} else if constexpr (std::is_same_v<StateType, qpp::cmat>) {
Eigen::VectorXcd diag = state.diagonal();
result.resize(state.rows());
#if defined(_OPENMP)
#pragma omp parallel for
#endif
for (Eigen::Index i = 0; i < state.rows(); ++i)
result[i] = hasEvenParity(i) ? diag(i).real() : -diag(i).real();
}
// Accumulate outside the for loop to ensure repeatability
return std::accumulate(result.begin(), result.end(), 0.0);
}
qpp::cmat toQppMatrix(const std::vector<std::complex<double>> &data,
std::size_t nTargets) {
auto nRows = (1UL << nTargets);
assert(data.size() == nRows * nRows &&
"Invalid number of gate matrix elements passed to toQppMatrix");
// we represent row major, they represent column major
return Eigen::Map<Eigen::Matrix<std::complex<double>, Eigen::Dynamic,
Eigen::Dynamic, Eigen::RowMajor>>(
const_cast<std::complex<double> *>(data.data()), nRows, nRows);
}
/// @brief Grow the state vector by one qubit.
void addQubitToState() override { addQubitsToState(1); }
/// @brief Override the default sized allocation of qubits
/// here to be a bit more efficient than the default implementation
void addQubitsToState(std::size_t qubitCount,
const void *stateDataIn = nullptr) override {
if (qubitCount == 0)
return;
auto *stateData = reinterpret_cast<std::complex<double> *>(
const_cast<void *>(stateDataIn));
if (state.size() == 0) {
// If this is the first time, allocate the state
if (stateData == nullptr) {
state = qpp::ket::Zero(stateDimension);
state(0) = 1.0;
} else
state = qpp::ket::Map(stateData, stateDimension);
return;
}
// If we are resizing an existing, allocate
// a zero state on a n qubit, and Kron-prod
// that with the existing state.
if (stateData == nullptr) {
qpp::ket zero_state = qpp::ket::Zero((1UL << qubitCount));
zero_state(0) = 1.0;
state = qpp::kron(zero_state, state);
} else {
qpp::ket initState = qpp::ket::Map(stateData, (1UL << qubitCount));
state = qpp::kron(initState, state);
}
return;
}
void addQubitsToState(const cudaq::SimulationState &in_state) override {
const QppState *const casted = dynamic_cast<const QppState *>(&in_state);
if (!casted)
throw std::invalid_argument(
"[QppCircuitSimulator] Incompatible state input");
if (state.size() == 0)
state = casted->state;
else
state = qpp::kron(casted->state, state);
}
/// @brief Reset the qubit state.
void deallocateStateImpl() override {
StateType tmp;
state = tmp;
}
void applyGate(const GateApplicationTask &task) override {
auto matrix = toQppMatrix(task.matrix, task.targets.size());
// First, convert all of the qubit indices to big endian.
std::vector<std::size_t> controls;
for (auto index : task.controls) {
controls.push_back(convertQubitIndex(index));
}
std::vector<std::size_t> targets;
for (auto index : task.targets) {
targets.push_back(convertQubitIndex(index));
}
if (controls.empty()) {
state = qpp::apply(state, matrix, targets);
return;
}
state = qpp::applyCTRL(state, matrix, controls, targets);
}
/// @brief Set the current state back to the |0> state.
void setToZeroState() override {
state = qpp::ket::Zero(stateDimension);
state(0) = 1.0;
}
/// @brief Measure the qubit and return the result. Collapse the
/// state vector.
bool measureQubit(const std::size_t index) override {
const auto qubitIdx = convertQubitIndex(index);
// If here, then we care about the result bit, so compute it.
const auto measurement_tuple =
qpp::measure(state, qpp::cmat::Identity(2, 2), {qubitIdx},
/*qudit dimension=*/2, /*destructive measmt=*/false);
const auto measurement_result = std::get<qpp::RES>(measurement_tuple);
const auto &post_meas_states = std::get<qpp::ST>(measurement_tuple);
const auto &collapsed_state = post_meas_states[measurement_result];
if constexpr (std::is_same_v<StateType, qpp::ket>) {
state = Eigen::Map<const StateType>(collapsed_state.data(),
collapsed_state.size());
} else {
state = Eigen::Map<const StateType>(collapsed_state.data(),
collapsed_state.rows(),
collapsed_state.cols());
}
CUDAQ_INFO("Measured qubit {} -> {}", qubitIdx, measurement_result);
return measurement_result == 1 ? true : false;
}
QubitOrdering getQubitOrdering() const override { return QubitOrdering::msb; }
public:
QppCircuitSimulator() {
// Populate the correct name so it is printed correctly during
// deconstructor.
summaryData.name = name();
}
virtual ~QppCircuitSimulator() = default;
void setRandomSeed(std::size_t seed) override {
qpp::RandomDevices::get_instance().get_prng().seed(seed);
}
bool canHandleObserve() override {
auto executionContext = cudaq::getExecutionContext();
// Do not compute <H> from matrix if shots based sampling requested
if (executionContext &&
executionContext->shots != static_cast<std::size_t>(-1)) {
return false;
}
return !shouldObserveFromSampling();
}
cudaq::observe_result observe(const cudaq::spin_op &op) override {
assert(cudaq::spin_op::canonicalize(op) == op);
flushGateQueue();
// The op is on the following target bits.
auto targets = op.degrees();
// Get the matrix as an Eigen matrix
auto matrix = op.to_matrix();
qpp::cmat asEigen = matrix.as_eigen();
// Compute the expected value
double ee = 0.0;
if constexpr (std::is_same_v<StateType, qpp::ket>) {
qpp::ket k = qpp::apply(state, asEigen, targets, 2);
ee = state.dot(k).real();
} else {
ee = qpp::apply(asEigen, state, targets).trace().real();
}
return cudaq::observe_result(
ee, op,
cudaq::sample_result(cudaq::ExecutionResult({}, op.to_string(), ee)));
}
/// @brief Reset the qubit
/// @param index 0-based index of qubit to reset
void resetQubit(const std::size_t index) override {
flushGateQueue();
flushAnySamplingTasks();
const auto qubitIdx = convertQubitIndex(index);
state = qpp::reset(state, {qubitIdx});
}
/// @brief Sample the multi-qubit state.
cudaq::ExecutionResult sample(const std::vector<std::size_t> &qubits,
const int shots,
bool includeSequentialData = true) override {
if (shots < 1) {
double expectationValue = calculateExpectationValue(qubits);
CUDAQ_INFO("Computed expectation value = {}", expectationValue);
return cudaq::ExecutionResult{{}, expectationValue};
}
std::vector<std::size_t> measuredBits;
for (auto index : qubits) {
measuredBits.push_back(convertQubitIndex(index));
}
auto sampleResult = qpp::sample(shots, state, measuredBits, 2);
// Convert to what we expect
std::stringstream bitstream;
cudaq::ExecutionResult counts;
// Expectation value from the counts
double expVal = 0.0;
for (auto [result, count] : sampleResult) {
// Push back each term in the vector of bits to the bitstring.
for (const auto &bit : result) {
bitstream << bit;
}
// Add to the sample result
// in mid-circ sampling mode this will append 1 bitstring
auto bitstring = bitstream.str();
if (includeSequentialData)
counts.appendResult(bitstring, count);
else
counts.counts[bitstring] += count;
auto par = cudaq::sample_result::has_even_parity(bitstring);
auto p = count / (double)shots;
if (!par) {
p = -p;
}
expVal += p;
// Reset the state.
bitstream.str("");
bitstream.clear();
}
counts.expectationValue = expVal;
return counts;
}
std::unique_ptr<cudaq::SimulationState> getSimulationState() override {
flushGateQueue();
return std::make_unique<QppState>(std::move(state));
}
bool isStateVectorSimulator() const override {
return std::is_same_v<StateType, qpp::ket>;
}
std::string name() const override { return "qpp"; }
NVQIR_SIMULATOR_CLONE_IMPL(QppCircuitSimulator<StateType>)
};
} // namespace nvqir
#ifndef __NVQIR_QPP_TOGGLE_CREATE
/// Register this Simulator with NVQIR.
NVQIR_REGISTER_SIMULATOR(nvqir::QppCircuitSimulator<qpp::ket>, qpp)
#endif