Accelerator.hpp 5.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/***********************************************************************************
 * Copyright (c) 2016, 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.
12
 *   * Neither the name of the xacc nor the
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 *     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
 *
 **********************************************************************************/
#ifndef XACC_ACCELERATOR_HPP_
#define XACC_ACCELERATOR_HPP_

#include <string>
35
#include "IRTransformation.hpp"
36
37
#include <array>
#include <bitset>
38
39
40

namespace xacc {

41
42
43
44
45
46
47
48
49
50
51
52
/**
 * The AcceleratorBits class provides a common
 * base class for allocating accelerator-specific
 * bit resources (for example, qubits). It takes an
 * integer template parameter at construction that indicates
 * the number of bits this AcceleratorBits models.
 *
 * Derived Accelerators should define a subclass of this
 * class that models the hardware.
 *
 * @author Alex McCaskey
 */
53
54
55
template<const int Number>
class AcceleratorBits {
public:
56
57
58
	/**
	 * Reference to the number of bits
	 */
59
	static constexpr int N = Number;
60
61
62
63
64
private:

	/**
	 *  The bits themselves
	 */
65
	std::bitset<(size_t)Number> bits;
66
67
68
69
70

	/**
	 * Return the current state of the bits
	 * @return
	 */
71
72
73
74
75
76
77
	std::bitset<(size_t)Number> toBits() {
		return bits;
	}

};

/**
78
79
80
81
82
83
84
 * The Accelerator class provides a high-level abstraction
 * for XACC's interaction with attached post-exascale
 * accelerators (quantum and neuromorphic processing units).
 *
 * Derived Accelerators must provide a valid execute implementation
 * that takes XACC IR and executes it on the attached hardware or
 * simulator.
85
 *
86
87
88
 * Derived Accelerators must provide a list of IRTransformation
 * instances that transform XACC IR to be amenable to execution
 * on the hardware.
89
 */
90
91
92
class Accelerator {

public:
93
94
95
96

	/**
	 * The types of Accelerators that XACC interacts with
	 */
97
98
	enum AcceleratorType { qpu_gate, qpu_aqc, npu };

99
100
101
102
103
	/**
	 * Return the type of this Accelerator.
	 *
	 * @return type The Accelerator type - Gate or AQC QPU, or NPU
	 */
104
105
	virtual AcceleratorType getType() = 0;

106
107
108
109
110
	/**
	 * Return any IR Transformations that must be applied to ensure
	 * the compiled IR is amenable to execution on this Accelerator.
	 * @return
	 */
111
	virtual std::vector<IRTransformation> getIRTransformations() = 0;
112

113
114
115
116
117
	/**
	 * Execute the provided XACC IR on this attached Accelerator.
	 *
	 * @param ir
	 */
118
119
120
	virtual void execute(const std::shared_ptr<IR> ir) = 0;

	/**
121
	 * Allocate bit resources (if needed).
122
	 *
123
	 * @return bits The AcceleratorBits derived type
124
125
126
127
128
129
130
131
132
133
134
135
136
	 */
	template<typename BitsType>
	BitsType allocate(const std::string& variableNameId) {
		static_assert(std::is_base_of<AcceleratorBits<BitsType::N>, BitsType>::value, "");
		if (!canAllocate(BitsType::N)) {
			QCIError("Error in allocated requested bits");
		}
		BitsType bits;
		NBitsAllocated = BitsType::N;
		bitVarId = variableNameId;
		return bits;
	}

137
138
139
140
141
142
	/**
	 * Return the number of bits that the user most recently
	 * requested.
	 *
	 * @return nBits The number of requested bits
	 */
143
144
145
	virtual int getAllocationSize() {
		return NBitsAllocated;
	}
146

147
148
149
150
151
152
	/**
	 * Return the variable name provided upon bit allocation
	 * (for example - qreg for gate model quantum bits in (qbit qreg[2];))
	 *
	 * @return varName The name of the bits allocated.
	 */
153
154
	virtual const std::string getVariableName() {
		return bitVarId;
155
156
	}

157
158
159
	/**
	 * Destructor
	 */
160
	virtual ~Accelerator() {}
161
162
163

protected:

164
165
166
167
	/**
	 * The number of bits allocated upon the most
	 * recent user request for bit resources.
	 */
168
169
	int NBitsAllocated = 0;

170
171
172
	/**
	 * The variable name of the bits
	 */
173
174
	std::string bitVarId;

175
176
177
178
179
180
	/**
	 * Return true if this Accelerator can allocate
	 * the provided number of bits.
	 * @param NBits The number of bits to allocate
	 * @return canAllocate True if can allocate, false if not.
	 */
181
	virtual bool canAllocate(const int NBits) = 0;
182
183
184
};
}
#endif