GateIR.cpp 1.96 KB
Newer Older
1
2
3
4
5
6
/*******************************************************************************
 * Copyright (c) 2017 UT-Battelle, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompanies this
 * distribution. The Eclipse Public License is available at
7
8
 * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
 *License is available at https://eclipse.org/org/documents/edl-v10.php
9
10
11
12
13
14
 *
 * Contributors:
 *   Alexander J. McCaskey - initial API and implementation
 *******************************************************************************/
#include "JsonVisitor.hpp"
#include "GateIR.hpp"
15
16
17
18
#define RAPIDJSON_HAS_STDSTRING 1

#include "rapidjson/prettywriter.h"
using namespace rapidjson;
19
20
21
22

namespace xacc {
namespace quantum {

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
std::string GateIR::toAssemblyString(const std::string &kernelName,
                                     const std::string &accBufferVarName) {
  std::string retStr = "";
  auto kernel = getKernel(kernelName);

  std::set<int> qubitsUsed;
  InstructionIterator it(kernel);
  while (it.hasNext()) {
    // Get the next node in the tree
    auto nextInst = it.next();

    // If enabled, invoke the accept
    // method which kicks off the visitor
    // to execute the appropriate lambda.
    if (nextInst->isEnabled() && !nextInst->isComposite()) {

      for (auto qi : nextInst->bits()) {
        qubitsUsed.insert(qi);
      }
    }
  }

  for (auto qi : qubitsUsed) {
    retStr += "qubit " + accBufferVarName + std::to_string(qi) + "\n";
  }
  for (auto f : kernels) {
    retStr += f->toString(accBufferVarName);
  }
  return retStr;
52
53
}

54
void GateIR::persist(std::ostream &outStream) {
55

56
  JsonVisitor<PrettyWriter<StringBuffer>,StringBuffer> visitor(kernels);
57
  outStream << visitor.write();
58

59
  return;
60
61
62
}

// FOR IR
63
64
void GateIR::load(std::istream &inStream) {
  // read(inStream);
65
66
}

67
68
} // namespace quantum
} // namespace xacc