ReadoutErrorAcceleratorBufferPostprocessorTester.cpp 4.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*******************************************************************************
 * 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
 *
 * Contributors:
 *   Alexander J. McCaskey - initial API and implementation
 *******************************************************************************/
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE ReadoutErrorAcceleratorBufferPostprocessorTester

#include <boost/test/included/unit_test.hpp>
#include "ReadoutErrorAcceleratorBufferPostprocessor.hpp"
#include "GateQIR.hpp"
#include <boost/math/constants/constants.hpp>
#include "XACC.hpp"
#include "GateFunction.hpp"
#include "GateInstruction.hpp"
23
#include "GateInstructionService.hpp"
24
25
26
27
28
29
30
31
32
33
34
35
36

using namespace xacc;

using namespace xacc::quantum;

using Term = std::map<int, std::string>;

std::shared_ptr<IR> createXACCIR(std::unordered_map<std::string, Term> terms) {
// Create a new GateQIR to hold the spin based terms
	auto newIr = std::make_shared<xacc::quantum::GateQIR>();
	int counter = 0;
	auto pi = boost::math::constants::pi<double>();

37
38
	GateInstructionService gateRegistry;

39
40
41
42
43
44
45
46
47
48
49
50
51
	// Populate GateQIR now...
	for (auto& inst : terms) {

		Term spinInst = inst.second;

		// Create a GateFunction and specify that it has
		// a parameter that is the Spin Instruction coefficient
		// that will help us get it to the user for their purposes.
		auto gateFunction = std::make_shared<xacc::quantum::GateFunction>(
				"term" + std::to_string(counter));

		// Loop over all terms in the Spin Instruction
		// and create instructions to run on the Gate QPU.
52
		std::vector<std::shared_ptr<xacc::Instruction>> measurements;
53
54
55
56
57
58
59
60
61
62
63

		std::vector<std::pair<int, std::string>> terms;
		for (auto& kv : spinInst) {
			if (kv.second != "I" && !kv.second.empty()) {
				terms.push_back( { kv.first, kv.second });
			}
		}

		for (int i = terms.size() - 1; i >= 0; i--) {
			auto qbit = terms[i].first;
			auto gateName = terms[i].second;
64
			auto meas = gateRegistry.create("Measure",
65
66
67
68
69
70
					std::vector<int> { qbit });
			xacc::InstructionParameter classicalIdx(qbit);
			meas->setParameter(0, classicalIdx);
			measurements.push_back(meas);

			if (gateName == "X") {
71
				auto hadamard = gateRegistry.create("H", std::vector<int> {
72
73
74
						qbit });
				gateFunction->addInstruction(hadamard);
			} else if (gateName == "Y") {
75
				auto rx = gateRegistry.create("Rx", std::vector<int> { qbit });
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
				InstructionParameter p(pi / 2.0);
				rx->setParameter(0, p);
				gateFunction->addInstruction(rx);
			}

		}

		for (auto m : measurements) {
			gateFunction->addInstruction(m);
		}

		newIr->addKernel(gateFunction);
		counter++;
	}

	return newIr;
}

class FakeTermAB : public AcceleratorBuffer {
public:

	FakeTermAB(const std::string& str, const int N) : AcceleratorBuffer(str, N) {}
	virtual const double getExpectationValueZ() {
		return .88;
	}
};

class FakeMeasureAB : public AcceleratorBuffer {
public:

	FakeMeasureAB(const std::string& str, const int N) : AcceleratorBuffer(str, N) {}
	virtual double computeMeasurementProbability(const std::string& bitStr) {
		if (bitStr == "1") {
			return .08;
		} else {
111
			return .01;
112
113
114
115
116
117
118
		}
	}

};

BOOST_AUTO_TEST_CASE(checkSimple) {

119
	xacc::Initialize();
120
121
122
	// Simple example H = X0
	std::vector<std::string> orderedTerms{"Z0"};

123
	auto gateRegistry = xacc::getService<InstructionService>("gate");
124
125
126
127
128
129
130
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

	// Construct IR for this
	auto ir = createXACCIR( { { "Z0", Term { { 0, "Z" } } } });
	auto f01 = std::make_shared<GateFunction>("measure0_qubit_0",
			"readout-error");
	auto meas01 = gateRegistry->create("Measure", std::vector<int> { 0 });
	InstructionParameter p(0);
	meas01->setParameter(0, p);
	f01->addInstruction(meas01);

	auto f10 = std::make_shared<GateFunction>("measure1_qubit_0",
			"readout-error");
	auto x = gateRegistry->create("X", std::vector<int> { 0 });
	auto meas10 = gateRegistry->create("Measure", std::vector<int> { 0 });
	InstructionParameter p2(0);
	meas10->setParameter(0, p2);
	f10->addInstruction(x);
	f10->addInstruction(meas01);

	ir->addKernel(f10);
	ir->addKernel(f01);

	std::cout << "IR: " << ir->getKernels().size() << "\n";
	// Construct sites map
	std::map<std::string, std::vector<int>> sites{{"Z0",{0}}};

	auto buff = std::make_shared<FakeTermAB>("buff", 1);

	auto mb1 = std::make_shared<FakeMeasureAB>("m1", 1);
	auto mb2 = std::make_shared<FakeMeasureAB>("m2", 1);

	std::vector<std::shared_ptr<AcceleratorBuffer>> buffers {buff, mb1, mb2};

	ReadoutErrorAcceleratorBufferPostprocessor processor(*ir, sites, orderedTerms);

	auto fixed = processor.process(buffers);

	std::cout << "HELLO: " << fixed[0]->getExpectationValueZ() << "\n";

163
	xacc::Finalize();
164
//	BOOST_VERIFY(std::fabs(fixed[0]->getExpectationValueZ() - .911111) < 1e-6);
165
166

}