Circuit.hpp 7.59 KB
Newer Older
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
/*******************************************************************************
 * 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
 * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
 *License is available at https://eclipse.org/org/documents/edl-v10.php
 *
 * Contributors:
 *   Alexander J. McCaskey - initial API and implementation
 *******************************************************************************/
#ifndef QUANTUM_GATE_IR_CIRCUIT_HPP_
#define QUANTUM_GATE_IR_CIRCUIT_HPP_

#include "CompositeInstruction.hpp"
#include "Gate.hpp"
#include "InstructionIterator.hpp"
#include "Utils.hpp"
#include "expression_parsing_util.hpp"
#include "xacc_service.hpp"
#include <memory>
#include <stdexcept>

namespace xacc {
namespace quantum {

class Circuit : public CompositeInstruction,
                public std::enable_shared_from_this<Circuit> {

private:
  void throwIfInvalidInstructionParameter(InstPtr instruction);

  void validateInstructionIndex(const std::size_t idx) {
    if (idx >= instructions.size()) {
      xacc::XACCLogger::instance()->error(
          "\nInvalid instruction index on Circuit\n"
          "CompositeInstruction:\nnInstructions() = " +
          std::to_string(nInstructions()) + "\nidx = " + std::to_string(idx));
      print_backtrace();
      exit(0);
    }
  }
  void validateInstructionPtr(InstPtr inst) {
    if (std::find(instructions.begin(), instructions.end(), inst) !=
        std::end(instructions)) {
      xacc::XACCLogger::instance()->error(
          "\nInvalid instruction:\nThis instruction pointer already added to "
          "Circuit.");
      print_backtrace();
      exit(0);
    }
  }

  void errorCircuitParameter() const {
    xacc::XACCLogger::instance()->error(
        "Circuit Instruction parameter API not implemented.");
    print_backtrace();
    exit(0);
  }

protected:
  std::vector<InstPtr> instructions{};
  std::vector<std::string> variables{};
  std::vector<std::string> _requiredKeys{};

  std::string circuitName = "";

  std::shared_ptr<ExpressionParsingUtil> parsingUtil;

71
72
  std::complex<double> coefficient = 1.0;

73
74
public:
  Circuit(const std::string &name)
75
76
77
78
79
      : circuitName(name) {}
  Circuit(const std::string &name, std::vector<std::string> &vars)
      : circuitName(name), variables(vars) {}
  Circuit(const std::string &name, std::vector<std::string> &&vars)
      : circuitName(name), variables(vars) {}
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

  Circuit(const Circuit &other)
      : circuitName(other.circuitName), variables(other.variables),
        instructions(other.instructions), parsingUtil(other.parsingUtil) {}

  const std::string name() const override { return circuitName; }
  const std::string description() const override { return ""; }

  void mapBits(std::vector<std::size_t> bitMap) override {}
  void setBits(const std::vector<std::size_t> bits) override {}
  const std::vector<std::size_t> bits() override { return {}; }

  const std::string toString() override {
    std::string retStr = "";
    for (auto i : instructions) {
      if (i->isEnabled()) {
        if (i->isComposite() &&
            !std::dynamic_pointer_cast<CompositeInstruction>(i)
                 ->hasChildren()) {
          retStr += i->name() + "()\n";
        } else {
          retStr += i->toString() + "\n";
        }
      }
    }
    return retStr;
  }

  const InstructionParameter
  getParameter(const std::size_t idx) const override {
    errorCircuitParameter();
    return InstructionParameter(0);
  }
  void setParameter(const std::size_t idx, InstructionParameter &p) override {
    errorCircuitParameter();
  }
  std::vector<InstructionParameter> getParameters() override {
    errorCircuitParameter();
    return {};
  }
  const int nParameters() override {
    errorCircuitParameter();
    return 0;
  }

  const int nRequiredBits() const override { return 0; }

  void enable() override {
    for (int i = 0; i < nInstructions(); i++) {
      getInstruction(i)->enable();
    }
  }

  void disable() override {
    for (int i = 0; i < nInstructions(); i++) {
      getInstruction(i)->disable();
    }
  }

  const bool isAnalog() const override {
    for (int i = 0; i < instructions.size(); i++) {
      auto inst = *std::next(instructions.begin(), i);
      if (inst->isAnalog()) {
        return true;
      }
    }
    return false;
  }

149
150
  void persist(std::ostream &outStream) override;
  void load(std::istream &inStream) override;
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

  const int nInstructions() override { return instructions.size(); }

  InstPtr getInstruction(const std::size_t idx) override {
    validateInstructionIndex(idx);
    return instructions[idx];
  }
  std::vector<InstPtr> getInstructions() override { return instructions; }
  void removeInstruction(const std::size_t idx) override {
    validateInstructionIndex(idx);
    instructions.erase(instructions.begin() + idx);
  }
  void replaceInstruction(const std::size_t idx, InstPtr newInst) override {
    validateInstructionIndex(idx);
    throwIfInvalidInstructionParameter(newInst);
    instructions[idx] = newInst;
  }
  void insertInstruction(const std::size_t idx, InstPtr newInst) override {
    validateInstructionIndex(idx);
    throwIfInvalidInstructionParameter(newInst);
    instructions.insert(instructions.begin() + idx, newInst);
  }

  void addInstruction(InstPtr instruction) override {
    throwIfInvalidInstructionParameter(instruction);
    validateInstructionPtr(instruction);
    instructions.push_back(instruction);
  }
  void addInstructions(std::vector<InstPtr> &insts) override {
    for (auto &i : insts)
      addInstruction(i);
  }
183
184
185
  void addInstructions(const std::vector<InstPtr>& insts) override {
      for (auto& i : insts) addInstruction(i);
  }
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

  bool hasChildren() const override { return !instructions.empty(); }
  bool expand(const HeterogeneousMap &runtimeOptions) override {
    for (auto &inst : instructions) {
      if (inst->isComposite()) {
        if (!(std::dynamic_pointer_cast<CompositeInstruction>(inst)->expand(
                runtimeOptions))) {
          return false;
        }
      }
    }
    return true;
  }

  const std::vector<std::string> requiredKeys() override {
    return _requiredKeys;
  }

  void addVariable(const std::string variableName) override {
    variables.push_back(variableName);
  }
  void addVariables(const std::vector<std::string> &vars) override {
    variables.insert(variables.end(), vars.begin(), vars.end());
  }
210
211
212
  const std::vector<std::string> getVariables() override {
      return variables;
  }
Mccaskey, Alex's avatar
Mccaskey, Alex committed
213
  const std::size_t nVariables() override {return variables.size();}
214

215
216
217
218

  const int depth() override;
  const std::string persistGraph() override;
  std::shared_ptr<Graph> toGraph() override;
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

  const std::size_t nLogicalBits() override { return 0; }
  const std::size_t nPhysicalBits() override { return 0; }

  std::shared_ptr<CompositeInstruction> enabledView() override {
    auto newF = std::make_shared<Circuit>(circuitName, variables);
    for (int i = 0; i < nInstructions(); i++) {
      auto inst = getInstruction(i);
      if (inst->isEnabled()) {
        newF->addInstruction(inst);
      }
    }
    return newF;
  }

234
235
236
237
  void setCoefficient(const std::complex<double> c) override {
    coefficient = c;
  }
  const std::complex<double> getCoefficient() override { return coefficient; }
238

239
240
241
242
243
244
245
246
247
248
249
  std::shared_ptr<CompositeInstruction>
  operator()(const std::vector<double> &params) override;

  DEFINE_VISITABLE()

  virtual ~Circuit() {}
};

} // namespace quantum
} // namespace xacc
#endif