Commit aa5b9e33 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Adding Swap gate, updating QFT in RigettiAcceleratorTester

parent cf5e549a
......@@ -153,6 +153,10 @@ public:
+ ") " + std::to_string(cp.bits()[0]) + " " + std::to_string(cp.bits()[1]) + "\n";
}
void visit(Swap& s) {
quilStr += "SWAP " + std::to_string(s.bits()[0]) + " " + std::to_string(s.bits()[1]) + "\n";
}
void visit(GateFunction& f) {
return;
}
......
......@@ -124,8 +124,20 @@ BOOST_AUTO_TEST_CASE(checkKernelExecution) {
BOOST_AUTO_TEST_CASE(buildQFT) {
std::function<std::vector<std::shared_ptr<Instruction>>(std::vector<int>&)> coreqft;
auto bitReversal =
[](std::vector<int> qubits) -> std::vector<std::shared_ptr<Instruction>> {
std::vector<std::shared_ptr<Instruction>> swaps;
auto endStart = qubits.size() - 1;
for (auto i = 0; i < std::floor(qubits.size() / 2.0); ++i) {
swaps.push_back(GateInstructionRegistry::instance()->create("Swap", std::vector<int> {qubits[i], qubits[endStart]}));
endStart--;
}
return swaps;
};
std::function<std::vector<std::shared_ptr<Instruction>>(std::vector<int>&)> coreqft;
coreqft =
[&](std::vector<int>& qubits) -> std::vector<std::shared_ptr<Instruction>> {
......@@ -181,13 +193,18 @@ BOOST_AUTO_TEST_CASE(buildQFT) {
std::vector<int> qbits {0, 1, 2};
auto qftInstructions = coreqft(qbits);
auto swaps = bitReversal(qbits);
for (auto s : swaps) {
qftInstructions.push_back(s);
}
auto qftKernel = std::make_shared<GateFunction>("foo");
for (auto i : qftInstructions) {
qftKernel->addInstruction(i);
}
JsonVisitor v(qftKernel);
std::cout << v.write() << "\n";
// JsonVisitor v(qftKernel);
// std::cout << v.write() << "\n";
std::string expectedQuil(
"H 2\n"
......@@ -195,7 +212,8 @@ BOOST_AUTO_TEST_CASE(buildQFT) {
"H 1\n"
"CPHASE(0.785398) 0 2\n"
"CPHASE(1.5708) 0 1\n"
"H 0\n");
"H 0\n"
"SWAP 0 2\n");
auto quilV = std::make_shared<QuilVisitor>();
......@@ -211,6 +229,7 @@ BOOST_AUTO_TEST_CASE(buildQFT) {
nextInst->accept(quilV);
}
}
std::cout << quilV->getQuilString() << "\n" << expectedQuil << "\n";
BOOST_VERIFY(quilV->getQuilString() == expectedQuil);
......@@ -229,17 +248,26 @@ BOOST_AUTO_TEST_CASE(buildQFT) {
"CPHASE(0.392699) 0 3\n"
"CPHASE(0.785398) 0 2\n"
"CPHASE(1.57078) 0 1\n"
"H 0\n";
"H 0\n"
"SWAP 0 4\n"
"SWAP 1 3\n";
std::vector<int> qbits5 {0, 1, 2, 3, 4};
auto qft5Instructions = coreqft(qbits5);
swaps = bitReversal(qbits5);
for (auto s : swaps) {
qft5Instructions.push_back(s);
}
auto qft5Kernel = std::make_shared<GateFunction>("foo");
for (auto i : qft5Instructions) {
qft5Kernel->addInstruction(i);
}
JsonVisitor v5(qft5Kernel);
std::cout << v5.write() << "\n";
// JsonVisitor v5(qft5Kernel);
// std::cout << v5.write() << "\n";
auto quilV5 = std::make_shared<QuilVisitor>();
......
......@@ -342,28 +342,32 @@ void SimpleAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer,
qubits->applyUnitary(localU);
};
auto swap = [&] (Swap& swapGate) {
XACCError("Swap Gate not implemented yet.");
};
// Create a Visitor that will execute our lambdas when
// we encounter one
auto visitor = std::make_shared<FunctionalGateInstructionVisitor>(hadamard,
cnot, x, y, z, rx, ry, rz, measure, cond, cphase);
cnot, x, y, z, rx, ry, rz, measure, cond, cphase, swap);
XACCInfo("Execution Simple Accelerator Simulation.");
// Our QIR is really a tree structure
// so create a pre-order tree traversal
// InstructionIterator to walk it
InstructionIterator it(kernel);
while (it.hasNext()) {
// Get the next node in the tree
auto nextInst = it.next();
// If enabled, invoke the accept
// method which kicks off the visitor
// to execute the appropriate lambda.
if (nextInst->isEnabled()) {
nextInst->accept(visitor);
}
// Our QIR is really a tree structure
// so create a pre-order tree traversal
// InstructionIterator to walk it
InstructionIterator it(kernel);
while (it.hasNext()) {
// Get the next node in the tree
auto nextInst = it.next();
// If enabled, invoke the accept
// method which kicks off the visitor
// to execute the appropriate lambda.
if (nextInst->isEnabled()) {
nextInst->accept(visitor);
}
}
// qubits->getAcceleratorBitState()
......
/***********************************************************************************
* 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
*
**********************************************************************************/
#include "Swap.hpp"
namespace xacc {
namespace quantum {
Swap::Swap(int controlQubit, int targetQubit) :
GateInstruction("Swap", std::vector<int> { controlQubit, targetQubit },
std::vector<InstructionParameter> {}) {
}
Swap::Swap(std::vector<int> qbits) :
GateInstruction("Swap", qbits, std::vector<InstructionParameter> {}) {
}
RegisterGateInstruction<Swap> SWAPTEMP("Swap");
}
}
/***********************************************************************************
* 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_GATE_GATEQIR_INSTRUCTIONS_SWAP_HPP_
#define QUANTUM_GATE_GATEQIR_INSTRUCTIONS_SWAP_HPP_
#include "GateInstruction.hpp"
namespace xacc {
namespace quantum {
class Swap: public virtual GateInstruction {
public:
Swap(std::vector<int> qbits);
Swap(int controlQubit, int targetQubit);
DEFINE_VISITABLE()
};
}
}
#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 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 SwapTester
#include <boost/test/included/unit_test.hpp>
#include "Swap.hpp"
using namespace xacc::quantum;
BOOST_AUTO_TEST_CASE(checkCreation) {
Swap sw(0, 1);
BOOST_VERIFY(sw.toString("qreg") == "Swap qreg0,qreg1");
BOOST_VERIFY(sw.bits().size() == 2);
BOOST_VERIFY(sw.bits()[0] == 0);
BOOST_VERIFY(sw.bits()[1] == 1);
BOOST_VERIFY(sw.getName() == "Swap");
Swap sw2(44, 45);
BOOST_VERIFY(sw2.toString("qreg") == "Swap qreg44,qreg45");
BOOST_VERIFY(sw2.bits().size() == 2);
BOOST_VERIFY(sw2.bits()[0] == 44);
BOOST_VERIFY(sw2.bits()[1] == 45);
BOOST_VERIFY(sw2.getName() == "Swap");
}
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
auto sw = GateInstructionRegistry::instance()->create("Swap", std::vector<int>{0, 1});
BOOST_VERIFY(sw->getName() == "Swap");
}
......@@ -42,6 +42,7 @@
#include "Rx.hpp"
#include "Ry.hpp"
#include "CPhase.hpp"
#include "Swap.hpp"
#include "Measure.hpp"
namespace xacc {
......@@ -63,6 +64,7 @@ class AllGateVisitor:
public InstructionVisitor<Y>,
public InstructionVisitor<Z>,
public InstructionVisitor<CPhase>,
public InstructionVisitor<Swap>,
public InstructionVisitor<Measure> {
};
......
......@@ -144,6 +144,10 @@ public:
baseGateInst(dynamic_cast<GateInstruction&>(cn));
}
void visit(Swap& s) {
baseGateInst(dynamic_cast<GateInstruction&>(s));
}
void visit(Rz& rz) {
baseGateInst(dynamic_cast<GateInstruction&>(rz), false);
writer->String("angle");
......
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