xacc_demo.cpp 2.35 KB
Newer Older
Mccaskey, Alex's avatar
Mccaskey, Alex committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "qcor.hpp"
#include "xacc.hpp"

// Demo steps (show off backend extensibility via XACC)
//
// qcor xacc_demo.cpp
// ./a.out 
// ./a.out --execute
// ./a.out --accelerator aer
// ./a.out --accelerator ibm:ibmq_jakarta
// ./a.out --accelerator aer:ibmq_jakarta --execute
// ./a.out --accelerator honeywell:HQS-LT-S1-APIVAL

using namespace qcor::arg;

int main(int argc, char** argv) {
  // Command line argument setup
  std::string default_qpu = "qpp";
  add_argument("--accelerator").default_value(default_qpu);
  add_argument("--execute").default_value(false).implicit_value(true);
  add_argument("--shots").default_value(100).action(
      [](const std::string& value) { return std::stoi(value); });
  parse_args(argc, argv);

  // Get user input
  auto qpu = get_argument("--accelerator");
  auto shots = get_argument<int>("--shots");
  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 
  auto backend = xacc::getAccelerator(qpu, {{"shots", shots}});

  // EXAMPLES, Bell State and GHZ State
  // Execute or Print Native Code

  {  // BELL STATE
    const int n_qubits = 2;
    xacc::IRBuilder builder;
    builder.h(0);
    builder.cnot(0, 1);
    for (int i = 0; i < n_qubits; i++) builder.mz(i);
    auto composite = builder.to_ir();

    // Exec if told to, else print the native code
    if (exec) {
      auto accelerator_buffer =
          std::make_shared<xacc::AcceleratorBuffer>("q", n_qubits);
      backend->execute(accelerator_buffer, composite);
      accelerator_buffer->print();
    } else {
      print("Native Code:\n", backend->getNativeCode(composite, {{"format", "qasm"}}));
    }
  }

  {  // GHZ STATE
    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();

    // Exec if told to, else print the native code
    if (exec) {
      auto accelerator_buffer =
          std::make_shared<xacc::AcceleratorBuffer>("q", n_qubits);
      backend->execute(accelerator_buffer, composite);
      accelerator_buffer->print();
    } else {
      print("Native Code:\n", backend->getNativeCode(composite, {{"format", "qasm"}}));
    }
  }

  return 0;
}