GateFunction.hpp 6.07 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
public:

61
62
63
64
65
66
	/**
	 * The constructor, takes the function unique id and its name.
	 *
	 * @param id
	 * @param name
	 */
67
68
69
70
	GateFunction(const std::string& name) :
			functionName(name), parameters(
					std::vector<InstructionParameter> { }) {
	}
71
72
73
74
	GateFunction(const std::string& name,
			std::vector<InstructionParameter> params) :
			functionName(name), parameters(params) {
	}
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

	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));
	}
95

Mccaskey, Alex's avatar
Mccaskey, Alex committed
96
97
98
99
100
101
	/**
	 * Add an instruction to this quantum
	 * intermediate representation.
	 *
	 * @param instruction
	 */
102
103
104
	virtual void addInstruction(InstPtr instruction) {
		instructions.push_back(instruction);
	}
105

Mccaskey, Alex's avatar
Mccaskey, Alex committed
106
107
108
109
110
111
112
	/**
	 * Replace the given current quantum instruction
	 * with the new replacingInst quantum Instruction.
	 *
	 * @param currentInst
	 * @param replacingInst
	 */
113
114
115
116
	virtual void replaceInstruction(const int idx, InstPtr replacingInst) {
		std::replace(instructions.begin(), instructions.end(),
				getInstruction(idx), replacingInst);
	}
Mccaskey, Alex's avatar
Mccaskey, Alex committed
117

118
119
120
121
	/**
	 * Return the name of this function
	 * @return
	 */
122
123
124
	virtual const std::string getName() {
		return functionName;
	}
Mccaskey, Alex's avatar
Mccaskey, Alex committed
125

126
127
128
129
	/**
	 * Return the qubits this function acts on.
	 * @return
	 */
130
131
132
	virtual const std::vector<int> bits() {
		return std::vector<int> { };
	}
Mccaskey, Alex's avatar
Mccaskey, Alex committed
133

134
135
136
137
138
	/**
	 * Return an assembly-like string representation for this function .
	 * @param bufferVarName
	 * @return
	 */
139
140
141
142
143
144
145
	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
146

147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
	virtual InstructionParameter getParameter(const int idx) {
		if (idx + 1 > parameters.size()) {
			XACCError(
					"Invalid Parameter requested.");
		}

		return parameters[idx];
	}

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

		parameters[idx] = p;
	}

	virtual bool isParameterized() {
		return nParameters() > 0;
	}

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

173
174
175
176
177
178
179
180
181
	std::map<int,int> cachedVariableInstructions;

	virtual void evaluateVariableParameters(std::vector<InstructionParameter> runtimeParameters) {
		for (auto inst : instructions) {
			if (inst->isComposite()) {
				std::dynamic_pointer_cast<Function>(inst)->evaluateVariableParameters(
						runtimeParameters);
			} else {

182
				if (inst->isParameterized() && inst->getName() != "Measure") {
183
184
185
186
187
188
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

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

						// See if this is a string parameter
						if (param.which() == 3) {
							auto variable = boost::get<std::string>(param);
							// Get index

							auto it = std::find(parameters.begin(),
									parameters.end(), param);
							if (it == parameters.end()) {
								XACCError("Variable " + variable + " not found in Function parameters.");
							} else {
								auto index = std::distance(parameters.begin(),
										it);
								inst->setParameter(index, runtimeParameters[index]);
								cachedVariableInstructions.insert(std::make_pair(i, index));
							}
						} else {
							// See if we have a cached instruction
							if (cachedVariableInstructions.find(i) != cachedVariableInstructions.end()) {
								auto idx = cachedVariableInstructions[i];
								inst->setParameter(idx, runtimeParameters[idx]);
							}
						}
					}
				}
			}
		}
	}
214

215
	DEFINE_VISITABLE()
Mccaskey, Alex's avatar
Mccaskey, Alex committed
216
217
218
219

};
}
}
220
221

#endif