Commit 78922f6d authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Properly handle bit order in the result and adding a test for noisy validation


Signed-off-by: Nguyen, Thien Minh's avatarThien Nguyen <nguyentm@ornl.gov>
parent ef7e3a54
......@@ -3,14 +3,61 @@ set(LIBRARY_NAME qcor-mirror-rb)
file(GLOB SRC *.cpp)
file(GLOB HEADERS *.hpp)
usfunctiongetresourcesource(TARGET ${LIBRARY_NAME} OUT SRC)
usfunctiongeneratebundleinit(TARGET ${LIBRARY_NAME} OUT SRC)
set(_bundle_name qcor_mirror_rb)
add_library(${LIBRARY_NAME} SHARED ${SRC})
target_include_directories(${LIBRARY_NAME} PUBLIC . ${XACC_ROOT}/include/eigen)
target_link_libraries(${LIBRARY_NAME} PUBLIC qcor qrt)
xacc_configure_library_rpath(${LIBRARY_NAME})
install(FILES ${HEADERS} DESTINATION include/qcor)
install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
set_target_properties(${LIBRARY_NAME}
PROPERTIES COMPILE_DEFINITIONS
US_BUNDLE_NAME=${_bundle_name}
US_BUNDLE_NAME
${_bundle_name})
usfunctionembedresources(TARGET
${LIBRARY_NAME}
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
FILES
manifest.json)
if(APPLE)
set_target_properties(${LIBRARY_NAME}
PROPERTIES INSTALL_RPATH "@loader_path/../lib;${LLVM_INSTALL_PREFIX}/lib")
set_target_properties(${LIBRARY_NAME}
PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
else()
set_target_properties(${LIBRARY_NAME}
PROPERTIES INSTALL_RPATH "$ORIGIN/../lib:${LLVM_INSTALL_PREFIX}/lib")
set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS "-shared")
endif()
install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
if (QCOR_BUILD_TESTS)
add_subdirectory(tests)
endif()
\ No newline at end of file
endif()
if(APPLE)
set_target_properties(${LIBRARY_NAME}
PROPERTIES INSTALL_RPATH "@loader_path/../lib;${LLVM_INSTALL_PREFIX}/lib")
set_target_properties(${LIBRARY_NAME}
PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
else()
set_target_properties(${LIBRARY_NAME}
PROPERTIES INSTALL_RPATH "$ORIGIN/../lib:${LLVM_INSTALL_PREFIX}/lib")
set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS "-shared")
endif()
install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
\ No newline at end of file
{
"bundle.symbolic_name" : "qcor_mirror_rb",
"bundle.activator" : true,
"bundle.name" : "Mirror Circuit Randomized Bechmarking",
"bundle.description" : ""
}
......@@ -7,7 +7,7 @@
#include "xacc_service.hpp"
#include <cassert>
#include <random>
#include "xacc_plugin.hpp"
namespace xacc {
namespace quantum {
// Helper to convert a gate
......@@ -182,6 +182,9 @@ std::pair<bool, xacc::HeterogeneousMap> MirrorCircuitValidator::validate(
qcor::MirrorCircuitValidator::createMirrorCircuit(program);
const std::string expectedBitString = [&]() {
std::string bitStr;
if (qpu->getBitOrder() == xacc::Accelerator::BitOrder::MSB) {
std::reverse(expected_result.begin(), expected_result.end());
}
for (const auto &bit : expected_result) {
bitStr += std::to_string(bit);
}
......@@ -195,8 +198,14 @@ std::pair<bool, xacc::HeterogeneousMap> MirrorCircuitValidator::validate(
auto mc_buffer = xacc::qalloc(mirror_circuit->nPhysicalBits());
qpu->execute(mc_buffer, mirror_circuit->as_xacc());
// mc_buffer->print();
{
std::stringstream ss;
ss << "Trial " << i << "\n";
ss << "Circuit:\n" << mirror_circuit->toString() << "\n";
ss << "Result:\n" << mc_buffer->toString() << "\n";
ss << "Expected bitstring:" << expectedBitString << "\n";
xacc::info(ss.str());
}
const auto bitStrProb =
mc_buffer->computeMeasurementProbability(expectedBitString);
trial_success_probs.emplace_back(bitStrProb);
......@@ -406,3 +415,4 @@ MirrorCircuitValidator::createMirrorCircuit(std::shared_ptr<CompositeInstruction
target_bitString);
}
} // namespace qcor
REGISTER_PLUGIN(qcor::MirrorCircuitValidator, qcor::BackendValidator)
......@@ -5,6 +5,8 @@
#include "mirror_circuit_rb.hpp"
#include "qcor_ir.hpp"
#include <random>
#include "NoiseModel.hpp"
namespace {
double random_angle() {
static std::uniform_real_distribution<double> dis(-M_PI, M_PI);
......@@ -167,6 +169,36 @@ TEST(MirrorCircuitTester, checkDeuteron) {
EXPECT_EQ(allBitStrings.size(), 4);
}
TEST(MirrorCircuitTester, checkNoise) {
const std::string msb_noise_model =
R"({"gate_noise": [{"gate_name": "CNOT", "register_location": ["0", "1"], "noise_channels": [{"matrix": [[[[0.99498743710662, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.99498743710662, 0.0], [0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0], [0.99498743710662, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.99498743710662, 0.0]]], [[[0.0, 0.0], [0.05773502691896258, 0.0], [0.0, 0.0], [0.0, 0.0]], [[0.05773502691896258, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.05773502691896258, 0.0]], [[0.0, 0.0], [0.0, 0.0], [0.05773502691896258, 0.0], [0.0, 0.0]]], [[[0.0, 0.0], [0.0, -0.05773502691896258], [0.0, 0.0], [0.0, 0.0]], [[0.0, 0.05773502691896258], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, -0.05773502691896258]], [[0.0, 0.0], [0.0, 0.0], [0.0, 0.05773502691896258], [0.0, 0.0]]], [[[0.05773502691896258, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [-0.05773502691896258, 0.0], [0.0, 0.0], [-0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0], [0.05773502691896258, 0.0], [0.0, 0.0]], [[0.0, 0.0], [-0.0, 0.0], [0.0, 0.0], [-0.05773502691896258, 0.0]]]]}]}], "bit_order": "MSB"})";
auto noiseModel = xacc::getService<xacc::NoiseModel>("json");
noiseModel->initialize({{"noise-model", msb_noise_model}});
const std::string ibmNoiseJson = noiseModel->toJson();
auto accelerator =
xacc::getAccelerator("aer", {{"noise-model", ibmNoiseJson}});
auto provider = xacc::getIRProvider("quantum");
auto circuit = provider->createComposite(std::string("test"));
circuit->addInstruction(provider->createInstruction("H", {0}));
circuit->addInstruction(provider->createInstruction("H", {1}));
circuit->addInstruction(provider->createInstruction("CNOT", {0, 1}));
// xacc::set_verbose(true);
auto validator = xacc::getService<qcor::BackendValidator>("mirror-rb");
{
auto [success, data] = validator->validate(
accelerator, std::make_shared<qcor::CompositeInstruction>(circuit));
EXPECT_TRUE(success);
}
{
// Validate with a tighter limit (0.01 == 1%)
auto [success, data] = validator->validate(
accelerator, std::make_shared<qcor::CompositeInstruction>(circuit),
{{"epsilon", 0.01}});
EXPECT_FALSE(success);
}
}
int main(int argc, char **argv) {
xacc::Initialize();
::testing::InitGoogleTest(&argc, argv);
......
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