Skip to content

Commit

Permalink
Add narrowing fixes to gates_cirq.
Browse files Browse the repository at this point in the history
  • Loading branch information
95-martin-orion committed May 27, 2020
1 parent 2e4025c commit e595ff5
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions lib/gates_cirq.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ using Matrix1q = std::array<std::array<std::complex<fp_type>, 2>, 2>;
template <typename fp_type>
using Matrix2q = std::array<std::array<std::complex<fp_type>, 4>, 4>;

constexpr double h = 0.5;
constexpr double pi = M_PI;
constexpr double inv_pi = 1.0 / pi;
constexpr double is2 = 0.7071067811865475;
constexpr double h_double = 0.5;
constexpr double pi_double = M_PI;
constexpr double is2_double = 0.7071067811865475;

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

Expand Down Expand Up @@ -124,6 +123,8 @@ struct XPowGate {
static constexpr char name[] = "XPowGate";
static constexpr unsigned num_qubits = 1;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0,
fp_type exponent, fp_type global_shift = 0) {
fp_type c = std::cos(pi * exponent * 0.5);
Expand All @@ -143,6 +144,8 @@ struct YPowGate {
static constexpr char name[] = "YPowGate";
static constexpr unsigned num_qubits = 1;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0,
fp_type exponent, fp_type global_shift = 0) {
fp_type c = std::cos(pi * exponent * 0.5);
Expand All @@ -162,6 +165,8 @@ struct ZPowGate {
static constexpr char name[] = "ZPowGate";
static constexpr unsigned num_qubits = 1;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0,
fp_type exponent, fp_type global_shift = 0) {
fp_type c = std::cos(pi * exponent);
Expand All @@ -181,6 +186,9 @@ struct HPowGate {
static constexpr char name[] = "HPowGate";
static constexpr unsigned num_qubits = 1;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);
static constexpr fp_type is2 = static_cast<fp_type>(is2_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0,
fp_type exponent, fp_type global_shift = 0) {
fp_type c = std::cos(pi * exponent * 0.5);
Expand All @@ -203,6 +211,8 @@ struct CZPowGate {
static constexpr char name[] = "CZPowGate";
static constexpr unsigned num_qubits = 2;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
fp_type exponent, fp_type global_shift = 0) {
fp_type gc = std::cos(pi * exponent * global_shift);
Expand Down Expand Up @@ -237,6 +247,8 @@ struct CXPowGate {
static constexpr char name[] = "CXPowGate";
static constexpr unsigned num_qubits = 2;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
fp_type exponent, fp_type global_shift = 0) {
fp_type c = std::cos(pi * exponent * 0.5);
Expand Down Expand Up @@ -330,6 +342,8 @@ struct H {
static constexpr char name[] = "H";
static constexpr unsigned num_qubits = 1;

static constexpr fp_type is2 = static_cast<fp_type>(is2_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0) {
return CreateGate<GateCirq<fp_type>, H>(
time, q0, {is2, 0, is2, 0, is2, 0, -is2, 0});
Expand All @@ -356,6 +370,8 @@ struct T {
static constexpr char name[] = "T";
static constexpr unsigned num_qubits = 1;

static constexpr fp_type is2 = static_cast<fp_type>(is2_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0) {
return CreateGate<GateCirq<fp_type>, T>(
time, q0, {1, 0, 0, 0, 0, 0, is2, is2});
Expand Down Expand Up @@ -464,6 +480,8 @@ struct PhasedXPowGate {
static constexpr char name[] = "PhasedXPowGate";
static constexpr unsigned num_qubits = 1;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0,
fp_type phase_exponent, fp_type exponent = 1,
fp_type global_shift = 0) {
Expand Down Expand Up @@ -494,6 +512,8 @@ struct PhasedXZGate {
static constexpr char name[] = "PhasedXZGate";
static constexpr unsigned num_qubits = 1;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0,
fp_type x_exponent, fp_type z_exponent,
fp_type axis_phase_exponent) {
Expand Down Expand Up @@ -527,6 +547,8 @@ struct XXPowGate {
static constexpr char name[] = "XXPowGate";
static constexpr unsigned num_qubits = 2;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
fp_type exponent, fp_type global_shift = 0) {
fp_type gc = std::cos(pi * exponent * global_shift);
Expand Down Expand Up @@ -569,6 +591,8 @@ struct YYPowGate {
static constexpr char name[] = "YYPowGate";
static constexpr unsigned num_qubits = 2;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
fp_type exponent, fp_type global_shift = 0) {
fp_type gc = std::cos(pi * exponent * global_shift);
Expand Down Expand Up @@ -611,6 +635,8 @@ struct ZZPowGate {
static constexpr char name[] = "ZZPowGate";
static constexpr unsigned num_qubits = 2;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
fp_type exponent, fp_type global_shift = 0) {
fp_type gc = std::cos(pi * exponent * global_shift);
Expand Down Expand Up @@ -717,6 +743,9 @@ struct SwapPowGate {
static constexpr char name[] = "SwapPowGate";
static constexpr unsigned num_qubits = 2;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);
static constexpr fp_type h = static_cast<fp_type>(h_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
fp_type exponent, fp_type global_shift = 0) {
fp_type gc = std::cos(pi * exponent * global_shift);
Expand Down Expand Up @@ -761,6 +790,9 @@ struct ISwapPowGate {
static constexpr char name[] = "ISwapPowGate";
static constexpr unsigned num_qubits = 2;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);
static constexpr fp_type h = static_cast<fp_type>(h_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
fp_type exponent, fp_type global_shift = 0) {
fp_type gc = std::cos(pi * exponent * global_shift);
Expand Down Expand Up @@ -803,6 +835,9 @@ struct riswap {
static constexpr char name[] = "riswap";
static constexpr unsigned num_qubits = 2;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);
static constexpr fp_type h = static_cast<fp_type>(h_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
fp_type phi) {
fp_type c = std::cos(phi);
Expand Down Expand Up @@ -835,6 +870,8 @@ struct SWAP {
static constexpr char name[] = "SWAP";
static constexpr unsigned num_qubits = 2;

static constexpr fp_type is2 = static_cast<fp_type>(is2_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1) {
return CreateGate<GateCirq<fp_type>, SWAP>(
time, q0, q1, {1, 0, 0, 0, 0, 0, 0, 0,
Expand All @@ -860,6 +897,9 @@ struct ISWAP {
static constexpr char name[] = "ISWAP";
static constexpr unsigned num_qubits = 2;

static constexpr fp_type h = static_cast<fp_type>(h_double);
static constexpr fp_type is2 = static_cast<fp_type>(is2_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1) {
return CreateGate<GateCirq<fp_type>, ISWAP>(
time, q0, q1, {1, 0, 0, 0, 0, 0, 0, 0,
Expand All @@ -886,6 +926,9 @@ struct PhasedISwapPowGate {
static constexpr char name[] = "PhasedISwapPowGate";
static constexpr unsigned num_qubits = 2;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);
static constexpr fp_type h = static_cast<fp_type>(h_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
fp_type phase_exponent = 0.25,
fp_type exponent = 1.0) {
Expand Down Expand Up @@ -927,6 +970,9 @@ struct givens {
static constexpr char name[] = "givens";
static constexpr unsigned num_qubits = 2;

static constexpr fp_type pi = static_cast<fp_type>(pi_double);
static constexpr fp_type h = static_cast<fp_type>(h_double);

static GateCirq<fp_type> Create(unsigned time, unsigned q0, unsigned q1,
fp_type phi) {
fp_type c = std::cos(phi);
Expand Down Expand Up @@ -961,6 +1007,8 @@ struct FSimGate {
static constexpr char name[] = "FSimGate";
static constexpr unsigned num_qubits = 2;

static constexpr fp_type is2 = static_cast<fp_type>(is2_double);

static GateCirq<fp_type> Create(
unsigned time, unsigned q0, unsigned q1, fp_type theta, fp_type phi) {
if (phi < 0) {
Expand Down

0 comments on commit e595ff5

Please sign in to comment.