forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CircuitSimulator.h
1416 lines (1209 loc) · 53.6 KB
/
CircuitSimulator.h
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************-*- C++ -*-****
* Copyright (c) 2022 - 2024 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. *
******************************************************************************/
#pragma once
#include "Gates.h"
#include "QIRTypes.h"
#include "common/Logger.h"
#include "common/MeasureCounts.h"
#include "common/NoiseModel.h"
#include "common/Timing.h"
#include "cudaq/host_config.h"
#include <cstdarg>
#include <cstddef>
#include <queue>
#include <sstream>
#include <string>
#include <variant>
namespace nvqir {
enum class QubitOrdering { lsb, msb };
// @brief Collect summary data and print upon simulator termination
struct SummaryData {
std::size_t gateCount = 0;
std::size_t controlCount = 0;
std::size_t targetCount = 0;
std::size_t svIO = 0;
std::size_t svFLOPs = 0;
bool enabled = false;
std::string name;
SummaryData() {
if (cudaq::isTimingTagEnabled(cudaq::TIMING_GATE_COUNT))
enabled = true;
}
/// @brief Update state-vector-based statistics for a logic gate
void svGateUpdate(const std::size_t nControls, const std::size_t nTargets,
const std::size_t stateDimension,
const std::size_t stateVectorSizeBytes) {
assert(nControls <= 63);
if (enabled) {
gateCount++;
controlCount += nControls;
targetCount += nTargets;
// Times 2 because operating on the state vector requires both reading
// and writing.
svIO += (2 * stateVectorSizeBytes) / (1 << nControls);
// For each element of the state vector, 2 complex multiplies and 1
// complex accumulate is needed. This is reduced if there if this is a
// controlled operation.
// Each complex multiply is 6 real ops.
// So 2 complex multiplies and 1 complex addition is 2*6+2 = 14 ops.
svFLOPs += stateDimension * (14 * nTargets) / (1 << nControls);
}
}
~SummaryData() {
if (enabled) {
cudaq::log("CircuitSimulator '{}' Total Program Metrics [tag={}]:", name,
cudaq::TIMING_GATE_COUNT);
cudaq::log("Gate Count = {}", gateCount);
cudaq::log("Control Count = {}", controlCount);
cudaq::log("Target Count = {}", targetCount);
cudaq::log("State Vector I/O (GB) = {:.6f}",
static_cast<double>(svIO) / 1e9);
cudaq::log("State Vector GFLOPs = {:.6f}",
static_cast<double>(svFLOPs) / 1e9);
}
}
};
/// @brief The CircuitSimulator defines a base class for all
/// simulators that are available to CUDA-Q via the NVQIR library.
/// This base class handles Qubit allocation and deallocation,
/// execution context handling, and defines all quantum operations pure
/// virtual methods that subtypes must implement. Subtypes should be responsible
/// for evolution of the concrete wave function representation (e.g.,
/// state vector), sampling, and measurements.
class CircuitSimulator {
protected:
/// @brief Flush the current queue of gates, i.e.
/// apply them to the state. Internal and meant for
/// subclasses to implement
virtual void flushGateQueueImpl() = 0;
/// @brief Statistics collected over the life of the simulator.
SummaryData summaryData;
public:
/// @brief The constructor
CircuitSimulator() = default;
/// @brief The destructor
virtual ~CircuitSimulator() = default;
/// @brief Flush the current queue of gates, i.e.
/// apply them to the state.
void flushGateQueue() { flushGateQueueImpl(); }
/// @brief Provide an opportunity for any tear-down
/// tasks before MPI Finalize is invoked. Here we leave
/// this unimplemented, it is meant for subclasses.
virtual void tearDownBeforeMPIFinalize() {
// do nothing
}
/// @brief Provide a mechanism for simulators to
/// create and return a `SimulationState` instance from
/// a user-specified data set.
virtual std::unique_ptr<cudaq::SimulationState>
createStateFromData(const cudaq::state_data &) = 0;
/// @brief Set the current noise model to consider when
/// simulating the state. This should be overridden by
/// simulation strategies that support noise modeling.
virtual void setNoiseModel(cudaq::noise_model &noise) = 0;
virtual void setRandomSeed(std::size_t seed) {
// do nothing
}
/// @brief Perform any flushing or synchronization to force that all
/// previously applied gates have truly been applied by the underlying
/// simulator.
virtual void synchronize() {}
/// @brief Apply exp(-i theta PauliTensorProd) to the underlying state.
/// This must be provided by subclasses.
virtual void applyExpPauli(double theta,
const std::vector<std::size_t> &controls,
const std::vector<std::size_t> &qubitIds,
const cudaq::spin_op &op) {
if (op.is_identity()) {
if (controls.empty()) {
// exp(i*theta*Id) is noop if this is not a controlled gate.
return;
} else {
// Throw an error if this exp_pauli(i*theta*Id) becomes a non-trivial
// gate due to control qubits.
// FIXME: revisit this once
// https://github.com/NVIDIA/cuda-quantum/issues/483 is implemented.
throw std::logic_error("Applying controlled global phase via exp_pauli "
"of identity operator is not supported");
}
}
flushGateQueue();
cudaq::info(" [CircuitSimulator decomposing] exp_pauli({}, {})", theta,
op.to_string(false));
std::vector<std::size_t> qubitSupport;
std::vector<std::function<void(bool)>> basisChange;
op.for_each_pauli([&](cudaq::pauli type, std::size_t qubitIdx) {
auto qId = qubitIds[qubitIdx];
if (type != cudaq::pauli::I)
qubitSupport.push_back(qId);
if (type == cudaq::pauli::Y)
basisChange.emplace_back([this, qId](bool reverse) {
rx(!reverse ? M_PI_2 : -M_PI_2, qId);
});
else if (type == cudaq::pauli::X)
basisChange.emplace_back([this, qId](bool) { h(qId); });
});
if (!basisChange.empty())
for (auto &basis : basisChange)
basis(false);
std::vector<std::pair<std::size_t, std::size_t>> toReverse;
for (std::size_t i = 0; i < qubitSupport.size() - 1; i++) {
x({qubitSupport[i]}, qubitSupport[i + 1]);
toReverse.emplace_back(qubitSupport[i], qubitSupport[i + 1]);
}
// Since this is a compute-action-uncompute type circuit, we only need to
// apply control on this rz gate.
rz(-2.0 * theta, controls, qubitSupport.back());
std::reverse(toReverse.begin(), toReverse.end());
for (auto &[i, j] : toReverse)
x({i}, j);
if (!basisChange.empty()) {
std::reverse(basisChange.begin(), basisChange.end());
for (auto &basis : basisChange)
basis(true);
}
}
/// @brief Compute the expected value of the given spin op
/// with respect to the current state, <psi | H | psi>.
virtual cudaq::observe_result observe(const cudaq::spin_op &term) = 0;
/// @brief Allocate a single qubit, return the qubit as a logical index
virtual std::size_t allocateQubit() = 0;
/// @brief Allocate `count` qubits.
virtual std::vector<std::size_t>
allocateQubits(std::size_t count, const void *state = nullptr,
cudaq::simulation_precision precision =
cudaq::simulation_precision::fp32) = 0;
virtual std::vector<std::size_t>
allocateQubits(std::size_t count, const cudaq::SimulationState *state) = 0;
/// @brief Deallocate the qubit with give unique index
virtual void deallocate(const std::size_t qubitIdx) = 0;
/// @brief Deallocate all the provided qubits.
virtual void deallocateQubits(const std::vector<std::size_t> &qubits) = 0;
/// @brief Reset the current execution context.
virtual void resetExecutionContext() = 0;
/// @brief Set the execution context
virtual void setExecutionContext(cudaq::ExecutionContext *context) = 0;
/// @brief Return the current execution context
virtual cudaq::ExecutionContext *getExecutionContext() = 0;
/// @brief Whether or not this is a state vector simulator
virtual bool isStateVectorSimulator() const { return false; }
/// @brief Apply a custom operation described by a matrix of data
/// represented as 1-D vector of elements in row-major order, as well
/// as the the control qubit and target indices
virtual void
applyCustomOperation(const std::vector<std::complex<double>> &matrix,
const std::vector<std::size_t> &controls,
const std::vector<std::size_t> &targets,
const std::string_view customUnitaryName = "") = 0;
#define CIRCUIT_SIMULATOR_ONE_QUBIT(NAME) \
void NAME(const std::size_t qubitIdx) { \
std::vector<std::size_t> tmp; \
NAME(tmp, qubitIdx); \
} \
virtual void NAME(const std::vector<std::size_t> &controls, \
const std::size_t qubitIdx) = 0;
#define CIRCUIT_SIMULATOR_ONE_QUBIT_ONE_PARAM(NAME) \
void NAME(const double angle, const std::size_t qubitIdx) { \
std::vector<std::size_t> tmp; \
NAME(angle, tmp, qubitIdx); \
} \
virtual void NAME(const double angle, \
const std::vector<std::size_t> &controls, \
const std::size_t qubitIdx) = 0;
/// @brief The X gate
CIRCUIT_SIMULATOR_ONE_QUBIT(x)
/// @brief The Y gate
CIRCUIT_SIMULATOR_ONE_QUBIT(y)
/// @brief The Z gate
CIRCUIT_SIMULATOR_ONE_QUBIT(z)
/// @brief The H gate
CIRCUIT_SIMULATOR_ONE_QUBIT(h)
/// @brief The S gate
CIRCUIT_SIMULATOR_ONE_QUBIT(s)
/// @brief The T gate
CIRCUIT_SIMULATOR_ONE_QUBIT(t)
/// @brief The Sdg gate
CIRCUIT_SIMULATOR_ONE_QUBIT(sdg)
/// @brief The Tdg gate
CIRCUIT_SIMULATOR_ONE_QUBIT(tdg)
/// @brief The RX gate
CIRCUIT_SIMULATOR_ONE_QUBIT_ONE_PARAM(rx)
/// @brief The RY gate
CIRCUIT_SIMULATOR_ONE_QUBIT_ONE_PARAM(ry)
/// @brief The RZ gate
CIRCUIT_SIMULATOR_ONE_QUBIT_ONE_PARAM(rz)
/// @brief The Phase gate
CIRCUIT_SIMULATOR_ONE_QUBIT_ONE_PARAM(r1)
/// @brief The IBM U1 gate
CIRCUIT_SIMULATOR_ONE_QUBIT_ONE_PARAM(u1)
// Undef those preprocessor defines.
#undef CIRCUIT_SIMULATOR_ONE_QUBIT
#undef CIRCUIT_SIMULATOR_ONE_QUBIT_ONE_PARAM
void u2(const double phi, const double lambda, const std::size_t qubitIdx) {
std::vector<std::size_t> controls;
u2(phi, lambda, controls, qubitIdx);
}
virtual void u2(const double phi, const double lambda,
const std::vector<std::size_t> &controls,
const std::size_t qubitIdx) = 0;
void phased_rx(const double phi, const double lambda,
const std::size_t qubitIdx) {
std::vector<std::size_t> controls;
phased_rx(phi, lambda, controls, qubitIdx);
}
virtual void phased_rx(const double phi, const double lambda,
const std::vector<std::size_t> &controls,
const std::size_t qubitIdx) = 0;
void u3(const double theta, const double phi, const double lambda,
const std::size_t qubitIdx) {
std::vector<std::size_t> controls;
u3(theta, phi, lambda, controls, qubitIdx);
}
virtual void u3(const double theta, const double phi, const double lambda,
const std::vector<std::size_t> &controls,
const std::size_t qubitIdx) = 0;
/// @brief Invoke the SWAP gate
void swap(const std::size_t srcIdx, const std::size_t tgtIdx) {
std::vector<std::size_t> tmp;
swap(tmp, srcIdx, tgtIdx);
}
/// @brief Invoke a general multi-control swap gate
virtual void swap(const std::vector<std::size_t> &ctrlBits,
const std::size_t srcIdx, const std::size_t tgtIdx) = 0;
/// @brief Measure the qubit with given index
virtual bool mz(const std::size_t qubitIdx) = 0;
/// @brief Measure operation. Here we check what the current execution
/// context is. If the context is sample, then we do nothing but store the
/// measure qubit, which we then use to do full state sampling when
/// flushAnySamplingTask() is called. If the context is sample-conditional,
/// then we have a circuit that contains if (`mz(q)`) and we measure the
/// qubit, collapse the state, and then store the sample qubit for final full
/// state sampling. We also return the bit result. If no execution context,
/// just measure, collapse, and return the bit.
virtual bool mz(const std::size_t qubitIdx,
const std::string ®isterName) = 0;
virtual void measureSpinOp(const cudaq::spin_op &op) = 0;
/// @brief Reset the qubit to the |0> state
virtual void resetQubit(const std::size_t qubitIdx) = 0;
/// @brief Sample the current multi-qubit state on the given qubit indices
/// over a certain number of shots
virtual cudaq::ExecutionResult
sample(const std::vector<std::size_t> &qubitIdxs, const int shots) = 0;
/// @brief Return the name of this CircuitSimulator
virtual std::string name() const = 0;
/// @brief Return a thread_local pointer to this CircuitSimulator
virtual CircuitSimulator *clone() = 0;
/// Determine the (preferred) precision of the simulator.
virtual bool isSinglePrecision() const = 0;
bool isDoublePrecision() const { return !isSinglePrecision(); }
};
/// @brief The CircuitSimulatorBase is the type that is meant to
/// be subclassed for new simulation strategies. The separation of
/// CircuitSimulator from CircuitSimulatorBase allows simulation sub-types
/// to specify the floating point precision for the simulation
template <typename ScalarType>
class CircuitSimulatorBase : public CircuitSimulator {
private:
/// @brief Reference to the current circuit name.
std::string currentCircuitName = "";
private:
/// @brief Return true if the simulator is in the tracer mode.
bool isInTracerMode() const {
return executionContext && executionContext->name == "tracer";
}
protected:
/// @brief The current Execution Context (typically this is null,
/// sampling, or spin_op observation.
cudaq::ExecutionContext *executionContext = nullptr;
/// @brief A tracker for qubit allocation
cudaq::QuditIdTracker tracker;
/// @brief The number of qubits that have been allocated
std::size_t nQubitsAllocated = 0;
/// @brief The dimension of the multi-qubit state.
std::size_t stateDimension = 0;
/// @brief Keep track of the previous state dimension
/// as we grow the state.
std::size_t previousStateDimension = 0;
/// @brief Vector containing qubit ids that are to be sampled
std::vector<std::size_t> sampleQubits;
/// @brief Map of register name to observed bit results for mid-circuit
/// sampling
std::unordered_map<std::string, std::vector<std::string>>
midCircuitSampleResults;
/// @brief Store the last observed register name, this will help us
/// know if we are writing to a classical bit vector
std::string lastMidCircuitRegisterName = "";
/// @brief Vector storing register names that are bit vectors
std::vector<std::string> vectorRegisters;
/// @brief Under certain execution contexts, we'll deallocate
/// before we are actually done with the execution task,
/// this vector keeps track of qubit ids that are to be
/// deallocated at a later time.
std::vector<std::size_t> deferredDeallocation;
/// @brief Map bit register names to the qubits that make it up
std::unordered_map<std::string, std::vector<std::size_t>>
registerNameToMeasuredQubit;
/// @brief Keep track of the current number of qubits in batch mode
std::size_t batchModeCurrentNumQubits = 0;
/// @brief Environment variable name that allows a programmer to
/// specify how expectation values should be computed. This
/// defaults to true.
static constexpr const char observeSamplingEnvVar[] =
"CUDAQ_OBSERVE_FROM_SAMPLING";
/// @brief A GateApplicationTask consists of a
/// matrix describing the quantum operation, a set of
/// possible control qubit indices, and a set of target indices.
struct GateApplicationTask {
const std::string operationName;
const std::vector<std::complex<ScalarType>> matrix;
const std::vector<std::size_t> controls;
const std::vector<std::size_t> targets;
const std::vector<ScalarType> parameters;
GateApplicationTask(const std::string &name,
const std::vector<std::complex<ScalarType>> &m,
const std::vector<std::size_t> &c,
const std::vector<std::size_t> &t,
const std::vector<ScalarType> ¶ms)
: operationName(name), matrix(m), controls(c), targets(t),
parameters(params) {}
};
/// @brief The current queue of operations to execute
std::queue<GateApplicationTask> gateQueue;
/// @brief Get the name of the current circuit being executed.
std::string getCircuitName() const { return currentCircuitName; }
/// @brief Return the current multi-qubit state dimension
virtual std::size_t calculateStateDim(const std::size_t numQubits) {
assert(numQubits < 64);
return 1ULL << numQubits;
}
/// @brief Add a new qubit to the state representation.
/// This is subclass specific.
virtual void addQubitToState() = 0;
/// @brief Subclass specific part of deallocateState().
/// It will be invoked by deallocateState()
virtual void deallocateStateImpl() = 0;
/// @brief Reset the qubit state back to dim = 0.
void deallocateState() {
deallocateStateImpl();
nQubitsAllocated = 0;
stateDimension = 0;
}
/// @brief Perform the actual mechanics of measuring a qubit,
/// left as a task for concrete subtypes.
virtual bool measureQubit(const std::size_t qubitIdx) = 0;
/// @brief Return true if this CircuitSimulator can
/// handle <psi | H | psi> instead of NVQIR applying measure
/// basis quantum gates to change to the Z basis and sample.
virtual bool canHandleObserve() { return false; }
/// @brief Return the internal state representation. This
/// is meant for subtypes to override
virtual std::unique_ptr<cudaq::SimulationState> getSimulationState() {
throw std::runtime_error(
"Simulation data not available for this simulator backend.");
}
/// @brief Handle basic sampling tasks by storing the qubit index for
/// processing in resetExecutionContext. Return true to indicate this is
/// sampling and to exit early. False otherwise.
bool handleBasicSampling(const std::size_t qubitIdx,
const std::string ®Name) {
if (executionContext && executionContext->name == "sample" &&
!executionContext->hasConditionalsOnMeasureResults) {
// Add the qubit to the sampling list
sampleQubits.push_back(qubitIdx);
auto processForRegName = [&](const std::string ®Str) {
// Insert the sample qubit into the register name map
auto iter = registerNameToMeasuredQubit.find(regStr);
if (iter == registerNameToMeasuredQubit.end())
registerNameToMeasuredQubit.emplace(
regStr, std::vector<std::size_t>{qubitIdx});
else if (std::find(iter->second.begin(), iter->second.end(),
qubitIdx) == iter->second.end())
iter->second.push_back(qubitIdx);
};
// Insert into global register and named register (if it exists)
processForRegName(cudaq::GlobalRegisterName);
if (!regName.empty())
processForRegName(regName);
return true;
}
return false;
}
/// @brief This function handles sampling in the presence of conditional
/// statements on qubit measurement results. Specifically, it will keep
/// track of a classical register for all measures encountered in the program
/// and store mid-circuit measures in the corresponding register.
void handleSamplingWithConditionals(const std::size_t qubitIdx,
const std::string bitResult,
const std::string ®isterName) {
// We still care about what qubit we are measuring if in the
// sample-conditional context
if (executionContext && executionContext->name == "sample" &&
executionContext->hasConditionalsOnMeasureResults) {
std::string mutableRegisterName = registerName;
// If no registerName, we'll just sample normally
if (registerName.empty()) {
// Either this is library mode and we have register names attached
// to the execution context
if (midCircuitSampleResults.size() <
executionContext->registerNames.size()) {
mutableRegisterName =
executionContext->registerNames[midCircuitSampleResults.size()];
} else {
// or no register names, in which case we'll just treat it as
// a regular sampled qubit and drop out
sampleQubits.push_back(qubitIdx);
return;
}
}
cudaq::info("Handling Sampling With Conditionals: {}, {}, {}", qubitIdx,
bitResult, mutableRegisterName);
// See if we've observed this register before, if not
// start a vector of bit results, if we have, add the
// bit result to the existing vector
auto iter = midCircuitSampleResults.find(mutableRegisterName);
if (iter == midCircuitSampleResults.end())
midCircuitSampleResults.emplace(mutableRegisterName,
std::vector<std::string>{bitResult});
else
iter->second.push_back(bitResult);
// If this register is the same as last time, then we are
// writing to a bit vector register (auto var = mz(qreg))
if (lastMidCircuitRegisterName == mutableRegisterName)
vectorRegisters.push_back(mutableRegisterName);
// Store the last register name
lastMidCircuitRegisterName = mutableRegisterName;
}
}
/// @brief Utility function that returns a string-view of the current
/// quantum instruction, intended for logging purposes.
std::string gateToString(const std::string_view gateName,
const std::vector<std::size_t> &controls,
const std::vector<ScalarType> ¶meters,
const std::vector<std::size_t> &targets) {
std::string angleStr = "";
if (!parameters.empty()) {
angleStr = std::to_string(parameters[0]);
for (std::size_t i = 1; i < parameters.size(); i++)
angleStr += ", " + std::to_string(parameters[i]);
angleStr += ", ";
}
std::stringstream bits, ret;
if (!controls.empty()) {
bits << controls[0];
for (size_t i = 1; i < controls.size(); i++) {
bits << ", " << controls[i];
}
bits << ", " << targets[0];
for (size_t i = 1; i < targets.size(); i++) {
bits << ", " << targets[i];
}
ret << "(apply) ctrl-" << gateName << "(" << angleStr << bits.str()
<< ")";
} else {
bits << targets[0];
for (size_t i = 1; i < targets.size(); i++) {
bits << ", " << targets[i];
}
ret << "(apply) " << gateName << "(" << angleStr << bits.str() << ")";
}
return ret.str();
}
/// @brief Return true if the current execution is in batch mode
bool isInBatchMode() {
if (!executionContext)
return false;
if (executionContext->totalIterations == 0)
return false;
return true;
}
/// @brief Return true if the current execution is the
/// last execution of batch mode.
bool isLastBatch() {
return executionContext && executionContext->batchIteration > 0 &&
executionContext->batchIteration ==
executionContext->totalIterations - 1;
}
/// @brief Add the given number of qubits to the state.
virtual void addQubitsToState(std::size_t count,
const void *state = nullptr) {
if (state != nullptr)
throw std::runtime_error("State initialization must be handled by "
"subclasses, override addQubitsToState.");
for (std::size_t i = 0; i < count; i++)
addQubitToState();
}
/// @brief Add (appending) the given simulation state to the current simulator
/// state.
virtual void addQubitsToState(const cudaq::SimulationState &state) {
throw std::runtime_error("State initialization must be handled by "
"subclasses, override addQubitsToState.");
}
/// @brief Execute a sampling task with the current set of sample qubits.
void flushAnySamplingTasks(bool force = false) {
if (sampleQubits.empty())
return;
if (executionContext->hasConditionalsOnMeasureResults && !force)
return;
// Sort the qubit indices
std::sort(sampleQubits.begin(), sampleQubits.end());
auto last = std::unique(sampleQubits.begin(), sampleQubits.end());
sampleQubits.erase(last, sampleQubits.end());
cudaq::info("Sampling the current state, with measure qubits = {}",
sampleQubits);
// Ask the subtype to sample the current state
auto execResult =
sample(sampleQubits, executionContext->hasConditionalsOnMeasureResults
? 1
: executionContext->shots);
if (registerNameToMeasuredQubit.empty()) {
executionContext->result.append(execResult);
} else {
for (auto &[regName, qubits] : registerNameToMeasuredQubit) {
// Measurements are sorted according to qubit allocation order
std::sort(qubits.begin(), qubits.end());
auto last = std::unique(qubits.begin(), qubits.end());
qubits.erase(last, qubits.end());
// Find the position of the qubits we have in the result bit string
// Create a map of qubit to bit string location
std::unordered_map<std::size_t, std::size_t> qubitLocMap;
for (std::size_t i = 0; i < qubits.size(); i++) {
auto iter =
std::find(sampleQubits.begin(), sampleQubits.end(), qubits[i]);
auto idx = std::distance(sampleQubits.begin(), iter);
qubitLocMap.insert({qubits[i], idx});
}
cudaq::ExecutionResult tmp(regName);
for (auto &[bits, count] : execResult.counts) {
std::string b = "";
b.reserve(qubits.size());
for (auto &qb : qubits)
b += bits[qubitLocMap[qb]];
tmp.appendResult(b, count);
}
executionContext->result.append(tmp);
}
}
sampleQubits.clear();
registerNameToMeasuredQubit.clear();
}
/// @brief Add a new gate application task to the queue
void enqueueGate(const std::string name,
const std::vector<std::complex<ScalarType>> &matrix,
const std::vector<std::size_t> &controls,
const std::vector<std::size_t> &targets,
const std::vector<ScalarType> ¶ms) {
if (isInTracerMode()) {
std::vector<cudaq::QuditInfo> controlsInfo, targetsInfo;
for (auto &c : controls)
controlsInfo.emplace_back(2, c);
for (auto &t : targets)
targetsInfo.emplace_back(2, t);
std::vector<double> anglesProcessed;
if constexpr (std::is_same_v<ScalarType, double>)
anglesProcessed = params;
else {
for (auto &a : params)
anglesProcessed.push_back(static_cast<ScalarType>(a));
}
executionContext->kernelTrace.appendInstruction(
name, anglesProcessed, controlsInfo, targetsInfo);
return;
}
gateQueue.emplace(name, matrix, controls, targets, params);
}
/// @brief This pure virtual method is meant for subtypes
/// to implement, and its goal is to apply the gate described
/// by the GateApplicationTask to the subtype-specific state
/// data representation.
virtual void applyGate(const GateApplicationTask &task) = 0;
/// @brief Provide a base-class method that can be invoked
/// after every gate application and will apply any noise
/// channels after the gate invocation based on a user-provided noise
/// model. Unimplemented on the base class, sub-types can implement noise
/// modeling.
virtual void applyNoiseChannel(const std::string_view gateName,
const std::vector<std::size_t> &controls,
const std::vector<std::size_t> &targets,
const std::vector<double> ¶ms) {}
/// @brief Flush the gate queue, run all queued gate
/// application tasks.
void flushGateQueueImpl() override {
while (!gateQueue.empty()) {
auto &next = gateQueue.front();
if (isStateVectorSimulator() && summaryData.enabled)
summaryData.svGateUpdate(
next.controls.size(), next.targets.size(), stateDimension,
stateDimension * sizeof(std::complex<ScalarType>));
try {
applyGate(next);
} catch (std::exception &e) {
while (!gateQueue.empty())
gateQueue.pop();
throw e;
} catch (...) {
while (!gateQueue.empty())
gateQueue.pop();
throw std::runtime_error("Unknown exception in applyGate");
}
if (executionContext && executionContext->noiseModel) {
std::vector<double> params(next.parameters.begin(),
next.parameters.end());
applyNoiseChannel(next.operationName, next.controls, next.targets,
params);
}
gateQueue.pop();
}
// For CUDA-based simulators, this calls cudaDeviceSynchronize()
synchronize();
}
/// @brief Set the current state to the |0> state,
/// retaining the current number of qubits.
virtual void setToZeroState() = 0;
/// @brief Return true if expectation values should be computed from
/// sampling + parity of bit strings.
/// Default is to enable observe from sampling, i.e., simulating the
/// change-of-basis circuit for each term.
///
/// The environment variable "CUDAQ_OBSERVE_FROM_SAMPLING" can be used to turn
/// on or off this setting.
bool shouldObserveFromSampling(bool defaultConfig = true) {
if (auto envVar = std::getenv(observeSamplingEnvVar); envVar) {
std::string asString = envVar;
std::transform(asString.begin(), asString.end(), asString.begin(),
[](auto c) { return std::tolower(c); });
if (asString == "false" || asString == "off" || asString == "0")
return false;
if (asString == "true" || asString == "on" || asString == "1")
return true;
}
return defaultConfig;
}
bool isSinglePrecision() const override {
return std::is_same_v<ScalarType, float>;
}
/// @brief Return this simulator's qubit ordering.
virtual QubitOrdering getQubitOrdering() const { return QubitOrdering::lsb; }
public:
/// @brief The constructor
CircuitSimulatorBase() = default;
/// @brief The destructor
virtual ~CircuitSimulatorBase() = default;
/// @brief Create a simulation-specific SimulationState
/// instance from a user-provided data set.
std::unique_ptr<cudaq::SimulationState>
createStateFromData(const cudaq::state_data &data) override {
return getSimulationState()->createFromData(data);
}
/// @brief Set the current noise model to consider when
/// simulating the state. This should be overridden by
/// simulation strategies that support noise modeling.
void setNoiseModel(cudaq::noise_model &noise) override {
// Fixme consider this as a warning instead of a hard error
throw std::runtime_error(
"The current backend does not support noise modeling.");
}
/// @brief Compute the expected value of the given spin op
/// with respect to the current state, <psi | H | psi>.
cudaq::observe_result observe(const cudaq::spin_op &term) override {
throw std::runtime_error("This CircuitSimulator does not implement "
"observe(const cudaq::spin_op &).");
}
/// @brief Allocate a single qubit, return the qubit as a logical index
std::size_t allocateQubit() override {
// Get a new qubit index
auto newIdx = tracker.getNextIndex();
if (isInBatchMode()) {
batchModeCurrentNumQubits++;
// In batch mode, we might already have an allocated state that
// has been set to |0..0>. We can reuse it as is, if the next qubit
// index is smaller than number of qubits of this allocated state.
if (newIdx < nQubitsAllocated)
return newIdx;
}
cudaq::info("Allocating new qubit with idx {} (nQ={}, dim={})", newIdx,
nQubitsAllocated, stateDimension);
// Increment the number of qubits and set
// the new state dimension
previousStateDimension = stateDimension;
nQubitsAllocated++;
stateDimension = calculateStateDim(nQubitsAllocated);
if (!isInTracerMode())
// Tell the subtype to grow the state representation
addQubitToState();
// May be that the state grows enough that we
// want to handle observation via sampling
if (executionContext)
executionContext->canHandleObserve = canHandleObserve();
// return the new qubit index
return newIdx;
}
/// @brief Allocate `count` qubits.
std::vector<std::size_t>
allocateQubits(std::size_t count, const void *state = nullptr,
cudaq::simulation_precision precision =
cudaq::simulation_precision::fp32) override {
// Make sure if someone gives us state data, that the precision
// is correct for this simulation.
if (state != nullptr) {
if constexpr (std::is_same_v<ScalarType, float>) {
if (precision == cudaq::simulation_precision::fp64)
throw std::runtime_error(
"Invalid user-provided state data. Simulator "
"is FP32 but state data is FP64.");
} else {
if (precision == cudaq::simulation_precision::fp32)
throw std::runtime_error(
"Invalid user-provided state data. Simulator "
"is FP64 but state data is FP32.");
}
}
std::vector<std::size_t> qubits;
for (std::size_t i = 0; i < count; i++)
qubits.emplace_back(tracker.getNextIndex());
if (isInBatchMode()) {
// Store the current number of qubits requested
batchModeCurrentNumQubits += count;
// We have an allocated state, it has been set to |0>,
// we want to reuse it as is. If the state needs to grow, then
// we will ask the subtype to add more qubits.
if (qubits.back() < nQubitsAllocated)
count = 0;
else
count = qubits.back() + 1 - nQubitsAllocated;
}
cudaq::info("Allocating {} new qubits.", count);
previousStateDimension = stateDimension;
nQubitsAllocated += count;
stateDimension = calculateStateDim(nQubitsAllocated);
if (!isInTracerMode())
// Tell the subtype to allocate more qubits
addQubitsToState(count, state);
// May be that the state grows enough that we
// want to handle observation via sampling
if (executionContext)
executionContext->canHandleObserve = canHandleObserve();
return qubits;
}
/// @brief Allocate `count` qubits in a specific state.
std::vector<std::size_t>
allocateQubits(std::size_t count,
const cudaq::SimulationState *state) override {
if (!state)
return allocateQubits(count);
if (!isInTracerMode() && count != state->getNumQubits())
throw std::invalid_argument("Dimension mismatch: the input state doesn't "
"match the number of qubits");
std::vector<std::size_t> qubits;
for (std::size_t i = 0; i < count; i++)
qubits.emplace_back(tracker.getNextIndex());
if (isInBatchMode()) {
// Store the current number of qubits requested
batchModeCurrentNumQubits += count;
// We have an allocated state, it has been set to |0>,
// we want to reuse it as is. If the state needs to grow, then
// we will ask the subtype to add more qubits.
if (qubits.back() < nQubitsAllocated)
count = 0;
else
count = qubits.back() + 1 - nQubitsAllocated;
}
cudaq::info("Allocating {} new qubits.", count);
previousStateDimension = stateDimension;
nQubitsAllocated += count;
stateDimension = calculateStateDim(nQubitsAllocated);
if (!isInTracerMode())
// Tell the subtype to allocate more qubits
addQubitsToState(*state);
// May be that the state grows enough that we
// want to handle observation via sampling
if (executionContext)
executionContext->canHandleObserve = canHandleObserve();
return qubits;
}
/// @brief Deallocate the qubit with give index
void deallocate(const std::size_t qubitIdx) override {
if (executionContext && executionContext->name != "tracer") {
cudaq::info("Deferring qubit {} deallocation", qubitIdx);
deferredDeallocation.push_back(qubitIdx);
return;
}
cudaq::info("Deallocating qubit {}", qubitIdx);
// Reset the qubit
if (!isInTracerMode())
resetQubit(qubitIdx);
// Return the index to the tracker
tracker.returnIndex(qubitIdx);
--nQubitsAllocated;
// Reset the state if we've deallocated all qubits.
if (tracker.allDeallocated()) {
cudaq::info("Deallocated all qubits, reseting state vector.");
// all qubits deallocated,
deallocateState();
while (!gateQueue.empty())