Commit c118f666 authored by WrathfulSpatula's avatar WrathfulSpatula
Browse files

Qrack plugin: adding new unit tests from qpp, fixing CMake OpenCL version warning


Signed-off-by: default avatarWrathfulSpatula <stranoj@gmail.com>
parent 1922986e
......@@ -14,3 +14,8 @@
add_xacc_test(QrackAccelerator)
target_link_libraries(QrackAcceleratorTester xacc-qrack)
find_package(OpenCL)
if (OpenCL_FOUND)
target_compile_definitions (QrackAcceleratorTester PUBLIC CL_HPP_TARGET_OPENCL_VERSION=200)
target_compile_definitions (QrackAcceleratorTester PUBLIC CL_HPP_MINIMUM_OPENCL_VERSION=100)
endif()
......@@ -272,6 +272,62 @@ TEST(QrackAcceleratorTester, testConditional)
EXPECT_EQ(resultCount, nbTests);
}
TEST(QrackAcceleratorTester, testISwap)
{
// Get reference to the Accelerator
xacc::set_verbose(false);
const int nbShots = 100;
auto accelerator = xacc::getAccelerator("qrack", { std::make_pair("shots", nbShots) });
auto xasmCompiler = xacc::getCompiler("xasm");
auto ir = xasmCompiler->compile(R"(__qpu__ void testISwap(qbit q) {
X(q[0]);
iSwap(q[0], q[3]);
Measure(q[0]);
Measure(q[1]);
Measure(q[2]);
Measure(q[3]);
Measure(q[4]);
})", accelerator);
auto program = ir->getComposite("testISwap");
// Allocate some qubits (5)
auto buffer = xacc::qalloc(5);
accelerator->execute(buffer, program);
// 10000 => i00010 after iswap
buffer->print();
EXPECT_EQ(buffer->getMeasurementCounts()["00010"], nbShots);
}
TEST(QrackAcceleratorTester, testFsim)
{
// Get reference to the Accelerator
const int nbShots = 1000;
auto accelerator = xacc::getAccelerator("qrack", { std::make_pair("shots", nbShots) });
auto xasmCompiler = xacc::getCompiler("xasm");
auto ir = xasmCompiler->compile(R"(__qpu__ void testFsim(qbit q, double x, double y) {
X(q[0]);
fSim(q[0], q[2], x, y);
Measure(q[0]);
Measure(q[2]);
})", accelerator);
auto program = ir->getComposites()[0];
const auto angles = xacc::linspace(-xacc::constants::pi, xacc::constants::pi, 10);
for (const auto& a : angles)
{
auto buffer = xacc::qalloc(3);
auto evaled = program->operator()({ a, 0.0 });
accelerator->execute(buffer, evaled);
const auto expectedProb = std::sin(a) * std::sin(a);
std::cout << "Angle = " << a << "\n";
buffer->print();
// fSim mixes 01 and 10 states w.r.t. the theta angle.
EXPECT_NEAR(buffer->computeMeasurementProbability("01"), expectedProb, 0.1);
EXPECT_NEAR(buffer->computeMeasurementProbability("10"), 1.0 - expectedProb, 0.1);
}
}
int main(int argc, char **argv) {
xacc::Initialize();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment