Commit b6171f6c authored by Mccaskey, Alex's avatar Mccaskey, Alex

minor cleanup, adding ability to set optimizer for qfast decompose

Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 8f7b3a62
Pipeline #113190 passed with stages
in 60 minutes and 20 seconds
from xacc/ubuntu:18.04
run apt-get update && apt-get install -y ninja-build \
&& git clone https://github.com/hfinkel/llvm-project-csp llvm \
&& git clone https://github.com/ornl-qci/llvm-project-csp llvm \
&& cd llvm && mkdir build && cd build \
&& cmake -G Ninja ../llvm -DCMAKE_INSTALL_PREFIX=$HOME/.llvm -DBUILD_SHARED_LIBS=TRUE -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" -DLLVM_ENABLE_PROJECTS=clang \
&& cmake --build . --target install \
......
from qcor/qcor-base
from qcor/deploy-base
run git clone --recursive https://github.com/eclipse/xacc && cd xacc && mkdir build && cd build \
&& cmake .. \
&& make -j$(nproc) install
......
......@@ -10,7 +10,9 @@ __qpu__ void ccnot(qreg q) {
}
// To program at the unitary matrix level,
// simply indicate you are using qcor::unitary namespace
// invoke the decompose call, indicating which
// buffer to target, can optionally provide decomposition
// algorithm name and an optimizer.
decompose {
// Create the unitary matrix
UnitaryMatrix ccnot = UnitaryMatrix::Identity(8, 8);
......@@ -19,7 +21,7 @@ __qpu__ void ccnot(qreg q) {
ccnot(6, 7) = 1.0;
ccnot(7, 6) = 1.0;
}
(q, QFAST);
(q);
// Add some measures
for (int i = 0; i < q.size(); i++) {
......
......@@ -119,9 +119,15 @@ std::string run_token_collector(clang::Preprocessor &PP,
i++;
}
if (arguments.size() < 1) {
if (arguments.size() == 0) {
xacc::error("Invalid decompose arguments, must at least provide the qreg variable");
}
if (arguments.size() == 1) {
arguments.push_back("QFAST");
}
std::map<int, std::function<void(const std::string arg)>> arg_to_action{
{0,
[&](const std::string arg) {
......
......@@ -83,10 +83,18 @@ void UnitaryTokenCollector::collect(clang::Preprocessor &PP,
}
ss << "\n";
const auto optimizer_provided =
ss.str().find("decompose_optimizer = ") != std::string::npos;
if (optimizer_provided) {
ss << "auto decomposed_program = "
"__internal__::decompose_unitary(decompose_algo_name, "
<< var_name << ", decompose_buffer_name, decompose_optimizer);\n";
} else {
ss << "auto decomposed_program = "
"__internal__::decompose_unitary(decompose_algo_name, "
<< var_name << ", decompose_buffer_name);\n";
}
// Add the qfast decomp and hook up to the qrt program.
ss << "auto decomposed_program = "
"__internal__::decompose_unitary(decompose_algo_name, "
<< var_name << ", decompose_buffer_name);\n";
ss << "parent_kernel->addInstruction(decomposed_program);\n";
}
......
......@@ -34,7 +34,8 @@ get_transformation(const std::string &transform_type) {
return xacc::getService<xacc::IRTransformation>(transform_type);
}
std::shared_ptr<qcor::CompositeInstruction>
decompose_unitary(const std::string algorithm, UnitaryMatrix &mat, const std::string buffer_name) {
decompose_unitary(const std::string algorithm, UnitaryMatrix &mat,
const std::string buffer_name) {
auto tmp = xacc::getService<xacc::Instruction>(algorithm);
auto decomposed = std::dynamic_pointer_cast<CompositeInstruction>(tmp);
......@@ -48,14 +49,41 @@ decompose_unitary(const std::string algorithm, UnitaryMatrix &mat, const std::st
}
for (auto inst : decomposed->getInstructions()) {
std::vector<std::string> buffer_names;
for (int i = 0; i < inst->nRequiredBits(); i++) {
buffer_names.push_back(buffer_name);
}
inst->setBufferNames(buffer_names);
std::vector<std::string> buffer_names;
for (int i = 0; i < inst->nRequiredBits(); i++) {
buffer_names.push_back(buffer_name);
}
inst->setBufferNames(buffer_names);
}
return decomposed;
}
std::shared_ptr<qcor::CompositeInstruction>
decompose_unitary(const std::string algorithm, UnitaryMatrix &mat,
const std::string buffer_name,
std::shared_ptr<xacc::Optimizer> optimizer) {
auto tmp = xacc::getService<xacc::Instruction>(algorithm);
auto decomposed = std::dynamic_pointer_cast<CompositeInstruction>(tmp);
// default Adam
const bool expandOk = decomposed->expand(
{std::make_pair("unitary", mat), std::make_pair("optimizer", optimizer)});
if (!expandOk) {
xacc::error("Could not decmpose unitary with " + algorithm);
}
for (auto inst : decomposed->getInstructions()) {
std::vector<std::string> buffer_names;
for (int i = 0; i < inst->nRequiredBits(); i++) {
buffer_names.push_back(buffer_name);
}
inst->setBufferNames(buffer_names);
}
return decomposed;
}
} // namespace __internal__
} // namespace qcor
\ No newline at end of file
......@@ -5,6 +5,7 @@
#include "IRTransformation.hpp"
#include "qrt.hpp"
#include "xacc_internal_compiler.hpp"
#include "Optimizer.hpp"
#include <Eigen/Dense>
#include <functional>
......@@ -103,6 +104,10 @@ std::shared_ptr<qcor::IRProvider> get_provider();
std::shared_ptr<qcor::CompositeInstruction>
decompose_unitary(const std::string algorithm, UnitaryMatrix &mat, const std::string buffer_name);
// Decompose the given unitary matrix with the specified decomposition algorithm and optimizer
std::shared_ptr<qcor::CompositeInstruction>
decompose_unitary(const std::string algorithm, UnitaryMatrix &mat, const std::string buffer_name, std::shared_ptr<xacc::Optimizer> optimizer);
// Utility for calling a Functor via mapping a tuple of Args to
// a sequence of Args...
template <typename Function, typename Tuple, size_t... I>
......
......@@ -85,12 +85,15 @@ def main(argv=None):
sys.argv.remove(qrtOptLevel)
sys.argv.remove('-opt')
sys.argv += ['-D__internal__qcor__compile__opt__level='+qrtOptLevel]
# Enable runtime kernel optimization stats print-out:
# i.e. passes that are executed and their info (walltime, gate count reduction, etc.)
# Enable runtime kernel optimization stats print-out:
# i.e. passes that are executed and their info (walltime, gate count reduction, etc.)
if '-print-opt-stats' in sys.argv[1:]:
sys.argv.remove('-print-opt-stats')
sys.argv += ['-D__internal__qcor__compile__opt__print__stats']
if '-print-opt-stats' in sys.argv[1:]:
sys.argv.remove('-print-opt-stats')
sys.argv += ['-D__internal__qcor__compile__opt__print__stats']
# Get the filename we are compiling or the object file
filename = ''
fileType = ''
......
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