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

59
60
61
62
63
64
	/**
	 * The constructor, takes the function unique id and its name.
	 *
	 * @param id
	 * @param name
	 */
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
	GateFunction(const std::string& name) : functionName(name) {}

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

Mccaskey, Alex's avatar
Mccaskey, Alex committed
87
88
89
90
91
92
	/**
	 * Add an instruction to this quantum
	 * intermediate representation.
	 *
	 * @param instruction
	 */
93
94
95
	virtual void addInstruction(InstPtr instruction) {
		instructions.push_back(instruction);
	}
96

Mccaskey, Alex's avatar
Mccaskey, Alex committed
97
98
99
100
101
102
103
	/**
	 * Replace the given current quantum instruction
	 * with the new replacingInst quantum Instruction.
	 *
	 * @param currentInst
	 * @param replacingInst
	 */
104
105
106
107
	virtual void replaceInstruction(const int idx, InstPtr replacingInst) {
		std::replace(instructions.begin(), instructions.end(),
				getInstruction(idx), replacingInst);
	}
Mccaskey, Alex's avatar
Mccaskey, Alex committed
108

109
110
111
112
	/**
	 * Return the name of this function
	 * @return
	 */
113
114
115
	virtual const std::string getName() {
		return functionName;
	}
Mccaskey, Alex's avatar
Mccaskey, Alex committed
116

117
118
119
120
	/**
	 * Return the qubits this function acts on.
	 * @return
	 */
121
122
123
	virtual const std::vector<int> bits() {
		return std::vector<int> { };
	}
Mccaskey, Alex's avatar
Mccaskey, Alex committed
124

125
126
127
128
129
	/**
	 * Return an assembly-like string representation for this function .
	 * @param bufferVarName
	 * @return
	 */
130
131
132
133
134
135
136
	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
137

138
	DEFINE_VISITABLE()
139

140
141
142
143
144
145
	/**
	 * This method should simply be implemented to invoke the
	 * visit() method on the provided QInstructionVisitor.
	 *
	 * @param visitor
	 */
146
//	virtual void accept(std::shared_ptr<InstructionVisitor> visitor);
147
148
149
150
151
152
153
154
//	{
//		auto v = std::dynamic_pointer_cast<GateInstructionVisitor>(visitor);
//		if (v) {
//			v->visit(*this);
//		} else {
//			visitor->visit(*this);
//		}
//	}
Mccaskey, Alex's avatar
Mccaskey, Alex committed
155
156

};
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

///**
// */
//template<typename... Ts>
//using GateFunctionRegistry = Registry<GateFunction, Ts...>;
//
///**
// */
//template<typename T, typename... Ts>
//class RegisterGateFunction {
//public:
//	RegisterGateFunction(const std::string& name) {
//		GateFunctionRegistry<Ts...>::instance()->add(name,
//				(std::function<
//						std::shared_ptr<xacc::quantum::GateFunction>(
//								Ts...)>) ([](Ts... args) {
//					return std::make_shared<T>(args...);
//				}));
//	}
//};
Mccaskey, Alex's avatar
Mccaskey, Alex committed
177
178
}
}
179
180

#endif