DWKernel.hpp 4.49 KB
Newer Older
1
2
3
4
5
6
7
8
/*******************************************************************************
 * 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
9
10
 *
 * Contributors:
11
12
 *   Alexander J. McCaskey - initial API and implementation
 *******************************************************************************/
13
14
15
16
17
#ifndef QUANTUM_AQC_DWKERNEL_HPP_
#define QUANTUM_AQC_DWKERNEL_HPP_

#include "Function.hpp"
#include "DWQMI.hpp"
18
#include "XACC.hpp"
19
20
21
22
23

namespace xacc {
namespace quantum {

/**
24
25
 * The DWKernel is an XACC Function that contains
 * DWQMI Instructions.
26
 */
27
class DWKernel: public virtual Function, public std::enable_shared_from_this<DWKernel> {
28
29
30
31
32

protected:

	std::list<InstPtr> instructions;

33
	std::string _name;
34

35
36
37
38
39
public:

	/**
	 * The constructor, takes the function unique id and its name.
	 *
40
	 *
41
42
43
	 * @param id
	 * @param name
	 */
44
	DWKernel(std::string kernelName) : _name(kernelName) {
45
46
47
48
49
50
51
	}

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

	virtual InstPtr getInstruction(const int idx) {
52
		InstPtr i;
53
		if (instructions.size() > idx) {
54
			i = *std::next(instructions.begin(), idx);
55
		} else {
56
			xacc::error("DWKernel getInstruction invalid instruction index - " + std::to_string(idx) + ".");
57
		}
58
		return i;
59
60
61
62
63
64
65
66
67
68
	}

	virtual std::list<InstPtr> getInstructions() {
		return instructions;
	}

	virtual void removeInstruction(const int idx) {
		instructions.remove(getInstruction(idx));
	}

69
70
71
72
	virtual const std::string getTag() {
		return "";
	}

73
74
	virtual void mapBits(std::vector<int> bitMap) {
	}
75

76
77
78
79
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
	/**
	 * Add an instruction to this quantum
	 * intermediate representation.
	 *
	 * @param instruction
	 */
	virtual void addInstruction(InstPtr instruction) {
		instructions.push_back(instruction);
	}

	/**
	 * Replace the given current quantum instruction
	 * with the new replacingInst quantum Instruction.
	 *
	 * @param currentInst
	 * @param replacingInst
	 */
	virtual void replaceInstruction(const int idx, InstPtr replacingInst) {
		std::replace(instructions.begin(), instructions.end(),
				getInstruction(idx), replacingInst);
	}

	virtual void insertInstruction(const int idx, InstPtr newInst) {
		auto iter = std::next(instructions.begin(), idx);
		instructions.insert(iter, newInst);
	}

	/**
	 * Return the name of this function
	 * @return
	 */
107
108
109
110
111
112
	virtual const std::string name() const {
		return _name;
	}

	virtual const std::string description() const {
		return "";
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
	}

	/**
	 * Return the qubits this function acts on.
	 * @return
	 */
	virtual const std::vector<int> bits() {
		return std::vector<int> { };
	}

	/**
	 * Return an assembly-like string representation for this function .
	 * @param bufferVarName
	 * @return
	 */
	virtual const std::string toString(const std::string& bufferVarName) {
		std::stringstream ss;
		for (auto i : instructions) {
			ss << i->toString("") << "\n";
		}
		return ss.str();
	}

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
	std::vector<double> getAllBiases() {
		std::vector<double> biases;
		for (auto i : instructions) {
			if (i->bits()[0] == i->bits()[1]) {
				biases.push_back(boost::get<double>(i->getParameter(0)));
			}
		}

		return biases;
	}

	std::vector<double> getAllCouplers() {
		std::vector<double> weights;
		for (auto i : instructions) {
			if (i->bits()[0] != i->bits()[1]) {
				weights.push_back(boost::get<double>(i->getParameter(0)));
			}
		}

		return weights;
	}

158
	virtual InstructionParameter getParameter(const int idx) const {
159
160
		xacc::error("DWKernel does not contain runtime parameters.");
		return InstructionParameter(0);
161
162
163
	}

	virtual void setParameter(const int idx, InstructionParameter& p) {
164
		xacc::error("DWKernel does not contain runtime parameters.");
165
166
167
	}

	virtual std::vector<InstructionParameter> getParameters() {
168
169
		xacc::error("DWKernel does not contain runtime parameters.");
		return {};
170
171
	}

172
	virtual void addParameter(InstructionParameter instParam) {
173
		xacc::error("DWKernel does not contain runtime parameters.");
174
175
	}

176
177
178
179
180
181
182
183
184
	virtual bool isParameterized() {
		return false;
	}

	virtual const int nParameters() {
		return 0;
	}

	virtual void evaluateVariableParameters(std::vector<InstructionParameter> parameters) {
185
		xacc::error("DWKernel does not contain runtime parameters.");
186
187
188
189
190
191
192
193
194
195
	}

	EMPTY_DEFINE_VISITABLE()

};

}
}

#endif