Skip to content

Commit

Permalink
add tests for grid_synth and qasm_synth
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Guo committed Oct 3, 2023
1 parent 7f028f5 commit cccecce
Show file tree
Hide file tree
Showing 11 changed files with 7,769 additions and 59 deletions.
2 changes: 1 addition & 1 deletion examples/rz_test1.qasm
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ include "qelib1.inc";

qreg q[2];

rz(2*pi/4) q;
rx(2*pi/4) q[0];
6 changes: 3 additions & 3 deletions examples/rz_test2.qasm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ include "qelib1.inc";

qreg q[2];

rz(0.3) q[0];
rx(3/10) q[0];
ry(3*100/10/100) q[1];
rz(-0.3) q[0];
rz(-3/10) q[0];
rz(9*-27/100*10/81) q[0];
37 changes: 24 additions & 13 deletions include/grid_synth/exact_synthesis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,38 @@ namespace grid_synth {
// 1/(sqrt(omega)).
inline str_t check_common_cases(real_t theta, const real_t& eps) {

while (theta > real_t("2"))
theta = theta - real_t("2");
// Normalize theta to the range [0,4)
while (theta >= real_t("4"))
theta = theta - real_t("4");
while (theta < 0)
theta = theta + real_t("2");
theta = theta + real_t("4");

// Deal with the case where theta is in [2,4)
str_t ret = "";
if (theta >= real_t("2")) {
theta = theta - real_t("2");
ret = "WWWW";
}

if (abs(theta - real_t("0.25")) < eps) {
return "Tw";
// Check multiples of 1/4 in [0,2)
if (abs(theta) < eps) {
if (ret != "")
return ret;
return "I";
} else if (abs(theta - real_t("0.25")) < eps) {
return "Tw" + ret;
} else if (abs(theta - real_t("0.5")) < eps) {
return "SWWWWWWW";
return "SWWWWWWW" + ret;
} else if (abs(theta - real_t("0.75")) < eps) {
return "STWWWWWWWw";
return "STWWWWWWWw" + ret;
} else if (abs(theta - real_t("1")) < eps) {
return "SSWWWWWW";
return "SSWWWWWW" + ret;
} else if (abs(theta - real_t("1.25")) < eps) {
return "SSTWWWWWWw";
return "SSTWWWWWWw" + ret;
} else if (abs(theta - real_t("1.5")) < eps) {
return "SSSWWWWW";
return "SSSWWWWW" + ret;
} else if (abs(theta - real_t("1.75")) < eps) {
return "SSSTWWWWWw";
} else if (abs(theta - real_t("2")) < eps) {
return "WWWW";
return "SSSTWWWWWw" + ret;
} else {
return "";
}
Expand Down
65 changes: 32 additions & 33 deletions include/grid_synth/grid_synth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,30 @@
#ifndef GRID_SYNTH_GRID_SYNTH_HPP_
#define GRID_SYNTH_GRID_SYNTH_HPP_

#include "grid_synth/exact_synthesis.hpp"
#include "grid_synth/matrix.hpp"
#include "grid_synth/rz_approximation.hpp"
#include "grid_synth/types.hpp"
#include "exact_synthesis.hpp"
#include "matrix.hpp"
#include "rz_approximation.hpp"
#include "s3_table.hpp"
#include "types.hpp"

namespace staq {
namespace grid_synth {

/*! \brief Converts a GMP float to a string suitable for hashing. */
/* Converts a GMP float to a string suitable for hashing. */
static str_t to_string(const mpf_class& x) {
mp_exp_t exp;
// Use base 32 to get a shorter string; truncate the string
// to keep only the significant figures.
int sig_figs = mpf_get_default_prec() / 5;
int sig_len = mpf_get_default_prec() / 5;
if (x < 0)
++sig_figs; // account for leading minus sign
str_t s = x.get_str(exp, 32).substr(0, sig_figs);
++sig_len; // account for leading minus sign
str_t s = x.get_str(exp, 32).substr(0, sig_len);
return s + str_t(" ") + std::to_string(exp);
}

/* Options passed when constructing a GridSynthesizer. */
struct GridSynthOptions {
long int prec;
long int prec; // Precision in base 10 as a positive integer (10^p)
int factor_effort = MAX_ATTEMPTS_POLLARD_RHO;
bool check = false;
bool details = false;
Expand All @@ -59,29 +61,31 @@ struct GridSynthOptions {
class GridSynthesizer {
private:
std::unordered_map<str_t, str_t> rz_approx_cache_;
// static constexpr std::unordered_map<size_t, str_t> S3_TABLE2{{1,""}};
domega_matrix_table_t S3_TABLE;
const domega_matrix_table_t S3_TABLE;

real_t eps_;
bool check_;
bool details_;
bool verbose_;
bool timer_;

long long duration_;
bool valid_;

/* Construct GridSynthesizer objects using the make_synthesizer
* factory function.
*/
GridSynthesizer(domega_matrix_table_t s3_table, real_t eps, bool check,
bool details, bool verbose, bool timer)
: rz_approx_cache_(), S3_TABLE(std::move(s3_table)),
eps_(std::move(eps)), check_(check), details_(details),
verbose_(verbose), timer_(timer), duration_(0) {}
verbose_(verbose), timer_(timer), duration_(0), valid_(true) {}

public:
~GridSynthesizer() {
if (timer_) {
std::cerr << std::fixed
<< "Duration = " << (static_cast<double>(duration_) / 1e6)
<< " seconds" << '\n';
}
}
~GridSynthesizer() {}

double get_duration() const { return static_cast<double>(duration_) / 1e6; }
bool is_valid() const { return valid_; }

/*! \brief Find RZ-approximation for an angle. */
str_t get_rz_approx(const real_t& angle) {
Expand Down Expand Up @@ -145,12 +149,13 @@ class GridSynthesizer {

if (verbose_)
std::cerr << "Synthesis complete." << '\n';
if (check_) {
std::cerr << "Check flag = "
<< (rz_approx.matrix() ==
domega_matrix_from_str(full_simplify_str(op_str)))
<< '\n';
}
bool good = (rz_approx.matrix() ==
domega_matrix_from_str(full_simplify_str(op_str)));
valid_ = valid_ && good;
valid_ = valid_ && (rz_approx.error() < eps_);

if (check_)
std::cerr << "Check flag = " << good << '\n';
if (details_) {
real_t scale = gmpf::pow(SQRT2, rz_approx.matrix().k());
std::cerr << "angle = " << std::scientific << angle << '\n';
Expand Down Expand Up @@ -183,14 +188,8 @@ class GridSynthesizer {
};

/*! \brief Initializes a GridSynthesizer object. */
GridSynthesizer make_synthesizer(const GridSynthOptions& opt) {
domega_matrix_table_t s3_table = read_s3_table(DEFAULT_TABLE_FILE);
/*
for (auto& it : s3_table) {
std::cerr << DOmegaMatrixHash()(it.first) << ' ' << it.second <<
std::endl;
}
*/
inline GridSynthesizer make_synthesizer(const GridSynthOptions& opt) {
domega_matrix_table_t s3_table = load_s3_table();

real_t eps = gmpf::pow(real_t(10), -opt.prec);
MP_CONSTS = initialize_constants(opt.prec);
Expand Down
4 changes: 4 additions & 0 deletions include/grid_synth/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ inline str_t full_simplify_str(str_t str) {
return curr_str;
}

// ========================================================================= //
// Note: These functions are no longer used by grid_synth.
// ========================================================================= //

// Generate the set of all unitary matrices with SDE less than three
inline domega_matrix_table_t generate_s3_table() {
using namespace std;
Expand Down
Loading

0 comments on commit cccecce

Please sign in to comment.