Unverified Commit 33a8e690 authored by Mccaskey, Alex's avatar Mccaskey, Alex Committed by GitHub
Browse files

Merge pull request #98 from zpparks314/master

Renaming DWKernel -> DWFunction, changing addInstruction() to add par…
parents 67456992 6f56c659
Pipeline #28734 passed with stages
in 9 minutes and 13 seconds
......@@ -2,13 +2,13 @@
# 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 v.10 which accompany 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
# and Eclipse Distribution License v.10 which accompany 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
# Alexander J. McCaskey - initial API and implementation
# *******************************************************************************/
set (LIBRARY_NAME xacc-quantum-aqc)
......@@ -51,7 +51,7 @@ usFunctionEmbedResources(TARGET ${LIBRARY_NAME}
manifest.json
)
target_link_libraries(${LIBRARY_NAME} PUBLIC xacc)
target_link_libraries(${LIBRARY_NAME} PUBLIC xacc)
if(APPLE)
set_target_properties(xacc-quantum-aqc PROPERTIES INSTALL_RPATH "@loader_path")
......
......@@ -10,8 +10,8 @@
* Contributors:
* Alexander J. McCaskey - initial API and implementation
*******************************************************************************/
#ifndef QUANTUM_AQC_DWKERNEL_HPP_
#define QUANTUM_AQC_DWKERNEL_HPP_
#ifndef QUANTUM_AQC_DWFUNCTION_HPP_
#define QUANTUM_AQC_DWFUNCTION_HPP_
#include "Function.hpp"
#include "DWQMI.hpp"
......@@ -30,9 +30,9 @@ using parser_t = exprtk::parser<double>;
namespace xacc {
namespace quantum {
class DWKernel : public Function,
class DWFunction : public Function,
public GraphProvider<DWVertex>,
public std::enable_shared_from_this<DWKernel> {
public std::enable_shared_from_this<DWFunction> {
protected:
std::list<InstPtr> instructions;
......@@ -49,14 +49,14 @@ public:
* @param id
* @param name
*/
DWKernel(std::string kernelName)
DWFunction(std::string kernelName)
: _name(kernelName), parameters(std::vector<InstructionParameter>{}) {}
DWKernel(std::string kernelName, std::vector<InstructionParameter> p)
DWFunction(std::string kernelName, std::vector<InstructionParameter> p)
: _name(kernelName), parameters(p) {}
std::shared_ptr<Function> enabledView() override {
auto newF = std::make_shared<DWKernel>(_name, parameters);
auto newF = std::make_shared<DWFunction>(_name, parameters);
for (int i = 0; i < nInstructions(); i++) {
auto inst = getInstruction(i);
if (inst->isEnabled()) {
......@@ -73,7 +73,7 @@ public:
if (instructions.size() > idx) {
i = *std::next(instructions.begin(), idx);
} else {
xacc::error("DWKernel getInstruction invalid instruction index - " +
xacc::error("DWFunction getInstruction invalid instruction index - " +
std::to_string(idx) + ".");
}
return i;
......@@ -86,11 +86,11 @@ public:
}
void mapBits(std::vector<int> bitMap) override {
xacc::error("DWKernel.mapBits not implemented");
xacc::error("DWFunction.mapBits not implemented");
}
const int nRequiredBits() const override {
XACCLogger::instance()->error("DWKernel nRequiredBits() not implemented.");
XACCLogger::instance()->error("DWFunction nRequiredBits() not implemented.");
return 0;
}
/**
......@@ -100,11 +100,21 @@ const int nRequiredBits() const override {
* @param instruction
*/
void addInstruction(InstPtr instruction) override {
xacc::InstructionParameter param = instruction->getParameter(0);
bool dupParam = false;
for (auto p : parameters) {
if (p.toString() == param.toString()) {
dupParam = true;
}
}
if (!dupParam) {
parameters.push_back(param);
}
instructions.push_back(instruction);
}
const int depth() override {
xacc::error("DWKernel graph is undirected, cannot compute depth.");
xacc::error("DWFunction graph is undirected, cannot compute depth.");
return 0;
}
......@@ -174,7 +184,7 @@ const int nRequiredBits() const override {
const std::string toString() override {
return toString("");
}
/**
* Return the number of logical qubits.
*
......@@ -185,14 +195,14 @@ const int nRequiredBits() const override {
}
/**
* Return the number of physical qubits.
*
* Return the number of physical qubits.
*
* @return nPhysical The number of physical qubits.
*/
const int nPhysicalBits() override {
return 0;
}
std::vector<double> getAllBiases() {
std::vector<double> biases;
for (auto i : instructions) {
......@@ -222,7 +232,7 @@ const int nRequiredBits() const override {
void setParameter(const int idx, InstructionParameter &p) override {
if (idx + 1 > parameters.size()) {
XACCLogger::instance()->error(
"DWKernel.setParameter: Invalid Parameter requested.");
"DWFunction.setParameter: Invalid Parameter requested.");
}
parameters[idx] = p;
......@@ -242,7 +252,7 @@ const int nRequiredBits() const override {
std::shared_ptr<Function> operator()(const Eigen::VectorXd &params) override {
if (params.size() != nParameters()) {
xacc::error("Invalid DWKernel evaluation: number "
xacc::error("Invalid DWFunction evaluation: number "
"of parameters don't match. " +
std::to_string(params.size()) + ", " +
std::to_string(nParameters()));
......@@ -268,7 +278,7 @@ const int nRequiredBits() const override {
return expr.value();
};
auto evaluatedFunction = std::make_shared<DWKernel>("evaled_" + name());
auto evaluatedFunction = std::make_shared<DWFunction>("evaled_" + name());
for (auto inst : getInstructions()) {
if (inst->isComposite()) {
// If a Function, call this method recursively
......@@ -333,10 +343,10 @@ const int nRequiredBits() const override {
*/
void setOption(const std::string optName,
InstructionParameter option) override {
XACCLogger::instance()->error("setOption not implemented for DWKernel.");
return;
XACCLogger::instance()->error("setOption not implemented for DWFunction.");
return;
}
/**
* Get the value of an option with the given name.
*
......@@ -344,8 +354,8 @@ const int nRequiredBits() const override {
* @return option The value of the option.
*/
InstructionParameter getOption(const std::string optName) override {
XACCLogger::instance()->error("getOption not implemented for DWKernel.");
return InstructionParameter(0);
XACCLogger::instance()->error("getOption not implemented for DWFunction.");
return InstructionParameter(0);
}
/**
......@@ -354,8 +364,8 @@ const int nRequiredBits() const override {
* @return optMap The options map.
*/
std::map<std::string, InstructionParameter> getOptions() override {
XACCLogger::instance()->error("getOptions not implemented for DWKernel.");
return std::map<std::string,InstructionParameter>();
XACCLogger::instance()->error("getOptions not implemented for DWFunction.");
return std::map<std::string,InstructionParameter>();
}
EMPTY_DEFINE_VISITABLE()
};
......
......@@ -13,7 +13,7 @@ DWIRProvider::createInstruction(const std::string name, std::vector<int> bits,
std::shared_ptr<Function>
DWIRProvider::createFunction(const std::string name, std::vector<int> bits,
std::vector<InstructionParameter> parameters) {
return std::make_shared<DWKernel>(name, parameters);
return std::make_shared<DWFunction>(name, parameters);
}
std::shared_ptr<IR> DWIRProvider::createIR() {
......
......@@ -4,7 +4,7 @@
#include "XACC.hpp"
#include "Identifiable.hpp"
#include "DWQMI.hpp"
#include "DWKernel.hpp"
#include "DWFunction.hpp"
#include "IRProvider.hpp"
namespace xacc {
......
add_xacc_test(DWKernel)
add_xacc_test(DWFunction)
add_xacc_test(DWQMI)
......@@ -11,17 +11,17 @@
* Alexander J. McCaskey - initial API and implementation
*******************************************************************************/
#include <gtest/gtest.h>
#include "DWKernel.hpp"
#include "DWFunction.hpp"
using namespace xacc::quantum;
TEST(DWKernelTester, checkDWKernelConstruction) {
TEST(DWFunctionTester, checkDWKernelConstruction) {
auto qmi = std::make_shared<DWQMI>(0, 1, 2.2);
auto qmi2 = std::make_shared<DWQMI>(0);
auto qmi3 = std::make_shared<DWQMI>(22, 3.3);
DWKernel kernel("foo");
DWFunction kernel("foo");
kernel.addInstruction(qmi);
kernel.addInstruction(qmi2);
kernel.addInstruction(qmi3);
......@@ -37,13 +37,13 @@ TEST(DWKernelTester, checkDWKernelConstruction) {
EXPECT_TRUE(kernel.toString("") == expected);
}
TEST(DWKernelTester, checkGraph) {
TEST(DWFunctionTester, checkGraph) {
auto qmi = std::make_shared<DWQMI>(0, 1, 2.2);
auto qmi2 = std::make_shared<DWQMI>(0, 1.2);
auto qmi3 = std::make_shared<DWQMI>(1, 3.3);
DWKernel kernel("foo");
DWFunction kernel("foo");
kernel.addInstruction(qmi);
kernel.addInstruction(qmi2);
kernel.addInstruction(qmi3);
......@@ -52,6 +52,42 @@ TEST(DWKernelTester, checkGraph) {
graph.write(std::cout);
}
TEST(DWFunctionTester, checkVariableParameterEval) {
auto param1 = xacc::InstructionParameter("param1");
auto param2 = xacc::InstructionParameter("param1");
auto param3 = xacc::InstructionParameter("param2");
auto qmi = std::make_shared<DWQMI>(0, 1, param1);
auto qmi2 = std::make_shared<DWQMI>(2, 3, param1);
auto qmi3 = std::make_shared<DWQMI>(3,4 , param3);
DWFunction kernel("foo");
kernel.addInstruction(qmi);
kernel.addInstruction(qmi2);
kernel.addInstruction(qmi3);
std::cout << kernel.nParameters() << std::endl;
for (auto param : kernel.getParameters()) {
std::cout << boost::get<std::string>(param) << std::endl;
}
EXPECT_TRUE(kernel.nParameters() == 2);
EXPECT_TRUE(kernel.nInstructions() == 3);
EXPECT_TRUE(kernel.name() == "foo");
EXPECT_TRUE(kernel.getInstruction(0) == qmi);
EXPECT_TRUE(kernel.getInstruction(1) == qmi2);
EXPECT_TRUE(kernel.getInstruction(2) == qmi3);
const std::string expected = "0 1 param1;\n2 3 param1;\n3 4 param2;\n";
EXPECT_TRUE(kernel.toString("") == expected);
Eigen::VectorXd pars(2);
pars(0) = 3.4;
pars(1) = 4.5;
auto evaled = kernel(pars);
std::cout << evaled->toString("") << std::endl;
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
......
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