ScaffoldCompilerTester.cpp 4.02 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
 *
 **********************************************************************************/
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE ScaffoldCompilerTester

#include <boost/test/included/unit_test.hpp>
35
36
37
#include "ScaffoldCompiler.hpp"

using namespace xacc::quantum;
38
using GraphType = QuantumCircuit;
39

40
41
struct F {
	F() :
42
			compiler(xacc::CompilerRegistry::instance()->create("scaffold")) {
43
44
45
46
47
48
49
50
51
52
53
		BOOST_TEST_MESSAGE("setup fixture");
		BOOST_VERIFY(compiler);
	}
	~F() {
		BOOST_TEST_MESSAGE("teardown fixture");
	}

	std::shared_ptr<xacc::Compiler> compiler;
};

//____________________________________________________________________________//
54

55
56
57
BOOST_FIXTURE_TEST_SUITE( s, F )

BOOST_AUTO_TEST_CASE(checkSimpleCompile) {
58

59
	const std::string src("__qpu__ eprCreation () {\n"
60
61
62
63
			"   qbit qreg[2];\n"
			"   H(qreg[0]);\n"
			"   CNOT(qreg[0],qreg[1]);\n"
			"}\n");
64

65
	auto ir = compiler->compile(src);
66
	BOOST_VERIFY(ir);
67
68

	auto graphir = std::dynamic_pointer_cast<xacc::GraphIR<QuantumCircuit>>(ir);
69
70
71
	BOOST_VERIFY(graphir);

	// The above code should produce a graph
72
	// with 4 nodes (initial qubits state, Hadamard, and CNot, sink final state),
73
74
	// with 3 edges (q0 lifeline to H, q0 lifeline from H to CNot,
	// and q1 lifeline to CNot)
75
76
	BOOST_VERIFY(graphir->order() == 4);
	BOOST_VERIFY(graphir->size() == 5);
77

78
79
	graphir->persist(std::cout);

80
81
}

82
BOOST_AUTO_TEST_CASE(checkCodeWithMeasurementIf) {
83
	const std::string src("__qpu__ teleport () {\n"
84
85
86
87
88
89
90
91
92
93
94
			"   qbit qreg[3];\n"
			"   cbit creg[2];\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");
95

96
	auto ir = compiler->compile(src);
97
	BOOST_VERIFY(ir);
98
	auto graphir = std::dynamic_pointer_cast<xacc::GraphIR<QuantumCircuit>>(ir);
99
100
	BOOST_VERIFY(graphir);

101
	graphir->persist(std::cout);
102
103
104
	BOOST_VERIFY(graphir->order() == 16);
	BOOST_VERIFY(graphir->size() == 23);

105
}
106
107
108
109

BOOST_AUTO_TEST_CASE(checkCodeWithArgument) {

	const std::string src("__qpu__ kernel (qbit qreg[1], double phi) {\n"
110
111
			"   Rz(qreg[0], phi);\n"
			"}\n");
112
113
114

	auto ir = compiler->compile(src);
	BOOST_VERIFY(ir);
115
	auto graphir = std::dynamic_pointer_cast<xacc::GraphIR<QuantumCircuit>>(ir);
116
117
118
119
	BOOST_VERIFY(graphir);

	graphir->persist(std::cout);
}
120
121

BOOST_AUTO_TEST_SUITE_END()