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

Merge pull request #482 from tnguyen-ornl/tnguyen/qaoa-placement

Add placement option to QAOA
parents ca57febd 2021f2e0
Pipeline #160996 passed with stage
in 37 minutes and 4 seconds
......@@ -20,7 +20,7 @@
#include "xacc_observable.hpp"
#include "CompositeInstruction.hpp"
#include "AlgorithmGradientStrategy.hpp"
#include "IRTransformation.hpp"
#include <cassert>
#include <iomanip>
......@@ -70,8 +70,21 @@ bool QAOA::initialize(const HeterogeneousMap &parameters) {
if (parameters.pointerLikeExists<Observable>("ref-ham")) {
m_refHamObs = parameters.getPointerLike<Observable>("ref-ham");
}
m_irTransformation = nullptr;
// This QPU has topology-constraint
if (!m_qpu->getConnectivity().empty()) {
if (parameters.pointerLikeExists<xacc::IRTransformation>("placement")) {
m_irTransformation = xacc::as_shared_ptr(
parameters.getPointerLike<xacc::IRTransformation>("placement"));
if (m_irTransformation->type() !=
xacc::IRTransformationType::Placement) {
xacc::error(m_irTransformation->name() +
" is not a placement service.");
}
}
}
}
// Check if an initial composite instruction set has been provided
if (parameters.pointerLikeExists<CompositeInstruction>("initial-state")) {
m_initial_state = parameters.getPointerLike<CompositeInstruction>("initial-state");
......@@ -175,6 +188,12 @@ void QAOA::execute(const std::shared_ptr<AcceleratorBuffer> buffer) const {
auto tmpBuffer = xacc::qalloc(buffer->size());
std::vector<std::shared_ptr<CompositeInstruction>> fsToExec{
kernels[0]->operator()(x)};
if (m_irTransformation) {
for (auto &composite : fsToExec) {
m_irTransformation->apply(
composite, xacc::as_shared_ptr<xacc::Accelerator>(m_qpu));
}
}
m_qpu->execute(tmpBuffer, fsToExec);
double energy = m_costHamObs->postProcess(tmpBuffer);
// We will only have one child buffer for each parameter set.
......@@ -261,6 +280,12 @@ void QAOA::execute(const std::shared_ptr<AcceleratorBuffer> buffer) const {
}
auto tmpBuffer = xacc::qalloc(buffer->size());
if (m_irTransformation) {
for (auto &composite : fsToExec) {
m_irTransformation->apply(
composite, xacc::as_shared_ptr<xacc::Accelerator>(m_qpu));
}
}
m_qpu->execute(tmpBuffer, fsToExec);
auto buffers = tmpBuffer->getChildren();
......@@ -388,6 +413,12 @@ QAOA::execute(const std::shared_ptr<AcceleratorBuffer> buffer,
// just execute the single observed kernel:
std::vector<std::shared_ptr<CompositeInstruction>> fsToExec{
kernels[0]->operator()(x)};
if (m_irTransformation) {
for (auto &composite : fsToExec) {
m_irTransformation->apply(
composite, xacc::as_shared_ptr<xacc::Accelerator>(m_qpu));
}
}
m_qpu->execute(buffer, fsToExec);
const double finalCost = m_costHamObs->postProcess(buffer);
// std::cout << "Compute energy from grouping: " << finalCost << "\n";
......@@ -420,6 +451,12 @@ QAOA::execute(const std::shared_ptr<AcceleratorBuffer> buffer,
}
auto tmpBuffer = xacc::qalloc(buffer->size());
if (m_irTransformation) {
for (auto &composite : fsToExec) {
m_irTransformation->apply(composite,
xacc::as_shared_ptr<xacc::Accelerator>(m_qpu));
}
}
m_qpu->execute(tmpBuffer, fsToExec);
auto buffers = tmpBuffer->getChildren();
......
......@@ -18,6 +18,8 @@
#include "AlgorithmGradientStrategy.hpp"
namespace xacc {
class IRTransformation;
namespace algorithm {
class QAOA : public Algorithm
{
......@@ -42,6 +44,7 @@ private:
bool m_maximize = false;
CompositeInstruction* m_initial_state = nullptr;
bool m_shuffleTerms = false;
std::shared_ptr<xacc::IRTransformation> m_irTransformation;
};
} // namespace algorithm
} // namespace xacc
......@@ -20,7 +20,7 @@
#include "xacc_observable.hpp"
#include "CompositeInstruction.hpp"
#include "AlgorithmGradientStrategy.hpp"
#include "IRTransformation.hpp"
#include <cassert>
#include <iomanip>
#include <random>
......@@ -241,6 +241,21 @@ namespace algorithm {
if (parameters.pointerLikeExists<Observable>("ref-ham")) {
m_refHamObs = parameters.getPointerLike<Observable>("ref-ham");
}
m_irTransformation = nullptr;
// This QPU has topology-constraint
if (!m_qpu->getConnectivity().empty()) {
if (parameters.pointerLikeExists<xacc::IRTransformation>(
"placement")) {
m_irTransformation = xacc::as_shared_ptr(
parameters.getPointerLike<xacc::IRTransformation>(
"placement"));
if (m_irTransformation->type() !=
xacc::IRTransformationType::Placement) {
xacc::error(m_irTransformation->name() +
" is not a placement service.");
}
}
}
}
// Check if a parameter initialization routine has been specified
......@@ -285,6 +300,9 @@ namespace algorithm {
} else if (m_initial_state){
m.insert("initial-state", m_initial_state);
}
if (m_irTransformation) {
m.insert("placement", m_irTransformation);
}
// Initialize QAOA
auto qaoa = xacc::getAlgorithm("QAOA", m);
// Allocate some qubits and execute
......@@ -312,6 +330,10 @@ namespace algorithm {
m.insert("initial-state", m_initial_state);
}
if (m_irTransformation) {
m.insert("placement", m_irTransformation);
}
// Initialize QAOA
auto qaoa = xacc::getAlgorithm("QAOA", m);
// Allocate some qubits and execute
......
......@@ -19,6 +19,7 @@
#include "AlgorithmGradientStrategy.hpp"
namespace xacc {
class IRTransformation;
namespace algorithm {
class maxcut_qaoa : public Algorithm
{
......@@ -47,6 +48,7 @@ private:
bool m_maximize = false;
CompositeInstruction* m_initial_state = nullptr;
bool m_shuffleTerms = false;
std::shared_ptr<xacc::IRTransformation> m_irTransformation;
};
} // namespace algorithm
} // namespace xacc
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