Commit 30c2ac71 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding the IRBuilder, making it easier to construct gate model quantum ir



Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent eca69797
......@@ -42,6 +42,7 @@ add_library(xacc SHARED
utils/Utils.cpp
utils/CLIParser.cpp
utils/heterogeneous.cpp
ir/IRBuilder.cpp
service/ServiceRegistry.cpp
service/xacc_service.cpp
accelerator/remote/RemoteAccelerator.cpp)
......
#include "IRBuilder.hpp"
#include "xacc_service.hpp"
#include <iostream>
#include "xacc.hpp"
namespace xacc {
IRBuilder::IRBuilder() {
provider = xacc::getIRProvider("quantum");
program = provider->createComposite("__internal__builder__xacc__program");
__init__inst_map();
}
IRBuilder::IRBuilder(const std::string name) {
provider = xacc::getIRProvider("quantum");
program = provider->createComposite(name);
__init__inst_map();
}
void IRBuilder::__init__inst_map() {
auto insts = xacc::getServices<xacc::Instruction>();
for (auto inst : insts) {
if (!inst->isComposite()) {
std::string data = inst->name();
std::transform(data.begin(), data.end(), data.begin(),
[](unsigned char c) { return std::tolower(c); });
if (data == "measure") {
data = "mz";
}
method_name_to_xacc_name.insert({data, inst->name()});
}
}
}
} // namespace xacc
\ No newline at end of file
#pragma once
#include "IRProvider.hpp"
// Enumerate XACC Instruction Nodes
// If you want your new XACC Instruction to be supported by
// the IRBuilder, then you have to add it here, categorized by 1,2 qubits
#define XACC_SINGLE_QUBIT_NODE_LIST_WITH_CODE(code) \
code(h) code(mz) code(rz) code(rx) code(ry) code(x) code(y) code(z) code(s) \
code(t) code(tdg) code(u) code(u1) code(rphi) code(reset)
#define XACC_TWO_QUBIT_NODE_LIST_WITH_CODE(code) \
code(cnot) code(cphase) code(swap) code(iswap) code(fsim) code(cz) code(cy) \
code(crz) code(ch) code(xx) code(xy) code(rzz)
namespace xacc {
using QubitType = std::pair<std::string, std::size_t>;
class IRBuilder {
private:
std::map<std::string, std::string> method_name_to_xacc_name;
// Build instructions with the Provider
std::shared_ptr<xacc::IRProvider> provider;
// Composite storing the instructions
std::shared_ptr<xacc::CompositeInstruction> program;
// Build up method_name_to_xacc_name
void __init__inst_map();
// Temp holder for InstructionParameters
// facilitates variadic params to vector conversion
std::vector<InstructionParameter> m_tmp_holder;
// Variadic to vector...
void internal_push_back() {}
void internal_push_back(InstructionParameter p) {
m_tmp_holder.push_back(p);
if (m_tmp_holder.back().isVariable()) {
program->addVariable(m_tmp_holder.back().toString());
}
}
template <typename First, typename... T>
void internal_push_back(First f, T... t) {
m_tmp_holder.push_back(f);
if (m_tmp_holder.back().isVariable()) {
program->addVariable(m_tmp_holder.back().toString());
}
internal_push_back(t...);
}
public:
IRBuilder();
IRBuilder(const std::string name);
auto to_ir() { return program; }
// Generate at compile time all one-qubit instruction method names
#define FUNC_SIG_GEN(TYPENAME) void TYPENAME
#define DEFINE_VISIT_ONE_QUBIT(type) \
template <typename... Params> \
FUNC_SIG_GEN(type) \
(std::size_t qubit, Params... params) { \
internal_push_back(params...); \
program->addInstruction(provider->createInstruction( \
method_name_to_xacc_name[#type], std::vector<std::size_t>{qubit}, \
m_tmp_holder)); \
m_tmp_holder.clear(); \
}
XACC_SINGLE_QUBIT_NODE_LIST_WITH_CODE(DEFINE_VISIT_ONE_QUBIT)
#undef DEFINE_VISIT_ONE_QUBIT
#define DEFINE_VISIT_ONE_QUBIT_TYPE(type) \
template <typename... Params> \
FUNC_SIG_GEN(type) \
(QubitType qubit, Params... params) { \
internal_push_back(params...); \
program->addInstruction(provider->createInstruction( \
method_name_to_xacc_name[#type], std::vector<QubitType>{qubit}, \
m_tmp_holder)); \
m_tmp_holder.clear(); \
}
XACC_SINGLE_QUBIT_NODE_LIST_WITH_CODE(DEFINE_VISIT_ONE_QUBIT_TYPE)
#undef DEFINE_VISIT_ONE_QUBIT_TYPE
#define DEFINE_VISIT_TWO_QUBIT(type) \
template <typename... Params> \
FUNC_SIG_GEN(type) \
(std::size_t ctrl, std::size_t tgt, Params... params) { \
internal_push_back(params...); \
program->addInstruction(provider->createInstruction( \
method_name_to_xacc_name[#type], std::vector<std::size_t>{ctrl, tgt}, \
m_tmp_holder)); \
m_tmp_holder.clear(); \
}
XACC_TWO_QUBIT_NODE_LIST_WITH_CODE(DEFINE_VISIT_TWO_QUBIT)
#undef DEFINE_VISIT_TWO_QUBIT
#define DEFINE_VISIT_TWO_QUBIT_TYPE(type) \
template <typename... Params> \
FUNC_SIG_GEN(type) \
(QubitType ctrl, QubitType tgt, Params... params) { \
internal_push_back(params...); \
program->addInstruction(provider->createInstruction( \
method_name_to_xacc_name[#type], std::vector<QubitType>{ctrl, tgt}, \
m_tmp_holder)); \
m_tmp_holder.clear(); \
}
XACC_TWO_QUBIT_NODE_LIST_WITH_CODE(DEFINE_VISIT_TWO_QUBIT_TYPE)
#undef DEFINE_VISIT_TWO_QUBIT_TYPE
};
} // namespace xacc
......@@ -12,3 +12,5 @@
# *******************************************************************************/
#add_xacc_test(InstructionParameter xacc)
add_xacc_test(IRBuilder)
target_link_libraries(IRBuilderTester xacc)
/*******************************************************************************
* 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
*******************************************************************************/
#include <gtest/gtest.h>
#include "xacc.hpp"
TEST(IRBuilderTester, checkSimple) {
{
xacc::IRBuilder builder;
builder.h(0);
builder.h(1);
builder.cnot(0, 1);
builder.rz(1, 2.2);
builder.cphase(0, 1, 3.1415);
builder.rx(0, "theta");
builder.u(2, 1.1, 2.2, 3.3);
builder.mz(0);
builder.mz(1);
auto composite = builder.to_ir();
std::cout << composite->toString() << "\n";
EXPECT_EQ(9, composite->nInstructions());
}
{
xacc::IRBuilder builder("test");
builder.h(std::make_pair("qregQQ", 0));
builder.cnot(std::make_pair("qregQQ", 0), std::make_pair("qregQQ", 1));
auto composite = builder.to_ir();
std::cout << composite->toString() << "\n";
}
}
int main(int argc, char **argv) {
xacc::Initialize(argc, argv);
::testing::InitGoogleTest(&argc, argv);
auto ret = RUN_ALL_TESTS();
xacc::Finalize();
return ret;
}
......@@ -20,6 +20,7 @@
#include "Algorithm.hpp"
#include "Optimizer.hpp"
#include "IRTransformation.hpp"
#include "IRBuilder.hpp"
#include "heterogeneous.hpp"
......
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