Unverified Commit 86372d2b authored by Mccaskey, Alex's avatar Mccaskey, Alex Committed by GitHub
Browse files

Merge branch 'thien_pr' into tnguyen/devel

parents a2140f31 d6ba24a8
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
| Branch | Status |
|:-------|:-------|
|master | [![pipeline status](https://code.ornl.gov/qci/qcor/badges/master/pipeline.svg)](https://code.ornl.gov/qci/qcor/commits/master) |
|devel | [![pipeline status](https://code.ornl.gov/qci/qcor/badges/devel/pipeline.svg)](https://code.ornl.gov/qci/qcor/commits/devel) |
![qcor](docs/assets/qcor_full_logo.svg)

| master | 
|:-------|
| [![pipeline status](https://code.ornl.gov/qci/qcor/badges/master/pipeline.svg)](https://code.ornl.gov/qci/qcor/commits/master) |

# QCOR

+63 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 24.1.3, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 viewBox="0 0 388.5 107.1" style="enable-background:new 0 0 388.5 107.1;" xml:space="preserve">
<style type="text/css">
	.st0{fill:#FFFFFF;}
	.st1{opacity:0.7;fill:#255AEF;}
	.st2{opacity:0.7;fill:#1ED660;}
	.st3{fill:#255AEF;}
	.st4{fill:#1ED660;}
	.st5{opacity:0.5;}
</style>
<g>
	<g>
		<g>
			<path class="st4" d="M97.6,8.8c6.9,0,12.6,5.6,12.6,12.6l0,31.6c0,2.4,0.9,4.6,2.6,6.3l6.2,6.2l0-44.2C119,9.6,109.4,0,97.6,0
				l-45,0l6.2,6.2c1.7,1.7,3.9,2.6,6.3,2.6H97.6z"/>
		</g>
		<g>
			<path class="st4" d="M208.3,21.4c0-6.9,5.6-12.6,12.6-12.6l28.6,0c2.4,0,4.6-0.9,6.3-2.6l6.2-6.2l-41.2,0
				c-11.8,0-21.4,9.6-21.4,21.4l0,44.4l6.2-6.2c1.7-1.7,2.6-3.9,2.6-6.3V21.4z"/>
			<path class="st4" d="M259.6,51.5c0,6.9-5.6,12.6-12.6,12.6l-28.6,0c-2.4,0-4.6,0.9-6.3,2.6l-6.2,6.2l41.2,0
				c11.8,0,21.4-9.6,21.4-21.4l0-45.2l-6.2,6.2c-1.7,1.7-2.6,3.9-2.6,6.3V51.5z"/>
		</g>
		<path class="st4" d="M180.5,64.1l-28.6,0c-6.9,0-12.6-5.6-12.6-12.6V40.8v-3.1V21.4c0-6.9,5.6-12.6,12.6-12.6l28.6,0
			c2.4,0,4.6-0.9,6.3-2.6L193,0l-41.2,0c-11.8,0-21.4,9.6-21.4,21.4v14.7v8.4v6.9c0,11.8,9.6,21.4,21.4,21.4l41.2,0l-6.2-6.2
			C185.1,65,182.8,64.1,180.5,64.1z"/>
		<path class="st4" d="M326.4,42.5c-4.4-4.4-10.4-6.9-16.7-6.9h-21.8V8.1l28.7,0c7.4,0,13.8,5.6,14.3,13c0.1,1.8-0.1,3.6-0.7,5.3
			c-1.3,3.8-0.4,7.9,2.5,10.7l0.2,0.2c4-4.2,6.2-9.8,6.2-15.6C338.9,9.6,328.6,0,316.4,0l-33.4,0c-1.8-0.3-3.7,1-3.7,3.2v69.7h8.5
			V44.1h21.8c4,0,7.8,1.6,10.7,4.4L341,69.2c2.3,2.3,5.3,3.5,8.5,3.5l7.1,0L326.4,42.5z"/>
		<path class="st4" d="M107.9,66.6c-1.7-1.7-3.9-2.6-6.3-2.6H68.1c-6.9,0-12.6-5.6-12.6-12.6V18.7c0-2.4-0.9-4.6-2.6-6.3l-6.2-6.2
			v45.2c0,11.8,9.6,21.4,21.4,21.4l30.3,0l0,0c4,0,5.8,1.6,8.7,4.4l3.8,3.8c1.5,1.5,3.5,2.3,5.6,2.4l8.8,0L107.9,66.6z"/>
	</g>
	<g class="st5">
		<g>
			<path d="M9,105.8c-0.5,0.3-1.6,1-3.3,1c-3.2,0-5.7-2.4-5.7-5.7c0-3.3,2.5-5.7,5.7-5.7c3.1,0,5.7,2.4,5.7,5.7c0,2-0.9,3.3-1.5,3.9
				l1.6,1.7H9.9L9,105.8z M9,104.1c0.4-0.4,1.2-1.4,1.2-3c0-2.5-2-4.5-4.4-4.5c-2.5,0-4.5,2-4.5,4.5c0,2.5,2,4.5,4.5,4.5
				c0.7,0,1.6-0.2,2.4-0.7l-2.6-2.7h1.6L9,104.1z"/>
			<path d="M30.6,102.3c0,0.9,0,1.9,0.9,2.7c0.4,0.4,1.2,0.6,1.8,0.6s1.4-0.2,1.8-0.6c0.9-0.7,0.9-1.7,0.9-2.7v-6.7h1.2v7
				c0,0.9-0.1,1.8-0.7,2.6c-0.7,0.9-1.8,1.5-3.2,1.5c-1.4,0-2.5-0.6-3.2-1.5c-0.6-0.8-0.7-1.7-0.7-2.6v-7h1.2V102.3z"/>
			<path d="M61.8,103.5h-4.7l-1.3,3.1h-1.3l5-11.5l4.9,11.5h-1.3L61.8,103.5z M61.3,102.3l-1.8-4.4l-1.9,4.4H61.3z"/>
			<path d="M81.7,106.6V95.2l8.5,8.9v-8.5h1.2v11.4l-8.5-8.9v8.5H81.7z"/>
			<path d="M112.8,96.8v9.8h-1.2v-9.8h-2.7v-1.1h6.5v1.1H112.8z"/>
			<path d="M134,102.3c0,0.9,0,1.9,0.9,2.7c0.4,0.4,1.2,0.6,1.8,0.6s1.4-0.2,1.8-0.6c0.9-0.7,0.9-1.7,0.9-2.7v-6.7h1.2v7
				c0,0.9-0.1,1.8-0.7,2.6c-0.7,0.9-1.8,1.5-3.2,1.5c-1.4,0-2.5-0.6-3.2-1.5c-0.6-0.8-0.7-1.7-0.7-2.6v-7h1.2V102.3z"/>
			<path d="M158.8,106.6l1.8-11.6l4.2,9.2l4.3-9.3l1.7,11.6h-1.2l-1-7.6l-3.7,8.1l-3.7-8.1l-1.1,7.6H158.8z"/>
			<path d="M217.1,98c-0.3-0.3-0.9-0.8-1.4-1c-0.6-0.3-1.2-0.4-1.9-0.4c-2.5,0-4.4,2-4.4,4.6c0,3,2.4,4.5,4.4,4.5
				c0.7,0,1.5-0.2,2-0.5c0.6-0.3,1.1-0.7,1.3-0.9v1.5c-1.2,0.9-2.5,1.1-3.3,1.1c-3.3,0-5.7-2.5-5.7-5.6c0-3.3,2.4-5.7,5.7-5.7
				c0.7,0,1.9,0.1,3.2,1.1V98z"/>
			<path d="M234.5,101.1c0-3.3,2.5-5.7,5.7-5.7s5.7,2.4,5.7,5.7c0,3.3-2.5,5.7-5.7,5.7C237,106.8,234.5,104.3,234.5,101.1z
				 M235.7,101.1c0,2.5,2,4.5,4.5,4.5c2.5,0,4.4-2,4.4-4.5c0-2.5-2-4.5-4.4-4.5C237.7,96.6,235.7,98.6,235.7,101.1z"/>
			<path d="M263.5,106.6l1.8-11.6l4.2,9.2l4.3-9.3l1.7,11.6h-1.2l-1-7.6l-3.7,8.1l-3.7-8.1l-1.1,7.6H263.5z"/>
			<path d="M295.8,95.6c1.1,0,2,0.1,2.8,0.8c0.8,0.7,1,1.7,1,2.3c0,1.5-1,3.1-3.5,3.1H295v4.8h-1.2v-11H295.8z M295,100.7h1
				c1.4,0,2.3-0.7,2.3-2c0-1.7-1.6-1.9-2.4-1.9H295V100.7z"/>
			<path d="M318.2,95.6v11H317v-11H318.2z"/>
			<path d="M338,95.6v9.8h3v1.1h-4.2v-11H338z"/>
			<path d="M364.1,96.8h-4.5v3.3h4.3v1.1h-4.3v4.3h4.5v1.1h-5.7v-11h5.7V96.8z"/>
			<path d="M383.9,95.6c0.7,0,1.8,0,2.7,0.6c0.6,0.4,1.2,1.3,1.2,2.4c0,1.7-1.1,2.9-2.8,3l3.5,4.9H387l-3.3-4.8h-0.3v4.8h-1.2v-11
				H383.9z M383.4,100.7h0.8c1.5,0,2.4-0.7,2.4-2c0-0.6-0.2-1.2-0.9-1.6c-0.5-0.3-1.1-0.3-1.6-0.3h-0.7V100.7z"/>
		</g>
	</g>
</g>
</svg>
+1 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ add_library(${LIBRARY_NAME}
            token_collector_util.cpp)

target_include_directories(${LIBRARY_NAME}
                           PUBLIC .
                           PUBLIC . ${CMAKE_SOURCE_DIR}/runtime/qrt
                                  ${CLANG_INCLUDE_DIRS}
                                  ${LLVM_INCLUDE_DIRS})

+1 −107
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
#include <limits>
#include <qalloc>

#include "AllGateVisitor.hpp"
#include "qrt_mapper.hpp"

#include "clang/Basic/TokenKinds.h"
#include "clang/Lex/Token.h"
@@ -55,112 +55,6 @@ run_token_collector(clang::Preprocessor &PP, clang::CachedTokens &Toks,
  return std::make_pair(kernel_src, compiler_name);
}

using namespace xacc::quantum;

class qrt_mapper : public AllGateVisitor,
                   public xacc::InstructionVisitor<Circuit> {
protected:
  std::stringstream ss;
  // The kernel name of the CompositeInstruction 
  // that this mapper is visiting.
  std::string kernelName;
  void addOneQubitGate(const std::string name, xacc::Instruction &inst) {
    auto expr = inst.getBitExpression(0);
    ss << "quantum::" + name + "(" << inst.getBufferNames()[0] << "["
         << (expr.empty() ? std::to_string(inst.bits()[0]) : expr) << "]";
    if (inst.isParameterized() && inst.name() != "Measure") {
      ss << ", " << inst.getParameter(0).toString();
      for (int i = 1; i < inst.nParameters(); i++) {
        ss << ", " << inst.getParameter(i).toString() << "\n";
      }
    }
    ss << ");\n";
  }

  void addTwoQubitGate(const std::string name, xacc::Instruction &inst) {
    auto expr_src = inst.getBitExpression(0);
    auto expr_tgt = inst.getBitExpression(1);
    ss << "quantum::" + name + "(" << inst.getBufferNames()[0] << "["
       << (expr_src.empty() ? std::to_string(inst.bits()[0]) : expr_src)
       << "], " << inst.getBufferNames()[1] << "["
       << (expr_tgt.empty() ? std::to_string(inst.bits()[1]) : expr_tgt) << "]";
    // Handle parameterized gate:
    if (inst.isParameterized()) {
      ss << ", " << inst.getParameter(0).toString();
      for (int i = 1; i < inst.nParameters(); i++) {
        ss << ", " << inst.getParameter(i).toString();
      }
    }
    ss << ");\n";   
  }

public:
  // Ctor: cache the kernel name of the CompositeInstruction
  qrt_mapper(const std::string& top_level_kernel_name):
    kernelName(top_level_kernel_name)
  {}

  auto get_new_src() { return ss.str(); }
  // One-qubit gates
  void visit(Hadamard &h) override { addOneQubitGate("h", h); }
  void visit(Rz &rz) override { addOneQubitGate("rz", rz); }
  void visit(Ry &ry) override { addOneQubitGate("ry", ry); }
  void visit(Rx &rx) override { addOneQubitGate("rx", rx); }
  void visit(U1 &u1) override { addOneQubitGate("u1", u1); }
  void visit(X &x) override { addOneQubitGate("x", x); }
  void visit(Y &y) override { addOneQubitGate("y", y); }
  void visit(Z &z) override { addOneQubitGate("z", z); }
  void visit(S &s) override { addOneQubitGate("s", s); }
  void visit(Sdg &sdg) override { addOneQubitGate("sdg", sdg); }
  void visit(T &t) override { addOneQubitGate("t", t); }
  void visit(Tdg &tdg) override { addOneQubitGate("tdg", tdg); }
  
  // Two-qubit gates
  void visit(CNOT &cnot) override { addTwoQubitGate("cnot", cnot); }
  void visit(CY &cy) override { addTwoQubitGate("cy", cy); }
  void visit(CZ &cz) override { addTwoQubitGate("cz", cz); }
  void visit(Swap &s) override { addTwoQubitGate("swap", s); }
  void visit(CRZ &crz) override { addTwoQubitGate("crz", crz); }
  void visit(CH &ch) override { addTwoQubitGate("ch", ch); }
  void visit(CPhase &cphase) override { addTwoQubitGate("cphase", cphase); }
    
  void visit(Measure &measure) override { addOneQubitGate("mz", measure); }
  void visit(Identity &i) override { addOneQubitGate("i", i); }
  void visit(U &u) override { addOneQubitGate("u", u); }
  void visit(Circuit &circ) override {
    if (circ.name() == kernelName) {
      return;
    }
    if (circ.name() == "exp_i_theta") {
      ss << "quantum::exp(" << circ.getBufferNames()[0] << ", "
         << circ.getArguments()[0]->name << ", " << circ.getArguments()[1]->name
         << ");\n";
    }
    else {
      // Call a previously-defined QCOR kernel:
      // In this context, we disable __execute flag around this hence
      // this sub-kernel will not be submitted.
      // i.e. only the outer-most kernel will be submitted.
      // Open a new scope since we use a local var '__cached_execute_flag'
      ss << "{\n";
      // Cache the state of the __execute flag
      ss << "const auto __cached_execute_flag = __execute;\n";
      // Reset the flag:
      ss << "__execute = false;\n";
      // Add the circuit invocation.
      ss << circ.name() << "(" << circ.getBufferNames()[0];
      for (const auto& arg : circ.getArguments()){
        ss << ", " << arg->name;
      }
      ss << ")" << ";\n";
      // Reinstate the __execute flag
      ss << "__execute = __cached_execute_flag;\n";
      ss << "}\n";
    }
  }
  void visit(IfStmt &ifStmt) override {}
};

void run_token_collector_llvm_rt(clang::Preprocessor &PP,
                                 clang::CachedTokens &Toks,
                                 const std::string &function_prototype,
+49 −52
Original line number Diff line number Diff line
@@ -10,9 +10,9 @@
#include "Observable.hpp"
#include "Optimizer.hpp"

#include "PauliOperator.hpp"
#include "qalloc"
#include "xacc_internal_compiler.hpp"
#include "PauliOperator.hpp"

#include "qrt.hpp"

@@ -25,17 +25,11 @@ using Optimizer = xacc::Optimizer;
using CompositeInstruction = xacc::CompositeInstruction;
using PauliOperator = xacc::quantum::PauliOperator;

PauliOperator X(int idx){
  return PauliOperator({{idx, "X"}});
}
PauliOperator X(int idx) { return PauliOperator({{idx, "X"}}); }

PauliOperator Y(int idx){
  return PauliOperator({{idx, "Y"}});
}
PauliOperator Y(int idx) { return PauliOperator({{idx, "Y"}}); }

PauliOperator Z(int idx){
  return PauliOperator({{idx, "Z"}});
}
PauliOperator Z(int idx) { return PauliOperator({{idx, "Z"}}); }

PauliOperator allZs(const int nQubits) {
  auto ret = Z(0);
@@ -45,22 +39,18 @@ PauliOperator allZs(const int nQubits) {
  return ret;
}

template<typename T>
PauliOperator operator+(T coeff, PauliOperator &op){
template <typename T> PauliOperator operator+(T coeff, PauliOperator &op) {
  return PauliOperator(coeff) + op;
}
template<typename T>
PauliOperator operator+(PauliOperator &op, T coeff){
template <typename T> PauliOperator operator+(PauliOperator &op, T coeff) {
  return PauliOperator(coeff) + op;
}

template<typename T>
PauliOperator operator-(T coeff, PauliOperator &op){
template <typename T> PauliOperator operator-(T coeff, PauliOperator &op) {
  return -1.0 * coeff + op;
}

template<typename T>
PauliOperator operator-(PauliOperator &op, T coeff){
template <typename T> PauliOperator operator-(PauliOperator &op, T coeff) {
  return -1.0 * coeff + op;
}

@@ -119,6 +109,9 @@ template <typename Function, typename Tuple> auto call(Function f, Tuple t) {
template <typename QuantumKernel, typename... Args>
std::shared_ptr<CompositeInstruction>
kernel_as_composite_instruction(QuantumKernel &k, Args... args) {
#ifdef QCOR_USE_QRT
  quantum::clearProgram();
#endif
  // turn off execution
  xacc::internal_compiler::__execute = false;
  // Execute to compile, this will store and we can get it
@@ -147,6 +140,12 @@ std::shared_ptr<ObjectiveFunction> get_objective(const std::string & type);

} // namespace __internal__

template <typename QuantumKernel, typename... Args>
void print_kernel(std::ostream &os, QuantumKernel &kernel, Args... args) {
  os << __internal__::kernel_as_composite_instruction(kernel, args...)
            ->toString();
}

// The ObjectiveFunction represents a functor-like data structure that
// models a general parameterized scalar function. It is initialized with a
// problem-specific Observable and Quantum Kernel, and exposes a method for
@@ -277,11 +276,9 @@ auto observe(QuantumKernel &kernel, std::shared_ptr<Observable> obs,
}

template <typename QuantumKernel, typename... Args>
auto observe(QuantumKernel &kernel, Observable &obs,
             Args... args) {
auto observe(QuantumKernel &kernel, Observable &obs, Args... args) {
  auto program = __internal__::kernel_as_composite_instruction(kernel, args...);
  return [program, &obs](Args... args) {

    // Get the first argument, which should be a qreg
    auto q = std::get<0>(std::forward_as_tuple(args...));
    // std::cout << "\n" << program->toString() << "\n";
Loading