py-qcor.cpp 45 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*******************************************************************************
 * Copyright (c) 2018-, UT-Battelle, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the BSD 3-Clause License 
 * which accompanies this distribution. 
 *
 * Contributors:
 *   Alexander J. McCaskey - initial API and implementation
 *   Thien Nguyen - implementation
 *******************************************************************************/
11
12
#include <pybind11/complex.h>
#include <pybind11/eigen.h>
13
#include <pybind11/functional.h>
14
#include <pybind11/iostream.h>
15
#include <pybind11/numpy.h>
16
#include <pybind11/operators.h>
17
18
19
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
20

21
22
23
#include "base/qcor_qsim.hpp"
#include "py_costFunctionEvaluator.hpp"
#include "py_qsimWorkflow.hpp"
24
#include "qcor_jit.hpp"
25
#ifdef QCOR_BUILD_MLIR_PYTHON_API
26
#include "qcor_mlir_api.hpp"
27
#endif
28

29
30
#include "objective_function.hpp"
#include "qcor_ir.hpp"
31
#include "qcor_observable.hpp"
32
#include "qcor_optimizer.hpp"
33
34
35
#include "qrt.hpp"
#include "xacc.hpp"
#include "xacc_internal_compiler.hpp"
36
#include "xacc_service.hpp"
37

38
namespace py = pybind11;
39
40
41
42
43
44
45
using namespace xacc;

namespace pybind11 {
namespace detail {
template <typename... Ts>
struct type_caster<Variant<Ts...>> : variant_caster<Variant<Ts...>> {};

46
47
template <>
struct visit_helper<Variant> {
48
  template <typename... Args>
49
  static auto call(Args &&...args) -> decltype(mpark::visit(args...)) {
50
51
52
53
54
55
56
57
    return mpark::visit(args...);
  }
};

template <typename... Ts>
struct type_caster<mpark::variant<Ts...>>
    : variant_caster<mpark::variant<Ts...>> {};

58
59
template <>
struct visit_helper<mpark::variant> {
60
  template <typename... Args>
61
  static auto call(Args &&...args) -> decltype(mpark::visit(args...)) {
62
63
64
    return mpark::visit(args...);
  }
};
65
66
}  // namespace detail
}  // namespace pybind11
67

68
namespace {
69

70
71
// We only allow certain argument types for quantum kernel functors in python
// Here we enumerate them as a Variant
72
using AllowedKernelArgTypes =
73
    xacc::Variant<bool, int, double, std::string, xacc::internal_compiler::qreg,
74
                  xacc::internal_compiler::qubit, std::vector<double>,
75
76
                  std::vector<int>, qcor::Operator, qcor::PairList<int>,
                  std::vector<qcor::Operator>, std::vector<std::string>>;
77
78

// We will take as input a mapping of arg variable names to the argument itself.
79
using KernelArgDict = std::map<std::string, AllowedKernelArgTypes>;
80
81

// Utility for mapping KernelArgDict to a HeterogeneousMap
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class KernelArgDictToHeterogeneousMap {
 protected:
  xacc::HeterogeneousMap &m;
  const std::string &key;

 public:
  KernelArgDictToHeterogeneousMap(xacc::HeterogeneousMap &map,
                                  const std::string &k)
      : m(map), key(k) {}
  template <typename T>
  void operator()(const T &t) {
    m.insert(key, t);
  }
};
96

97
// Add type name to this list to support receiving from Python.
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
98
using PyHeterogeneousMapTypes =
99
    xacc::Variant<bool, int, double, std::string, std::vector<int>,
100
                  std::vector<std::pair<int, int>>,
101
                  std::shared_ptr<qcor::IRTransformation>,
102
103
                  std::shared_ptr<qcor::Optimizer>, std::vector<double>,
                  std::vector<std::vector<double>>>;
104
105
106
107
using PyHeterogeneousMap = std::map<std::string, PyHeterogeneousMapTypes>;

// Helper to convert a Python *dict* (as a map of variants) into a native
// HetMap.
108
109
xacc::HeterogeneousMap heterogeneousMapConvert(
    const PyHeterogeneousMap &in_pyMap) {
110
111
112
113
114
115
116
117
  xacc::HeterogeneousMap result;
  for (auto &item : in_pyMap) {
    auto visitor = [&](const auto &value) { result.insert(item.first, value); };
    mpark::visit(visitor, item.second);
  }

  return result;
}
118

119
qcor::Operator convertToQCOROperator(py::object op, bool keep_fermion = false) {
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  if (py::hasattr(op, "terms")) {
    // this is from openfermion
    if (py::hasattr(op, "is_two_body_number_conserving")) {
      // This is a fermion Operator
      auto terms = op.attr("terms");
      // terms is a list of tuples
      std::stringstream ss;
      int i = 0;
      for (auto term : terms) {
        auto term_tuple = term.cast<py::tuple>();
        if (!term_tuple.empty()) {
          ss << terms[term].cast<std::complex<double>>() << " ";
          for (auto element : term_tuple) {
            auto element_pair = element.cast<std::pair<int, int>>();
            ss << element_pair.first << (element_pair.second ? "^" : "") << " ";
          }
        } else {
          // this was identity
138
139
140
141
142
143
144
145
146
147
148
149
          try {
            auto coeff = terms[term].cast<double>();
            ss << coeff;
          } catch (std::exception &e) {
            try {
              auto coeff = terms[term].cast<std::complex<double>>();
              ss << coeff;
            } catch (std::exception &e) {
              qcor::error(
                  "Could not cast identity coefficient to double or complex.");
            }
          }
150
151
152
153
154
155
156
        }
        i++;
        if (i != py::len(terms)) {
          ss << " + ";
        }
      }
      auto obs_tmp = qcor::createOperator("fermion", ss.str());
157
158
159
160
161
      if (keep_fermion) {
        return obs_tmp;
      } else {
        return qcor::operatorTransform("jw", obs_tmp);
      }
162
163

    } else {
164
      if (keep_fermion) {
165
166
167
        xacc::error(
            "Error - you asked for a qcor::FermionOperator, but this is an "
            "OpenFermion QubitOperator.");
168
      }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
      // this is a qubit  operator
      auto terms = op.attr("terms");
      // terms is a list of tuples
      std::stringstream ss;
      int i = 0;
      for (auto term : terms) {
        auto term_tuple = term.cast<py::tuple>();
        if (!term_tuple.empty()) {
          ss << terms[term].cast<std::complex<double>>() << " ";
          for (auto element : term_tuple) {
            auto element_pair = element.cast<std::pair<int, std::string>>();
            ss << element_pair.second << element_pair.first << " ";
          }
        } else {
          // this was identity
184
185
186
187
188
189
190
191
192
193
194
195
196

          try {
            auto coeff = terms[term].cast<double>();
            ss << coeff;
          } catch (std::exception &e) {
            try {
              auto coeff = terms[term].cast<std::complex<double>>();
              ss << coeff;
            } catch (std::exception &e) {
              qcor::error(
                  "Could not cast identity coefficient to double or complex.");
            }
          }
197
198
199
200
201
202
203
204
        }
        i++;
        if (i != py::len(terms)) {
          ss << " + ";
        }
      }
      return qcor::createOperator(ss.str());
    }
205
206
207
208
209
210
211
212
  } else if (py::hasattr(op, "toString") && py::hasattr(op, "observe")) {
    auto string_rep = op.attr("toString");
    auto op_str = string_rep().cast<std::string>();
    if (op_str.find("^") != std::string::npos) {
      return qcor::createOperator("fermion", op_str);
    } else {
      return qcor::createOperator(op_str);
    }
213
214
215
216
217
  } else {
    // throw an error
    qcor::error(
        "Invalid python object passed as a QCOR Operator/Observable. "
        "Currently, we only accept OpenFermion datastructures.");
218
    return qcor::Operator();
219
220
221
  }
}

222
}  // namespace
223

224
225
namespace qcor {

Mccaskey, Alex's avatar
Mccaskey, Alex committed
226
227
228
229
230
231
232
233
234
235
// PyObjectiveFunction implements ObjectiveFunction to
// enable the utility of pythonic quantum kernels with the
// existing qcor ObjectiveFunction infrastructure. This class
// keeps track of the quantum kernel as a py::object, which it uses
// in tandem with the QCOR QJIT engine to create an executable
// functor representation of the quantum code at runtime. It exposes
// the ObjectiveFunction operator()() overloads to map vector<double>
// x to the correct pythonic argument structure. It delegates to the
// usual helper ObjectiveFunction (like vqe) for execution of the
// actual pre-, execution, and post-processing.
236
237
238
239
240
241
242
243
244
245
class PyObjectiveFunction : public qcor::ObjectiveFunction {
 protected:
  py::object py_kernel;
  std::shared_ptr<ObjectiveFunction> helper;
  xacc::internal_compiler::qreg qreg;
  QJIT qjit;

 public:
  const std::string name() const override { return "py-objective-impl"; }
  const std::string description() const override { return ""; }
246
  PyObjectiveFunction(py::object q, qcor::Operator qq, const int n_dim,
247
248
                      const std::string &helper_name,
                      xacc::HeterogeneousMap opts = {})
249
      : py_kernel(q) {
250
    // Set the OptFunction dimensions
251
    _dim = n_dim;
252

253
254
    qreg = ::qalloc(qq.nBits());

255
    // Set the helper objective
256
    helper = xacc::getService<qcor::ObjectiveFunction>(helper_name);
257

258
259
260
261
262
263
264
265
266
    // Store the observable pointer and give it to the helper
    observable = qq;
    options = opts;
    options.insert("observable", observable);
    helper->set_options(options);
    helper->update_observable(observable);

    // Extract the QJIT source code
    auto src = py_kernel.attr("get_internal_src")().cast<std::string>();
267
268
269
270
    auto extra_cpp_src =
        py_kernel.attr("get_extra_cpp_code")().cast<std::string>();
    auto sorted_kernel_deps = py_kernel.attr("get_sorted_kernels_deps")()
                                  .cast<std::vector<std::string>>();
271
272
273

    // QJIT compile
    // this will be fast if already done, and we just do it once
274
    qjit.jit_compile(src, true, sorted_kernel_deps, extra_cpp_src);
275
276
    qjit.write_cache();
  }
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307

  // PyObjectiveFunction(py::object q, std::shared_ptr<qcor::Observable> &qq,
  //                     const int n_dim, const std::string &helper_name,
  //                     xacc::HeterogeneousMap opts = {})
  //     : py_kernel(q) {
  //   // Set the OptFunction dimensions
  //   _dim = n_dim;
  //   qreg = ::qalloc(qq->nBits());

  //   // Set the helper objective
  //   helper = xacc::getService<qcor::ObjectiveFunction>(helper_name);

  //   // Store the observable pointer and give it to the helper
  //   observable = qq;
  //   options = opts;
  //   options.insert("observable", observable);
  //   helper->set_options(options);
  //   helper->update_observable(observable);

  //   // Extract the QJIT source code
  //   auto src = py_kernel.attr("get_internal_src")().cast<std::string>();
  //   auto extra_cpp_src =
  //       py_kernel.attr("get_extra_cpp_code")().cast<std::string>();
  //   auto sorted_kernel_deps = py_kernel.attr("get_sorted_kernels_deps")()
  //                                 .cast<std::vector<std::string>>();

  //   // QJIT compile
  //   // this will be fast if already done, and we just do it once
  //   qjit.jit_compile(src, true, sorted_kernel_deps, extra_cpp_src);
  //   qjit.write_cache();
  // }
Mccaskey, Alex's avatar
Mccaskey, Alex committed
308
309
  // Evaluate this ObjectiveFunction at the dictionary of kernel args,
  // return the scalar value
310
  double operator()(const KernelArgDict args, std::vector<double> &dx) {
311
312
    std::function<std::shared_ptr<qcor::CompositeInstruction>(
        std::vector<double>)>
313
        kernel_evaluator = [&](std::vector<double> x) {
314
315
          // qreg = ::qalloc(observable->nBits());
          // std::cout << "Allocating " << qreg.name() << "\n";
316
317
318
319
320
321
322
323
          auto _args =
              py_kernel.attr("translate")(qreg, x).cast<KernelArgDict>();
          // Map the kernel args to a hetmap
          xacc::HeterogeneousMap m;
          for (auto &item : _args) {
            KernelArgDictToHeterogeneousMap vis(m, item.first);
            mpark::visit(vis, item.second);
          }
324

325
326
327
328
329
330
331
          // Get the kernel as a CompositeInstruction
          auto kernel_name =
              py_kernel.attr("kernel_name")().cast<std::string>();
          return qjit.extract_composite_with_hetmap(kernel_name, m);
        };

    kernel = kernel_evaluator(current_iterate_parameters);
332
    helper->update_kernel(kernel);
333
    helper->update_options("kernel-evaluator", kernel_evaluator);
334

335
336
    return (*helper)(qreg, dx);
  }
337
338

  // Evaluate this ObjectiveFunction at the parameters x
339
340
341
342
343
344
  double operator()(const std::vector<double> &x,
                    std::vector<double> &dx) override {
    current_iterate_parameters = x;
    helper->update_current_iterate_parameters(x);

    // Translate x into kernel args
345
    // qreg = ::qalloc(observable->nBits());
346
347
    auto args = py_kernel.attr("translate")(qreg, x).cast<KernelArgDict>();
    // args will be a dictionary, arg_name to arg
348
    return operator()(args, dx);
349
  }
350

351
352
353
354
355
  virtual double operator()(xacc::internal_compiler::qreg &qreg,
                            std::vector<double> &dx) {
    throw std::bad_function_call();
    return 0.0;
  }
356
357

  xacc::internal_compiler::qreg get_qreg() override { return qreg; }
358
359
};

Mccaskey, Alex's avatar
Mccaskey, Alex committed
360
361
362
363
364
// PyKernelFunctor is a subtype of KernelFunctor from the qsim library
// that returns a CompositeInstruction representation of a pythonic
// quantum kernel given a vector of parameters x. This will
// leverage the QJIT infrastructure to create executable functor
// representation of the python kernel.
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
class PyKernelFunctor : public qcor::KernelFunctor {
 protected:
  py::object py_kernel;
  QJIT qjit;
  std::size_t n_qubits;

 public:
  PyKernelFunctor(py::object q, const std::size_t nq, const std::size_t np)
      : py_kernel(q), n_qubits(nq) {
    nbParams = np;
    auto src = py_kernel.attr("get_internal_src")().cast<std::string>();
    auto extra_cpp_src =
        py_kernel.attr("get_extra_cpp_code")().cast<std::string>();
    auto sorted_kernel_deps = py_kernel.attr("get_sorted_kernels_deps")()
                                  .cast<std::vector<std::string>>();

    // this will be fast if already done, and we just do it once
    qjit.jit_compile(src, true, sorted_kernel_deps, extra_cpp_src);
    qjit.write_cache();
  }

  // Delegate to QJIT to create a CompositeInstruction representation
  // of the pythonic quantum kernel.
  std::shared_ptr<qcor::CompositeInstruction> evaluate_kernel(
      const std::vector<double> &x) override {
    // Translate x into kernel args
    auto qreg = ::qalloc(n_qubits);
    auto args = py_kernel.attr("translate")(qreg, x).cast<KernelArgDict>();
    xacc::HeterogeneousMap m;
    for (auto &item : args) {
      KernelArgDictToHeterogeneousMap vis(m, item.first);
      mpark::visit(vis, item.second);
    }
    auto kernel_name = py_kernel.attr("kernel_name")().cast<std::string>();
    return qjit.extract_composite_with_hetmap(kernel_name, m);
  }
};
402
403
}  // namespace qcor

404
405
PYBIND11_MODULE(_pyqcor, m) {
  m.doc() = "Python bindings for QCOR.";
406
407
408
409
410
411
412
413
414
415

  py::class_<AllowedKernelArgTypes>(
      m, "AllowedKernelArgTypes",
      "The AllowedKernelArgTypes provides a variant structure "
      "to provide parameters to qcor quantum kernels HeterogeneousMaps.")
      .def(py::init<int>(), "Construct as an int.")
      .def(py::init<bool>(), "Construct as a bool")
      .def(py::init<double>(), "Construct as a double.")
      .def(py::init<std::string>(), "Construct as a string.")
      .def(py::init<xacc::internal_compiler::qreg>(), "Construct as qreg")
416
      .def(py::init<std::vector<double>>(), "Construct as a List[double].");
417

418
  // Expose QCOR API functions
419
  // Handle QCOR CLI arguments:
420
  // when using via Python, we use this to set those runtime parameters.
421
  m.def(
422
423
424
      "Initialize",
      [](py::kwargs kwargs) {
        if (kwargs) {
425
426
427
428
429
430
          // QRT (if provided) must be set before quantum::initialize
          if (kwargs.contains("qrt")) {
            const auto value = std::string(py::str(kwargs["qrt"]));
            // QRT (if provided) should be set before quantum::initialize
            ::quantum::set_qrt(value);
          }
431

432
433
434
435
          for (auto arg : kwargs) {
            const auto key = std::string(py::str(arg.first));
            // Handle "qpu" key
            if (key == "qpu") {
436
437
438
439
440
              const auto value = std::string(py::str(arg.second));
              ::quantum::initialize(value, "empty");
            } else if (key == "shots") {
              const auto value = arg.second.cast<int>();
              ::quantum::set_shots(value);
441
442
443
444
445
446
447
448
449
450
451
452
453
454
            } else if (key == "opt") {
              const auto value = arg.second.cast<int>();
              xacc::internal_compiler::__opt_level = value;
            } else if (key == "print-opt-stats") {
              const auto value = arg.second.cast<bool>();
              xacc::internal_compiler::__print_opt_stats = value;
            } else if (key == "placement") {
              const auto value = std::string(py::str(arg.second));
              xacc::internal_compiler::__placement_name = value;
            } else if (key == "opt-pass") {
              const auto value = std::string(py::str(arg.second));
              xacc::internal_compiler::__user_opt_passes = value;
            } else if (key == "qubit-map") {
              const auto value = std::string(py::str(arg.second));
455
456
              xacc::internal_compiler::__qubit_map =
                  xacc::internal_compiler::parse_qubit_map(value.c_str());
457
458
459
            }
            /// TODO: handle other CLI parameters.
          }
460
        }
461
462
      },
      "Initialize QCOR runtime environment.");
463

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
  py::class_<qcor::Optimizer, std::shared_ptr<qcor::Optimizer>>(
      m, "Optimizer",
      "The Optimizer interface provides optimization routine implementations "
      "for use in algorithms.")
      .def(py::init<>(), "")
      .def("name", &qcor::Optimizer::name, "")
      .def(
          "optimize",
          [&](qcor::Optimizer &o, py::function &f, const int ndim) {
            qcor::ObjectiveFunction opt(
                [&](const std::vector<double> &x, std::vector<double> &grad) {
                  auto ret = f(x);
                  if (py::isinstance<py::tuple>(ret)) {
                    auto result =
                        ret.cast<std::pair<double, std::vector<double>>>();
                    for (int i = 0; i < grad.size(); i++) {
                      grad[i] = result.second[i];
                    }
                    return result.first;
                  } else {
                    return ret.cast<double>();
                  }
                },
                ndim);
            return o.optimize(opt);
          },
          "")
      .def(
          "optimize",
          [&](qcor::Optimizer &o, py::object &f) {
            if (!py::hasattr(f, "__call__")) {
              xacc::error(
                  "Invalid object passed to optimizer optimize, must have "
                  "__call__ implemented.");
            }
            if (!py::hasattr(f, "dimensions")) {
              xacc::error(
                  "Invalid object passed to optimizer optimize, must have "
                  "dimensions() implemented.");
            }
            qcor::ObjectiveFunction opt(
                [&](const std::vector<double> &x, std::vector<double> &grad) {
                  if (grad.empty()) {
                    return f.attr("__call__")(x).cast<double>();
                  } else {
                    auto result =
                        f.attr("__call__")(x, grad)
                            .cast<std::pair<double, std::vector<double>>>();
                    grad = result.second;
                    return result.first;
                  }
                },
                f.attr("dimensions")().cast<int>());
            return o.optimize(opt);
          },
          "");

  py::class_<qcor::Operator>(m, "Operator")
      .def(py::init<const std::string &, const std::string &>())
      .def(py::self + py::self)
      .def(py::self += py::self)
      .def(py::self *= py::self)
      .def(py::self *= double())
      .def(py::self * double())
      .def(double() * py::self)
      .def("__add__", [](const qcor::Operator &op,
                         double idCoeff) { return op + idCoeff; })
      .def("__radd__", [](const qcor::Operator &op,
                          double idCoeff) { return op + idCoeff; })
      .def("__sub__", [](const qcor::Operator &op,
                         double idCoeff) { return op - idCoeff; })
      .def("__rsub__", [](const qcor::Operator &op,
                          double idCoeff) { return idCoeff - op; })
      .def(py::self * py::self)
      .def(py::self *= std::complex<double>())
      .def(py::self * std::complex<double>())
      .def(std::complex<double>() * py::self)
      .def(py::self -= py::self)
      .def(py::self - py::self)
      .def("__eq__", &qcor::Operator::operator==)
      .def("__repr__", &qcor::Operator::toString)
      .def("toString", &qcor::Operator::toString)
      .def("toBinaryVectors", &qcor::Operator::toBinaryVectors)
      .def("nQubits", &qcor::Operator::nQubits)
      .def("coefficient", &qcor::Operator::coefficient)
      .def("nBits", &qcor::Operator::nBits)
      .def("getNonIdentitySubTerms", &qcor::Operator::getNonIdentitySubTerms)
      .def("getIdentitySubTerm", &qcor::Operator::getIdentitySubTerm)
      .def("to_numpy", [](qcor::Operator &op) {
        auto mat_el = op.to_sparse_matrix();
        auto size = std::pow(2, op.nBits());
        Eigen::MatrixXcd mat = Eigen::MatrixXcd::Zero(size, size);
        for (auto el : mat_el) {
          mat(el.row(), el.col()) = el.coeff();
        }
        return mat;
      });

  py::class_<qcor::CompositeInstruction,
             std::shared_ptr<qcor::CompositeInstruction>>(
      m, "CompositeInstruction", "")
      .def("nLogicalBits", &qcor::CompositeInstruction::nLogicalBits, "")
      .def("nPhysicalBits", &qcor::CompositeInstruction::nPhysicalBits, "")
      .def("nInstructions", &qcor::CompositeInstruction::nInstructions, "")
      .def("getInstruction", &qcor::CompositeInstruction::getInstruction, "")
      .def("getInstructions", &qcor::CompositeInstruction::getInstructions, "")
      // .def("removeInstruction",
      // &qcor::CompositeInstruction::removeInstruction,
      //      "")
      // .def("replaceInstruction",
      //      &qcor::CompositeInstruction::replaceInstruction, "")
      // .def("insertInstruction",
      // &qcor::CompositeInstruction::insertInstruction,
      //      "")
      .def("__str__", &qcor::CompositeInstruction::toString, "")
      .def("name", &qcor::CompositeInstruction::name, "")
      .def("toString", &qcor::CompositeInstruction::toString, "")
      .def("depth", &qcor::CompositeInstruction::depth, "")
      .def("as_xacc", &qcor::CompositeInstruction::as_xacc, "");

584
585
586
  // Expose QCOR API functions
  m.def(
      "createOptimizer",
587
588
      [](const std::string &name, PyHeterogeneousMap p = {}) {
        return qcor::createOptimizer(name, heterogeneousMapConvert(p));
589
      },
590
      py::arg("name"), py::arg("p") = PyHeterogeneousMap(),
591
592
      py::return_value_policy::reference,
      "Return the Optimizer with given name.");
593
594
  m.def("set_verbose", &qcor::set_verbose, "");
  m.def("createTransformation", &qcor::createTransformation, "");
595
596
  m.def(
      "set_qpu",
Mccaskey, Alex's avatar
Mccaskey, Alex committed
597
598
599
      [](const std::string &name, PyHeterogeneousMap p = {}) {
        xacc::internal_compiler::qpu =
            xacc::getAccelerator(name, heterogeneousMapConvert(p));
600
      },
Mccaskey, Alex's avatar
Mccaskey, Alex committed
601
      py::arg("name"), py::arg("p") = PyHeterogeneousMap(),
602
603
      "Set the QPU backend.");

604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
  m.def(
      "set_opt_level",
      [](int level) { xacc::internal_compiler::__opt_level = level; },
      py::arg("level"), "Set QCOR runtime optimization level.");

  m.def(
      "add_pass",
      [](const std::string &pass_name) {
        // Note: we expect __user_opt_passes to be a comma-separated list of
        // pass names.
        if (xacc::internal_compiler::__user_opt_passes.empty()) {
          xacc::internal_compiler::__user_opt_passes = pass_name;
        } else {
          xacc::internal_compiler::__user_opt_passes += ("," + pass_name);
        }
      },
      py::arg("pass_name"),
      "Add an optimization pass to be run by the PassManager.");

  m.def(
      "get_placement_names",
      []() {
        std::vector<std::string> result;
627
        auto ir_transforms = xacc::getServices<qcor::IRTransformation>();
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
        for (const auto &plugin : ir_transforms) {
          if (plugin->type() == xacc::IRTransformationType::Placement) {
            result.emplace_back(plugin->name());
          }
        }
        return result;
      },
      "Get names of all available placement plugins.");

  m.def(
      "set_placement",
      [](const std::string &placement_name) {
        xacc::internal_compiler::__placement_name = placement_name;
      },
      py::arg("placement_name"), "Set the placement strategy.");

644
645
646
  m.def(
      "qalloc", [](int size) { return ::qalloc(size); },
      "Allocate qubit register.");
647
  m.def("set_shots", &qcor::set_shots, "");
648
649
650
651
652
  m.def("set_validate",
        [](bool validate) {
          xacc::internal_compiler::__validate_nisq_execution = validate;
        },
        "Enable/disable backend execution validation.");
653
  py::class_<xacc::internal_compiler::qubit>(m, "qubit", "");
654
655
  py::class_<xacc::internal_compiler::qreg>(m, "qreg", "")
      .def("size", &xacc::internal_compiler::qreg::size, "")
Mccaskey, Alex's avatar
Mccaskey, Alex committed
656
657
      .def("print", &xacc::internal_compiler::qreg::print, "")
      .def("counts", &xacc::internal_compiler::qreg::counts, "")
658
659
660
661
662
663
664
665
      .def(
          "extract_range",
          [](xacc::internal_compiler::qreg &q, std::size_t start,
             std::size_t end) {
            std::vector<std::size_t> r{start, end};
            return q.extract_range(r);
          },
          "")
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
      .def(
          "head", [](xacc::internal_compiler::qreg &q) { return q.head(); }, "")
      .def(
          "head",
          [](xacc::internal_compiler::qreg &q, const std::size_t n) {
            return q.head(n);
          },
          "")
      .def(
          "tail", [](xacc::internal_compiler::qreg &q) { return q.tail(); }, "")
      .def(
          "tail",
          [](xacc::internal_compiler::qreg &q, const std::size_t n) {
            return q.tail(n);
          },
          "")
682
      .def("exp_val_z", &xacc::internal_compiler::qreg::exp_val_z, "")
683
684
685
686
687
688
689
      .def(
          "results",
          [](xacc::internal_compiler::qreg &q) {
            auto buffer = q.results_shared();
            return buffer;
          },
          "")
690
691
692
693
694
      .def(
          "getInformation",
          [](xacc::internal_compiler::qreg &q, const std::string &key) {
            return q.results()->getInformation(key);
          },
695
696
697
698
          "")
      .def(
          "__getitem__",
          [](xacc::internal_compiler::qreg &q, int index) { return q[index]; },
699
          "");
700
  // m.def("createObjectiveFunction", [](const std::string name, ))
701
702
  py::class_<qcor::QJIT, std::shared_ptr<qcor::QJIT>>(m, "QJIT", "")
      .def(py::init<>(), "")
703
      .def("write_cache", &qcor::QJIT::write_cache, "")
704
705
706
707
708
709
710
      .def(
          "jit_compile",
          [](qcor::QJIT &qjit, const std::string src) {
            bool turn_on_hetmap_kernel_ctor = true;
            qjit.jit_compile(src, turn_on_hetmap_kernel_ctor, {});
          },
          "")
Mccaskey, Alex's avatar
Mccaskey, Alex committed
711
712
      .def(
          "internal_python_jit_compile",
713
          [](qcor::QJIT &qjit, const std::string src,
714
             const std::vector<std::string> &dependency = {},
715
716
             const std::string &extra_cpp_code = "",
             std::vector<std::string> extra_headers = {}) {
Mccaskey, Alex's avatar
Mccaskey, Alex committed
717
            bool turn_on_hetmap_kernel_ctor = true;
718
            qjit.jit_compile(src, turn_on_hetmap_kernel_ctor, dependency,
719
                             extra_cpp_code, extra_headers);
Mccaskey, Alex's avatar
Mccaskey, Alex committed
720
721
          },
          "")
722
723
724
725
726
727
      .def(
          "run_syntax_handler",
          [](qcor::QJIT &qjit, const std::string src) {
            return qjit.run_syntax_handler(src, true);
          },
          "")
728
729
730
731
732
733
734
735
736
737
      .def(
          "invoke",
          [](qcor::QJIT &qjit, const std::string name, KernelArgDict args) {
            xacc::HeterogeneousMap m;
            for (auto &item : args) {
              KernelArgDictToHeterogeneousMap vis(m, item.first);
              mpark::visit(vis, item.second);
            }
            qjit.invoke_with_hetmap(name, m);
          },
738
          "")
739

740
741
742
743
744
745
746
747
      .def("extract_composite",
           [](qcor::QJIT &qjit, const std::string name, KernelArgDict args) {
             xacc::HeterogeneousMap m;
             for (auto &item : args) {
               KernelArgDictToHeterogeneousMap vis(m, item.first);
               mpark::visit(vis, item.second);
             }
             return qjit.extract_composite_with_hetmap(name, m);
748
749
750
751
752
753
754
755
756
           })
      .def("internal_as_unitary",
           [](qcor::QJIT &qjit, const std::string name, KernelArgDict args) {
             xacc::HeterogeneousMap m;
             for (auto &item : args) {
               KernelArgDictToHeterogeneousMap vis(m, item.first);
               mpark::visit(vis, item.second);
             }
             auto composite = qjit.extract_composite_with_hetmap(name, m);
757
758
759
760
761
762
763
764
765
766
767
768
             return qcor::__internal__::map_composite_to_unitary_matrix(
                 composite);
             //  auto n_qubits = composite->nLogicalBits();
             //  qcor::KernelToUnitaryVisitor visitor(n_qubits);
             //  InstructionIterator iter(composite);
             //  while (iter.hasNext()) {
             //    auto inst = iter.next();
             //    if (!inst->isComposite() && inst->isEnabled()) {
             //      inst->accept(&visitor);
             //    }
             //  }
             //  return visitor.getMat();
769
770
771
772
773
774
           })
      .def(
          "get_kernel_function_ptr",
          [](qcor::QJIT &qjit, const std::string &kernel_name) {
            return qjit.get_kernel_function_ptr(kernel_name);
          },
775
776
777
778
779
780
781
782
783
784
785
786
787
788
          "")
      .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));
          },
789
          "");
790
791
792
793
794
795
796
797
798

  py::class_<qcor::ObjectiveFunction, std::shared_ptr<qcor::ObjectiveFunction>>(
      m, "ObjectiveFunction", "")
      .def("dimensions", &qcor::ObjectiveFunction::dimensions, "")
      .def(
          "__call__",
          [](qcor::ObjectiveFunction &obj, std::vector<double> x) {
            return obj(x);
          },
799
800
801
802
803
804
805
806
          "")
      .def(
          "__call__",
          [](qcor::ObjectiveFunction &obj, std::vector<double> x,
             std::vector<double> &dx) {
            auto val = obj(x, dx);
            return std::make_pair(val, dx);
          },
807
808
          "")
      .def("get_qreg", &qcor::ObjectiveFunction::get_qreg, "");
809

810
811
  m.def(
      "createObjectiveFunction",
812
      [](py::object kernel, qcor::Operator &obs, const int n_params) {
813
814
815
816
817
818
819
        auto q = ::qalloc(obs.nBits());
        std::shared_ptr<qcor::ObjectiveFunction> obj =
            std::make_shared<qcor::PyObjectiveFunction>(kernel, obs, n_params,
                                                        "vqe");
        return obj;
      },
      "");
820
821
822
  m.def(
      "createObjectiveFunction",
      [](py::object kernel, py::object &py_obs, const int n_params) {
823
        auto obs = convertToQCOROperator(py_obs);
824
        auto q = ::qalloc(obs.nBits());
825
        std::shared_ptr<qcor::ObjectiveFunction> obj =
826
827
828
829
830
            std::make_shared<qcor::PyObjectiveFunction>(kernel, obs, n_params,
                                                        "vqe");
        return obj;
      },
      "");
831
832
  m.def(
      "createObjectiveFunction",
833
      [](py::object kernel, qcor::Operator &obs, const int n_params,
834
835
836
837
838
839
840
841
842
         PyHeterogeneousMap &options) {
        auto nativeHetMap = heterogeneousMapConvert(options);
        auto q = ::qalloc(obs.nBits());
        std::shared_ptr<qcor::ObjectiveFunction> obj =
            std::make_shared<qcor::PyObjectiveFunction>(kernel, obs, n_params,
                                                        "vqe", nativeHetMap);
        return obj;
      },
      "");
843
844
845
846
847
  m.def(
      "createObjectiveFunction",
      [](py::object kernel, py::object &py_obs, const int n_params,
         PyHeterogeneousMap &options) {
        auto nativeHetMap = heterogeneousMapConvert(options);
848
        auto obs = convertToQCOROperator(py_obs);
849
        auto q = ::qalloc(obs.nBits());
850
851
852
853
854
855
        std::shared_ptr<qcor::ObjectiveFunction> obj =
            std::make_shared<qcor::PyObjectiveFunction>(kernel, obs, n_params,
                                                        "vqe", nativeHetMap);
        return obj;
      },
      "");
856

857
858
859
860
861
862
  m.def(
      "createOperator",
      [](const std::string &repr) { return qcor::createOperator(repr); }, "");
  m.def(
      "createOperator",
      [](const std::string &type, const std::string &repr) {
863
        return qcor::createOperator(type, repr);
864
865
866
867
868
869
870
871
872
      },
      "");
  m.def(
      "createOperator",
      [](const std::string &type, PyHeterogeneousMap &options) {
        auto nativeHetMap = heterogeneousMapConvert(options);
        return qcor::createOperator(type, nativeHetMap);
      },
      "");
873
874
875
  m.def("createOperator", [](const std::string &type, py::object pyobject) {
    return convertToQCOROperator(pyobject, type == "fermion");
  });
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
  m.def(
      "createObservable",
      [](const std::string &repr) { return qcor::createOperator(repr); }, "");
  m.def(
      "createObservable",
      [](const std::string &type, const std::string &repr) {
        return qcor::createOperator(type, repr);
      },
      "");
  m.def(
      "createObservable",
      [](const std::string &type, PyHeterogeneousMap &options) {
        auto nativeHetMap = heterogeneousMapConvert(options);
        return qcor::createOperator(type, nativeHetMap);
      },
      "");

893
894
  m.def(
      "operatorTransform",
895
      [](const std::string &type, qcor::Operator &obs) {
896
897
898
        return qcor::operatorTransform(type, obs);
      },
      "");
899
900
  m.def(
      "internal_observe",
901
902
      [](std::shared_ptr<qcor::CompositeInstruction> kernel,
         qcor::Operator &obs) {
903
904
905
906
        auto q = ::qalloc(obs.nBits());
        return qcor::observe(kernel, obs, q);
      },
      "");
907
908
  m.def(
      "internal_observe",
909
      [](std::shared_ptr<qcor::CompositeInstruction> kernel, py::object obs) {
910
        auto observable = convertToQCOROperator(obs);
911
        auto q = ::qalloc(observable.nBits());
912
913
914
        return qcor::observe(kernel, observable, q);
      },
      "");
915
916
917
918
919
920
921
922
923
924
925
926
927
  m.def("internal_observe",
        [](std::shared_ptr<qcor::CompositeInstruction> kernel,
           qcor::Operator &obs, xacc::internal_compiler::qreg &q) {
          return qcor::observe(kernel, obs, q);
        },
        "");
  m.def("internal_observe",
        [](std::shared_ptr<qcor::CompositeInstruction> kernel, py::object obs,
           xacc::internal_compiler::qreg &q) {
          auto observable = convertToQCOROperator(obs);
          return qcor::observe(kernel, observable, q);
        },
        "");
928
929
  m.def(
      "internal_autograd",
930
      [](py::function &kernel_eval, qcor::Operator &obs,
931
932
         std::vector<double> x) -> std::tuple<double, std::vector<double>> {
        try {
933
          std::function<std::shared_ptr<qcor::CompositeInstruction>(
934
935
936
              std::vector<double>)>
              cpp_kernel_eval = [&](std::vector<double> x_vec) {
                auto ret = kernel_eval(x_vec);
937
938
                auto kernel =
                    ret.cast<std::shared_ptr<qcor::CompositeInstruction>>();
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
                return kernel;
              };

          auto gradiend_method = qcor::__internal__::get_gradient_method(
              qcor::__internal__::DEFAULT_GRADIENT_METHOD, cpp_kernel_eval,
              obs);

          auto program = cpp_kernel_eval(x);
          auto q = ::qalloc(
              std::max((int)obs.nBits(), (int)program->nPhysicalBits()));
          auto cost_val = qcor::observe(program, obs, q);
          auto dx = (*gradiend_method)(x, cost_val);
          return std::make_tuple(cost_val, dx);
        } catch (std::exception &e) {
          qcor::error("Invalid kernel evaluator.");
          return std::make_tuple(0.0, std::vector<double>{});
        }
      },
      "");
958

959
  m.def("internal_get_all_instructions", []() -> std::vector<py::tuple> {
960
    auto insts = xacc::getServices<xacc::Instruction>();
961
962
963
    std::vector<py::tuple> ret;
    for (auto inst : insts) {
      if (!inst->isComposite()) {
964
965
        ret.push_back(py::make_tuple(inst->name(), inst->nRequiredBits(),
                                     inst->isParameterized()));
966
967
968
969
970
      }
    }
    return ret;
  });

971
#ifdef QCOR_BUILD_MLIR_PYTHON_API
972
973
  m.def("openqasm_to_mlir",
        [](const std::string &oqasm_src, const std::string &kernel_name,
974
975
976
977
978
           bool add_entry_point, int opt_level, bool qiskit_compat) {
          std::map<std::string, std::string> extra_args;
          if (qiskit_compat) {
            extra_args.insert({"qiskit_compat", "true"});
          }
979
          return qcor::mlir_compile(oqasm_src, kernel_name,
980
981
                                    qcor::OutputType::MLIR, add_entry_point,
                                    opt_level, extra_args);
982
983
        });

984
  m.def("openqasm_to_llvm_mlir",
985
        [](const std::string &oqasm_src, const std::string &kernel_name,
986
987
988
989
990
           bool add_entry_point, int opt_level, bool qiskit_compat) {
          std::map<std::string, std::string> extra_args;
          if (qiskit_compat) {
            extra_args.insert({"qiskit_compat", "true"});
          }
991
          return qcor::mlir_compile(oqasm_src, kernel_name,
992
993
                                    qcor::OutputType::LLVMMLIR, add_entry_point,
                                    opt_level, extra_args);
994
        });
995
996
997
998
999
1000

  m.def("openqasm_to_llvm_ir", [](const std::string &oqasm_src,
                                  const std::string &kernel_name,
                                  bool add_entry_point, int opt_level, bool qiskit_compat) {
    std::map<std::string, std::string> extra_args;
    if (qiskit_compat) {
For faster browsing, not all history is shown. View entire blame