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

updating unitary decompose to use new decompose {...}(...); syntax

parent cdd1509f
Loading
Loading
Loading
Loading
+26 −26
Original line number Diff line number Diff line
@@ -11,17 +11,17 @@ __qpu__ void ccnot(qreg q) {

  // To program at the unitary matrix level,
  // simply indicate you are using qcor::unitary namespace
  using qcor::unitary;
  
  decompose {
    // Create the unitary matrix
    UnitaryMatrix ccnot = UnitaryMatrix::Identity(8, 8);
    ccnot(6, 6) = 0.0;
    ccnot(7, 7) = 0.0;
    ccnot(6, 7) = 1.0;
    ccnot(7, 6) = 1.0;
  }
  (q, QFAST);

  // Switch back to xasm to add some measures
  using qcor::xasm;
  // Add some measures
  for (int i = 0; i < q.size(); i++) {
    Measure(q[i]);
  }
+90 −1
Original line number Diff line number Diff line
@@ -6,8 +6,8 @@
#include <limits>
#include <qalloc>

#include "qrt_mapper.hpp"
#include "qrt.hpp"
#include "qrt_mapper.hpp"

#include "clang/Basic/TokenKinds.h"
#include "clang/Lex/Token.h"
@@ -62,6 +62,95 @@ std::string run_token_collector(clang::Preprocessor &PP,

      token_collector = xacc::getService<TokenCollector>(lang_name);
      tmp_cache.clear();
    } else if (PP.getSpelling(Toks[i]) == "decompose") {
      // Flush the current CachedTokens...
      if (!tmp_cache.empty())
        token_collector->collect(PP, tmp_cache, bufferNames, code_ss);

      auto last_tc = token_collector;
      token_collector = xacc::getService<TokenCollector>("unitary");
      tmp_cache.clear();

      // skip decompose
      i++;

      // must open scope
      if (Toks[i].isNot(clang::tok::l_brace)) {
        xacc::error("Invalid decompose statement, must be of form decompose "
                    "{...} (...); with args in parenthesis being optional");
      }

      // skip l_brace
      i++; 

      // slurp up all tokens in the decompose scope {...}(...);
      int l_brace_count = 1;
      while (true) {
        if (Toks[i].is(clang::tok::l_brace)) {
          l_brace_count++;
        }
        if (Toks[i].is(clang::tok::r_brace)) {
          l_brace_count--;
        }
        if (l_brace_count == 0) {
          break;
        }
        tmp_cache.push_back(Toks[i]);
        i++;
      }

      // advance past the r_brace
      i++;
      if (Toks[i].isNot(clang::tok::l_paren)) {
        xacc::error("Invalid decompose statement, after scope close you must "
                    "provide at least buffer_name argument.");
      }

      // get the args, can be
      // (buffer_name)
      // (buffer_name, decompose_algo_name)
      // (buffer_name, decompose_algo_name, optimizer)
      i++;
      std::vector<std::string> arguments;
      while (Toks[i].isNot(clang::tok::r_paren)) {
          std::cout << "TOKEN HERE: " << PP.getSpelling(Toks[i]) << "\n";
        if (Toks[i].isNot(clang::tok::comma)) {
            std::cout << "Adding " << PP.getSpelling(Toks[i]) << " to args.\n";
          arguments.push_back(PP.getSpelling(Toks[i]));
        }
        i++;
      }

      if (arguments.size() < 1) {

      }
      std::map<int, std::function<void(const std::string arg)>> arg_to_action{
          {0,
           [&](const std::string arg) {
             code_ss << "auto decompose_buffer_name = " << arg << ".name();\n";
           }
           },
          {1,
           [&](const std::string arg) {
             code_ss << "auto decompose_algo_name = \"" << arg << "\";\n";
           }
           },
          {2, [&](const std::string arg) {
             code_ss << "auto decompose_optimizer = " << arg << ";\n";
           }
           }
      };
      for (int i = 0; i < arguments.size(); i++) {
        arg_to_action[i](arguments[i]);
      }

      if (!tmp_cache.empty())
        token_collector->collect(PP, tmp_cache, bufferNames, code_ss);

      // advance past the r_paren and semi colon
      i+=2;
      tmp_cache.clear();
      token_collector = last_tc;
    }

    tmp_cache.push_back(Toks[i]);
+2 −2
Original line number Diff line number Diff line
@@ -87,8 +87,8 @@ void UnitaryTokenCollector::collect(clang::Preprocessor &PP,
  std::cout << ss.str() << "\n";
  // Add the qfast decomp and hook up to the qrt program.
  ss << "auto decomposed_program = "
        "__internal__::decompose_unitary(\"QFAST\", "
     << var_name << ", " << bufferNames[0] << ".name());\n";
        "__internal__::decompose_unitary(decompose_algo_name, "
     << var_name << ", decompose_buffer_name);\n";

  ss << "parent_kernel->addInstruction(decomposed_program);\n";
}