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

Merge branch 'master' of https://github.com/ornl-qci/qcor

parents 3a5c2a51 d30f905a
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ add_qcor_compile_and_exe_test(multi_ctrl_test ctrl-gates/multiple_controls.cpp)
add_qcor_compile_and_exe_test(qrt_obj_func_simple simple/simple-objective-function.cpp)
add_qcor_compile_and_exe_test(qrt_kernel_autograd_simple simple/gradients_optimization.cpp)
add_qcor_compile_and_exe_test(qrt_bell_ctrl bell/bell_control.cpp)
add_qcor_compile_and_exe_test(qrt_print_native_code simple/print_native_code.cpp)

# Lambda tests
add_qcor_compile_and_exe_test(qrt_qpu_lambda_simple qpu_lambda/lambda_test.cpp)
+26 −0
Original line number Diff line number Diff line
__qpu__ void bell(qreg q) {
  H(q[0]);
  CX(q[0], q[1]);
  Measure(q);
}

int main() {
  auto q = qalloc(2);
  // print_native_code for QuantumKernel
  bell::print_native_code(q);

  auto bell_lambda = qpu_lambda([](qreg q) {
    H(q[0]);
    X::ctrl(q[0], q[1]);
    Measure(q);
  });

  // print_native_code for qpu_lambda
  bell_lambda.print_native_code(q);


  using BellSignature = KernelSignature<qreg>;
  BellSignature callable(bell);
  // print_native_code for KernelSignature
  callable.print_native_code(q);
}
+28 −0
Original line number Diff line number Diff line
from qcor import * 

# Print native code targetting a specific QPU backend

# NOTE Programmers must type annotate their function arguments
set_qpu('ibm:ibmq_manhattan')
# Define a Bell kernel
@qjit
def bell(q : qreg):
    H(q[0])
    CX(q[0], q[1])
    for i in range(q.size()):
        Measure(q[i])

# Allocate 2 qubits
q = qalloc(2)

print('XACC IR:')
bell.print_kernel(q)

# Default: QObj for IBM
print('Native QObj code:')
bell.print_native_code(q)
print('===========================')

# Can set the format with format key:
print('Native QASM code:')
bell.print_native_code(q, format='qasm')
+14 −0
Original line number Diff line number Diff line
@@ -757,6 +757,20 @@ PYBIND11_MODULE(_pyqcor, m) {
          [](qcor::QJIT &qjit, const std::string &kernel_name) {
            return qjit.get_kernel_function_ptr(kernel_name);
          },
          "")
      .def(
          "get_native_code",
          [](qcor::QJIT &qjit, const std::string name, KernelArgDict args, PyHeterogeneousMap options = {}) {
            xacc::HeterogeneousMap m;
            for (auto &item : args) {
              KernelArgDictToHeterogeneousMap vis(m, item.first);
              mpark::visit(vis, item.second);
            }
            auto program = qjit.extract_composite_with_hetmap(name, m);
            xacc::internal_compiler::execute_pass_manager(program);
            return xacc::internal_compiler::get_native_code(
                program, heterogeneousMapConvert(options));
          },
          "");

  py::class_<qcor::ObjectiveFunction, std::shared_ptr<qcor::ObjectiveFunction>>(
+7 −0
Original line number Diff line number Diff line
@@ -713,6 +713,13 @@ class qjit(object):
        """
        print(self.extract_composite(*args).toString())

    def print_native_code(self, *args, **kwargs):
        """
        Print the native code targeting the Accelerator backend
        """
        args_dict = self.construct_arg_dict(*args)
        print(self._qjit.get_native_code(self.function.__name__, args_dict, kwargs))

    def n_instructions(self, *args):
        """
        Return the number of quantum instructions in this kernel. 
Loading