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


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 91cc8278
Pipeline #167663 passed with stage
in 47 minutes and 20 seconds
......@@ -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);
......
......@@ -2,7 +2,7 @@
#include "xacc_service.hpp"
// qcor qrt_demo.cpp
// ./a.out
// ./a.out
// qcor qrt_demo.cpp -qpu aer:ibmq_jakarta
// ./a.out
......@@ -12,7 +12,7 @@ int main() {
// Hard code the shots for NISQ to 100
qcor::set_shots(100);
{ // NISQ Mode Execution
{ // NISQ Mode Execution
// Allocate some qubits
auto q = qalloc(2);
......@@ -40,7 +40,7 @@ int main() {
qrt_impl = xacc::getService<QuantumRuntime>("ftqc");
qrt_impl->initialize("ftqc_test");
{ // FTQC Mode Execution
{ // FTQC Mode Execution
// Allocate the qubits
auto q = qalloc(2);
......@@ -50,8 +50,8 @@ int main() {
QuantumRuntime& local_qrt = *qrt_impl.get();
local_qrt.set_current_buffer(q.results());
// Loop over sequential application of the
// gate instructions, get measurement feedback, and
// Loop over sequential application of the
// gate instructions, get measurement feedback, and
// reset the state of the qubits.
for (int i = 0; i < 20; i++) {
local_qrt.h(q.head());
......@@ -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
#include "AllGateVisitor.hpp"
#include "qcor.hpp"
#include "xacc.hpp"
// Demo steps (show off backend extensibility via XACC)
//
// qcor xacc_demo.cpp
// ./a.out
// ./a.out
// ./a.out --execute
// ./a.out --accelerator aer
// ./a.out --accelerator ibm:ibmq_jakarta
......@@ -28,8 +29,8 @@ int main(int argc, char** argv) {
auto exec = get_argument<bool>("--execute");
print("You chose the", qpu, "accelerator.");
// This is the key piece. Ultimately under the hood
// for OpenQASM3, we delegate to a call like this
// This is the key piece. Ultimately under the hood
// for OpenQASM3, we delegate to a call like this
auto backend = xacc::getAccelerator(qpu, {{"shots", shots}});
// EXAMPLES, Bell State and GHZ State
......@@ -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);
}
}
......
......@@ -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:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment