Commit 17c30f48 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Merge branch 'master' of https://github.com/eclipse/xacc

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