ProgramTester.cpp 6.11 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
35
 *     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
 *
 **********************************************************************************/
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE ProgramTester

#include <boost/test/included/unit_test.hpp>
#include "Program.hpp"
36
37
#include "FakeIR.hpp"
#include "FakeAccelerator.hpp"
38
#include "FireTensorAccelerator.hpp"
39
40
41

using namespace xacc;

42
class DummyCompiler : public Compiler {
43
public:
44
45
	virtual std::shared_ptr<xacc::IR> compile(const std::string& src,
			std::shared_ptr<IAccelerator> acc) {
46
		return std::make_shared<FakeIR>();
47
48
49
50
51
52

	}

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

53
54
55
56
57
	}

	virtual void modifySource() {

	}
58
59
60
	virtual std::string getBitType() {
		return "hello";
	}
61
62
63
64
65

	virtual ~DummyCompiler() {
	}
};

66
67
// Register the ScaffoldCompiler with the CompilerRegistry.
static xacc::RegisterCompiler<DummyCompiler> X("dummy");
68
69
70
71

BOOST_AUTO_TEST_CASE(checkBuildRuntimeArguments) {

	const std::string src("__qpu__ void teleport() {"
72
					"       qbit q[3];"
73
74
75
76
77
78
79
80
81
82
83
84
					"		H(q[1]);"
					"		CNot(q[1],q[2]);"
					"		CNot(q[0], q[1]);"
					"		H(q[1]);"
					"		if(q[1].measure()) {"
					"			X(q[2]);"
					"		}"
					"		if(q[1].measure()) {"
					"			Z(q[2]);"
					"		}"
					"}");

85
	auto acc = std::make_shared<FakeAccelerator>();
86
	acc->createBuffer("qreg");
87
88
89
90
	Program prog(acc, src);
	prog.build("--compiler dummy");
}

91
92
93
BOOST_AUTO_TEST_CASE(checkRuntimeGateParameter) {

	// Quantum Kernel executing a parameterized gate
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//	const std::string src("__qpu__ rotate (qbit qreg, double phi) {\n"
//			"   H(qreg[0]);\n"
//			"   Rz(qreg[0], phi);\n"
//			"}\n");
//
//	// Create a convenient alias for our simulator...
//	using CircuitSimulator = xacc::quantum::FireTensorAccelerator<6>;
//
//	// Create a reference to the 6 qubit simulation Accelerator
//	auto qpu = std::make_shared<CircuitSimulator>();
//
//	// Allocate 1 qubit, give them a unique identifier...
//	auto qubitReg = qpu->createBuffer("qreg", 1);
//	using QubitRegisterType = decltype(qubitReg);
//
//	// Construct a new XACC Program
//	xacc::Program quantumProgram(qpu, src);
//
//	// Build the program using Scaffold comipler
//	// and output the Graph Intermediate Representation
//	quantumProgram.build("--compiler scaffold");
//
//	// Retrieve the created kernel. It takes a
//	// qubit register as input
//	auto rotate = quantumProgram.getKernel<QubitRegisterType, double&>(
//			"teleport");
//
//	// Execute the kernel with the qubit register!
//	double angle = std::acos(-1) / 4.0;
//	rotate(qubitReg, angle);
//
//	// Pretty print the resultant state
//	qubitReg->printBufferState(std::cout);
//
//	BOOST_VERIFY(std::real(qubitReg->getState()(0)) == (1.0/std::sqrt(2.0)));
//	BOOST_VERIFY(std::real(qubitReg->getState()(1)) == 0.5);
//	BOOST_VERIFY(std::fabs(std::imag(qubitReg->getState()(1)) - 0.5) < 1e-6);
131
132
133
134
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

}

BOOST_AUTO_TEST_CASE(checkTeleportScaffold) {
	// Quantum Kernel executing teleportation of
	// qubit state to another.
	const std::string src("__qpu__ teleport (qbit qreg) {\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"
			"}\n");

	// Create a convenient alias for our simulator...
	using CircuitSimulator = xacc::quantum::FireTensorAccelerator<6>;

	// Create a reference to the 6 qubit simulation Accelerator
	auto qpu = std::make_shared<CircuitSimulator>();

	// Allocate 3 qubits, give them a unique identifier...
	auto qreg = qpu->createBuffer("qreg", 3);
	using QubitRegisterType = decltype(qreg);

	// Construct a new XACC Program
	xacc::Program quantumProgram(qpu, src);

	// Build the program using Scaffold comipler
	// and output the Graph Intermediate Representation
Mccaskey, Alex's avatar
Mccaskey, Alex committed
167
	quantumProgram.build("--compiler scaffold");
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

	// Retrieve the created kernel. It takes a
	// qubit register as input
	auto teleport = quantumProgram.getKernel<QubitRegisterType>("teleport");

	// Execute the kernel with the qubit register!
	teleport(qreg);

	// Pretty print the resultant state
	qreg->printBufferState(std::cout);

	BOOST_VERIFY(std::real(qreg->getState()(1) * qreg->getState()(1)) == 1 ||
				std::real(qreg->getState()(5) * qreg->getState()(5)) == 1 ||
				std::real(qreg->getState()(3) * qreg->getState()(3)) == 1 ||
				std::real(qreg->getState()(7) * qreg->getState()(7)) == 1);

}