Commit e0a66e1b authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

strengthen the test for ctrl on compute action segment

parent 670d735e
Loading
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
#include "gtest/gtest.h"
#include "qcor_mlir_api.hpp"
#include <fstream>

namespace {
// returns count of non-overlapping occurrences of 'sub' in 'str'
@@ -59,12 +60,25 @@ gate test22 q, r, s, v {
ctrl @ test22 ww, qq, rr, ss, vv;

)#";
  std::cout << qcor::mlir_compile(src, "test", qcor::OutputType::MLIR, true)
            << "\n";

  auto llvm = qcor::mlir_compile(src, "test", qcor::OutputType::LLVMIR, true);
  std::cout << "LLVM:\n" << llvm << "\n";
  // 2 rxs, 6 hs, 6 cnots, 1 rz + decls == 19
  EXPECT_EQ(countSubstring(llvm, "__quantum__qis"), 19);

  // Execute and assert we have the single CRZ
  std::string tmp_filename = "__tmp__ctrl_test.xacc";
  qcor::execute(src, "test", 3, {{"qrt", "nisq"}, {"print_final_submission", tmp_filename}});
  std::ifstream ifs(tmp_filename);
  std::string content( (std::istreambuf_iterator<char>(ifs) ),
                       (std::istreambuf_iterator<char>()    ) );
  
  EXPECT_EQ(countSubstring(content, "CNOT"), 6);
  EXPECT_EQ(countSubstring(content, "H"), 6);
  EXPECT_EQ(countSubstring(content, "Rx"), 2);
  EXPECT_EQ(countSubstring(content, "CRZ"), 1);
  
  std::remove(tmp_filename.c_str());
}

int main(int argc, char **argv) {
+2 −2
Original line number Diff line number Diff line
@@ -91,11 +91,11 @@ const std::string mlir_compile(const std::string &src,
}

int execute(const std::string &src, const std::string &kernel_name,
            int opt_level) {
            int opt_level, std::map<std::string, std::string> extra_args) {
  mlir::registerAsmPrinterCLOptions();
  mlir::registerMLIRContextCLOptions();

  auto mlir_gen_result = qcor::util::mlir_gen(src, kernel_name, true);
  auto mlir_gen_result = qcor::util::mlir_gen(src, kernel_name, true, extra_args);
  mlir::OwningModuleRef &module = *(mlir_gen_result.module_ref);
  mlir::MLIRContext &context = *(mlir_gen_result.mlir_context);
  std::vector<std::string> &unique_function_names =
+2 −1
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
#include <memory>
#include <string>
#include <vector>
#include <map>

namespace llvm {
class Module;
@@ -19,7 +20,7 @@ const std::string mlir_compile(const std::string &src,
                               bool add_entry_point, int opt_level = 3);

int execute(const std::string &src, const std::string &kernel_name,
            int opt_level = 3);
            int opt_level = 3, std::map<std::string, std::string> extra_args = {});

int execute(const std::string &src, const std::string &kernel_name,
            std::vector<std::unique_ptr<llvm::Module>> &extra_code_to_link,
+3 −1
Original line number Diff line number Diff line
@@ -85,8 +85,10 @@ void __quantum__rt__set_config_parameter(int8_t *key, int8_t *value) {
  } else if (casted_key == "shots") {
    shots = std::stoi(casted_value);
  } else if (casted_key == "print_final_submission") {
    std::cout << "ADDING PRINT FINAL SUBMISSION\n";
    ::__print_final_submission = true;
    if (!casted_value.empty()) {
      ::__print_final_submission_filename = casted_value;
    }
  }

}
+14 −2
Original line number Diff line number Diff line
#include <Eigen/Dense>
#include <Utils.hpp>
#include <fstream>
#include <unistd.h>
#include <stdio.h>

#include "CommonGates.hpp"
#include "FermionOperator.hpp"
@@ -368,6 +371,11 @@ class NISQ : public ::quantum::QuantumRuntime,
    } else {
      if (__print_final_submission) {
        std::cout << "SUBMIT:\n" << program->toString() << "\n";
        if (!__print_final_submission_filename.empty()) {
          std::ofstream os(__print_final_submission_filename);
          os << program->toString();
          os.close();
        }
      }
      xacc::internal_compiler::execute(
          buffer, program->as_xacc());
@@ -398,9 +406,13 @@ class NISQ : public ::quantum::QuantumRuntime,

    if (__print_final_submission) {
      std::cout << "SUBMIT:\n" << program->toString() << "\n";
      if (!__print_final_submission_filename.empty()) {
        std::ofstream os(__print_final_submission_filename);
        os << program->toString();
        os.close();
      }
    xacc::internal_compiler::execute(
        buffers, nBuffers, program->as_xacc());
    }
    xacc::internal_compiler::execute(buffers, nBuffers, program->as_xacc());
  }

  void set_current_program(std::shared_ptr<CompositeInstruction> p) override {
Loading