Skip to content

Commit e595ff5

Browse files
Add narrowing fixes to gates_cirq.
1 parent 2e4025c commit e595ff5

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

lib/gates_cirq.h

+52-4
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ using Matrix1q = std::array<std::array<std::complex<fp_type>, 2>, 2>;
7676
template <typename fp_type>
7777
using Matrix2q = std::array<std::array<std::complex<fp_type>, 4>, 4>;
7878

79-
constexpr double h = 0.5;
80-
constexpr double pi = M_PI;
81-
constexpr double inv_pi = 1.0 / pi;
82-
constexpr double is2 = 0.7071067811865475;
79+
constexpr double h_double = 0.5;
80+
constexpr double pi_double = M_PI;
81+
constexpr double is2_double = 0.7071067811865475;
8382

8483
// Gates from cirq/ops/identity.py:
8584

@@ -124,6 +123,8 @@ struct XPowGate {
124123
static constexpr char name[] = "XPowGate";
125124
static constexpr unsigned num_qubits = 1;
126125

126+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
127+
127128
static GateCirq<fp_type> Create(unsigned time, unsigned q0,
128129
fp_type exponent, fp_type global_shift = 0) {
129130
fp_type c = std::cos(pi * exponent * 0.5);
@@ -143,6 +144,8 @@ struct YPowGate {
143144
static constexpr char name[] = "YPowGate";
144145
static constexpr unsigned num_qubits = 1;
145146

147+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
148+
146149
static GateCirq<fp_type> Create(unsigned time, unsigned q0,
147150
fp_type exponent, fp_type global_shift = 0) {
148151
fp_type c = std::cos(pi * exponent * 0.5);
@@ -162,6 +165,8 @@ struct ZPowGate {
162165
static constexpr char name[] = "ZPowGate";
163166
static constexpr unsigned num_qubits = 1;
164167

168+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
169+
165170
static GateCirq<fp_type> Create(unsigned time, unsigned q0,
166171
fp_type exponent, fp_type global_shift = 0) {
167172
fp_type c = std::cos(pi * exponent);
@@ -181,6 +186,9 @@ struct HPowGate {
181186
static constexpr char name[] = "HPowGate";
182187
static constexpr unsigned num_qubits = 1;
183188

189+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
190+
static constexpr fp_type is2 = static_cast<fp_type>(is2_double);
191+
184192
static GateCirq<fp_type> Create(unsigned time, unsigned q0,
185193
fp_type exponent, fp_type global_shift = 0) {
186194
fp_type c = std::cos(pi * exponent * 0.5);
@@ -203,6 +211,8 @@ struct CZPowGate {
203211
static constexpr char name[] = "CZPowGate";
204212
static constexpr unsigned num_qubits = 2;
205213

214+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
215+
206216
static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
207217
fp_type exponent, fp_type global_shift = 0) {
208218
fp_type gc = std::cos(pi * exponent * global_shift);
@@ -237,6 +247,8 @@ struct CXPowGate {
237247
static constexpr char name[] = "CXPowGate";
238248
static constexpr unsigned num_qubits = 2;
239249

250+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
251+
240252
static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
241253
fp_type exponent, fp_type global_shift = 0) {
242254
fp_type c = std::cos(pi * exponent * 0.5);
@@ -330,6 +342,8 @@ struct H {
330342
static constexpr char name[] = "H";
331343
static constexpr unsigned num_qubits = 1;
332344

345+
static constexpr fp_type is2 = static_cast<fp_type>(is2_double);
346+
333347
static GateCirq<fp_type> Create(unsigned time, unsigned q0) {
334348
return CreateGate<GateCirq<fp_type>, H>(
335349
time, q0, {is2, 0, is2, 0, is2, 0, -is2, 0});
@@ -356,6 +370,8 @@ struct T {
356370
static constexpr char name[] = "T";
357371
static constexpr unsigned num_qubits = 1;
358372

373+
static constexpr fp_type is2 = static_cast<fp_type>(is2_double);
374+
359375
static GateCirq<fp_type> Create(unsigned time, unsigned q0) {
360376
return CreateGate<GateCirq<fp_type>, T>(
361377
time, q0, {1, 0, 0, 0, 0, 0, is2, is2});
@@ -464,6 +480,8 @@ struct PhasedXPowGate {
464480
static constexpr char name[] = "PhasedXPowGate";
465481
static constexpr unsigned num_qubits = 1;
466482

483+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
484+
467485
static GateCirq<fp_type> Create(unsigned time, unsigned q0,
468486
fp_type phase_exponent, fp_type exponent = 1,
469487
fp_type global_shift = 0) {
@@ -494,6 +512,8 @@ struct PhasedXZGate {
494512
static constexpr char name[] = "PhasedXZGate";
495513
static constexpr unsigned num_qubits = 1;
496514

515+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
516+
497517
static GateCirq<fp_type> Create(unsigned time, unsigned q0,
498518
fp_type x_exponent, fp_type z_exponent,
499519
fp_type axis_phase_exponent) {
@@ -527,6 +547,8 @@ struct XXPowGate {
527547
static constexpr char name[] = "XXPowGate";
528548
static constexpr unsigned num_qubits = 2;
529549

550+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
551+
530552
static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
531553
fp_type exponent, fp_type global_shift = 0) {
532554
fp_type gc = std::cos(pi * exponent * global_shift);
@@ -569,6 +591,8 @@ struct YYPowGate {
569591
static constexpr char name[] = "YYPowGate";
570592
static constexpr unsigned num_qubits = 2;
571593

594+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
595+
572596
static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
573597
fp_type exponent, fp_type global_shift = 0) {
574598
fp_type gc = std::cos(pi * exponent * global_shift);
@@ -611,6 +635,8 @@ struct ZZPowGate {
611635
static constexpr char name[] = "ZZPowGate";
612636
static constexpr unsigned num_qubits = 2;
613637

638+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
639+
614640
static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
615641
fp_type exponent, fp_type global_shift = 0) {
616642
fp_type gc = std::cos(pi * exponent * global_shift);
@@ -717,6 +743,9 @@ struct SwapPowGate {
717743
static constexpr char name[] = "SwapPowGate";
718744
static constexpr unsigned num_qubits = 2;
719745

746+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
747+
static constexpr fp_type h = static_cast<fp_type>(h_double);
748+
720749
static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
721750
fp_type exponent, fp_type global_shift = 0) {
722751
fp_type gc = std::cos(pi * exponent * global_shift);
@@ -761,6 +790,9 @@ struct ISwapPowGate {
761790
static constexpr char name[] = "ISwapPowGate";
762791
static constexpr unsigned num_qubits = 2;
763792

793+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
794+
static constexpr fp_type h = static_cast<fp_type>(h_double);
795+
764796
static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
765797
fp_type exponent, fp_type global_shift = 0) {
766798
fp_type gc = std::cos(pi * exponent * global_shift);
@@ -803,6 +835,9 @@ struct riswap {
803835
static constexpr char name[] = "riswap";
804836
static constexpr unsigned num_qubits = 2;
805837

838+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
839+
static constexpr fp_type h = static_cast<fp_type>(h_double);
840+
806841
static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
807842
fp_type phi) {
808843
fp_type c = std::cos(phi);
@@ -835,6 +870,8 @@ struct SWAP {
835870
static constexpr char name[] = "SWAP";
836871
static constexpr unsigned num_qubits = 2;
837872

873+
static constexpr fp_type is2 = static_cast<fp_type>(is2_double);
874+
838875
static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1) {
839876
return CreateGate<GateCirq<fp_type>, SWAP>(
840877
time, q0, q1, {1, 0, 0, 0, 0, 0, 0, 0,
@@ -860,6 +897,9 @@ struct ISWAP {
860897
static constexpr char name[] = "ISWAP";
861898
static constexpr unsigned num_qubits = 2;
862899

900+
static constexpr fp_type h = static_cast<fp_type>(h_double);
901+
static constexpr fp_type is2 = static_cast<fp_type>(is2_double);
902+
863903
static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1) {
864904
return CreateGate<GateCirq<fp_type>, ISWAP>(
865905
time, q0, q1, {1, 0, 0, 0, 0, 0, 0, 0,
@@ -886,6 +926,9 @@ struct PhasedISwapPowGate {
886926
static constexpr char name[] = "PhasedISwapPowGate";
887927
static constexpr unsigned num_qubits = 2;
888928

929+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
930+
static constexpr fp_type h = static_cast<fp_type>(h_double);
931+
889932
static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
890933
fp_type phase_exponent = 0.25,
891934
fp_type exponent = 1.0) {
@@ -927,6 +970,9 @@ struct givens {
927970
static constexpr char name[] = "givens";
928971
static constexpr unsigned num_qubits = 2;
929972

973+
static constexpr fp_type pi = static_cast<fp_type>(pi_double);
974+
static constexpr fp_type h = static_cast<fp_type>(h_double);
975+
930976
static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
931977
fp_type phi) {
932978
fp_type c = std::cos(phi);
@@ -961,6 +1007,8 @@ struct FSimGate {
9611007
static constexpr char name[] = "FSimGate";
9621008
static constexpr unsigned num_qubits = 2;
9631009

1010+
static constexpr fp_type is2 = static_cast<fp_type>(is2_double);
1011+
9641012
static GateCirq<fp_type> Create(
9651013
unsigned time, unsigned q0, unsigned q1, fp_type theta, fp_type phi) {
9661014
if (phi < 0) {

0 commit comments

Comments
 (0)