Commit 3a9222a2 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Continuing work on QIR

parent 0a29622b
......@@ -29,10 +29,10 @@
#
#**********************************************************************************/
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/utils)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/gateir)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/gateir/instructions)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/gateqir)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/gateqir/instructions)
add_subdirectory(gateir)
add_subdirectory(gateqir)
add_subdirectory(compilers)
add_subdirectory(accelerators)
add_subdirectory(utils)
......
......@@ -42,5 +42,5 @@ install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
# Gather tests
file (GLOB test_files tests/*.cpp)
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR};${CMAKE_CURRENT_SOURCE_DIR}/instructions" "${LIBRARY_NAME};${Boost_LIBRARIES};xacc-qir")
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR};${CMAKE_CURRENT_SOURCE_DIR}/instructions" "${LIBRARY_NAME};${Boost_LIBRARIES}")
......@@ -28,39 +28,59 @@
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#include "QFunction.hpp"
#include "GateFunction.hpp"
namespace xacc {
namespace quantum {
QFunction::QFunction(int id, const std::string name) :
GateFunction::GateFunction(int id, const std::string name) :
functionId(id), functionName(name) {
}
void QFunction::accept(QInstructionVisitor& visitor) {
void GateFunction::accept(QInstructionVisitor& visitor) {
return;
}
void QFunction::addInstruction(InstPtr instruction) {
void GateFunction::addInstruction(InstPtr instruction) {
instructions.push_back(instruction);
}
void QFunction::replaceInstruction(InstPtr currentInst,
void GateFunction::replaceInstruction(InstPtr currentInst,
InstPtr replacingInst) {
}
void QFunction::replaceInstruction(int instId, InstPtr replacingInst) {
void GateFunction::replaceInstruction(int instId, InstPtr replacingInst) {
}
const std::string QFunction::toString(const std::string bufferVarName) {
const std::string GateFunction::toString(const std::string bufferVarName) {
std::string retStr = "";
for (auto i : instructions) {
retStr += i->toString(bufferVarName) + "\n";
}
return retStr;
}
const int GateFunction::getId() {
return functionId;
}
const std::string GateFunction::getName() {
return functionName;
}
const std::vector<int> GateFunction::qubits() {
return qbits;
}
const int GateFunction::nInstructions() {
return instructions.size();
}
}
}
/***********************************************************************************
* Copyright (c) 2017, UT-Battelle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the xacc nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Contributors:
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#ifndef QUANTUM_GATEQIR_QFUNCTION_HPP_
#define QUANTUM_GATEQIR_QFUNCTION_HPP_
#include "QFunction.hpp"
namespace xacc {
namespace quantum {
class GateFunction: public virtual QFunction {
protected:
std::vector<std::shared_ptr<QInstruction>> instructions;
std::string functionName;
int functionId;
std::vector<int> qbits;
public:
GateFunction();
GateFunction(int id, const std::string name);
/**
* Add an instruction to this quantum
* intermediate representation.
*
* @param instruction
*/
virtual void addInstruction(InstPtr instruction);
/**
* Replace the given current quantum instruction
* with the new replacingInst quantum Instruction.
*
* @param currentInst
* @param replacingInst
*/
virtual void replaceInstruction(InstPtr currentInst,
InstPtr replacingInst);
/**
* Replace the given current quantum instruction
* with the new replacingInst quantum Instruction.
*
* @param currentInst
* @param replacingInst
*/
virtual void replaceInstruction(int instId,
InstPtr replacingInst);
virtual const int getId();
virtual const std::string getName();
virtual const std::vector<int> qubits();
virtual const std::string toString(const std::string bufferVarName);
virtual void accept(QInstructionVisitor& visitor);
const int nInstructions();
};
}
}
#endif /* QUANTUM_GATE_IR_QFUNCTION_HPP_ */
......@@ -28,14 +28,17 @@
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#ifndef QUANTUM_GATE_GATEIR_GATEINSTRUCTION_HPP_
#define QUANTUM_GATE_GATEIR_GATEINSTRUCTION_HPP_
#ifndef QUANTUM_GATE_GATEQIR_GATEINSTRUCTION_HPP_
#define QUANTUM_GATE_GATEQIR_GATEINSTRUCTION_HPP_
#include "QInstruction.hpp"
namespace xacc {
namespace quantum {
/**
*
*/
class GateInstruction: public virtual QInstruction {
protected:
......@@ -88,4 +91,4 @@ public:
#endif /* QUANTUM_GATE_GATEIR_GATEINSTRUCTION_HPP_ */
#endif /* QUANTUM_GATE_GATEQIR_GATEINSTRUCTION_HPP_ */
......@@ -39,6 +39,17 @@ void GateQIR::generateGraph() {
std::string GateQIR::toString() {
int nQubits = buffer->size();
auto bufVarName = buffer->name();
std::string retStr = "";
for (int i = 0; i < nQubits; i++) {
retStr += "qubit " + bufVarName + std::to_string(i) + "\n";
}
for (auto f : kernels) {
retStr += f->toString(bufVarName);
}
return retStr;
}
void GateQIR::persist(std::ostream& outStream) {
......
......@@ -31,7 +31,7 @@
#ifndef QUANTUM_GATE_GATEQIR_HPP_
#define QUANTUM_GATE_GATEQIR_HPP_
#include "../../qir/QIR.hpp"
#include "QIR.hpp"
namespace xacc {
namespace quantum {
......@@ -43,8 +43,8 @@ namespace quantum {
* Parameters: Gate, Layer (ie time sequence), Gate Vertex Id,
* Qubit Ids that the gate acts on, enabled state, vector of parameters names
*/
class CircuitNode: public XACCVertex<std::string, int, int,
std::vector<int>, bool, std::vector<std::string>> {
class CircuitNode: public XACCVertex<std::string, int, int, std::vector<int>,
bool, std::vector<std::string>> {
public:
CircuitNode() :
XACCVertex() {
......@@ -67,12 +67,25 @@ public:
*/
class GateQIR: public virtual xacc::quantum::QIR<xacc::quantum::CircuitNode> {
protected:
/**
*
*/
std::shared_ptr<AcceleratorBuffer> buffer;
public:
GateQIR() : QFunction() {}
GateQIR() {
}
GateQIR(int id, const std::string name) : QFunction(id, name) {}
GateQIR(std::shared_ptr<AcceleratorBuffer> buf) :
buffer(buf) {
}
virtual void setAcceleratorBuffer(std::shared_ptr<AcceleratorBuffer> buf) {
buffer = buf;
}
/**
*
*/
......
......@@ -28,11 +28,12 @@
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#ifndef QUANTUM_GATE_GATEIR_PARAMETERIZEDGATEINSTRUCTION_HPP_
#define QUANTUM_GATE_GATEIR_PARAMETERIZEDGATEINSTRUCTION_HPP_
#ifndef QUANTUM_GATE_GATEQIR_PARAMETERIZEDGATEINSTRUCTION_HPP_
#define QUANTUM_GATE_GATEQIR_PARAMETERIZEDGATEINSTRUCTION_HPP_
#include "GateInstruction.hpp"
#include "XACCError.hpp"
#include "XaccUtils.hpp"
namespace xacc {
namespace quantum {
......@@ -42,26 +43,24 @@ protected:
std::tuple<InstructionParameter...> params;
public:
ParameterizedGateInstruction(int id, int layer, std::string name,
std::vector<int> qubts, InstructionParameter ... pars) :
GateInstruction(id, layer, name, qubts), params(
std::make_tuple(pars...)) {
ParameterizedGateInstruction(InstructionParameter ... pars) :
params(std::make_tuple(pars...)) {
}
auto getParameter(const std::size_t idx) {
if(idx + 1 > sizeof...(InstructionParameter)) {
if (idx + 1 > sizeof...(InstructionParameter)) {
XACCError("Invalid Parameter requested from Parameterized Gate Instruction.");
}
return xacc::runtime_get(params, idx);
return xacc::tuple_runtime_get(params, idx);
}
virtual const std::string toString(const std::string bufferVarName) {
auto str = gateName;
str += "(";
xacc::for_each(params, [&](auto element) {
xacc::tuple_for_each(params, [&](auto element) {
str += std::to_string(element) + ",";
});
str += str.substr(0, str.length() - 1) + ") ";
str = str.substr(0, str.length() - 1) + ") ";
for (auto q : qubits()) {
str += bufferVarName + std::to_string(q) + ",";
......@@ -74,7 +73,6 @@ public:
}
};
}
}
}}
#endif /* QUANTUM_GATE_GATEIR_PARAMETERIZEDGATEINSTRUCTION_HPP_ */
#endif
......@@ -28,10 +28,10 @@
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#ifndef QUANTUM_GATE_GATEIR_INSTRUCTIONS_CNOT_HPP_
#define QUANTUM_GATE_GATEIR_INSTRUCTIONS_CNOT_HPP_
#ifndef QUANTUM_GATE_GATEQIR_INSTRUCTIONS_CNOT_HPP_
#define QUANTUM_GATE_GATEQIR_INSTRUCTIONS_CNOT_HPP_
#include "GateInstruction.hpp"
#include "../../gateqir/GateInstruction.hpp"
class QInstructionVisitor;
namespace xacc {
......@@ -54,4 +54,4 @@ public:
};
}
}
#endif /* QUANTUM_GATE_GATEIR_INSTRUCTIONS_CNOT_HPP_ */
#endif /* QUANTUM_GATE_GATEQIR_INSTRUCTIONS_CNOT_HPP_ */
......@@ -31,7 +31,7 @@
#ifndef QUANTUM_GATE_IR_HADAMARD_HPP_
#define QUANTUM_GATE_IR_HADAMARD_HPP_
#include "GateInstruction.hpp"
#include "../../gateqir/GateInstruction.hpp"
class QInstructionVisitor;
namespace xacc {
......
......@@ -28,8 +28,8 @@
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#ifndef QUANTUM_GATE_GATEIR_INSTRUCTIONS_RZ_HPP_
#define QUANTUM_GATE_GATEIR_INSTRUCTIONS_RZ_HPP_
#ifndef QUANTUM_GATE_GATEQIR_INSTRUCTIONS_RZ_HPP_
#define QUANTUM_GATE_GATEQIR_INSTRUCTIONS_RZ_HPP_
#include "ParameterizedGateInstruction.hpp"
class QInstructionVisitor;
......@@ -39,8 +39,8 @@ namespace quantum {
class Rz: public virtual ParameterizedGateInstruction<double> {
public:
Rz(int id, int layer, int qbit, double theta) :
ParameterizedGateInstruction(id, layer, "Rz", std::vector<int> {
qbit }, theta) {
ParameterizedGateInstruction<double>(theta), GateInstruction(id,
layer, "Rz", std::vector<int> { qbit }) {
}
virtual void accept(QInstructionVisitor& visitor) {
......
/***********************************************************************************
* Copyright (c) 2016, UT-Battelle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the xacc nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Contributors:
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE HadamardTester
#include <boost/test/included/unit_test.hpp>
#include "CNOT.hpp"
using namespace xacc::quantum;
BOOST_AUTO_TEST_CASE(checkCreation) {
CNOT h(0, 0, 0, 1);
BOOST_VERIFY(h.toString("qreg") == "CNOT qreg0,qreg1");
BOOST_VERIFY(h.getId() == 0);
BOOST_VERIFY(h.layer() == 0);
BOOST_VERIFY(h.qubits().size() == 2);
BOOST_VERIFY(h.qubits()[0] == 0);
BOOST_VERIFY(h.qubits()[1] == 1);
BOOST_VERIFY(h.getName() == "CNOT");
CNOT h2(3, 22, 44, 46);
BOOST_VERIFY(h2.toString("qreg") == "CNOT qreg44,qreg46");
BOOST_VERIFY(h2.getId() == 3);
BOOST_VERIFY(h2.layer() == 22);
BOOST_VERIFY(h2.qubits().size() == 2);
BOOST_VERIFY(h2.qubits()[0] == 44);
BOOST_VERIFY(h2.qubits()[1] == 46);
BOOST_VERIFY(h2.getName() == "CNOT");
}
/***********************************************************************************
* Copyright (c) 2016, UT-Battelle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the xacc nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Contributors:
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE GateFunctionTester
#include <boost/test/included/unit_test.hpp>
#include "GateFunction.hpp"
#include "Hadamard.hpp"
#include "CNOT.hpp"
using namespace xacc::quantum;
const std::string expectedQasm =
"H qreg1\n"
"CNOT qreg1,qreg2\n"
"CNOT qreg0,qreg1\n"
"H qreg0\n";
BOOST_AUTO_TEST_CASE(checkCreation) {
GateFunction f(0, "foo");
BOOST_VERIFY(f.getId() == 0);
BOOST_VERIFY(f.getName() == "foo");
BOOST_VERIFY(f.nInstructions() == 0);
auto h = std::make_shared<Hadamard>(0, 0, 1);
auto cn1 = std::make_shared<CNOT>(1, 1, 1, 2);
auto cn2 = std::make_shared<CNOT>(2, 2, 0, 1);
auto h2 = std::make_shared<Hadamard>(3, 3, 0);
f.addInstruction(h);
f.addInstruction(cn1);
f.addInstruction(cn2);
f.addInstruction(h2);
BOOST_VERIFY(f.nInstructions() == 4);
BOOST_VERIFY(f.toString("qreg") == expectedQasm);
}
/***********************************************************************************
* Copyright (c) 2016, UT-Battelle
* All rights reserved.
......@@ -32,6 +33,7 @@
#define BOOST_TEST_MODULE GateQIRTester
#include <boost/test/included/unit_test.hpp>
#include "GateFunction.hpp"
#include "Hadamard.hpp"
#include "CNOT.hpp"
#include "Rz.hpp"
......@@ -39,7 +41,7 @@
using namespace xacc::quantum;
BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_AUTO_TEST_CASE(checkCreationToString) {
const std::string expectedQasm =
"qubit qreg0\n"
......@@ -48,28 +50,23 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
"H qreg1\n"
"CNOT qreg1,qreg2\n"
"CNOT qreg0,qreg1\n"
"H qreg0\n"
"MeasZ qreg0\n"
"MeasZ qreg1\n"
"H qreg2\n"
"CNOT qreg2,qreg1\n"
"H qreg2\n"
"CNOT qreg2,qreg0";
auto qir = std::make_shared<GateQIR>();
auto accBuffer = std::make_shared<AcceleratorBuffer>("qreg", 3);
qir->setAcceleratorBuffer(accBuffer);
auto rz = std::make_shared<Rz>(0, 0, 0, 3.14);
BOOST_VERIFY(rz->getParameter(0) == 3.14);
"H qreg0\n";
qir->addInstruction(rz);
auto buf = std::make_shared<AcceleratorBuffer>("qreg", 3);
auto qir = std::make_shared<GateQIR>(buf);
BOOST_VERIFY(qir->nInstructions() == 1);
auto f = std::make_shared<GateFunction>(0, "foo");
auto h = std::make_shared<Hadamard>(0, 0, 1);
auto cn1 = std::make_shared<CNOT>(1, 1, 1, 2);
auto cn2 = std::make_shared<CNOT>(2, 2, 0, 1);
auto h2 = std::make_shared<Hadamard>(3, 3, 0);
f->addInstruction(h);
f->addInstruction(cn1);
f->addInstruction(cn2);
f->addInstruction(h2);
std::cout << rz->getParameter(0) << "\n";
qir->addQuantumKernel(f);
BOOST_VERIFY(qir->toString() == expectedQasm);
}
/***********************************************************************************
* Copyright (c) 2016, UT-Battelle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the xacc nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;