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

Working integration example



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 433520cf
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line

// Driver to tie them all together:
// - Declare the exported QASM3 callable 
// - Use that as input to the Q# operation

// Compile with:
// qcor -qrt ftqc op_takes_callable.qs kernel.qasm driver.cpp -v

// Note: this driver can be part of QASM3 as well, but
// we don't have the concept of Callable type in QASM3 yet,
// hence, we cannot declare the Q# kernel taking Callable argumements as extern
// in QASM3 yet.
#include "qir-types.hpp"

// QASM3 function wrapping the quantum sub-routine as a QIR Callable
extern "C" ::Callable* qasm_x__callable(); 
// Q# function
extern "C" void QCOR__ApplyKernelToEachQubit__body(::Callable *);

int main() {
  // Get the callable (QASM3)
  auto qasm3_callable = qasm_x__callable();
  // Pass it to Q#
  QCOR__ApplyKernelToEachQubit__body(qasm3_callable);
  return 0;
}
 No newline at end of file
+7 −0
Original line number Diff line number Diff line
OPENQASM 3;
include "stdgates.inc";

def qasm_x qubit:qb {
  print("Call X gate from QASM3!");
  x qb;
}
+19 −0
Original line number Diff line number Diff line
namespace QCOR 
{
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Canon;

operation ApplyKernelToEachQubit(singleElementOperation : (Qubit => Unit is Adj + Ctl)): Unit {
  use q = Qubit[5];
  ApplyToEachCA(singleElementOperation, q);
  for idx in 0..4 {
    let res = M(q[idx]);    
    if res == One {
      Message("Get one");
    } else {
      Message("Get zero");
    }
  }
}
}
 No newline at end of file
+10 −5
Original line number Diff line number Diff line
@@ -36,12 +36,17 @@ LogicalResult TupleUnpackOpLowering::matchAndRewrite(
  for (const auto &result : tuple_unpack_op.result()) {
    if (result.getType().isa<mlir::OpaqueType>() &&
        result.getType().cast<mlir::OpaqueType>().getTypeData() == "Array") {
      auto array_type =
          LLVM::LLVMPointerType::get(get_quantum_type("Array", context));
      tuple_struct_type_list.push_back(array_type);
      tuple_struct_type_list.push_back(
          LLVM::LLVMPointerType::get(get_quantum_type("Array", context)));
    } else if (result.getType().isa<mlir::OpaqueType>() &&
               result.getType().cast<mlir::OpaqueType>().getTypeData() ==
                   "Qubit") {
      tuple_struct_type_list.push_back(
          LLVM::LLVMPointerType::get(get_quantum_type("Qubit", context)));
    } else if (result.getType().isa<mlir::FloatType>()) {
      auto float_type = mlir::FloatType::getF64(context);
      tuple_struct_type_list.push_back(float_type);
      tuple_struct_type_list.push_back(mlir::FloatType::getF64(context));
    } else if (result.getType().isa<mlir::IntegerType>()) {
      tuple_struct_type_list.push_back(mlir::IntegerType::get(context, 64));
    }
  }

+11 −2
Original line number Diff line number Diff line
@@ -456,9 +456,18 @@ def main(argv=None):
        bc_file_name = base_name + '.bc'
        object_file_name = base_name+'.o'
        extra_args = ['-internal-func-name', base_name]
        has_cpp_driver = False
        if '-no-entrypoint' in sys.argv[1:]:
            sys.argv.remove('-no-entrypoint')
            extra_args.append('-no-entrypoint')
        else:
            # Check if there is a accompanying .cpp file in the list of files:
            # Assume no entry point in this case as well
            for arg in sys.argv[1:]:
                if os.path.isfile(arg) and os.path.splitext(arg)[1] == '.cpp':
                    extra_args.append('-no-entrypoint')
                    has_cpp_driver = True
                    break

        # FIXME: define pass-mamanegement CLI options for qcor
        if '--q-optimize' in sys.argv[1:]:
@@ -512,7 +521,7 @@ def main(argv=None):
            print('[qcor-exec]: ', '{}'.format(' '.join([llvm_bin_path+'/llc', '-filetype=obj', bc_file_name ])) )
        result = subprocess.run([llvm_bin_path+'/llvm-as', ll_file_name, '-o', bc_file_name ], check=True)
        result = subprocess.run([llvm_bin_path+'/llc', '-filetype=obj', bc_file_name ], check=True)
        if '-no-entrypoint' in extra_args:
        if ('-no-entrypoint' in extra_args) and (not has_cpp_driver):
            exit(0)

        sys.argv.remove(filename)