Commit 3b261e6f authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding visitor demo to xacc_demo, adding comments and clean up to others

parent 91cc8278
Loading
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -9,6 +9,18 @@
// i.e. T|1> = exp(i*pi/4)|1>
// We use 3 counting bits => totally 4 qubits.

// Using QIR Implementation library C-API

// qcor qir_demo.cpp
// ./a.out -qrt nisq -shots 1000

// code /home/cades/dev/qcor/mlir/qir_qrt/qir-qis-base.cpp and look at some impls

// qcor qir_demo.cpp -S -emit-llvm 
// wc -l qir_demo.ll
// qcor qir_demo.cpp -emit-llvm -S -O3 -internal-no-pch
// wc -l qir_demo.ll

Qubit* extract_qubit(Array* a, int idx);
void iqft(Array* q);

+26 −5
Original line number Diff line number Diff line
@@ -69,4 +69,25 @@ int main() {
      local_qrt.reset(q[1]);
    }
  }

  {  // Convenience API for these calls

    auto q = qalloc(2);
    ::quantum::h(q[0]);
    ::quantum::cnot(q[0], q[1]);
  }

  // See how we use this with the Clang SH
  // Uncomment and paste to terminal
//   printf "__qpu__ void f(qreg q) {
//       H(q[0]);
//   Measure(q[0]);
// }
// int main() {
//   auto q = qalloc(1);
//   f(q);
//   q.print();
// }
// " | qcor -print-csp-source -x c++ -

}
 No newline at end of file
+45 −5
Original line number Diff line number Diff line
#include "AllGateVisitor.hpp"
#include "qcor.hpp"
#include "xacc.hpp"

@@ -50,7 +51,8 @@ int main(int argc, char** argv) {
      backend->execute(accelerator_buffer, composite);
      accelerator_buffer->print();
    } else {
      print("Native Code:\n", backend->getNativeCode(composite, {{"format", "qasm"}}));
      print("Native Code:\n",
            backend->getNativeCode(composite, {{"format", "qasm"}}));
    }
  }

@@ -69,7 +71,45 @@ int main(int argc, char** argv) {
      backend->execute(accelerator_buffer, composite);
      accelerator_buffer->print();
    } else {
      print("Native Code:\n", backend->getNativeCode(composite, {{"format", "qasm"}}));
      print("Native Code:\n",
            backend->getNativeCode(composite, {{"format", "qasm"}}));
    }
  }

  {  // HOW SIMULATORS WORK

    // Define an XACC IR Visitor, AllGateVisitor is a convenient
    // supertype exposing visit() for all gates
    struct MyVisitor : public xacc::quantum::AllGateVisitor {
      std::complex<double>* MY_SIM_DATA;

      void visit(xacc::quantum::Hadamard& h) {
        print("Applying Hadamard", h.bits()[0], "to internal simulation data!");
      }
      void visit(xacc::quantum::CNOT& cnot) {
        print("Applying CNOT", cnot.bits()[0], cnot.bits()[1],
              "to internal simulation data!");
      }
    };

    // Define some IR again
    const int n_qubits = 3;
    xacc::IRBuilder builder;
    builder.h(0);
    for (int i = 0; i < n_qubits - 1; i++) builder.cnot(i, i + 1);
    for (int i = 0; i < n_qubits; i++) builder.mz(i);
    auto composite = builder.to_ir();

    // The following is a common pattern in Accelerator implementations!

    // Create a Visitor
    auto visitor = std::make_shared<MyVisitor>();

    // Walk the IR tree pre-order, visit each node
    xacc::InstructionIterator iter(composite);
    while (iter.hasNext()) {
      auto next = iter.next();
      next->accept(visitor);
    }
  }

+5 −4
Original line number Diff line number Diff line
@@ -304,8 +304,9 @@ def main(argv=None):
        if verbose:
            info('Rebuilding pre-compiled header for qcor_lang_ext.hpp.')

        result = subprocess.run([compiler, '-std=c++17', '-x', 'c++-header', '@CMAKE_INSTALL_PREFIX@/include/qcor/qcor_lang_ext.hpp',
                                 '-o', '@CMAKE_INSTALL_PREFIX@/include/qcor/qcor_lang_ext.hpp.pch'] + baseIncludes, check=True)
        pch_commands = [compiler, '-std=c++17', '-x', 'c++-header', '@CMAKE_INSTALL_PREFIX@/include/qcor/qcor_lang_ext.hpp',
                                 '-o', '@CMAKE_INSTALL_PREFIX@/include/qcor/qcor_lang_ext.hpp.pch']
        result = subprocess.run(pch_commands + baseIncludes, check=True)
        exit(0)
    
    # This flag is primarily for our testers to point to the 
@@ -884,7 +885,7 @@ def main(argv=None):
        sys.argv.append(filename)

        sys.argv[0] = compiler
        commands = [compiler] + defaultFlags + sHandlerArgs + baseIncludes
        commands = [compiler] + defaultFlags + ['-Wno-unused-command-line-argument', '-Wno-override-module'] + sHandlerArgs + baseIncludes
        if compileOnly:
            commands += sys.argv[1:]
        else:
@@ -903,7 +904,7 @@ def main(argv=None):
    elif fileType == '' and filename == '':
        if '-x' in sys.argv[1:] and 'c++' in sys.argv[1:]:
            sys.argv[0] = compiler
            commands = [compiler] + defaultFlags + sHandlerArgs + baseIncludes 
            commands = [compiler] + defaultFlags + ['-Wno-unused-command-line-argument', '-Wno-override-module'] + sHandlerArgs + baseIncludes 
            if compileOnly:
                commands += sys.argv[1:]
            else: