-
Notifications
You must be signed in to change notification settings - Fork 0
/
quantum.h
1655 lines (1458 loc) · 47.4 KB
/
quantum.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
/*******************************************************
* Copyright (c) 2022, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
/**
* @file quantum.h
* @brief Provides the core implementation of the AQS Library functions and classes
*
*/
#pragma once
#include <arrayfire.h>
#include "utils.h"
#include "version.h"
#include <array>
#include <cassert>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
/**
*
* ******* Arrayfire Quantum Simulator AQS **********
*
* ////// How to use //////
* 1. Initialize the library by calling `aqs::initialize()` and passing the
* command line arguments to it
*
* e.g. int main(int argc, char** argv) {
* ...
* aqs::initialize(argc, argv);
* ...
* }
*
*
* 2. Create a Quantum Circuit by using the class `aqs::QCircuit`.
* Specify the number of qubits by passing it to the contructor
*
* e.g. aqs::QCircuit qc{2};
*
* 3. Insert gates by using the `<<` (left shift) operator with the Quantum
* Circuit at the left and any added gates at the right. To create the gates,
* call the constructor of each gate. Depending on the gate being added, you may
* need to specify to which qubit the gate wil be added.
*
* e.g. qc << aqs::H{0} << aqs::CNOT{0, 1};
*
* 4. Call the QCircuit member function `aqs::QCircuit::compile` to generate the
* internal matrix representation of the circuit. This step is not required if
* the internal matrix representation of the circuit is not needed. Note that
* this function can be call anywhere, but it will only generate the matrix of
* the all gates that have been added prior to that point. This function
* executes the compilation (math calculations) of the circuit, so it is
* performance intensive; for more than 10 qubits it might take a while.
*
* e.g. qc.compile();
*
* 5. To simulate, create the context of the quantum simulator with the class
* `aqs::QSimulator` It must be initialized with the same number of qubits as
* the circuit, and can be added an initial state to setup the statevector in.
*
* e.g. aqs::QSimulator qs{2, aqs::QState::one()}; // All the qubits are set
* into an inital |1> state
*
* 6. Finally, to execute the simulation, call the QSimulator Member function
* `aqs::QSimulator::simulate` using the simulator created and pass the circuit
* as the argument. This will result in a complete Quantum Computer Simulation
*
* Note that this also a performance intensive function, and the result of
* the simulation is stored in the QSimultor
*
* e.g. qs.simulate(qc);
*
*
* 7. To obtain information from the simulation, you may use
* `aqs::QSimulator::statevector` to obtain the statevector, call
* `aqs::QSimulator::measure` to collapse the state of a given qubit and return
* the output; `aqs::QSimulator::measure_all` to collapse the state of all the
* qubits and return it as binary string; `aqs::QSimulator::peek_measure` and
* `aqs::QSimulator::peek_measure_all()` for the non-collapsing/non-affecting
* versions for it; and `aqs::QSimulator::profile_measure` and
* `aqs::QSimulator::profile_measure_all` to execute a random measurements
* multiple times
*
* e.g. auto result = qs.peek_measure_all(); // get one random result for all
* the qubit measured states auto profile = qs.profile_measure(2, 100); //
* measure 100 times the collpased state of the simulation of qubit 2
*
*
* ////// Important Implementation Details //////
* - The internal simulation of a Quantum Computer's state is done using a
* statevector instead of a density matrix
* - The qubits are indexed from 0 to qubit_count - 1.
* - The first qubit (qubit 0) is the most significant qubit and the last qubit
* is least signficant qubit in terms of positioning This means that first qubit
* is always first in the tensor product and the last qubit will go last in the
* tensor proudct, and that measuring will result in the result of the last
* qubit being stored in the least significant bit of the binary string
* - The representation of complex states and calculations use single floating
* point numbers.
* - The maximum number of qubits that can be used in the simulations is 30.
* - All the matrix and vectors are stored in ArrayFire arrays of type c32 (1
* complex float = 2 floats)
*
*/
/**
* @brief ArrayFire Quantum Simulator
*
*/
namespace aqs {
static constexpr float pi = 3.14159265358979323846f;
static constexpr uint32_t max_qubit_count = 30;
class QGate;
class QState;
class QCircuit;
class QSimulator;
/**
* @brief Prepares functionality for the ArrayFire Quantum Simulation functions
*
* @details Initializes ArrayFire and internal arrays
*
* @param argc
* @param argv
* @param backend arrayfire backend for the simulator to use
*/
void initialize(int argc, char** argv,
af::Backend backend = af::Backend::AF_BACKEND_DEFAULT);
/**
* @brief Clears the list of cached circuits
*
*/
void clear_circuit_cache();
/**
* @brief Class managing the behaviour of one qubit
*
*/
class QState {
public:
QState() = default;
QState(const QState&) = default;
QState(QState&&) = default;
~QState() = default;
QState& operator=(const QState&) = default;
QState& operator=(QState&&) = default;
/**
* @brief Construct a new QState object given the coefficients of the state
*
* @details It normalizes the state passed
*
* @param zeroState coefficient of the |0> state
* @param oneState coefficient of the |1> state
*/
QState(const std::complex<float>& zeroState,
const std::complex<float>& oneState);
/**
* @brief Construct a new QState object given the coefficients of the state
*
* @details It normalizes the state passed
*
* @param states array with the complex number state |0> in the [0] entry
* and |1> in the [1] entry
*/
QState(const std::array<std::complex<float>, 2>& states);
/**
* @brief Create a qubit state object given its polar coordinates
*
* @param polar_angle angle in the xy plane
* @param azimuthal_angle angle with respect to the xy plane
* @return std::array<std::complex<float>, 2>
*/
static std::array<std::complex<float>, 2> create_state(
float polar_angle, float azimuthal_angle) {
return {std::cos(polar_angle / 2.f),
std::complex<float>{std::cos(azimuthal_angle),
std::sin(azimuthal_angle)} *
std::sin(polar_angle / 2.f)};
}
/**
* @brief Sets the state of the object to given the coefficients of the
* state
*
* @details It normalizes the states passed
*
* @param zero_state coefficient of the |0> state
* @param one_state coefficient of the |1> state
* @return QState& reference to current QState object
*/
QState& set(const std::complex<float>& zero_state,
const std::complex<float>& one_state);
/**
* @brief Executes a measurement without collapsing the state
*
* @return true for |1> state measurement
* @return false for |0> state measurement
*/
bool peek_measure() const;
/**
* @brief Executes a measurement and collapses the state to it
*
* @return true for |1> state measurement
* @return false for |0> state measurement
*/
bool measure();
/**
* @brief Executes the given number of measurements on the given state
* and returns the results of the number of measurements of each
* state
*
* @param rep_count number of measurements to be done
* @return std::array<int, 2> array containing the results
*/
std::array<uint32_t, 2> profile_measure(uint32_t rep_count) const;
/**
* @brief Returns the probability of finding the state in the |1> state
*
* @return float probability |1> measurement
*/
float probability_true() const noexcept {
return state_[1].real * state_[1].real +
state_[1].imag * state_[1].imag;
}
/**
* @brief Returns the probability of finding the state in the |0> state
*
* @return float probability of |0> measurement
*/
float probability_false() const noexcept {
return 1.f - probability_true();
}
/**
* @brief Returns the complex number of the state selected
*
* @param index 0 or 1 depending on the state to obtain
* @return const af::cfloat&
*/
const af::cfloat& operator[](bool index) const noexcept {
return state_[static_cast<int>(index)];
}
bool operator!=(const aqs::QState& other) const noexcept {
return state_[0] != other.state_[0] || state_[1] != other.state_[1];
}
bool operator==(const aqs::QState& other) const noexcept {
return !(*this != other);
}
/**
* @brief Returns a pointer to the array of stored |0> and |1> states
*
* @return af::cfloat*
*/
af::cfloat* data() noexcept { return state_; }
/**
* @brief Returns a const pointer to the array of stored |0> and |1> states
*
* @return af::cfloat*
*/
const af::cfloat* data() const noexcept { return state_; }
/**
* @brief Returns an af::array of the stored |0> and |1> states
*
* @return af::array with dims [2,1,1,1]
*/
af::array to_array() const { return af::array(2, state_); }
/**
* @brief Returns a qubit in zero |0> state
*
* @return const QState&
*/
static const QState& zero() {
const static QState zero_state{1.0f, 0.0f};
return zero_state;
};
/**
* @brief Returns a qubit in one |1> state
*
* @return const QState&
*/
static const QState& one() {
const static QState one_state{0.0f, 1.0f};
return one_state;
};
/**
* @brief Returns a qubit in plus |+> state (1/sqrt(2) |0> + 1/sqrt(2) |1>)
*
* @return const QState&
*/
static const QState& plus() {
const static QState plus_state{0.70710678118f, 0.70710678118f};
return plus_state;
}
/**
* @brief Returns a qubit in minus |-> state (1/sqrt(2) |0> - 1/sqrt(2) |1>)
*
* @return const QState&
*/
static const QState& minus() {
const static QState minus_state{0.70710678118f, -0.70710678118f};
return minus_state;
}
private:
af::cfloat state_[2]{{1.0f, 0.0f}, {0.0f, 0.0f}};
/**
* @brief Normalizes the internal states of the qubit
*
*/
void force_normalize();
};
/**
* @brief Class managing the behavior of a Quantum Circuit
* including the addition of gates, the circuit matrix,
* and its representation
*
*/
class QCircuit {
public:
friend class QSimulator;
QCircuit() = delete;
QCircuit(const QCircuit&) = default;
QCircuit(QCircuit&&) = default;
~QCircuit() = default;
QCircuit& operator=(const QCircuit&) = default;
QCircuit& operator=(QCircuit&&) = default;
/**
* @brief Construct a new QCircuit object given the number of qubits
* and initial state for all the qubits
*
* @param qubit_count number of qubits for the simulation
*/
QCircuit(uint32_t qubit_count);
template<typename T>
friend QCircuit& operator<<(QCircuit& qc, const T& gate);
template<typename T>
friend QCircuit& operator<<(QCircuit& qc, const std::vector<T>& gates);
/**
* @brief Returns the number of qubit registers in the circuit
*
* @return uint32_t
*/
uint32_t qubit_count() const noexcept { return qubits_; }
/**
* @brief Returns the number of different unique orthogonal states
* that the state vector can be represented in
*
* @return uint32_t
*/
uint32_t state_count() const noexcept { return fast_pow2(qubits_); }
/**
* @brief Returns a reference to the current matrix representation of the
* circuit
*
* @warning If the array is modified, it must maintain its unitary property
* and dimensions Modifying the internal circuit incorrectly may result in
* undefined behavior Only use for low level access and functionality that
* the gates/circuit do not provide
*
* @return af::array&
*/
af::array& circuit() noexcept { return circuit_; }
/**
* @brief Returns a const reference to the current matrix representation of
* the circuit
*
* @return const af::array&
*/
const af::array& circuit() const noexcept { return circuit_; }
/**
* @brief Returns a reference to the list of gates that have been added
*
* @return std::vector<std::shared_ptr<aqs::QGate>>&
*/
auto& gate_list() noexcept { return gate_list_; }
/**
* @brief Returns a const reference to the list of gates that have been
* added
*
* @return const std::vector<std::shared_ptr<aqs::QGate>>&
*/
const auto& gate_list() const noexcept { return gate_list_; }
std::string& representation() noexcept { return representation_; }
const std::string& representation() const noexcept {
return representation_;
}
/**
* @brief Starts the computation of the matrix representation of the circuit
* from the added gates
*
* @note Adding gates after this call will not update matrix representation,
* a new call for this function must be added to update it
*
* @details This function caches its result to compute only the gates that
* have not been computed into the circuit matrix
*
*/
void compile();
/**
* @brief Clears the circuit by removing all gates and resetting the
* internal circuit matrix
*
*/
void clear();
/**
* @brief Clears the circuit matrix and resets the cache index of the
* compiled gates
*
*/
void clear_cache();
friend bool operator==(const QCircuit& lhs, const QCircuit& rhs);
private:
std::vector<std::shared_ptr<QGate>> gate_list_;
af::array circuit_;
std::string representation_;
uint32_t qubits_ = 0;
std::size_t cached_index_ = 0;
};
/**
* @brief Class managing the logic for the simulation of quantum noise models
*
*/
class QNoise {};
/**
* @brief Class managing the simulation of a quantum circuit and a initial
* state
*
*/
class QSimulator {
public:
/**
* @brief Enum of the different types Basis that the qubits can be measured
* in
*
*/
enum class Basis : int8_t {
// |0_z> = |0>
// |1_z> = |1>
Z,
// |0_y> = 1/sqrt(2)|0> + i/sqrt(2) |1>
// |1_y> = 1/sqrt(2)|0> - i/sqrt(2) |1>
Y,
// |0_x> = 1/sqrt(2)|0> + 1/sqrt(2) |1>
// |1_x> = 1/sqrt(2)|0> - 1/sqrt(2) |1>
X
};
/**
* @brief Construct a new QSimulator object
*
* @param qubit_count number of qubits of the simulator
* @param initial_state initial state for all the qubits
* @param noise_generator the noise generator to be used
*/
explicit QSimulator(uint32_t qubit_count,
const QState& initial_state = aqs::QState::zero(),
const QNoise& noise_generator = QNoise{});
/**
* @brief Construct a new QSimulator object
*
* @param qubit_count number of qubits of the simulator
* @param initial_states list for the initial state for each qubit
* @param noise_generator the noise generator to be used
*/
explicit QSimulator(uint32_t qubit_count,
std::vector<QState> initial_states,
const QNoise& noise_generator = QNoise{});
/**
* @brief Construct a new QSimulator object
*
* @param qubit_count number of qubits of the simulator
* @param statevector the statevector initialized to
* @param noise_generator the noise generator to be used
*/
explicit QSimulator(uint32_t qubit_count, const af::array& statevector,
const QNoise& noise_generator = QNoise{});
/**
* @brief Calculates the statevector from all the individual states of the
* qubits
*
*/
void generate_statevector();
/**
* @brief Computes the simulation for the circuit with the given initial
* conditions and updates the statevector to the result
*
* @param circuit circuit to simulate
*
*/
void simulate(const QCircuit& circuit);
/**
* @brief Measures the given qubit without collapsing the state
*
* @param qubit index of the qubit
*
* @return true for |1> state measurement
* @return false for |0> state measurement
*/
bool peek_measure(uint32_t qubit) const;
/**
* @brief Measures the given qubit and collapses its state (and updates the
* statevector) to it
*
* @details In the statevector, all states that contain the given
* measurement are updated according to the previous probability using
* conditional probability, while the other states are set to 0
*
* @param qubit index of the qubit
*
* @return true for |1> state measurement
* @return false for |0> state measurement
*/
bool measure(uint32_t qubit);
/**
* @brief Returns a bit representation of the statevector measured after
* measuring all bits. It collapses the statevector to that state
*
* @return uint32_t measurement
*/
uint32_t measure_all();
/**
* @brief Returns a bit representation of the statevector measured after
* measuring all bits. It does not collpase the statevector
*
* @return uint32_t measurement
*/
uint32_t peek_measure_all() const;
/**
* @brief Profiles the measurement of the given state of a qubit from the
* stored statevector using the given number of tests
*
* @param qubit qubit to measure
* @param rep_count number of measurements to be done for profiling
* @return std::array<int, 2> [0] = # of measurements for state |0>; [1] = #
* of measurements for state |1>
*/
std::array<uint32_t, 2> profile_measure(uint32_t qubit,
uint32_t rep_count) const;
/**
* @brief Profiles the measurement of the given output state from the stored
* statevector using the given number of tests
*
* @param rep_count number of measurements to be done for profiling
* @return std::vector<int> vector with the measurements [int(xxxx...)] =
* #number of measurements of state |xxxxx> where xxxxx is the binary output
* state
*/
std::vector<uint32_t> profile_measure_all(uint32_t rep_count) const;
/**
* @brief Returns a reference to the initial QState of given qubit from the
* simulator
*
* @param index index of the qubit
* @return QState&
*/
QState& qubit(uint32_t index) noexcept {
assert(index < qubit_count());
return states_[index];
};
/**
* @brief Returns a reference to the initial QState of given qubit from the
* simulator
*
* @param index index of the qubit
* @return const QState&
*/
const QState& qubit(uint32_t index) const noexcept {
assert(index < qubit_count());
return states_[index];
};
/**
* @brief Returns the complex number of the state in the statevector vector
*
* @param state int representation of the binary unique state
* @return af::cfloat
*/
af::cfloat state(uint32_t state) const noexcept {
assert(state < state_count());
return statevector_(state).scalar<af::cfloat>();
}
/**
* @brief Returns the probability to measure the given qubit in the |1>
* state
*
* @param qubit index of the qubit
* @return float probability of |1> state
*/
float qubit_probability_true(uint32_t qubit) const;
/**
* @brief Returns the probability to measure the given qubit in the |0>
* state
*
* @param qubit index of the qubit
* @return float probability of |0> state
*/
float qubit_probability_false(uint32_t qubit) const {
return 1.f - qubit_probability_true(qubit);
}
/**
* @brief Returns the probability to measure the given state from teh
* statevector
*
* @param state int representation of the binary unique state
* @return float probability of |xxxxxx> where xxxxx is the state
*/
float state_probability(uint32_t state) const;
/**
* @brief Returns a list of the probabilities for all the states
*
* @return std::vector<float>
*/
std::vector<float> probabilities() const;
/**
* @brief Returns the number of qubits of the simulator
*
* @return int number of qubits
*/
uint32_t qubit_count() const noexcept { return qubits_; }
/**
* @brief Returns the number of unique states that can be measured from the
* statevector
*
* @return int number of states
*/
uint32_t state_count() const noexcept { return fast_pow2(qubits_); }
/**
* @brief Returns the basis meaasurement direction
*
* @return Basis
*/
Basis get_basis() const noexcept { return basis_; }
/**
* @brief Changes the basis for measurement to the passed direction
*
* @param basis basis direction
*/
void set_basis(Basis basis);
/**
* @brief Returns a af::array& to the internal statevector stored by the
* simulator
*
* @warning If the array is modified, it must maintain its normalized
* property and dimensions Modifying the internal statevector incorrectly
* may result in undefined behavior Only use for low level access and
* functionality that the simulator do not provide
*
* @return af::array&
*/
af::array& statevector() noexcept { return statevector_; }
/**
* @brief Returns a const af::array& to the internal statevector stored by
* the simulator
*
* @return const af::array&
*/
const af::array& statevector() const noexcept { return statevector_; }
private:
std::vector<QState> states_;
af::array statevector_;
QNoise noise_;
uint32_t qubits_;
Basis basis_ = Basis::Z;
};
/**
* @brief Pure virtual base class delineating the structure for Quantum Gates
*
*/
class QGate {
public:
/**
* @brief Applies the gate matrix to the circuit matrix
*
* @param qc circuit to apply the gate to
* @return QCircuit& reference to the input circuit
*/
virtual QCircuit& operator()(QCircuit& qc) const = 0;
/**
* @brief Returns a string representation of the gate
*
* @return std::string
*/
virtual std::string to_string() const = 0;
/**
* @brief Returns a unsigned integer that uniquely identifies
* the gate
*
* @return uint32_t
*/
virtual uint32_t type() const noexcept = 0;
/**
* @brief Checks if the gate can be added to the passed circuit
*
* @note This function may throw with the error before returning false
*
* @param qc circuit in which the gate will be added
* @return true the gate will be added successfully to the circuit
* @return false the gate will not be added to the circuit
*/
virtual bool check(const QCircuit& qc) const = 0;
/**
* @brief Compares if two gates are equal
*
* @param rhs gate to compare
* @return true if they are the same type, affect the same qubit, and have
* the same properties
* @return false otherwise
*/
virtual bool operator==(const QGate& rhs) const noexcept = 0;
protected:
/**
* @brief Lists of unique identifiers for the internal gates implemented
*
*/
enum class GateTypes : uint32_t {
Barrier,
X,
Y,
Z,
Hadamard,
Phase,
Swap,
RotX,
RotY,
RotZ,
CX,
CY,
CZ,
CHadamard,
CPhase,
CSwap,
CRotX,
CRotY,
CRotZ,
CCX,
Or,
Circuit,
ControlCircuit
};
};
template<typename T>
QCircuit& operator<<(QCircuit& qc, const T& gate) {
static_assert(std::is_base_of<QGate, T>::value,
"Gate must inherit from QGate class");
if (gate.check(qc)) {
qc.representation_.append(gate.to_string());
qc.gate_list().push_back(std::make_shared<T>(gate));
}
return qc;
}
template<typename T>
QCircuit& operator<<(QCircuit& qc, const std::vector<T>& gates) {
static_assert(std::is_base_of<QGate, T>::value,
"Gate must inherit from QGate class");
for (const auto& gate : gates) {
if (gate.check(qc)) {
qc.representation_.append(gate.to_string());
qc.gate_list().push_back(std::make_shared<T>(gate));
}
}
return qc;
}
/**
* @brief Barrier gate: marks a separation between prior and subsequent gates in
* the circuit Can be made visible or invisible when displaying the circuit
*
*/
class Barrier : public QGate {
public:
Barrier(bool visible_ = true) noexcept : visible{visible_} {}
bool check(const QCircuit&) const override { return true; }
QCircuit& operator()(QCircuit& qc) const override { return qc; }
std::string to_string() const override { return visible ? "B;" : "P;"; }
uint32_t type() const noexcept override {
return static_cast<uint32_t>(GateTypes::Barrier);
}
bool operator==(const QGate& rhs) const noexcept override {
return type() == rhs.type();
}
static constexpr uint32_t static_type() noexcept {
return static_cast<uint32_t>(GateTypes::Barrier);
}
bool visible = true;
};
/**
* @brief Pauli X gate: applies the pauli X matrix on the given qubit
* (Equivalent to applying a NOT gate)
*
*/
class X : public QGate {
public:
X(uint32_t target_qubit_) noexcept : target_qubit{target_qubit_} {}
QCircuit& operator()(QCircuit&) const override;
std::string to_string() const override;
uint32_t type() const noexcept override {
return static_cast<uint32_t>(GateTypes::X);
}
bool check(const QCircuit&) const override;
bool operator==(const QGate& rhs) const noexcept override;
static constexpr uint32_t static_type() noexcept {
return static_cast<uint32_t>(GateTypes::X);
}
static const QCircuit& gate() {
static QCircuit qc = []() {
QCircuit qc(1);
qc << X{0};
qc.compile();
return qc;
}();
return qc;
}
uint32_t target_qubit;
};
/**
* @brief NOT gate: applies a NOT gate to the given qubit
* (Equivalent to applying X gate)
*
*/
using Not = X;
/**
* @brief Pauli Y gate: applies the pauli Y gate matrix on the given qubit
*
*/
class Y : public QGate {
public:
Y(uint32_t target_qubit_) noexcept : target_qubit{target_qubit_} {}
std::string to_string() const override;
bool check(const QCircuit&) const override;
QCircuit& operator()(QCircuit&) const override;
uint32_t type() const noexcept override {
return static_cast<uint32_t>(GateTypes::Y);
}
bool operator==(const QGate& rhs) const noexcept override;
static constexpr uint32_t static_type() noexcept {
return static_cast<uint32_t>(GateTypes::Y);
}
static const QCircuit& gate() {
static QCircuit qc = []() {
QCircuit qc(1);
qc << Y{0};
qc.compile();
return qc;
}();
return qc;
}
uint32_t target_qubit;
};
/**
* @brief Pauli Z gate: applies the Pauli Z matrix to the given qubit
* (Equivalent to a Phase-Pi gate)
*
*/
class Z : public QGate {
public:
Z(uint32_t target_qubit_) noexcept : target_qubit{target_qubit_} {}
std::string to_string() const override;
bool check(const QCircuit&) const override;
QCircuit& operator()(QCircuit&) const override;
uint32_t type() const noexcept override {
return static_cast<uint32_t>(GateTypes::Z);
}
bool operator==(const QGate& rhs) const noexcept override;
static constexpr uint32_t static_type() noexcept {
return static_cast<uint32_t>(GateTypes::Z);
}
static const QCircuit& gate() {
static QCircuit qc = []() {
QCircuit qc(1);
qc << Z{0};
qc.compile();
return qc;
}();
return qc;
}
uint32_t target_qubit;
};
/**
* @brief Rotation X gate: applies a rotation around the x-axis to the given
* qubit
*
*/
class RotX : public QGate {
public:
RotX(uint32_t target_qubit_, float angle_) noexcept
: target_qubit{target_qubit_}, angle{angle_} {}
std::string to_string() const override;
bool check(const QCircuit&) const override;
QCircuit& operator()(QCircuit&) const override;
uint32_t type() const noexcept override {
return static_cast<uint32_t>(GateTypes::RotX);
}
bool operator==(const QGate& rhs) const noexcept override;