Commit 86e14c66 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding placement ir transformation wrapping staq swap shortest path



Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 9635c152
......@@ -12,7 +12,7 @@
# *******************************************************************************/
set(LIBRARY_NAME xacc-staq-compiler)
file(GLOB SRC
compiler/*.cpp *.cpp optimizers/*.cpp utils/*.cpp)
compiler/*.cpp *.cpp transformations/*.cpp utils/*.cpp)
usfunctiongetresourcesource(TARGET ${LIBRARY_NAME} OUT SRC)
usfunctiongeneratebundleinit(TARGET ${LIBRARY_NAME} OUT SRC)
......@@ -20,7 +20,7 @@ usfunctiongeneratebundleinit(TARGET ${LIBRARY_NAME} OUT SRC)
add_library(${LIBRARY_NAME} SHARED ${SRC})
target_include_directories(${LIBRARY_NAME}
PUBLIC compiler utils optimizers ${STAQ_DIR}/include ${STAQ_DIR}/libs)
PUBLIC compiler utils transformations ${STAQ_DIR}/include ${STAQ_DIR}/libs)
target_link_libraries(${LIBRARY_NAME}
PUBLIC xacc xacc-quantum-gate
......@@ -57,7 +57,7 @@ endif()
if(XACC_BUILD_TESTS)
add_subdirectory(compiler/tests)
add_subdirectory(optimizers/tests)
add_subdirectory(transformations/tests)
endif()
install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
......@@ -124,14 +124,9 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src,
// Visit Program to find out how many qreg there are and
// use that to build up openqasm xacc function prototype
// std::cout << "HELLO:\n";
// prog->pretty_print(std::cout);
// exit(0);
internal_staq::StaqToXasm translate;
translate.visit(*prog);
// std::cout << "XASM:\n" << translate.ss.str() << "\n";
std::string kernel;
if (isXaccKernel) {
if (!ancillas.ancillas.empty()) {
......@@ -183,21 +178,21 @@ const std::string
StaqCompiler::translate(std::shared_ptr<xacc::CompositeInstruction> function) {
std::map<std::string,int> bufNamesToSize;
InstructionIterator iter(function);
// First search buffer names and see if we have
while(iter.hasNext()) {
auto &next = *iter.next();
if (next.isEnabled()) {
for (int i = 0; i < next.nRequiredBits(); i++) {
auto bufName = next.getBufferName(i);
int size = next.bits()[i]+1;
if (bufNamesToSize.count(bufName)) {
if (bufNamesToSize[bufName] < next.bits()[i]+1) {
bufNamesToSize[bufName] = next.bits()[i]+1;
if (bufNamesToSize[bufName] < size) {
bufNamesToSize[bufName] = size;
}
} else {
bufNamesToSize.insert({bufName, next.bits()[i]+1});
bufNamesToSize.insert({bufName, size});
}
}
}
}
......
......@@ -18,7 +18,6 @@
TEST(StaqCompilerTester, checkSimple) {
auto compiler = xacc::getCompiler("staq");
auto IR = compiler->compile(R"(OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
creg c[2];
U(0,0,0) q[0];
......
......@@ -12,6 +12,7 @@
*******************************************************************************/
#include "staq_compiler.hpp"
#include "staq_rotation_folding.hpp"
#include "staq_swap_short.hpp"
#include "cppmicroservices/BundleActivator.h"
#include "cppmicroservices/BundleContext.h"
......@@ -39,6 +40,10 @@ public:
auto c1 = std::make_shared<xacc::quantum::RotationFolding>();
context.RegisterService<xacc::IRTransformation>(c1);
auto c2 = std::make_shared<xacc::quantum::SwapShort>();
context.RegisterService<xacc::IRTransformation>(c2);
}
/**
......
......@@ -24,8 +24,8 @@ using namespace staq;
using namespace staq::ast;
void RotationFolding::apply(std::shared_ptr<CompositeInstruction> program,
const std::shared_ptr<Accelerator> accelerator,
const HeterogeneousMap &options) {
const std::shared_ptr<Accelerator> accelerator,
const HeterogeneousMap &options) {
// map to openqasm
auto staq = xacc::getCompiler("staq");
......@@ -37,7 +37,7 @@ void RotationFolding::apply(std::shared_ptr<CompositeInstruction> program,
// fold rotations
optimization::fold_rotations(*prog);
optimization::simplify(*prog);
// map prog back to staq src string and
// compile to ir
std::stringstream ss;
......
......@@ -24,13 +24,14 @@ class RotationFolding : public IRTransformation {
public:
RotationFolding() {}
void apply(std::shared_ptr<CompositeInstruction> program,
const std::shared_ptr<Accelerator> accelerator,
const HeterogeneousMap& options = {}) override;
const IRTransformationType type() const override {return IRTransformationType::Optimization;}
const std::shared_ptr<Accelerator> accelerator,
const HeterogeneousMap &options = {}) override;
const IRTransformationType type() const override {
return IRTransformationType::Optimization;
}
const std::string name() const override { return "rotation-folding"; }
const std::string description() const override { return ""; }
};
} // namespace quantum
} // namespace xacc
......
/*******************************************************************************
* Copyright (c) 2020 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
*******************************************************************************/
#include "staq_swap_short.hpp"
#include "mapping/device.hpp"
#include "mapping/mapping/swap.hpp"
#include "mapping/layout/basic.hpp"
#include "xacc_service.hpp"
#include "xacc.hpp"
#include "staq_visitors.hpp"
#include <regex>
namespace xacc {
namespace quantum {
using namespace staq;
using namespace staq::ast;
void SwapShort::apply(std::shared_ptr<CompositeInstruction> program,
const std::shared_ptr<Accelerator> qpu,
const HeterogeneousMap &options) {
if (!qpu) {
xacc::warning("[SwapShort Placement] Provided QPU was null. Cannot run shortest path swap placement.");
return;
}
// First get total number of qubits on device
std::set<int> qbitIdxs;
auto connectivity = qpu->getConnectivity();
for (auto &edge : connectivity) {
qbitIdxs.insert(edge.first);
qbitIdxs.insert(edge.second);
}
auto nQubits = *std::max_element(qbitIdxs.begin(), qbitIdxs.end()) + 1;
// Create the adjacency matrix, init to all 0s first
std::vector<std::vector<bool>> adj(nQubits);
for (int i = 0; i < nQubits; i++) {
adj[i] = std::vector<bool>(nQubits);
}
// Now set true where there are connections
for (auto &edge : connectivity) {
adj[edge.first][edge.second] = true;
adj[edge.second][edge.first] = true;
}
// map to openqasm
auto staq = xacc::getCompiler("staq");
auto src = staq->translate(program);
// parse that to get staq ast
auto prog = parser::parse_string(src);
mapping::Device device(qpu->getSignature(), nQubits, adj);//, oneq,couplings);
auto layout = mapping::compute_basic_layout(device, *prog);
mapping::apply_layout(layout, *prog);
mapping::map_onto_device(device, *prog);
// map prog back to staq src string and
// compile to ir
std::stringstream ss;
prog->pretty_print(ss);
src = ss.str();
auto ir = staq->compile(src);
// reset the program and add optimized instructions
program->clear();
program->addInstructions(ir->getComposites()[0]->getInstructions());
return;
}
} // namespace quantum
} // namespace xacc
\ No newline at end of file
/*******************************************************************************
* Copyright (c) 2020 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
*******************************************************************************/
#ifndef QUANTUM_GATE_COMPILER_STAQ_SWAP_SHORT_HPP_
#define QUANTUM_GATE_COMPILER_STAQ_SWAP_SHORT_HPP_
#include "IRTransformation.hpp"
#include "InstructionIterator.hpp"
namespace xacc {
namespace quantum {
class SwapShort : public IRTransformation {
public:
SwapShort() {}
void apply(std::shared_ptr<CompositeInstruction> program,
const std::shared_ptr<Accelerator> accelerator,
const HeterogeneousMap &options = {}) override;
const IRTransformationType type() const override {
return IRTransformationType::Placement;
}
const std::string name() const override { return "swap-shortest-path"; }
const std::string description() const override { return ""; }
};
} // namespace quantum
} // namespace xacc
#endif
......@@ -11,4 +11,7 @@
# Alexander J. McCaskey - initial API and implementation
# *******************************************************************************/
add_xacc_test(RotationFolding)
target_link_libraries(RotationFoldingTester xacc )
\ No newline at end of file
target_link_libraries(RotationFoldingTester xacc )
add_xacc_test(Mapping)
target_link_libraries(MappingTester xacc )
\ No newline at end of file
/*******************************************************************************
* Copyright (c) 2020 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
*******************************************************************************/
#include "gtest/gtest.h"
#include "xacc.hpp"
#include "xacc_service.hpp"
using namespace xacc;
class AcceleratorWithConnectivity : public xacc::Accelerator {
protected:
std::vector<std::pair<int, int>> edges;
public:
AcceleratorWithConnectivity(std::vector<std::pair<int, int>> &&connections)
: edges(connections) {}
const std::string name() const override { return "acc_with_connectivity"; }
const std::string description() const override { return ""; }
void initialize(const HeterogeneousMap &params = {}) override { return; }
void execute(std::shared_ptr<xacc::AcceleratorBuffer> buf,
std::shared_ptr<xacc::CompositeInstruction> f) override {}
void execute(std::shared_ptr<AcceleratorBuffer> buffer,
const std::vector<std::shared_ptr<CompositeInstruction>>
functions) override {}
void updateConfiguration(const HeterogeneousMap &config) override {}
const std::vector<std::string> configurationKeys() override { return {}; }
std::vector<std::pair<int, int>> getConnectivity() override { return edges; }
};
TEST(MappingTester, checkSwapShort) {
auto qpu = std::make_shared<AcceleratorWithConnectivity>(
std::vector<std::pair<int, int>>{{0, 1},
{0, 5},
{1, 2},
{1, 4},
{2, 3},
{3, 4},
{3, 8},
{4, 5},
{4, 7},
{5, 6},
{6, 7},
{7, 8}});
auto x = xacc::qalloc(9);
x->setName("x");
xacc::storeBuffer(x);
auto irt = xacc::getIRTransformation("swap-shortest-path");
auto compiler = xacc::getCompiler("xasm");
auto program = compiler->compile(R"(__qpu__ void test_simple(qreg x) {
CX(x[0], x[2]);
Measure(x[0]);
})")->getComposite("test_simple");
EXPECT_EQ(2, program->nInstructions());
EXPECT_EQ("CNOT", program->getInstruction(0)->name());
irt->apply(program, nullptr);
EXPECT_EQ(2, program->nInstructions());
irt->apply(program, qpu);
EXPECT_EQ(5, program->nInstructions());
std::cout << program->toString() << "\n";
}
int main(int argc, char **argv) {
xacc::Initialize(argc, argv);
xacc::set_verbose(true);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
......@@ -29,7 +29,7 @@ void XACCToStaqOpenQasm::visit(Hadamard &h) {
<< h.bits() << ";\n";
}
void XACCToStaqOpenQasm::visit(CNOT &cx) {
ss << "cx " << (cx.getBufferNames().empty() ? "q" : cx.getBufferName(0))
ss << "CX " << (cx.getBufferNames().empty() ? "q" : cx.getBufferName(0))
<< "[" << cx.bits()[0] << "], " << (cx.getBufferNames().empty() ? "q" : cx.getBufferName(1)) << "[" << cx.bits()[1] << "];\n";
}
void XACCToStaqOpenQasm::visit(Rz &rz) {
......
Supports Markdown
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