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

cleanup and adding docs


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 30c2ac71
/*******************************************************************************
* Copyright (c) 2021 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompanies this
* distribution. The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
* License is available at https://eclipse.org/org/documents/edl-v10.php
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
*******************************************************************************/
#include "IRBuilder.hpp"
#include "xacc_service.hpp"
#include <iostream>
......
/*******************************************************************************
* Copyright (c) 2021 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompanies this
* distribution. The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
* License is available at https://eclipse.org/org/documents/edl-v10.php
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
*******************************************************************************/
#pragma once
#include "IRProvider.hpp"
// Enumerate XACC Instruction Nodes
// -------- NOTE -------
// If you want your new XACC Instruction to be supported by
// the IRBuilder, then you have to add it here, categorized by 1,2 qubits
// ---------------------
#define XACC_SINGLE_QUBIT_NODE_LIST_WITH_CODE(code) \
code(h) code(mz) code(rz) code(rx) code(ry) code(x) code(y) code(z) code(s) \
code(t) code(tdg) code(u) code(u1) code(rphi) code(reset)
......@@ -14,10 +28,18 @@
code(crz) code(ch) code(xx) code(xy) code(rzz)
namespace xacc {
// Define QubitType to be our usual pair qreg_name, idx
using QubitType = std::pair<std::string, std::size_t>;
// The IRBuilder provides a high-level interface for the
// construction of quantum gate model XACC IR. It exposes a
// construction method for every gate instruction in XACC. Programmers
// can use these methods to construct the IR CompositeInstruction instance
// and call to_ir() to extract that CompositeInstruction.
class IRBuilder {
private:
// Map lowercase gate method names to their corresponding XACC Instruction names
std::map<std::string, std::string> method_name_to_xacc_name;
// Build instructions with the Provider
std::shared_ptr<xacc::IRProvider> provider;
......@@ -32,7 +54,7 @@ private:
// facilitates variadic params to vector conversion
std::vector<InstructionParameter> m_tmp_holder;
// Variadic to vector...
// Variadic to vector helper functions...
void internal_push_back() {}
void internal_push_back(InstructionParameter p) {
m_tmp_holder.push_back(p);
......@@ -50,11 +72,16 @@ private:
}
public:
// The default constructor
IRBuilder();
// The constructor, takes the name of the CompositeInstruction
IRBuilder(const std::string name);
// Return the CompositeInstruction
auto to_ir() { return program; }
// Generate at compile time all one-qubit instruction method names
// Generate at compile time all one-qubit instruction methods
// Takes qubit indices as input and potential parameters
#define FUNC_SIG_GEN(TYPENAME) void TYPENAME
#define DEFINE_VISIT_ONE_QUBIT(type) \
template <typename... Params> \
......@@ -69,6 +96,7 @@ public:
XACC_SINGLE_QUBIT_NODE_LIST_WITH_CODE(DEFINE_VISIT_ONE_QUBIT)
#undef DEFINE_VISIT_ONE_QUBIT
// Same, takes QubitType instead of indices
#define DEFINE_VISIT_ONE_QUBIT_TYPE(type) \
template <typename... Params> \
FUNC_SIG_GEN(type) \
......@@ -82,6 +110,8 @@ public:
XACC_SINGLE_QUBIT_NODE_LIST_WITH_CODE(DEFINE_VISIT_ONE_QUBIT_TYPE)
#undef DEFINE_VISIT_ONE_QUBIT_TYPE
// Generate at compile time all two-qubit instruction methods
// Takes qubit indices as input and potential parameters
#define DEFINE_VISIT_TWO_QUBIT(type) \
template <typename... Params> \
FUNC_SIG_GEN(type) \
......@@ -95,6 +125,7 @@ public:
XACC_TWO_QUBIT_NODE_LIST_WITH_CODE(DEFINE_VISIT_TWO_QUBIT)
#undef DEFINE_VISIT_TWO_QUBIT
// Same, takes QubitType instead of indices
#define DEFINE_VISIT_TWO_QUBIT_TYPE(type) \
template <typename... Params> \
FUNC_SIG_GEN(type) \
......
/*******************************************************************************
* Copyright (c) 2017 UT-Battelle, LLC.
* Copyright (c) 2021 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompanies this
......@@ -28,7 +28,6 @@ TEST(IRBuilderTester, checkSimple) {
builder.mz(1);
auto composite = builder.to_ir();
std::cout << composite->toString() << "\n";
EXPECT_EQ(9, composite->nInstructions());
}
......@@ -38,6 +37,7 @@ TEST(IRBuilderTester, checkSimple) {
builder.cnot(std::make_pair("qregQQ", 0), std::make_pair("qregQQ", 1));
auto composite = builder.to_ir();
std::cout << composite->toString() << "\n";
EXPECT_EQ(2, composite->nInstructions());
}
}
......
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