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

adding lexer error listener for pyxacc compiler, adding InstructionIterator to...


adding lexer error listener for pyxacc compiler, adding InstructionIterator to python api, cleaning up pyxacc.py qpu decorator, updating docs

Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 772c4634
......@@ -10,24 +10,30 @@ To install this plugin, run the following
.. code::
$ xacc-install-plugins.py -p xacc-dwave
$ git clone https://github.com/ornl-qci/xacc-dwave
$ cd xacc-dwave && mkdir build && cd build
$ cmake .. -DXACC_DIR=$HOME/.xacc
You have now installed the D-Wave plugin. It is located in ``$XACC_ROOT/lib/plugins/accelerator`` and ``$XACC_ROOT/lib/plugins/compilers``, where XACC_ROOT is your XACC install prefix. Depending on your installation location, administrative priviledges may be required, e.g., sudo.
Alternatively, if you installed the XACC Python bindings, then you can run
Extensibility for Minor Graph Embedding Algorithms
---------------------------------------------------
XACC has extensibility built in for minor graph embedding
algorithms. We currently have one supported embedding algorithm (with more coming online), a wrapper around the D-Wave SAPI Cai, Macready, Roi algorithm. In order to install this as a plugin, run the following
.. code::
.. note::
$ cmake .. -DXACC_DIR=$(python -m pyxacc -L)
The following embedding algorithm needs to leverage the proprietary
D-Wave SAPI header file and associated shared library: ``dwave_sapi.h`` and ``libdwave_sapi.so`` (or ``libdwave_sapi.dylib`` on macosx).
In order for the installation below to work, place dwave_sapi.h in ``/usr/local/include/`` and ``libdwave_sapi.so`` or ``libdwave_sapi.dylib`` in ``/usr/local/lib/``
ensuring that the ``pyxacc`` module is in your ``PYTHONPATH``.
.. code::
You have now installed the D-Wave plugin. It is located in ``$XACC_ROOT/plugins``,
where ``XACC_ROOT`` is your XACC install prefix.
Setting Credentials
-------------------
$ xacc-install-plugins.py -p xacc-dwsapi-embedding
In order to target the D-Wave Qubist remote resources you must provide
XACC with your API key. By default
XACC will search for a config file at ``$HOME/.dwave_config``.
You have now installed the D-Wave plugin. It is located in ``$XACC_ROOT/lib/plugins/embedding``, where ``XACC_ROOT`` is your XACC install prefix.
If you installed the XACC Python bindings, then you can run
.. code::
$ python -m pyxacc -c dwave -k YOURAPIKEY
......@@ -18,10 +18,12 @@ The following software is required before building XACC:
| CMake | Required | 3.2+ |
+------------------------+------------+-----------+
Note that you must have a C++11 compliant compiler.
For GCC, this means version 4.8.1+, for Clang, this means 3.3+.
Note that you must have a C++11 compliant compiler. If you prefer ``gcc/g++``,
it is recommended that you use at least version 5.0 and up. Using ``[4.8,5)`` will
cause XACC to skip building and installing Antlr, which is required for most XACC
Compiler extensions.
These dependencies are relatively easy to install on various operating
The other dependencies are relatively easy to install on various operating
systems. Any of the following commands will work for Mac, Fedora/RedHat/CentOS, or Ubuntu:
.. code::
......@@ -37,9 +39,12 @@ Clone the XACC repository:
.. code::
$ git clone --recursive https://github.com/ornl-qci/xacc
$ git clone --recursive https://github.com/eclipse/xacc
XACC requires CMake 3.2+ to build. Run the following to
Note you must pass the ``--recursive`` flag. If you don't you must run
``git submodule update --init --recursive``.
XACC requires CMake 3.2+. Run the following to
configure and build XACC:
.. code:: bash
......
......@@ -5,48 +5,32 @@ Example Usage
--------------
Suppose you had the Rigetti plugin installed `see here <rigetti.rst>`_, and
you wanted to program a simple teleportation code and launch it on the Rigetti QVM
you wanted to program a simple teleportation code and launch it on the Rigetti QVM.
To program this with the XACC Python bindings you could write the following python script:
.. code::
import pyxacc as xacc
from pyxacc import qpu
# Create the source code
src = """__qpu__ test(AcceleratorBuffer b) {
X 0
H 1
CNOT 1 2
CNOT 0 1
CNOT 1 2
CNOT 2 0
MEASURE 2 [0]
}
"""
# Initialize the framework
xacc.Initialize()
# Get reference to the Rigetti QVM
qpu = xacc.getAccelerator('rigetti')
# Create some qubits to operate on
qreg = qpu.createBuffer('qreg',3)
# Create the program and build it
p = xacc.Program(qpu, src)
p.build()
# Get the teleport kernel
k = p.getKernel('teleport')
# Execute on qreg with no InstructionParameters
k.execute(qreg, [])
# Create the source code
@qpu(accelerator='ibm')
def teleport():
X(0)
H(1)
CNOT(1,2)
CNOT(0,1)
CNOT(1,2)
CNOT(2,0)
Measure(2, 0)
return
results = teleport()
# Display the results
print(qreg.getMeasurementStrings())
print(qreg.getExpectationValueZ())
print(results.getMeasurementStrings())
# Finalize the framework
xacc.Finalize()
xacc.Finalize()
\ No newline at end of file
......@@ -28,6 +28,9 @@ std::shared_ptr<IR> PyXACCCompiler::compile(const std::string& src,
// Setup the Antlr Parser
ANTLRInputStream input(src);
PyXACCIRLexer lexer(&input);
lexer.removeErrorListeners();
lexer.addErrorListener(new PyXACCErrorListener());
CommonTokenStream tokens(&lexer);
PyXACCIRParser parser(&tokens);
parser.removeErrorListeners();
......
......@@ -31,7 +31,7 @@ class PyXACCErrorListener : public antlr4::BaseErrorListener {
xacc::error(output.str());
}
};
/**
* The PyXACCCompiler is an XACC Compiler that compiles
* python-like gate instruction source code to produce a
......
......@@ -91,9 +91,18 @@ class qpu(object):
compiler = getCompiler('xacc-py')
qpu = getAccelerator(self.kwargs['accelerator'])
ir = compiler.compile(src, qpu)
buf = qpu.createBuffer('q')
program = Program(qpu, ir)
kernel = program.getKernels()[0]
nBits = 0
it = InstructionIterator(kernel)
while(kernel.hasNext()):
i = it.next()
if i.isEnabled() and not i.isComposite():
for b in i.bits():
if b > nBits:
nBits = b
nBits = nBits+1
buf = qpu.createBuffer('q',nBits)
kernel.execute(buf, list(args))
return buf
return wrapped_f
......
......@@ -60,19 +60,21 @@ PYBIND11_MODULE(_pyxacc, m) {
.def(py::init<float>(), "Construct as a float.");
py::class_<xacc::Instruction, std::shared_ptr<xacc::Instruction>>(m, "Instruction", "")
.def("nParameters", &xacc::Instruction::nParameters, "")
.def("toString", &xacc::Instruction::toString, "")
.def("bits", &xacc::Instruction::bits, "")
.def("getParameter", &xacc::Instruction::getParameter, "")
.def("getParameters", &xacc::Instruction::getParameters, "")
.def("setParameter", &xacc::Instruction::setParameter, "")
.def("mapBits", &xacc::Instruction::mapBits, "")
.def("getTag", &xacc::Instruction::getTag, "")
.def("name", &xacc::Instruction::name, "")
.def("nParameters", &xacc::Instruction::nParameters, "")
.def("toString", &xacc::Instruction::toString, "")
.def("isEnabled", &xacc::Instruction::isEnabled, "")
.def("isComposite", &xacc::Instruction::isComposite, "")
.def("bits", &xacc::Instruction::bits, "")
.def("getParameter", &xacc::Instruction::getParameter, "")
.def("getParameters", &xacc::Instruction::getParameters, "")
.def("setParameter", &xacc::Instruction::setParameter, "")
.def("mapBits", &xacc::Instruction::mapBits, "")
.def("getTag", &xacc::Instruction::getTag, "")
.def("name", &xacc::Instruction::name, "")
.def("description", &xacc::Instruction::description, "");
py::class_<xacc::Function, std::shared_ptr<xacc::Function>>(m, "Function", "")
.def("nInstructions", &xacc::Function::nInstructions, "")
.def("nInstructions", &xacc::Function::nInstructions, "")
.def("getInstruction", &xacc::Function::getInstruction, "")
.def("getInstructions", &xacc::Function::getInstructions, "")
.def("removeInstruction", &xacc::Function::removeInstruction, "")
......@@ -94,11 +96,18 @@ PYBIND11_MODULE(_pyxacc, m) {
// Expose the IR interface
py::class_<xacc::IR, std::shared_ptr<xacc::IR>> (m, "IR", "The XACC Intermediate Representation, "
"serves as a container for XACC Functions.")
.def("getKernels", &xacc::IR::getKernels, "Return the kernels in this IR")
.def("addKernel", &xacc::IR::addKernel, "");
.def("getKernels", &xacc::IR::getKernels, "Return the kernels in this IR")
.def("addKernel", &xacc::IR::addKernel, "");
py::class_<xacc::IRPreprocessor, std::shared_ptr<xacc::IRPreprocessor>> (m, "IRPreprocesor", "").def("process", &xacc::IRPreprocessor::process, "");
py::class_<xacc::AcceleratorBufferPostprocessor, std::shared_ptr<xacc::AcceleratorBufferPostprocessor>> (m, "AcceleratorBufferPostprocessor", "").def("process", &xacc::AcceleratorBufferPostprocessor::process, "");
py::class_<xacc::InstructionIterator>(m, "InstructionIterator", "")
.def(py::init<std::shared_ptr<xacc::Function>>())
.def("hasNext", &xacc::InstructionIterator::hasNext, "")
.def("next", &xacc::InstructionIterator::next, "");
py::class_<xacc::IRPreprocessor, std::shared_ptr<xacc::IRPreprocessor>> (m, "IRPreprocesor", "")
.def("process", &xacc::IRPreprocessor::process, "");
py::class_<xacc::AcceleratorBufferPostprocessor, std::shared_ptr<xacc::AcceleratorBufferPostprocessor>> (m, "AcceleratorBufferPostprocessor", "")
.def("process", &xacc::AcceleratorBufferPostprocessor::process, "");
py::class_<xacc::IRGenerator, std::shared_ptr<xacc::IRGenerator>>(m, "IRGenerator", "")
.def("generate", (std::shared_ptr<xacc::Function> (xacc::IRGenerator::*)(
......
Supports Markdown
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