RigettiAccelerator.hpp 5.78 KB
Newer Older
1
/***********************************************************************************
2
 * Copyright (c) 2017, UT-Battelle
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
 * 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
 *
 **********************************************************************************/
31
32
#ifndef QUANTUM_GATE_ACCELERATORS_RIGETTIACCELERATOR_HPP_
#define QUANTUM_GATE_ACCELERATORS_RIGETTIACCELERATOR_HPP_
33

34
#include "Accelerator.hpp"
35
36
#include "InstructionIterator.hpp"
#include "QuilVisitor.hpp"
37
38
39
#include "AsioNetworkingTool.hpp"
#include "RuntimeOptions.hpp"
#include <boost/algorithm/string.hpp>
40
#include <boost/filesystem.hpp>
41
42
43
44
45
46
47

using namespace xacc;

namespace xacc {
namespace quantum {

/**
48
49
50
51
52
53
 * The RigettiAccelerator is a QPUGate Accelerator that
 * provides an execute implementation that maps XACC IR
 * to an equivalent Quil string, and executes it on the
 * Rigetti superconducting quantum chip at api.rigetti.com/qvm
 * through Fire's HTTP Client utilities.
 *
54
 */
55
class RigettiAccelerator : virtual public Accelerator {
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public:

	/**
	 * Create, store, and return an AcceleratorBuffer with the given
	 * variable id string and of the given number of bits.
	 * The string id serves as a unique identifier
	 * for future lookups and reuse of the AcceleratorBuffer.
	 *
	 * @param varId
	 * @param size
	 * @return
	 */
	std::shared_ptr<AcceleratorBuffer> createBuffer(const std::string& varId,
			const int size);

71
72
73
74
75
76
	virtual std::shared_ptr<AcceleratorBuffer> createBuffer(
				const std::string& varId);

	virtual void initialize() {

	}
77
78
79
80
81
82
83
84
85
	/**
	 * Return true if this Accelerator can allocated
	 * NBits number of bits.
	 * @param NBits
	 * @return
	 */
	virtual bool isValidBufferSize(const int NBits);

	/**
86
87
	 * Execute the kernel on the provided AcceleratorBuffer through a
	 * HTTP Post of Quil instructions to the Rigetti QPU at api.rigetti.com/qvm
88
89
90
	 *
	 * @param ir
	 */
91
92
	virtual void execute(std::shared_ptr<AcceleratorBuffer> buffer,
			const std::shared_ptr<xacc::Function> kernel);
93

94
95
96
97
98
99
100
101
102
103
104
105
106
	/**
	 * This Accelerator models QPU Gate accelerators.
	 * @return
	 */
	virtual AcceleratorType getType() {
		return AcceleratorType::qpu_gate;
	}

	/**
	 * We have no need to transform the IR for this Accelerator,
	 * so return an empty list, for now.
	 * @return
	 */
107
108
	virtual std::vector<std::shared_ptr<IRTransformation>> getIRTransformations() {
		std::vector<std::shared_ptr<IRTransformation>> v;
109
110
111
		return v;
	}

112
113
114
115
116
	/**
	 * Return all relevant RigettiAccelerator runtime options.
	 * Users can set the api-key, execution type, and number of triels
	 * from the command line with these options.
	 */
117
118
119
	virtual std::shared_ptr<options_description> getOptions() {
		auto desc = std::make_shared<options_description>(
				"Rigetti Accelerator Options");
120
121
		desc->add_options()("rigetti-api-key", value<std::string>(),
				"Provide the Rigetti Forest API key. This is used if $HOME/.pyquil_config is not found")("rigetti-type",
122
123
				value<std::string>(),
				"Provide the execution type: multishot, wavefunction, "
124
				"multishot-measure, ping, or version.")
125
				("rigetti-trials", value<std::string>(), "Provide the number of trials to execute.");
126
127
128
		return desc;
	}

129
130
131
132
133
134
135
136
137
	RigettiAccelerator() : httpClient(std::make_shared<
			fire::util::AsioNetworkingTool<SimpleWeb::HTTPS>>(
			"api.rigetti.com", false)) {
		auto runtimeOptions = RuntimeOptions::instance();
		if (runtimeOptions->exists("rigetti-type") && (*runtimeOptions)["rigetti-type"] == "pyquillow") {
			httpClient = std::make_shared<
					fire::util::AsioNetworkingTool<SimpleWeb::HTTPS>>(
					"job.rigetti.com", false);
		}
138
139
140
141
142
143
	}

	RigettiAccelerator(std::shared_ptr<fire::util::INetworkingTool> http) :
			httpClient(http) {
	}

Mccaskey, Alex's avatar
Mccaskey, Alex committed
144
145
146
147
148
149
150
151
152
	virtual const std::string name() const {
		return "rigetti";
	}

	virtual const std::string description() const {
		return "The Rigetti Accelerator interacts with "
				"the Forest QVM to execute quantum IR.";
	}

153
154
155
156
157
	/**
	 * The destructor
	 */
	virtual ~RigettiAccelerator() {}

158
159
160
161
protected:

	std::shared_ptr<fire::util::INetworkingTool> httpClient;

162
163
164
165
166
167
168
private:

	/**
	 * Private utility to search for the Rigetti
	 * API key in $HOME/.pyquil_config, $PYQUIL_CONFIG,
	 * or --api-key command line arg
	 */
169
	void searchAPIKey(std::string& key, std::string& id);
170
171
172
173
174

	/**
	 * Private utility to search for key in the config
	 * file.
	 */
175
	void findApiKeyInFile(std::string& key, std::string& id, boost::filesystem::path &p);
176

177
178
179
};

}
180
}
181
182
183
184
185




#endif