KernelReplacementPreprocessorTester.cpp 5.51 KB
Newer Older
1
2
3
4
5
6
7
8
/*******************************************************************************
 * Copyright (c) 2017 UT-Battelle, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompanies this
 * distribution. The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License
 * is available at https://eclipse.org/org/documents/edl-v10.php
9
10
 *
 * Contributors:
11
12
 *   Alexander J. McCaskey - initial API and implementation
 *******************************************************************************/
13
14
15
16
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE KernelReplacementPPTester
#include <boost/test/included/unit_test.hpp>
#include "KernelReplacementPreprocessor.hpp"
17
18
19
#include "Compiler.hpp"
#include "Accelerator.hpp"
#include "GateQIR.hpp"
20
#include "XACC.hpp"
21
22
#include "ServiceRegistry.hpp"

23
using namespace xacc;
24
25
26

using namespace xacc::quantum;

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class DummyCompiler: public xacc::Compiler {

public:
	virtual std::shared_ptr<IR> compile(const std::string& src,
			std::shared_ptr<Accelerator> acc) {
		return std::make_shared<GateQIR>();
	}

	virtual std::shared_ptr<IR> compile(const std::string& src) {
		return std::make_shared<GateQIR>();
	}

	virtual const std::string translate(const std::string& bufferVariable,
			std::shared_ptr<Function> function) {
		std::string translated = "H(qreg[2]);\n"
				"// BEGIN CPHASE GATE\n"
				"Rz(qreg[2],0.785398);\n"
				"CNOT(qreg[2],qreg[1]);\n"
				"Rz(qreg[2],-0.785398);\n"
				"CNOT(qreg[2],qreg[1]);\n"
				"// END CPHASE GATE\n"
				"H(qreg[2]);\n"
				"// BEGIN CPHASE GATE\n"
				"Rz(qreg[2],0.392699);\n"
				"CNOT(qreg[2],qreg[0]);\n"
				"Rz(qreg[2],-0.392699);\n"
				"CNOT(qreg[2],qreg[0]);\n"
				"// END CPHASE GATE\n"
				"// BEGIN CPHASE GATE\n"
				"Rz(qreg[1],0.785398);\n"
				"CNOT(qreg[1],qreg[0]);\n"
				"Rz(qreg[1],-0.785398);\n"
				"CNOT(qreg[1],qreg[0]);\n"
				"// END CPHASE GATE\n"
				"H(qreg[0]);\n"
				"// BEGIN SWAP 0 2\n"
				"CNOT(qreg[2],qreg[0]);\n"
				"CNOT(qreg[0],qreg[2]);\n"
				"CNOT(qreg[2],qreg[0]);\n"
				"// END SWAP 0 2\n";

		return translated;
	}

Mccaskey, Alex's avatar
Mccaskey, Alex committed
71
72
73
74
75
76
77
78
79
	virtual const std::string name() const {
		return "";
	}

	virtual const std::string description() const {
		return "";
	}


80
81
82
83
84
85
86
87
88
};

class DummyAccelerator : public xacc::Accelerator {
public:

	virtual AcceleratorType getType() {
		return AcceleratorType::qpu_gate;
	}

89
90
91
92
	virtual void initialize() {

	}

93
94
	virtual std::vector<std::shared_ptr<IRTransformation>> getIRTransformations() {
		std::vector<std::shared_ptr<IRTransformation>> ts;
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
		return ts;
	}

	virtual void execute(std::shared_ptr<AcceleratorBuffer> buffer,
				const std::shared_ptr<Function> function) {

	}

	virtual std::shared_ptr<AcceleratorBuffer> createBuffer(
			const std::string& varId, const int size) {
		auto b = std::make_shared<AcceleratorBuffer>(varId, 3);
		storeBuffer(varId, b);
		return b;
	}

110
111
112
113
114
115
	virtual std::shared_ptr<AcceleratorBuffer> createBuffer(
				const std::string& varId) {
			auto b = std::make_shared<AcceleratorBuffer>(varId, 3);
			storeBuffer(varId, b);
			return b;
		}
116
117
118
119
	virtual bool isValidBufferSize(const int NBits) {
		return true;
	}

Mccaskey, Alex's avatar
Mccaskey, Alex committed
120
121
122
123
124
125
126
127
	virtual const std::string name() const {
		return "";
	}

	virtual const std::string description() const {
		return "";
	}

128
129
130

};

131
BOOST_AUTO_TEST_CASE(checkSimple) {
132
133
134
	auto testPath = std::string(XACC_BUILD_DIR) + std::string("/stage/usr/local/xacc/lib/libxacc-quantum-gate.so");

        xacc::Initialize(std::vector<std::string>{"--load",testPath});
135
136
137
138
139
140
141
142
143
144
145
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
	const std::string src("module teleport (qbit qreg[3]) {\n"
			"   cbit creg[2];\n"
			"   // Init qubit 0 to 1\n"
			"   X(qreg[0]);\n"
			"   // Now teleport...\n"
			"   H(qreg[1]);\n"
			"   CNOT(qreg[1],qreg[2]);\n"
			"   CNOT(qreg[0],qreg[1]);\n"
			"   H(qreg[0]);\n"
			"   creg[0] = MeasZ(qreg[0]);\n"
			"   creg[1] = MeasZ(qreg[1]);\n"
			"   if (creg[0] == 1) Z(qreg[2]);\n"
			"   if (creg[1] == 1) X(qreg[2]);\n"
			"   xacc::QFT(qreg);\n"
			"}\n");

	KernelReplacementPreprocessor preprocessor;
	auto compiler = std::make_shared<DummyCompiler>();
	auto acc = std::make_shared<DummyAccelerator>();
	acc->createBuffer("qreg", 3);

	auto newSrc = preprocessor.process(src, compiler, acc);

	std::string expected("module teleport (qbit qreg[3]) {\n"
			"   cbit creg[2];\n"
			"   // Init qubit 0 to 1\n"
			"   X(qreg[0]);\n"
			"   // Now teleport...\n"
			"   H(qreg[1]);\n"
			"   CNOT(qreg[1],qreg[2]);\n"
			"   CNOT(qreg[0],qreg[1]);\n"
			"   H(qreg[0]);\n"
			"   creg[0] = MeasZ(qreg[0]);\n"
			"   creg[1] = MeasZ(qreg[1]);\n"
			"   if (creg[0] == 1) Z(qreg[2]);\n"
			"   if (creg[1] == 1) X(qreg[2]);\n"
			"   H(qreg[2]);\n"
			"   // BEGIN CPHASE GATE\n"
			"   Rz(qreg[2],0.785398);\n"
			"   CNOT(qreg[2],qreg[1]);\n"
			"   Rz(qreg[2],-0.785398);\n"
			"   CNOT(qreg[2],qreg[1]);\n"
			"   // END CPHASE GATE\n"
			"   H(qreg[2]);\n"
			"   // BEGIN CPHASE GATE\n"
			"   Rz(qreg[2],0.392699);\n"
			"   CNOT(qreg[2],qreg[0]);\n"
			"   Rz(qreg[2],-0.392699);\n"
			"   CNOT(qreg[2],qreg[0]);\n"
			"   // END CPHASE GATE\n"
			"   // BEGIN CPHASE GATE\n"
			"   Rz(qreg[1],0.785398);\n"
			"   CNOT(qreg[1],qreg[0]);\n"
			"   Rz(qreg[1],-0.785398);\n"
			"   CNOT(qreg[1],qreg[0]);\n"
			"   // END CPHASE GATE\n"
			"   H(qreg[0]);\n"
			"   // BEGIN SWAP 0 2\n"
			"   CNOT(qreg[2],qreg[0]);\n"
			"   CNOT(qreg[0],qreg[2]);\n"
			"   CNOT(qreg[2],qreg[0]);\n"
			"   // END SWAP 0 2\n"
			"   \n"
			"}\n");

	BOOST_VERIFY(newSrc == expected);
201
        xacc::Finalize();
202
}