Skip to content
Snippets Groups Projects
Commit 4eede44e authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

updating with ir package, update qasm2qir

parent bb7579b9
No related branches found
No related tags found
No related merge requests found
......@@ -67,6 +67,7 @@ endif()
include_directories(${MPI_INCLUDE_PATH})
include_directories(${Boost_INCLUDE_DIRS})
include_directories("${CMAKE_SOURCE_DIR}/xacc")
include_directories("${CMAKE_SOURCE_DIR}/xacc/ir")
include_directories("${CMAKE_SOURCE_DIR}/xacc/compiler")
include_directories("${CMAKE_SOURCE_DIR}/xacc/program")
include_directories("${CMAKE_SOURCE_DIR}/xacc/accelerator")
......
......@@ -32,7 +32,6 @@
#define QUANTUM_GATE_ACCELERATORS_EIGENACCELERATOR_HPP_
#include "QPUGate.hpp"
#include "GraphIR.hpp"
#include "QuantumCircuit.hpp"
#include "SimulatedQubits.hpp"
#include <random>
......
......@@ -29,5 +29,8 @@
#
#**********************************************************************************/
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate/utils)
add_executable(qasm2graphir qasm2graphir.cpp)
target_link_libraries(qasm2graphir ${Boost_LIBRARIES})
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate/gateqir)
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate/gateqir/instructions)
add_executable(qasm2qir qasm2qir.cpp)
target_link_libraries(qasm2qir ${Boost_LIBRARIES} xacc-gateqir)
......@@ -28,10 +28,14 @@
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#include "QasmToGraph.hpp"
#include "GateFunction.hpp"
#include "GateInstruction.hpp"
#include "ParameterizedGateInstruction.hpp"
#include <boost/program_options.hpp>
#include <iostream>
#include "GraphIR.hpp"
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <regex>
using namespace boost::program_options;
......@@ -65,10 +69,124 @@ int main(int argc, char** argv) {
ss << inputStream.rdbuf();
inputStream.close();
auto graph = xacc::quantum::QasmToGraph::getCircuitGraph(ss.str());
auto flatQasmStr = ss.str();
std::map<std::string, int> qubitVarNameToId;
std::vector<std::string> qasmLines;
std::vector<int> allQbitIds;
std::regex newLineDelim("\n"), spaceDelim(" ");
std::regex qubitDeclarations("\\s*qubit\\s*\\w+");
std::sregex_token_iterator first{flatQasmStr.begin(), flatQasmStr.end(), newLineDelim, -1}, last;
int nQubits = 0, qbitId = 0, layer = 1, gateId = 1;
std::string qubitVarName;
qasmLines = {first, last};
xacc::GraphIR<decltype(graph)> xir(graph);
xir.persist(outputStream);
// Let's now loop over the qubit declarations,
// and construct a mapping of qubit var names to integer id,
// and get the total number of qubits
for (auto i = std::sregex_iterator(flatQasmStr.begin(), flatQasmStr.end(),
qubitDeclarations); i != std::sregex_iterator(); ++i) {
std::string qubitLine = (*i).str();
qubitLine.erase(std::remove(qubitLine.begin(), qubitLine.end(), '\n'), qubitLine.end());
std::sregex_token_iterator first{qubitLine.begin(), qubitLine.end(), spaceDelim, -1}, last;
std::vector<std::string> splitQubitLine = {first, last};
qubitVarNameToId[splitQubitLine[1]] = qbitId;
splitQubitLine[1].erase(
std::remove_if(splitQubitLine[1].begin(), splitQubitLine[1].end(), &isdigit),
splitQubitLine[1].end());
qubitVarName = splitQubitLine[1];
allQbitIds.push_back(qbitId);
qbitId++;
}
// Set the number of qubits
nQubits = qubitVarNameToId.size();
int c = 0;
auto function = std::make_shared<xacc::quantum::GateFunction>(inputFileName);
for (auto line : qasmLines) {
// If this is a gate line...
if (!boost::contains(line, "qubit") && !boost::contains(line, "cbit")) {
// Split the current qasm command at the spaces
std::sregex_token_iterator first { line.begin(),
line.end(), spaceDelim, -1 }, last;
std::vector<std::string> gateCommand = {first, last};
// Set the qubits this gate acts on
std::vector<int> actingQubits;
if (!boost::contains(gateCommand[1], ",")) {
actingQubits.push_back(qubitVarNameToId[gateCommand[1]]);
std::shared_ptr<xacc::Instruction> inst;
if (gateCommand[0] == "MeasZ" || gateCommand[0] == "MeasX"
|| gateCommand[0] == "Measure") {
inst = xacc::quantum::ParameterizedGateInstructionRegistry<
int>::instance()->create("Measure", actingQubits,
c);
c++;
} else {
if (gateCommand[0] == "T") {
inst =
xacc::quantum::ParameterizedGateInstructionRegistry<
double>::instance()->create("Rz",
actingQubits, (3.1415 / 4.0));
} else {
inst =
xacc::quantum::GateInstructionRegistry::instance()->create(
gateCommand[0], actingQubits);
}
}
function->addInstruction(inst);
} else {
// FIXME Need to differentiate between qubits and parameters here
// First we need the qubit register variable name
int counter = 0;
std::vector<std::string> splitComma;
std::vector<double> props;
boost::split(splitComma, gateCommand[1], boost::is_any_of(","));
for (auto segment : splitComma) {
if (boost::contains(segment, qubitVarName)) {
actingQubits.push_back(qubitVarNameToId[segment]);
} else {
// This is not a qubit, it must be a parameter for gate
std::cout << "param " << segment << "\n";
props.push_back(std::stod(segment));
}
}
if (props.empty()) {
auto inst =
xacc::quantum::GateInstructionRegistry::instance()->create(
gateCommand[0], actingQubits);
function->addInstruction(inst);
} else {
std::shared_ptr<xacc::Instruction> inst;
// We only allow parameters of size 2 and doubles
if (props.size() == 1) {
inst =
xacc::quantum::ParameterizedGateInstructionRegistry<
double>::instance()->create(
gateCommand[0], actingQubits,
props[0]);
} else if (props.size() == 2) {
inst =
xacc::quantum::ParameterizedGateInstructionRegistry<
double, double>::instance()->create(
gateCommand[0], actingQubits,
props[0],
props[1]);
}
function->addInstruction(inst);
}
}
}
}
std::cout << "FTOSTR:\n" << function->toString("qreg") << "\n";
return 0;
}
......@@ -28,21 +28,16 @@
# Initial API and implementation - Alex McCaskey
#
#**********************************************************************************/
set (PACKAGE_NAME "XACC Core Runtime")
set (PACKAGE_NAME "XACC Specification")
set (PACKAGE_DESCIPTION "The XACC Programming Framework")
set (LIBRARY_NAME xacc)
file (GLOB HEADERS program/*.hpp compiler/*.hpp accelerator/*.hpp utils/*.hpp)
file (GLOB SRC program/*.cpp compiler/*.cpp accelerator/*.cpp)
#add_library(${LIBRARY_NAME} SHARED ${SRC})
file (GLOB HEADERS ir/*.hpp program/*.hpp compiler/*.hpp accelerator/*.hpp utils/*.hpp)
# Get the test files
file(GLOB test_files tests/*Tester.cpp)
# Add the tests
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}/utils;${CMAKE_CURRENT_SOURCE_DIR}/tests;${CMAKE_CURRENT_SOURCE_DIR}/compiler;${CMAKE_CURRENT_SOURCE_DIR}/program;${CMAKE_CURRENT_SOURCE_DIR}/accelerator;${CMAKE_SOURCE_DIR}/quantum/gate/accelerators/firetensoraccelerator;${CMAKE_SOURCE_DIR}/quantum/gate/accelerators;${CMAKE_SOURCE_DIR}/quantum/gate/utils;${CMAKE_SOURCE_DIR}/quantum/gate/gateqir;${CMAKE_SOURCE_DIR}/quantum/gate/gateqir/instructions;${CMAKE_SOURCE_DIR}/quantum/gate/gateqir/functions;${CMAKE_SOURCE_DIR}/tpls/fire/tensors;${CMAKE_SOURCE_DIR}/tpls/fire/tensors/impl;${CMAKE_SOURCE_DIR}/tpls/fire/tpls/eigen" "${Boost_LIBRARIES};xacc-firetensor;xacc-gateqir")
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}/utils;${CMAKE_CURRENT_SOURCE_DIR}/compiler;${CMAKE_CURRENT_SOURCE_DIR}/program;${CMAKE_CURRENT_SOURCE_DIR}/accelerator;${CMAKE_SOURCE_DIR}/quantum/gate/accelerators/firetensoraccelerator;${CMAKE_SOURCE_DIR}/quantum/gate/accelerators;${CMAKE_SOURCE_DIR}/quantum/gate/utils;${CMAKE_SOURCE_DIR}/quantum/gate/gateqir;${CMAKE_SOURCE_DIR}/quantum/gate/gateqir/instructions;${CMAKE_SOURCE_DIR}/quantum/gate/gateqir/functions;${CMAKE_SOURCE_DIR}/tpls/fire/tensors;${CMAKE_SOURCE_DIR}/tpls/fire/tensors/impl;${CMAKE_SOURCE_DIR}/tpls/fire/tpls/eigen" "${Boost_LIBRARIES};xacc-firetensor;xacc-gateqir")
install(FILES ${HEADERS} DESTINATION include)
#install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
/***********************************************************************************
* 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
*
**********************************************************************************/
#ifndef QUANTUM_GRAPHIR_HPP_
#define QUANTUM_GRAPHIR_HPP_
#include "IR.hpp"
#include "Graph.hpp"
namespace xacc {
template<typename DerivedGraph>
class GraphIR : public IR {
protected:
DerivedGraph graph;
public:
GraphIR() {}
GraphIR(DerivedGraph& g) :
graph(g) {
}
virtual void setAcceleratorBuffer(std::shared_ptr<AcceleratorBuffer> buf) {}
virtual int order() {
return graph.order();
}
virtual int size() {
return graph.size();
}
virtual std::string toString() {
return "";
}
virtual void persist(std::ostream& outStream) {
graph.write(outStream);
}
virtual void load(std::istream& inStream) {
graph.read(inStream);
}
DerivedGraph& getGraph() {
return graph;
}
virtual void addKernel(std::shared_ptr<Function> kernel) {}
virtual std::shared_ptr<Function> getKernel(const std::string& name) {
}
};
}
#endif
File moved
File moved
File moved
File moved
/***********************************************************************************
* 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 <organization> 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 XACC_TESTS_FAKEACCELERATOR_HPP_
#define XACC_TESTS_FAKEACCELERATOR_HPP_
#include "Accelerator.hpp"
#include "IR.hpp"
using namespace xacc;
class FakeAccelerator: virtual public Accelerator {
public:
virtual AcceleratorType getType() {
return qpu_gate;
}
std::shared_ptr<AcceleratorBuffer> createBuffer(const std::string& varId) {
auto buffer = std::make_shared<AcceleratorBuffer>(varId);
return buffer;
}
std::shared_ptr<AcceleratorBuffer> createBuffer(const std::string& varId,
const int size) {
if (!isValidBufferSize(size)) {
XACCError("Invalid buffer size.");
}
auto buffer = std::make_shared<AcceleratorBuffer>(varId, size);
return buffer;
}
virtual bool isValidBufferSize(const int NBits) {
return NBits <= 10;
}
virtual std::vector<IRTransformation> getIRTransformations() {
std::vector<IRTransformation> v;
return v;
}
virtual void execute(std::shared_ptr<AcceleratorBuffer> buffer,
const std::shared_ptr<Function> ir) {
}
virtual ~FakeAccelerator() {
}
};
#endif
/***********************************************************************************
* 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 <organization> 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 XACC_TESTS_FAKECOMPILER_HPP_
#define XACC_TESTS_FAKECOMPILER_HPP_
#include "Compiler.hpp"
#include "FakeIR.hpp"
using namespace xacc;
class FakeCompiler: public Compiler {
public:
virtual std::shared_ptr<xacc::IR> compile(const std::string& src,
std::shared_ptr<Accelerator> acc) {
return std::make_shared<FakeIR>();
}
virtual std::shared_ptr<xacc::IR> compile(const std::string& src) {
return std::make_shared<FakeIR>();
}
virtual const std::string getName() {
return "Fake";
}
virtual void modifySource() {
}
virtual std::string getBitType() {
return "qbit";
}
virtual ~FakeCompiler() {
}
};
#endif
/***********************************************************************************
* 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 <organization> 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 XACC_TESTS_FAKEIR_HPP_
#define XACC_TESTS_FAKEIR_HPP_
#include "IR.hpp"
using namespace xacc;
class FakeIR: public IR {
public:
FakeIR() {
}
virtual std::string toString() { return std::string();}
virtual void persist(std::ostream& stream) {}
virtual void load(std::istream& inStream) {}
virtual void setAcceleratorBuffer(std::shared_ptr<AcceleratorBuffer> buf) {}
virtual void addKernel(std::shared_ptr<Function> kernel) {
}
virtual std::shared_ptr<Function> getKernel(const std::string& name) {
}
};
#endif
......@@ -32,13 +32,64 @@
#define BOOST_TEST_MODULE ProgramTester
#include <boost/test/included/unit_test.hpp>
#include "XACC.hpp"
#include "FakeIR.hpp"
#include "FakeAccelerator.hpp"
#include "FireTensorAccelerator.hpp"
#include "Program.hpp"
using namespace xacc;
class FakeIR: public IR {
public:
FakeIR() {
}
virtual std::string toString() { return std::string();}
virtual void persist(std::ostream& stream) {}
virtual void load(std::istream& inStream) {}
virtual void setAcceleratorBuffer(std::shared_ptr<AcceleratorBuffer> buf) {}
virtual void addKernel(std::shared_ptr<Function> kernel) {
}
virtual std::shared_ptr<Function> getKernel(const std::string& name) {
}
};
class FakeAccelerator: virtual public Accelerator {
public:
virtual AcceleratorType getType() {
return qpu_gate;
}
std::shared_ptr<AcceleratorBuffer> createBuffer(const std::string& varId) {
auto buffer = std::make_shared<AcceleratorBuffer>(varId);
return buffer;
}
std::shared_ptr<AcceleratorBuffer> createBuffer(const std::string& varId,
const int size) {
if (!isValidBufferSize(size)) {
XACCError("Invalid buffer size.");
}
auto buffer = std::make_shared<AcceleratorBuffer>(varId, size);
return buffer;
}
virtual bool isValidBufferSize(const int NBits) {
return NBits <= 10;
}
virtual std::vector<IRTransformation> getIRTransformations() {
std::vector<IRTransformation> v;
return v;
}
virtual void execute(std::shared_ptr<AcceleratorBuffer> buffer,
const std::shared_ptr<Function> ir) {
}
virtual ~FakeAccelerator() {
}
};
class DummyCompiler : public Compiler {
public:
virtual std::shared_ptr<xacc::IR> compile(const std::string& src,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment