-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuantumComputation.hpp
More file actions
1154 lines (1049 loc) · 43.9 KB
/
QuantumComputation.hpp
File metadata and controls
1154 lines (1049 loc) · 43.9 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
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
#pragma once
#include "Definitions.hpp"
#include "operations/ClassicControlledOperation.hpp"
#include "operations/NonUnitaryOperation.hpp"
#include "operations/StandardOperation.hpp"
#include "operations/SymbolicOperation.hpp"
#include "parsers/qasm_parser/Parser.hpp"
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <locale>
#include <map>
#include <memory>
#include <optional>
#include <random>
#include <sstream>
#include <string>
#include <vector>
namespace qc {
class CircuitOptimizer;
class QuantumComputation {
public:
using iterator = typename std::vector<std::unique_ptr<Operation>>::iterator;
using const_iterator =
typename std::vector<std::unique_ptr<Operation>>::const_iterator;
friend class CircuitOptimizer;
protected:
std::vector<std::unique_ptr<Operation>> ops{};
std::size_t nqubits = 0;
std::size_t nclassics = 0;
std::size_t nancillae = 0;
std::size_t maxControls = 0;
std::string name;
// register names are used as keys, while the values are `{startIndex,
// length}` pairs
QuantumRegisterMap qregs{};
ClassicalRegisterMap cregs{};
QuantumRegisterMap ancregs{};
std::mt19937_64 mt;
std::size_t seed = 0;
fp globalPhase = 0.;
std::unordered_set<sym::Variable> occuringVariables;
void importOpenQASM(std::istream& is);
void importReal(std::istream& is);
int readRealHeader(std::istream& is);
void readRealGateDescriptions(std::istream& is, int line);
void importTFC(std::istream& is);
int readTFCHeader(std::istream& is, std::map<std::string, Qubit>& varMap);
void readTFCGateDescriptions(std::istream& is, int line,
std::map<std::string, Qubit>& varMap);
void importQC(std::istream& is);
int readQCHeader(std::istream& is, std::map<std::string, Qubit>& varMap);
void readQCGateDescriptions(std::istream& is, int line,
std::map<std::string, Qubit>& varMap);
void importGRCS(std::istream& is);
template <class RegisterType>
static void printSortedRegisters(const RegisterMap<RegisterType>& regmap,
const std::string& identifier,
std::ostream& of) {
// sort regs by start index
std::map<decltype(RegisterType::first),
std::pair<std::string, RegisterType>>
sortedRegs{};
for (const auto& reg : regmap) {
sortedRegs.insert({reg.second.first, reg});
}
for (const auto& reg : sortedRegs) {
of << identifier << " " << reg.second.first << "["
<< reg.second.second.second << "];" << std::endl;
}
}
template <class RegisterType>
static void consolidateRegister(RegisterMap<RegisterType>& regs) {
bool finished = false;
while (!finished) {
for (const auto& qreg : regs) {
finished = true;
auto regname = qreg.first;
// check if lower part of register
if (regname.length() > 2 &&
regname.compare(regname.size() - 2, 2, "_l") == 0) {
auto lowidx = qreg.second.first;
auto lownum = qreg.second.second;
// search for higher part of register
auto highname = regname.substr(0, regname.size() - 1) + 'h';
auto it = regs.find(highname);
if (it != regs.end()) {
auto highidx = it->second.first;
auto highnum = it->second.second;
// fusion of registers possible
if (lowidx + lownum == highidx) {
finished = false;
auto targetname = regname.substr(0, regname.size() - 2);
auto targetidx = lowidx;
auto targetnum = lownum + highnum;
regs.insert({targetname, {targetidx, targetnum}});
regs.erase(regname);
regs.erase(highname);
}
}
break;
}
}
}
}
template <class RegisterType>
static void createRegisterArray(const RegisterMap<RegisterType>& regs,
RegisterNames& regnames,
decltype(RegisterType::second) defaultnumber,
const std::string& defaultname) {
regnames.clear();
std::stringstream ss;
if (!regs.empty()) {
// sort regs by start index
std::map<decltype(RegisterType::first),
std::pair<std::string, RegisterType>>
sortedRegs{};
for (const auto& reg : regs) {
sortedRegs.insert({reg.second.first, reg});
}
for (const auto& reg : sortedRegs) {
for (decltype(RegisterType::second) i = 0; i < reg.second.second.second;
i++) {
ss << reg.second.first << "[" << i << "]";
regnames.push_back(std::make_pair(reg.second.first, ss.str()));
ss.str(std::string());
}
}
} else {
for (decltype(RegisterType::second) i = 0; i < defaultnumber; i++) {
ss << defaultname << "[" << i << "]";
regnames.emplace_back(defaultname, ss.str());
ss.str(std::string());
}
}
}
[[nodiscard]] std::size_t getSmallestAncillary() const {
for (std::size_t i = 0; i < ancillary.size(); ++i) {
if (ancillary[i]) {
return i;
}
}
return ancillary.size();
}
[[nodiscard]] std::size_t getSmallestGarbage() const {
for (std::size_t i = 0; i < garbage.size(); ++i) {
if (garbage[i]) {
return i;
}
}
return garbage.size();
}
[[nodiscard]] bool isLastOperationOnQubit(const const_iterator& opIt) const {
const auto end = ops.cend();
return isLastOperationOnQubit(opIt, end);
}
void checkQubitRange(Qubit qubit) const;
void checkQubitRange(Qubit qubit, const Controls& controls) const;
void checkQubitRange(Qubit qubit0, Qubit qubit1,
const Controls& controls) const;
void checkQubitRange(const std::vector<Qubit>& qubits) const;
public:
QuantumComputation() = default;
explicit QuantumComputation(const std::size_t nq, const std::size_t s = 0)
: seed(s) {
addQubitRegister(nq);
addClassicalRegister(nq);
if (seed != 0) {
mt.seed(seed);
} else {
// create and properly seed rng
std::array<std::mt19937_64::result_type, std::mt19937_64::state_size>
randomData{};
std::random_device rd;
std::generate(std::begin(randomData), std::end(randomData),
[&rd]() { return rd(); });
std::seed_seq seeds(std::begin(randomData), std::end(randomData));
mt.seed(seeds);
}
}
explicit QuantumComputation(const std::string& filename,
const std::size_t s = 0U)
: seed(s) {
import(filename);
if (seed != 0U) {
mt.seed(seed);
} else {
// create and properly seed rng
std::array<std::mt19937_64::result_type, std::mt19937_64::state_size>
randomData{};
std::random_device rd;
std::generate(std::begin(randomData), std::end(randomData),
[&rd]() { return rd(); });
std::seed_seq seeds(std::begin(randomData), std::end(randomData));
mt.seed(seeds);
}
}
QuantumComputation(const QuantumComputation& qc) = delete;
QuantumComputation(QuantumComputation&& qc) noexcept = default;
QuantumComputation& operator=(const QuantumComputation& qc) = delete;
QuantumComputation& operator=(QuantumComputation&& qc) noexcept = default;
virtual ~QuantumComputation() = default;
[[nodiscard]] QuantumComputation clone() const {
auto qc = QuantumComputation(nqubits);
qc.nqubits = nqubits;
qc.nclassics = nclassics;
qc.nancillae = nancillae;
qc.maxControls = maxControls;
qc.name = name;
qc.qregs = qregs;
qc.cregs = cregs;
qc.ancregs = ancregs;
qc.initialLayout = initialLayout;
qc.outputPermutation = outputPermutation;
qc.ancillary = ancillary;
qc.garbage = garbage;
qc.seed = seed;
qc.mt = mt;
qc.occuringVariables = occuringVariables;
qc.globalPhase = globalPhase;
for (auto const& op : ops) {
qc.ops.emplace_back<>(op->clone());
}
return qc;
}
[[nodiscard]] virtual std::size_t getNops() const { return ops.size(); }
[[nodiscard]] std::size_t getNqubits() const { return nqubits + nancillae; }
[[nodiscard]] std::size_t getNancillae() const { return nancillae; }
[[nodiscard]] std::size_t getNqubitsWithoutAncillae() const {
return nqubits;
}
[[nodiscard]] std::size_t getNcbits() const { return nclassics; }
[[nodiscard]] std::string getName() const { return name; }
[[nodiscard]] const QuantumRegisterMap& getQregs() const { return qregs; }
[[nodiscard]] const ClassicalRegisterMap& getCregs() const { return cregs; }
[[nodiscard]] const QuantumRegisterMap& getANCregs() const { return ancregs; }
[[nodiscard]] decltype(mt)& getGenerator() { return mt; }
[[nodiscard]] fp getGlobalPhase() const { return globalPhase; }
void setName(const std::string& n) { name = n; }
// physical qubits are used as keys, logical qubits as values
Permutation initialLayout{};
Permutation outputPermutation{};
std::vector<bool> ancillary{};
std::vector<bool> garbage{};
[[nodiscard]] std::size_t getNindividualOps() const;
[[nodiscard]] std::size_t getNsingleQubitOps() const;
[[nodiscard]] std::size_t getDepth() const;
[[nodiscard]] std::string getQubitRegister(Qubit physicalQubitIndex) const;
[[nodiscard]] std::string getClassicalRegister(Bit classicalIndex) const;
static Qubit getHighestLogicalQubitIndex(const Permutation& permutation);
[[nodiscard]] Qubit getHighestLogicalQubitIndex() const {
return getHighestLogicalQubitIndex(initialLayout);
};
[[nodiscard]] std::pair<std::string, Qubit>
getQubitRegisterAndIndex(Qubit physicalQubitIndex) const;
[[nodiscard]] std::pair<std::string, Bit>
getClassicalRegisterAndIndex(Bit classicalIndex) const;
[[nodiscard]] Qubit
getIndexFromQubitRegister(const std::pair<std::string, Qubit>& qubit) const;
[[nodiscard]] Bit getIndexFromClassicalRegister(
const std::pair<std::string, std::size_t>& clbit) const;
[[nodiscard]] bool isIdleQubit(Qubit physicalQubit) const;
[[nodiscard]] bool isLastOperationOnQubit(const const_iterator& opIt,
const const_iterator& end) const;
[[nodiscard]] bool physicalQubitIsAncillary(Qubit physicalQubitIndex) const;
[[nodiscard]] bool
logicalQubitIsAncillary(const Qubit logicalQubitIndex) const {
return ancillary[logicalQubitIndex];
}
void setLogicalQubitAncillary(const Qubit logicalQubitIndex) {
ancillary[logicalQubitIndex] = true;
}
[[nodiscard]] bool
logicalQubitIsGarbage(const Qubit logicalQubitIndex) const {
return garbage[logicalQubitIndex];
}
void setLogicalQubitGarbage(Qubit logicalQubitIndex);
[[nodiscard]] const std::vector<bool>& getAncillary() const {
return ancillary;
}
[[nodiscard]] const std::vector<bool>& getGarbage() const { return garbage; }
/// checks whether the given logical qubit exists in the initial layout.
/// \param logicalQubitIndex the logical qubit index to check
/// \return whether the given logical qubit exists in the initial layout and
/// to which physical qubit it is mapped
[[nodiscard]] std::pair<bool, std::optional<Qubit>>
containsLogicalQubit(Qubit logicalQubitIndex) const;
/// Adds a global phase to the quantum circuit.
/// \param angle the angle to add
void gphase(const fp& angle) {
globalPhase += angle;
// normalize to [0, 2pi)
while (globalPhase < 0) {
globalPhase += 2 * PI;
}
while (globalPhase >= 2 * PI) {
globalPhase -= 2 * PI;
}
}
void i(const Qubit target) { i(target, Controls{}); }
void i(const Qubit target, const Control& control) {
i(target, Controls{control});
}
void i(const Qubit target, const Controls& controls) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::I);
}
void h(const Qubit target) { h(target, Controls{}); }
void h(const Qubit target, const Control& control) {
h(target, Controls{control});
}
void h(const Qubit target, const Controls& controls) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::H);
}
void x(const Qubit target) { x(target, Controls{}); }
void x(const Qubit target, const Control& control) {
x(target, Controls{control});
}
void x(const Qubit target, const Controls& controls) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::X);
}
void y(const Qubit target) { y(target, Controls{}); }
void y(const Qubit target, const Control& control) {
y(target, Controls{control});
}
void y(const Qubit target, const Controls& controls) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::Y);
}
void z(const Qubit target) { z(target, Controls{}); }
void z(const Qubit target, const Control& control) {
z(target, Controls{control});
}
void z(const Qubit target, const Controls& controls) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::Z);
}
void s(const Qubit target) { s(target, Controls{}); }
void s(const Qubit target, const Control& control) {
s(target, Controls{control});
}
void s(const Qubit target, const Controls& controls) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::S);
}
void sdag(const Qubit target) { sdag(target, Controls{}); }
void sdag(const Qubit target, const Control& control) {
sdag(target, Controls{control});
}
void sdag(const Qubit target, const Controls& controls) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::Sdag);
}
void t(const Qubit target) { t(target, Controls{}); }
void t(const Qubit target, const Control& control) {
t(target, Controls{control});
}
void t(const Qubit target, const Controls& controls) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::T);
}
void tdag(const Qubit target) { tdag(target, Controls{}); }
void tdag(const Qubit target, const Control& control) {
tdag(target, Controls{control});
}
void tdag(const Qubit target, const Controls& controls) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::Tdag);
}
void v(const Qubit target) { v(target, Controls{}); }
void v(const Qubit target, const Control& control) {
v(target, Controls{control});
}
void v(const Qubit target, const Controls& controls) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::V);
}
void vdag(const Qubit target) { vdag(target, Controls{}); }
void vdag(const Qubit target, const Control& control) {
vdag(target, Controls{control});
}
void vdag(const Qubit target, const Controls& controls) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::Vdag);
}
void u3(const Qubit target, const fp theta, const fp phi, const fp lambda) {
u3(target, Controls{}, theta, phi, lambda);
}
void u3(const Qubit target, const Control& control, const fp theta,
const fp phi, const fp lambda) {
u3(target, Controls{control}, theta, phi, lambda);
}
void u3(const Qubit target, const Controls& controls, const fp theta,
const fp phi, const fp lambda) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::U3,
std::vector{theta, phi, lambda});
}
void u3(const Qubit target, const SymbolOrNumber& theta,
const SymbolOrNumber& phi, const SymbolOrNumber& lambda) {
u3(target, Controls{}, theta, phi, lambda);
}
void u3(const Qubit target, const Control& control,
const SymbolOrNumber& theta, const SymbolOrNumber& phi,
const SymbolOrNumber& lambda) {
u3(target, Controls{control}, theta, phi, lambda);
}
void u3(const Qubit target, const Controls& controls,
const SymbolOrNumber& theta, const SymbolOrNumber& phi,
const SymbolOrNumber& lambda) {
checkQubitRange(target, controls);
addVariables(theta, phi, lambda);
emplace_back<SymbolicOperation>(getNqubits(), controls, target, qc::U3,
std::vector{theta, phi, lambda});
}
void u2(const Qubit target, const fp phi, const fp lambda) {
u2(target, Controls{}, phi, lambda);
}
void u2(const Qubit target, const Control& control, const fp phi,
const fp lambda) {
u2(target, Controls{control}, phi, lambda);
}
void u2(const Qubit target, const Controls& controls, const fp phi,
const fp lambda) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::U2,
std::vector{phi, lambda});
}
void u2(const Qubit target, const SymbolOrNumber& phi,
const SymbolOrNumber& lambda) {
u2(target, Controls{}, phi, lambda);
}
void u2(const Qubit target, const Control& control, const SymbolOrNumber& phi,
const SymbolOrNumber& lambda) {
u2(target, Controls{control}, phi, lambda);
}
void u2(const Qubit target, const Controls& controls,
const SymbolOrNumber& phi, const SymbolOrNumber& lambda) {
checkQubitRange(target, controls);
addVariables(phi, lambda);
emplace_back<SymbolicOperation>(getNqubits(), controls, target, qc::U2,
std::vector{phi, lambda});
}
void phase(const Qubit target, const fp lambda) {
phase(target, Controls{}, lambda);
}
void phase(const Qubit target, const Control& control, const fp lambda) {
phase(target, Controls{control}, lambda);
}
void phase(const Qubit target, const Controls& controls, const fp lambda) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::Phase,
std::vector{lambda});
}
void phase(const Qubit target, const SymbolOrNumber& lambda) {
phase(target, Controls{}, lambda);
}
void phase(const Qubit target, const Control& control,
const SymbolOrNumber& lambda) {
phase(target, Controls{control}, lambda);
}
void phase(const Qubit target, const Controls& controls,
const SymbolOrNumber& lambda) {
checkQubitRange(target, controls);
addVariables(lambda);
emplace_back<SymbolicOperation>(getNqubits(), controls, target, qc::Phase,
std::vector{lambda});
}
void sx(const Qubit target) { sx(target, Controls{}); }
void sx(const Qubit target, const Control& control) {
sx(target, Controls{control});
}
void sx(const Qubit target, const Controls& controls) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::SX);
}
void sxdag(const Qubit target) { sxdag(target, Controls{}); }
void sxdag(const Qubit target, const Control& control) {
sxdag(target, Controls{control});
}
void sxdag(const Qubit target, const Controls& controls) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::SXdag);
}
void rx(const Qubit target, const fp lambda) {
rx(target, Controls{}, lambda);
}
void rx(const Qubit target, const Control& control, const fp lambda) {
rx(target, Controls{control}, lambda);
}
void rx(const Qubit target, const Controls& controls, const fp lambda) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::RX,
std::vector{lambda});
}
void rx(const Qubit target, const SymbolOrNumber& lambda) {
rx(target, Controls{}, lambda);
}
void rx(const Qubit target, const Control& control,
const SymbolOrNumber& lambda) {
rx(target, Controls{control}, lambda);
}
void rx(const Qubit target, const Controls& controls,
const SymbolOrNumber& lambda) {
checkQubitRange(target, controls);
addVariables(lambda);
emplace_back<SymbolicOperation>(getNqubits(), controls, target, qc::RX,
std::vector{lambda});
}
void ry(const Qubit target, const fp lambda) {
ry(target, Controls{}, lambda);
}
void ry(const Qubit target, const Control& control, const fp lambda) {
ry(target, Controls{control}, lambda);
}
void ry(const Qubit target, const Controls& controls, const fp lambda) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::RY,
std::vector{lambda});
}
void ry(const Qubit target, const SymbolOrNumber& lambda) {
ry(target, Controls{}, lambda);
}
void ry(const Qubit target, const Control& control,
const SymbolOrNumber& lambda) {
ry(target, Controls{control}, lambda);
}
void ry(const Qubit target, const Controls& controls,
const SymbolOrNumber& lambda) {
checkQubitRange(target, controls);
addVariables(lambda);
emplace_back<SymbolicOperation>(getNqubits(), controls, target, qc::RY,
std::vector{lambda});
}
void rz(const Qubit target, const fp lambda) {
rz(target, Controls{}, lambda);
}
void rz(const Qubit target, const Control& control, const fp lambda) {
rz(target, Controls{control}, lambda);
}
void rz(const Qubit target, const Controls& controls, const fp lambda) {
checkQubitRange(target, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target, qc::RZ,
std::vector{lambda});
}
void rz(const Qubit target, const SymbolOrNumber& lambda) {
rz(target, Controls{}, lambda);
}
void rz(const Qubit target, const Control& control,
const SymbolOrNumber& lambda) {
rz(target, Controls{control}, lambda);
}
void rz(const Qubit target, const Controls& controls,
const SymbolOrNumber& lambda) {
checkQubitRange(target, controls);
addVariables(lambda);
emplace_back<SymbolicOperation>(getNqubits(), controls, target, qc::RZ,
std::vector{lambda});
}
void swap(const Qubit target0, const Qubit target1) {
swap(target0, target1, Controls{});
}
void swap(const Qubit target0, const Qubit target1, const Control& control) {
swap(target0, target1, Controls{control});
}
void swap(const Qubit target0, const Qubit target1,
const Controls& controls) {
checkQubitRange(target0, target1, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target0, target1,
qc::SWAP);
}
void iswap(const Qubit target0, const Qubit target1) {
iswap(target0, target1, Controls{});
}
void iswap(const Qubit target0, const Qubit target1, const Control& control) {
iswap(target0, target1, Controls{control});
}
void iswap(const Qubit target0, const Qubit target1,
const Controls& controls) {
checkQubitRange(target0, target1, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target0, target1,
qc::iSWAP);
}
void peres(const Qubit target0, const Qubit target1) {
peres(target0, target1, Controls{});
}
void peres(const Qubit target0, const Qubit target1, const Control& control) {
peres(target0, target1, Controls{control});
}
void peres(const Qubit target0, const Qubit target1,
const Controls& controls) {
checkQubitRange(target0, target1, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target0, target1,
qc::Peres);
}
void peresdag(const Qubit target0, const Qubit target1) {
peresdag(target0, target1, Controls{});
}
void peresdag(const Qubit target0, const Qubit target1,
const Control& control) {
peresdag(target0, target1, Controls{control});
}
void peresdag(const Qubit target0, const Qubit target1,
const Controls& controls) {
checkQubitRange(target0, target1, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target0, target1,
qc::Peresdag);
}
void dcx(const Qubit target0, const Qubit target1) {
dcx(target0, target1, Controls{});
}
void dcx(const Qubit target0, const Qubit target1, const Control& control) {
dcx(target0, target1, Controls{control});
}
void dcx(const Qubit target0, const Qubit target1, const Controls& controls) {
checkQubitRange(target0, target1, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target0, target1,
qc::DCX);
}
void ecr(const Qubit target0, const Qubit target1) {
ecr(target0, target1, Controls{});
}
void ecr(const Qubit target0, const Qubit target1, const Control& control) {
ecr(target0, target1, Controls{control});
}
void ecr(const Qubit target0, const Qubit target1, const Controls& controls) {
checkQubitRange(target0, target1, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target0, target1,
qc::ECR);
}
void rxx(const Qubit target0, const Qubit target1, const fp theta) {
rxx(target0, target1, Controls{}, theta);
}
void rxx(const Qubit target0, const Qubit target1, const Control& control,
const fp theta) {
rxx(target0, target1, Controls{control}, theta);
}
void rxx(const Qubit target0, const Qubit target1, const Controls& controls,
const fp theta) {
checkQubitRange(target0, target1, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target0, target1,
qc::RXX, std::vector{theta});
}
void rxx(const Qubit target0, const Qubit target1,
const SymbolOrNumber& theta) {
rxx(target0, target1, Controls{}, theta);
}
void rxx(const Qubit target0, const Qubit target1, const Control& control,
const SymbolOrNumber& theta) {
rxx(target0, target1, Controls{control}, theta);
}
void rxx(const Qubit target0, const Qubit target1, const Controls& controls,
const SymbolOrNumber& theta) {
checkQubitRange(target0, target1, controls);
addVariables(theta);
emplace_back<SymbolicOperation>(getNqubits(), controls, target0, target1,
qc::RXX, std::vector{theta});
}
void ryy(const Qubit target0, const Qubit target1, const fp theta) {
ryy(target0, target1, Controls{}, theta);
}
void ryy(const Qubit target0, const Qubit target1, const Control& control,
const fp theta) {
ryy(target0, target1, Controls{control}, theta);
}
void ryy(const Qubit target0, const Qubit target1, const Controls& controls,
const fp theta) {
checkQubitRange(target0, target1, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target0, target1,
qc::RYY, std::vector{theta});
}
void ryy(const Qubit target0, const Qubit target1,
const SymbolOrNumber& theta) {
ryy(target0, target1, Controls{}, theta);
}
void ryy(const Qubit target0, const Qubit target1, const Control& control,
const SymbolOrNumber& theta) {
ryy(target0, target1, Controls{control}, theta);
}
void ryy(const Qubit target0, const Qubit target1, const Controls& controls,
const SymbolOrNumber& theta) {
checkQubitRange(target0, target1, controls);
addVariables(theta);
emplace_back<SymbolicOperation>(getNqubits(), controls, target0, target1,
qc::RYY, std::vector{theta});
}
void rzz(const Qubit target0, const Qubit target1, const fp theta) {
rzz(target0, target1, Controls{}, theta);
}
void rzz(const Qubit target0, const Qubit target1, const Control& control,
const fp theta) {
rzz(target0, target1, Controls{control}, theta);
}
void rzz(const Qubit target0, const Qubit target1, const Controls& controls,
const fp theta) {
checkQubitRange(target0, target1, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target0, target1,
qc::RZZ, std::vector{theta});
}
void rzz(const Qubit target0, const Qubit target1,
const SymbolOrNumber& theta) {
rzz(target0, target1, Controls{}, theta);
}
void rzz(const Qubit target0, const Qubit target1, const Control& control,
const SymbolOrNumber& theta) {
rzz(target0, target1, Controls{control}, theta);
}
void rzz(const Qubit target0, const Qubit target1, const Controls& controls,
const SymbolOrNumber& theta) {
checkQubitRange(target0, target1, controls);
addVariables(theta);
emplace_back<SymbolicOperation>(getNqubits(), controls, target0, target1,
qc::RZZ, std::vector{theta});
}
void rzx(const Qubit target0, const Qubit target1, const fp theta) {
rzx(target0, target1, Controls{}, theta);
}
void rzx(const Qubit target0, const Qubit target1, const Control& control,
const fp theta) {
rzx(target0, target1, Controls{control}, theta);
}
void rzx(const Qubit target0, const Qubit target1, const Controls& controls,
const fp theta) {
checkQubitRange(target0, target1, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target0, target1,
qc::RZX, std::vector{theta});
}
void rzx(const Qubit target0, const Qubit target1,
const SymbolOrNumber& theta) {
rzx(target0, target1, Controls{}, theta);
}
void rzx(const Qubit target0, const Qubit target1, const Control& control,
const SymbolOrNumber& theta) {
rzx(target0, target1, Controls{control}, theta);
}
void rzx(const Qubit target0, const Qubit target1, const Controls& controls,
const SymbolOrNumber& theta) {
checkQubitRange(target0, target1, controls);
addVariables(theta);
emplace_back<SymbolicOperation>(getNqubits(), controls, target0, target1,
qc::RZX, std::vector{theta});
}
// NOLINTBEGIN(readability-identifier-naming)
void xx_minus_yy(const Qubit target0, const Qubit target1, const fp theta,
const fp beta) {
xx_minus_yy(target0, target1, Controls{}, theta, beta);
}
void xx_minus_yy(const Qubit target0, const Qubit target1,
const Control& control, const fp theta, const fp beta) {
xx_minus_yy(target0, target1, Controls{control}, theta, beta);
}
void xx_minus_yy(const Qubit target0, const Qubit target1,
const Controls& controls, const fp theta, const fp beta) {
checkQubitRange(target0, target1, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target0, target1,
qc::XXminusYY, std::vector{theta, beta});
}
void xx_minus_yy(const Qubit target0, const Qubit target1,
const SymbolOrNumber& theta, const SymbolOrNumber& beta) {
xx_minus_yy(target0, target1, Controls{}, theta, beta);
}
void xx_minus_yy(const Qubit target0, const Qubit target1,
const Control& control, const SymbolOrNumber& theta,
const SymbolOrNumber& beta) {
xx_minus_yy(target0, target1, Controls{control}, theta, beta);
}
void xx_minus_yy(const Qubit target0, const Qubit target1,
const Controls& controls, const SymbolOrNumber& theta,
const SymbolOrNumber& beta) {
checkQubitRange(target0, target1, controls);
addVariables(theta, beta);
emplace_back<SymbolicOperation>(getNqubits(), controls, target0, target1,
qc::XXminusYY, std::vector{theta, beta});
}
void xx_plus_yy(const Qubit target0, const Qubit target1, const fp theta,
const fp beta) {
xx_plus_yy(target0, target1, Controls{}, theta, beta);
}
void xx_plus_yy(const Qubit target0, const Qubit target1,
const Control& control, const fp theta, const fp beta) {
xx_plus_yy(target0, target1, Controls{control}, theta, beta);
}
void xx_plus_yy(const Qubit target0, const Qubit target1,
const Controls& controls, const fp theta, const fp beta) {
checkQubitRange(target0, target1, controls);
emplace_back<StandardOperation>(getNqubits(), controls, target0, target1,
qc::XXplusYY, std::vector{theta, beta});
}
void xx_plus_yy(const Qubit target0, const Qubit target1,
const SymbolOrNumber& theta, const SymbolOrNumber& beta) {
xx_plus_yy(target0, target1, Controls{}, theta, beta);
}
void xx_plus_yy(const Qubit target0, const Qubit target1,
const Control& control, const SymbolOrNumber& theta,
const SymbolOrNumber& beta) {
xx_plus_yy(target0, target1, Controls{control}, theta, beta);
}
void xx_plus_yy(const Qubit target0, const Qubit target1,
const Controls& controls, const SymbolOrNumber& theta,
const SymbolOrNumber& beta) {
checkQubitRange(target0, target1, controls);
addVariables(theta, beta);
emplace_back<SymbolicOperation>(getNqubits(), controls, target0, target1,
qc::XXplusYY, std::vector{theta, beta});
}
// NOLINTEND(readability-identifier-naming)
void measure(const Qubit qubit, const std::size_t clbit) {
checkQubitRange(qubit);
emplace_back<NonUnitaryOperation>(getNqubits(), qubit, clbit);
}
void measure(const Qubit qubit, const std::pair<std::string, Bit>& clbit) {
checkQubitRange(qubit);
if (const auto cRegister = cregs.find(clbit.first);
cRegister != cregs.end()) {
if (clbit.second >= cRegister->second.second) {
std::cerr << "The classical register \"" << clbit.first
<< "\" is too small!" << std::endl;
}
emplace_back<NonUnitaryOperation>(getNqubits(), qubit,
cRegister->second.first + clbit.second);
} else {
std::cerr << "The classical register \"" << clbit.first
<< "\" does not exist!" << std::endl;
}
}
void measure(const std::vector<Qubit>& qubitRegister,
const std::vector<Bit>& classicalRegister) {
checkQubitRange(qubitRegister);
emplace_back<NonUnitaryOperation>(getNqubits(), qubitRegister,
classicalRegister);
}
void reset(const Qubit target) {
checkQubitRange(target);
emplace_back<NonUnitaryOperation>(getNqubits(), std::vector<Qubit>{target},
qc::Reset);
}
void reset(const std::vector<Qubit>& targets) {
checkQubitRange(targets);
emplace_back<NonUnitaryOperation>(getNqubits(), targets, qc::Reset);
}
void barrier(const Qubit target) {
checkQubitRange(target);
emplace_back<NonUnitaryOperation>(getNqubits(), std::vector<Qubit>{target},
qc::Barrier);
}
void barrier(const std::vector<Qubit>& targets) {
checkQubitRange(targets);
emplace_back<NonUnitaryOperation>(getNqubits(), targets, qc::Barrier);
}
void classicControlled(const OpType op, const Qubit target,
const ClassicalRegister& controlRegister,
const std::uint64_t expectedValue = 1U,
const std::vector<fp>& params = {}) {
classicControlled(op, target, Controls{}, controlRegister, expectedValue,
params);
}
void classicControlled(const OpType op, const Qubit target,
const Control control,
const ClassicalRegister& controlRegister,
const std::uint64_t expectedValue = 1U,
const std::vector<fp>& params = {}) {
classicControlled(op, target, Controls{control}, controlRegister,
expectedValue, params);
}
void classicControlled(const OpType op, const Qubit target,
const Controls& controls,
const ClassicalRegister& controlRegister,
const std::uint64_t expectedValue = 1U,
const std::vector<fp>& params = {}) {
checkQubitRange(target, controls);
std::unique_ptr<Operation> gate = std::make_unique<StandardOperation>(
getNqubits(), controls, target, op, params);
emplace_back<ClassicControlledOperation>(std::move(gate), controlRegister,
expectedValue);
}
/// strip away qubits with no operations applied to them and which do not pop
/// up in the output permutation \param force if true, also strip away idle
/// qubits occurring in the output permutation
void stripIdleQubits(bool force = false, bool reduceIOpermutations = true);
void import(const std::string& filename);
void import(const std::string& filename, Format format);
void import(std::istream& is, Format format) {
import(std::move(is), format);
}
void import(std::istream&& is, Format format);
void initializeIOMapping();
// append measurements to the end of the circuit according to the tracked
// output permutation
void appendMeasurementsAccordingToOutputPermutation(
const std::string& registerName = "c");
// search for current position of target value in map and afterwards exchange
// it with the value at new position
static void findAndSWAP(Qubit targetValue, Qubit newPosition,
Permutation& map) {
for (const auto& q : map) {
if (q.second == targetValue) {
std::swap(map.at(newPosition), map.at(q.first));
break;
}
}
}
// this function augments a given circuit by additional registers
void addQubitRegister(std::size_t, const std::string& regName = "q");
void addClassicalRegister(std::size_t nc, const std::string& regName = "c");
void addAncillaryRegister(std::size_t nq, const std::string& regName = "anc");
// a function to combine all quantum registers (qregs and ancregs) into a
// single register (useful for circuits mapped to a device)
void unifyQuantumRegisters(const std::string& regName = "q");
// removes a specific logical qubit and returns the index of the physical
// qubit in the initial layout as well as the index of the removed physical
// qubit's output permutation i.e., initialLayout[physical_qubit] =
// logical_qubit and outputPermutation[physicalQubit] = output_qubit
std::pair<Qubit, std::optional<Qubit>> removeQubit(Qubit logicalQubitIndex);
// adds physical qubit as ancillary qubit and gives it the appropriate output
// mapping
void addAncillaryQubit(Qubit physicalQubitIndex,
std::optional<Qubit> outputQubitIndex);
// try to add logical qubit to circuit and assign it to physical qubit with
// certain output permutation value