Commit 6c99d330 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

wrote first pass at c++ plugin development doc


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 5087e21f
Pipeline #79731 passed with stage
in 3 minutes and 53 seconds
Developers
==========
Here we describe how XACC developers can extend the framework
with new Compilers, Accelerators, Instructions, IR Transformations, etc.
This can be done from both C++ and Python.
Writing a Plugin in C++
-----------------------
Let's demonstrate how one might add a new IR Transformation
implementation to XACC. This is a simple case, but the overall structure
works across most plugins.
First, we create a new project folder ``test_ir_transformation`` and
populate it with a ``CMakeLists.txt`` file, and a ``src`` folder containing another
``CMakeLists.txt`` file as well as ``manifest.json``, ``test_ir_transformation.hpp``,
``test_ir_transformation.cpp``, and ``test_ir_transformation_activator.cpp``. You should have
the following directory structure
::
project
├── CMakeLists.txt
├── src
├── CMakeLists.txt
└── manifest.json
└── test_ir_transformation.hpp
└── test_ir_transformation.cpp
└── test_ir_transformation_activator.cpp
In the top-level ``CMakeLists.txt`` we add the following:
.. code:: bash
project(test_ir_transformation CXX)
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
find_package(XACC REQUIRED)
add_subdirectory(src)
Basically here we are defining a CMake project, setting the minimum version, locating our XACC install, and
adding the ``src`` directory to the build.
In the ``src/CMakeLists.txt`` file, we add the following
.. code:: bash
set(LIBRARY_NAME test-ir-transformation)
file(GLOB SRC *.cpp)
usfunctiongetresourcesource(TARGET
${LIBRARY_NAME}
OUT
SRC)
usfunctiongeneratebundleinit(TARGET
${LIBRARY_NAME}
OUT
SRC)
add_library(${LIBRARY_NAME} SHARED ${SRC})
target_link_libraries(${LIBRARY_NAME} PRIVATE xacc::xacc)
set(_bundle_name test_ir_transformation)
set_target_properties(${LIBRARY_NAME}
PROPERTIES COMPILE_DEFINITIONS
US_BUNDLE_NAME=${_bundle_name}
US_BUNDLE_NAME
${_bundle_name})
usfunctionembedresources(TARGET
${LIBRARY_NAME}
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
FILES
manifest.json)
xacc_configure_plugin_rpath(${LIBRARY_NAME})
install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
Here we define the library name, collect all source files, run some
CppMicroServices functions that append extra information to our library,
build the library and link in all required XACC libraries. Next we add
more information to this shared library from the ``manifest.json`` file,
configure the libraries RPATH, and install to the correct
``plugins`` folder in XACC. ``manifest.json`` should contain the following json
.. code:: bash
{
"bundle.symbolic_name" : "test_ir_transformation",
"bundle.activator" : true,
"bundle.name" : "Test IR Transformation",
"bundle.description" : ""
}
Next we provide the actual code for the test IR Transformation. In the ``test_ir_transformation.hpp``
we add the following
.. code:: cpp
#pragma once
#include "IRTransformation.hpp"
using namespace xacc;
namespace test {
class Test : public IRTransformation {
public:
Test() {}
void apply(std::shared_ptr<CompositeInstruction> program,
const std::shared_ptr<Accelerator> accelerator,
const HeterogeneousMap& options = {}) override;
const IRTransformationType type() const override {return IRTransformationType::Optimization;}
const std::string name() const override { return "test-irt"; }
const std::string description() const override { return ""; }
};
}
and in ``test_ir_transformation.cpp`` we implement ``apply``
.. code:: cpp
#include "test_ir_transformation.hpp"
namespace test {
void Test::apply(std::shared_ptr<CompositeInstruction> circuit,
const std::shared_ptr<Accelerator> accelerator,
const HeterogeneousMap &options) {
// do transformation on circuit here...
}
}
Finally, we add a ``BundleActivator`` that creates a ``shared_ptr`` to our
IR Transformation and registers it with the CppMicroServices framework.
.. code:: cpp
#include "test_ir_transformation.hpp"
#include "cppmicroservices/BundleActivator.h"
#include "cppmicroservices/BundleContext.h"
#include "cppmicroservices/ServiceProperties.h"
#include <memory>
using namespace cppmicroservices;
namespace {
class US_ABI_LOCAL TestIRTransformationActivator: public BundleActivator {
public:
TestIRTransformationActivator() {
}
void Start(BundleContext context) {
auto t = std::make_shared<test::Test>();
context.RegisterService<xacc::IRTransformation>(t);
}
void Stop(BundleContext /*context*/) {
}
};
}
CPPMICROSERVICES_EXPORT_BUNDLE_ACTIVATOR(TestIRTransformationActivator)
The majority of this is standard CppMicroservices boilerplate code. The crucial bit that
requires your attention when developing a new plugin is the implementation of ``Start``.
Here you create a ``shared_ptr`` to your instances and register it against the
correct XACC interface type, here ``IRTransformation``.
Now, all that is left to do is build your shared library, and install it for use
in the XACC framework
.. code:: bash
$ cd test_ir_transformation && mkdir build && cd build
$ cmake .. -DXACC_DIR=~/.xacc
$ make install
Writing a Plugin in Python
--------------------------
\ No newline at end of file
--------------------------
......@@ -638,9 +638,6 @@ or in Python
print(qbits['opt-params'])
RDM
+++
Accelerator Decorators
----------------------
ROErrorDecorator
......
......@@ -160,8 +160,162 @@
<div class="section" id="developers">
<h1>Developers<a class="headerlink" href="#developers" title="Permalink to this headline"></a></h1>
<p>Here we describe how XACC developers can extend the framework
with new Compilers, Accelerators, Instructions, IR Transformations, etc.
This can be done from both C++ and Python.</p>
<div class="section" id="writing-a-plugin-in-c">
<h2>Writing a Plugin in C++<a class="headerlink" href="#writing-a-plugin-in-c" title="Permalink to this headline"></a></h2>
<p>Let’s demonstrate how one might add a new IR Transformation
implementation to XACC. This is a simple case, but the overall structure
works across most plugins.</p>
<p>First, we create a new project folder <code class="docutils literal notranslate"><span class="pre">test_ir_transformation</span></code> and
populate it with a <code class="docutils literal notranslate"><span class="pre">CMakeLists.txt</span></code> file, and a <code class="docutils literal notranslate"><span class="pre">src</span></code> folder containing another
<code class="docutils literal notranslate"><span class="pre">CMakeLists.txt</span></code> file as well as <code class="docutils literal notranslate"><span class="pre">manifest.json</span></code>, <code class="docutils literal notranslate"><span class="pre">test_ir_transformation.hpp</span></code>,
<code class="docutils literal notranslate"><span class="pre">test_ir_transformation.cpp</span></code>, and <code class="docutils literal notranslate"><span class="pre">test_ir_transformation_activator.cpp</span></code>. You should have
the following directory structure</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>project
├── CMakeLists.txt
├── src
├── CMakeLists.txt
└── manifest.json
└── test_ir_transformation.hpp
└── test_ir_transformation.cpp
└── test_ir_transformation_activator.cpp
</pre></div>
</div>
<p>In the top-level <code class="docutils literal notranslate"><span class="pre">CMakeLists.txt</span></code> we add the following:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>project<span class="o">(</span>test_ir_transformation CXX<span class="o">)</span>
cmake_minimum_required<span class="o">(</span>VERSION <span class="m">3</span>.9 FATAL_ERROR<span class="o">)</span>
find_package<span class="o">(</span>XACC REQUIRED<span class="o">)</span>
add_subdirectory<span class="o">(</span>src<span class="o">)</span>
</pre></div>
</div>
<p>Basically here we are defining a CMake project, setting the minimum version, locating our XACC install, and
adding the <code class="docutils literal notranslate"><span class="pre">src</span></code> directory to the build.</p>
<p>In the <code class="docutils literal notranslate"><span class="pre">src/CMakeLists.txt</span></code> file, we add the following</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>set<span class="o">(</span>LIBRARY_NAME test-ir-transformation<span class="o">)</span>
file<span class="o">(</span>GLOB SRC *.cpp<span class="o">)</span>
usfunctiongetresourcesource<span class="o">(</span>TARGET
<span class="si">${</span><span class="nv">LIBRARY_NAME</span><span class="si">}</span>
OUT
SRC<span class="o">)</span>
usfunctiongeneratebundleinit<span class="o">(</span>TARGET
<span class="si">${</span><span class="nv">LIBRARY_NAME</span><span class="si">}</span>
OUT
SRC<span class="o">)</span>
add_library<span class="o">(</span><span class="si">${</span><span class="nv">LIBRARY_NAME</span><span class="si">}</span> SHARED <span class="si">${</span><span class="nv">SRC</span><span class="si">}</span><span class="o">)</span>
target_link_libraries<span class="o">(</span><span class="si">${</span><span class="nv">LIBRARY_NAME</span><span class="si">}</span> PRIVATE xacc::xacc<span class="o">)</span>
set<span class="o">(</span>_bundle_name test_ir_transformation<span class="o">)</span>
set_target_properties<span class="o">(</span><span class="si">${</span><span class="nv">LIBRARY_NAME</span><span class="si">}</span>
PROPERTIES COMPILE_DEFINITIONS
<span class="nv">US_BUNDLE_NAME</span><span class="o">=</span><span class="si">${</span><span class="nv">_bundle_name</span><span class="si">}</span>
US_BUNDLE_NAME
<span class="si">${</span><span class="nv">_bundle_name</span><span class="si">}</span><span class="o">)</span>
usfunctionembedresources<span class="o">(</span>TARGET
<span class="si">${</span><span class="nv">LIBRARY_NAME</span><span class="si">}</span>
WORKING_DIRECTORY
<span class="si">${</span><span class="nv">CMAKE_CURRENT_SOURCE_DIR</span><span class="si">}</span>
FILES
manifest.json<span class="o">)</span>
xacc_configure_plugin_rpath<span class="o">(</span><span class="si">${</span><span class="nv">LIBRARY_NAME</span><span class="si">}</span><span class="o">)</span>
install<span class="o">(</span>TARGETS <span class="si">${</span><span class="nv">LIBRARY_NAME</span><span class="si">}</span> DESTINATION <span class="si">${</span><span class="nv">CMAKE_INSTALL_PREFIX</span><span class="si">}</span>/plugins<span class="o">)</span>
</pre></div>
</div>
<p>Here we define the library name, collect all source files, run some
CppMicroServices functions that append extra information to our library,
build the library and link in all required XACC libraries. Next we add
more information to this shared library from the <code class="docutils literal notranslate"><span class="pre">manifest.json</span></code> file,
configure the libraries RPATH, and install to the correct
<code class="docutils literal notranslate"><span class="pre">plugins</span></code> folder in XACC. <code class="docutils literal notranslate"><span class="pre">manifest.json</span></code> should contain the following json</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">{</span>
<span class="s2">&quot;bundle.symbolic_name&quot;</span> : <span class="s2">&quot;test_ir_transformation&quot;</span>,
<span class="s2">&quot;bundle.activator&quot;</span> : true,
<span class="s2">&quot;bundle.name&quot;</span> : <span class="s2">&quot;Test IR Transformation&quot;</span>,
<span class="s2">&quot;bundle.description&quot;</span> : <span class="s2">&quot;&quot;</span>
<span class="o">}</span>
</pre></div>
</div>
<p>Next we provide the actual code for the test IR Transformation. In the <code class="docutils literal notranslate"><span class="pre">test_ir_transformation.hpp</span></code>
we add the following</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="cp">#pragma once</span>
<span class="cp">#include</span> <span class="cpf">&quot;IRTransformation.hpp&quot;</span><span class="cp"></span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">xacc</span><span class="p">;</span>
<span class="k">namespace</span> <span class="n">test</span> <span class="p">{</span>
<span class="k">class</span> <span class="nc">Test</span> <span class="o">:</span> <span class="k">public</span> <span class="n">IRTransformation</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
<span class="n">Test</span><span class="p">()</span> <span class="p">{}</span>
<span class="kt">void</span> <span class="n">apply</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">CompositeInstruction</span><span class="o">&gt;</span> <span class="n">program</span><span class="p">,</span>
<span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Accelerator</span><span class="o">&gt;</span> <span class="n">accelerator</span><span class="p">,</span>
<span class="k">const</span> <span class="n">HeterogeneousMap</span><span class="o">&amp;</span> <span class="n">options</span> <span class="o">=</span> <span class="p">{})</span> <span class="k">override</span><span class="p">;</span>
<span class="k">const</span> <span class="n">IRTransformationType</span> <span class="nf">type</span><span class="p">()</span> <span class="k">const</span> <span class="k">override</span> <span class="p">{</span><span class="k">return</span> <span class="n">IRTransformationType</span><span class="o">::</span><span class="n">Optimization</span><span class="p">;}</span>
<span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">name</span><span class="p">()</span> <span class="k">const</span> <span class="k">override</span> <span class="p">{</span> <span class="k">return</span> <span class="s">&quot;test-irt&quot;</span><span class="p">;</span> <span class="p">}</span>
<span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">description</span><span class="p">()</span> <span class="k">const</span> <span class="k">override</span> <span class="p">{</span> <span class="k">return</span> <span class="s">&quot;&quot;</span><span class="p">;</span> <span class="p">}</span>
<span class="p">};</span>
<span class="p">}</span>
</pre></div>
</div>
<p>and in <code class="docutils literal notranslate"><span class="pre">test_ir_transformation.cpp</span></code> we implement <code class="docutils literal notranslate"><span class="pre">apply</span></code></p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&quot;test_ir_transformation.hpp&quot;</span><span class="cp"></span>
<span class="k">namespace</span> <span class="n">test</span> <span class="p">{</span>
<span class="kt">void</span> <span class="n">Test</span><span class="o">::</span><span class="n">apply</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">CompositeInstruction</span><span class="o">&gt;</span> <span class="n">circuit</span><span class="p">,</span>
<span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Accelerator</span><span class="o">&gt;</span> <span class="n">accelerator</span><span class="p">,</span>
<span class="k">const</span> <span class="n">HeterogeneousMap</span> <span class="o">&amp;</span><span class="n">options</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// do transformation on circuit here...</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Finally, we add a <code class="docutils literal notranslate"><span class="pre">BundleActivator</span></code> that creates a <code class="docutils literal notranslate"><span class="pre">shared_ptr</span></code> to our
IR Transformation and registers it with the CppMicroServices framework.</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&quot;test_ir_transformation.hpp&quot;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&quot;cppmicroservices/BundleActivator.h&quot;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&quot;cppmicroservices/BundleContext.h&quot;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&quot;cppmicroservices/ServiceProperties.h&quot;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;memory&gt;</span><span class="cp"></span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">cppmicroservices</span><span class="p">;</span>
<span class="k">namespace</span> <span class="p">{</span>
<span class="k">class</span> <span class="nc">US_ABI_LOCAL</span> <span class="nl">TestIRTransformationActivator</span><span class="p">:</span> <span class="k">public</span> <span class="n">BundleActivator</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
<span class="n">TestIRTransformationActivator</span><span class="p">()</span> <span class="p">{</span>
<span class="p">}</span>
<span class="kt">void</span> <span class="n">Start</span><span class="p">(</span><span class="n">BundleContext</span> <span class="n">context</span><span class="p">)</span> <span class="p">{</span>
<span class="k">auto</span> <span class="n">t</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">make_shared</span><span class="o">&lt;</span><span class="n">test</span><span class="o">::</span><span class="n">Test</span><span class="o">&gt;</span><span class="p">();</span>
<span class="n">context</span><span class="p">.</span><span class="n">RegisterService</span><span class="o">&lt;</span><span class="n">xacc</span><span class="o">::</span><span class="n">IRTransformation</span><span class="o">&gt;</span><span class="p">(</span><span class="n">t</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span> <span class="n">Stop</span><span class="p">(</span><span class="n">BundleContext</span> <span class="cm">/*context*/</span><span class="p">)</span> <span class="p">{</span>
<span class="p">}</span>
<span class="p">};</span>
<span class="p">}</span>
<span class="n">CPPMICROSERVICES_EXPORT_BUNDLE_ACTIVATOR</span><span class="p">(</span><span class="n">TestIRTransformationActivator</span><span class="p">)</span>
</pre></div>
</div>
<p>The majority of this is standard CppMicroservices boilerplate code. The crucial bit that
requires your attention when developing a new plugin is the implementation of <code class="docutils literal notranslate"><span class="pre">Start</span></code>.
Here you create a <code class="docutils literal notranslate"><span class="pre">shared_ptr</span></code> to your instances and register it against the
correct XACC interface type, here <code class="docutils literal notranslate"><span class="pre">IRTransformation</span></code>.</p>
<p>Now, all that is left to do is build your shared library, and install it for use
in the XACC framework</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ <span class="nb">cd</span> test_ir_transformation <span class="o">&amp;&amp;</span> mkdir build <span class="o">&amp;&amp;</span> <span class="nb">cd</span> build
$ cmake .. -DXACC_DIR<span class="o">=</span>~/.xacc
$ make install
</pre></div>
</div>
</div>
<div class="section" id="writing-a-plugin-in-python">
<h2>Writing a Plugin in Python<a class="headerlink" href="#writing-a-plugin-in-python" title="Permalink to this headline"></a></h2>
......
......@@ -106,7 +106,6 @@
<li class="toctree-l2"><a class="reference internal" href="#algorithms">Algorithms</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#vqe">VQE</a></li>
<li class="toctree-l3"><a class="reference internal" href="#ddcl">DDCL</a></li>
<li class="toctree-l3"><a class="reference internal" href="#rdm">RDM</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#accelerator-decorators">Accelerator Decorators</a><ul>
......@@ -1023,9 +1022,6 @@ strategies will shift each parameter by plus or minus pi over 2.</p>
</pre></div>
</div>
</div>
<div class="section" id="rdm">
<h3>RDM<a class="headerlink" href="#rdm" title="Permalink to this headline"></a></h3>
</div>
</div>
<div class="section" id="accelerator-decorators">
<h2>Accelerator Decorators<a class="headerlink" href="#accelerator-decorators" title="Permalink to this headline"></a></h2>
......
......@@ -251,7 +251,6 @@ You can ask questions by creating a new issue with the question tag.</p>
<li class="toctree-l2"><a class="reference internal" href="extensions.html#algorithms">Algorithms</a><ul>
<li class="toctree-l3"><a class="reference internal" href="extensions.html#vqe">VQE</a></li>
<li class="toctree-l3"><a class="reference internal" href="extensions.html#ddcl">DDCL</a></li>
<li class="toctree-l3"><a class="reference internal" href="extensions.html#rdm">RDM</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="extensions.html#accelerator-decorators">Accelerator Decorators</a><ul>
......
This diff is collapsed.
Developers
==========
Here we describe how XACC developers can extend the framework
with new Compilers, Accelerators, Instructions, IR Transformations, etc.
This can be done from both C++ and Python.
Writing a Plugin in C++
-----------------------
Let's demonstrate how one might add a new IR Transformation
implementation to XACC. This is a simple case, but the overall structure
works across most plugins.
First, we create a new project folder ``test_ir_transformation`` and
populate it with a ``CMakeLists.txt`` file, and a ``src`` folder containing another
``CMakeLists.txt`` file as well as ``manifest.json``, ``test_ir_transformation.hpp``,
``test_ir_transformation.cpp``, and ``test_ir_transformation_activator.cpp``. You should have
the following directory structure
::
project
├── CMakeLists.txt
├── src
├── CMakeLists.txt
└── manifest.json
└── test_ir_transformation.hpp
└── test_ir_transformation.cpp
└── test_ir_transformation_activator.cpp
In the top-level ``CMakeLists.txt`` we add the following:
.. code:: bash
project(test_ir_transformation CXX)
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
find_package(XACC REQUIRED)
add_subdirectory(src)
Basically here we are defining a CMake project, setting the minimum version, locating our XACC install, and
adding the ``src`` directory to the build.
In the ``src/CMakeLists.txt`` file, we add the following
.. code:: bash
set(LIBRARY_NAME test-ir-transformation)
file(GLOB SRC *.cpp)
usfunctiongetresourcesource(TARGET
${LIBRARY_NAME}
OUT
SRC)
usfunctiongeneratebundleinit(TARGET
${LIBRARY_NAME}
OUT
SRC)
add_library(${LIBRARY_NAME} SHARED ${SRC})
target_link_libraries(${LIBRARY_NAME} PRIVATE xacc::xacc)
set(_bundle_name test_ir_transformation)
set_target_properties(${LIBRARY_NAME}
PROPERTIES COMPILE_DEFINITIONS
US_BUNDLE_NAME=${_bundle_name}
US_BUNDLE_NAME
${_bundle_name})
usfunctionembedresources(TARGET
${LIBRARY_NAME}
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
FILES
manifest.json)
xacc_configure_plugin_rpath(${LIBRARY_NAME})
install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
Here we define the library name, collect all source files, run some
CppMicroServices functions that append extra information to our library,
build the library and link in all required XACC libraries. Next we add
more information to this shared library from the ``manifest.json`` file,
configure the libraries RPATH, and install to the correct
``plugins`` folder in XACC. ``manifest.json`` should contain the following json
.. code:: bash
{
"bundle.symbolic_name" : "test_ir_transformation",
"bundle.activator" : true,
"bundle.name" : "Test IR Transformation",
"bundle.description" : ""
}
Next we provide the actual code for the test IR Transformation. In the ``test_ir_transformation.hpp``
we add the following
.. code:: cpp
#pragma once
#include "IRTransformation.hpp"
using namespace xacc;
namespace test {
class Test : public IRTransformation {
public:
Test() {}
void apply(std::shared_ptr<CompositeInstruction> program,
const std::shared_ptr<Accelerator> accelerator,
const HeterogeneousMap& options = {}) override;
const IRTransformationType type() const override {return IRTransformationType::Optimization;}
const std::string name() const override { return "test-irt"; }
const std::string description() const override { return ""; }
};
}
and in ``test_ir_transformation.cpp`` we implement ``apply``
.. code:: cpp
#include "test_ir_transformation.hpp"
namespace test {
void Test::apply(std::shared_ptr<CompositeInstruction> circuit,
const std::shared_ptr<Accelerator> accelerator,
const HeterogeneousMap &options) {
// do transformation on circuit here...
}
}
Finally, we add a ``BundleActivator`` that creates a ``shared_ptr`` to our
IR Transformation and registers it with the CppMicroServices framework.
.. code:: cpp
#include "test_ir_transformation.hpp"
#include "cppmicroservices/BundleActivator.h"
#include "cppmicroservices/BundleContext.h"
#include "cppmicroservices/ServiceProperties.h"
#include <memory>
using namespace cppmicroservices;
namespace {
class US_ABI_LOCAL TestIRTransformationActivator: public BundleActivator {
public:
TestIRTransformationActivator() {
}
void Start(BundleContext context) {
auto t = std::make_shared<test::Test>();
context.RegisterService<xacc::IRTransformation>(t);
}
void Stop(BundleContext /*context*/) {
}
};
}
CPPMICROSERVICES_EXPORT_BUNDLE_ACTIVATOR(TestIRTransformationActivator)
The majority of this is standard CppMicroservices boilerplate code. The crucial bit that
requires your attention when developing a new plugin is the implementation of ``Start``.
Here you create a ``shared_ptr`` to your instances and register it against the
correct XACC interface type, here ``IRTransformation``.
Now, all that is left to do is build your shared library, and install it for use
in the XACC framework
.. code:: bash
$ cd test_ir_transformation && mkdir build && cd build
$ cmake .. -DXACC_DIR=~/.xacc
$ make install
Writing a Plugin in Python
--------------------------
\ No newline at end of file
--------------------------
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