GateFunction.hpp 6.77 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
/***********************************************************************************
 * Copyright (c) 2017, UT-Battelle
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the xacc nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors:
 *   Initial API and implementation - Alex McCaskey
 *
 **********************************************************************************/
Mccaskey, Alex's avatar
Mccaskey, Alex committed
31
32
#ifndef QUANTUM_GATEQIR_QFUNCTION_HPP_
#define QUANTUM_GATEQIR_QFUNCTION_HPP_
33

34
#include "Registry.hpp"
35
#include "Function.hpp"
36
#include "Utils.hpp"
37
38
39
40

namespace xacc {
namespace quantum {

41
42
43
44
45
/**
 * The GateFunction is a QFunction for gate-model
 * quantum computing. It is composed of QInstructions that
 * are themselves derivations of the GateInstruction class.
 */
46
class GateFunction: public virtual Function {
47
48

protected:
Mccaskey, Alex's avatar
Mccaskey, Alex committed
49

50
51
52
	/**
	 * The name of this function
	 */
Mccaskey, Alex's avatar
Mccaskey, Alex committed
53
54
	std::string functionName;

55
	std::list<InstPtr> instructions;
Mccaskey, Alex's avatar
Mccaskey, Alex committed
56

57
58
	std::vector<InstructionParameter> parameters;

59
60
61
62
63
	/**
	 * Map of Instruction Index to ( Instruction's Runtime Parameter Index, Dependent Variable name)
	 */
	std::map<int, std::pair<int, std::string>> cachedVariableInstructions;

64
65
public:

66
67
68
69
70
71
	/**
	 * The constructor, takes the function unique id and its name.
	 *
	 * @param id
	 * @param name
	 */
72
73
74
75
	GateFunction(const std::string& name) :
			functionName(name), parameters(
					std::vector<InstructionParameter> { }) {
	}
76
77
78
79
	GateFunction(const std::string& name,
			std::vector<InstructionParameter> params) :
			functionName(name), parameters(params) {
	}
80

81
82
83
84
	GateFunction(const GateFunction& other) :
			functionName(other.functionName), parameters(other.parameters) {
	}

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
	virtual const int nInstructions() {
		return instructions.size();
	}

	virtual InstPtr getInstruction(const int idx) {
		if (instructions.size() > idx) {
			return *std::next(instructions.begin(), idx);
		} else {
			XACCError("Invalid instruction index.");
		}
	}

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

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

Mccaskey, Alex's avatar
Mccaskey, Alex committed
105
106
107
108
109
110
	/**
	 * Add an instruction to this quantum
	 * intermediate representation.
	 *
	 * @param instruction
	 */
111
112
113
	virtual void addInstruction(InstPtr instruction) {
		instructions.push_back(instruction);
	}
114

Mccaskey, Alex's avatar
Mccaskey, Alex committed
115
116
117
118
119
120
121
	/**
	 * Replace the given current quantum instruction
	 * with the new replacingInst quantum Instruction.
	 *
	 * @param currentInst
	 * @param replacingInst
	 */
122
123
124
125
	virtual void replaceInstruction(const int idx, InstPtr replacingInst) {
		std::replace(instructions.begin(), instructions.end(),
				getInstruction(idx), replacingInst);
	}
Mccaskey, Alex's avatar
Mccaskey, Alex committed
126

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

132
133
134
135
	/**
	 * Return the name of this function
	 * @return
	 */
136
137
138
	virtual const std::string getName() {
		return functionName;
	}
Mccaskey, Alex's avatar
Mccaskey, Alex committed
139

140
141
142
143
	/**
	 * Return the qubits this function acts on.
	 * @return
	 */
144
145
146
	virtual const std::vector<int> bits() {
		return std::vector<int> { };
	}
Mccaskey, Alex's avatar
Mccaskey, Alex committed
147

148
149
150
151
152
	/**
	 * Return an assembly-like string representation for this function .
	 * @param bufferVarName
	 * @return
	 */
153
154
155
156
157
158
159
	virtual const std::string toString(const std::string& bufferVarName) {
		std::string retStr = "";
		for (auto i : instructions) {
			retStr += i->toString(bufferVarName) + "\n";
		}
		return retStr;
	}
Mccaskey, Alex's avatar
Mccaskey, Alex committed
160

161
	virtual InstructionParameter getParameter(const int idx) const {
162
		if (idx + 1 > parameters.size()) {
163
			XACCError("Invalid Parameter requested.");
164
165
166
167
168
169
170
		}

		return parameters[idx];
	}

	virtual void setParameter(const int idx, InstructionParameter& p) {
		if (idx + 1 > parameters.size()) {
171
			XACCError("Invalid Parameter requested.");
172
173
174
175
176
		}

		parameters[idx] = p;
	}

177
178
179
180
	virtual std::vector<InstructionParameter> getParameters() {
		return parameters;
	}

181
182
183
184
185
186
187
188
	virtual bool isParameterized() {
		return nParameters() > 0;
	}

	virtual const int nParameters() {
		return parameters.size();
	}

189

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
	virtual void evaluateVariableParameters(
			std::vector<InstructionParameter> runtimeParameters) {

		std::map<std::string, InstructionParameter> varToValMap;

		int i = 0;
		for (auto funcParam : parameters) {
			varToValMap.insert(
					std::make_pair(boost::get<std::string>(funcParam),
							runtimeParameters[i]));
			i++;
		}

		for (const auto& gateIdVarName : cachedVariableInstructions) {
			auto inst = getInstruction(gateIdVarName.first);
			auto varInstDependsOn = gateIdVarName.second.second;
			auto instParamIdx = gateIdVarName.second.first;

			int indexOfRuntimeParam, counter = 0;
			for (auto p : parameters) {
				if (boost::get<std::string>(p) == varInstDependsOn) {
					indexOfRuntimeParam = counter;
					break;
				}
				counter++;
			}

			inst->setParameter(instParamIdx, runtimeParameters[indexOfRuntimeParam]);
		}

		i = 0;
221
222
223
224
		for (auto inst : instructions) {
			if (inst->isComposite()) {
				std::dynamic_pointer_cast<Function>(inst)->evaluateVariableParameters(
						runtimeParameters);
225
226
227
228
229
230
231
232
233
234
235
236
237
238
			} else if (inst->isParameterized() && inst->getName() != "Measure") {

				for (int j = 0; j < inst->nParameters(); ++j) {
					auto instParam = inst->getParameter(j);

					if (instParam.which() == 3) {
						// This is a variable
						auto variable = boost::get<std::string>(instParam);

						auto runtimeParameter = varToValMap[variable];

						inst->setParameter(j, runtimeParameter);

						cachedVariableInstructions.insert(std::make_pair(i, std::make_pair(j, variable)));
239
240
241
					}
				}
			}
242
			i++;
243
244
		}
	}
245

246
	DEFINE_VISITABLE()
Mccaskey, Alex's avatar
Mccaskey, Alex committed
247
248

};
249

Mccaskey, Alex's avatar
Mccaskey, Alex committed
250
251
}
}
252
253

#endif