Commit ba04ecca authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Expose native code printing to Python



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 96693fd5
Loading
Loading
Loading
Loading
+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.