Skip to content

Commit

Permalink
Merge pull request #52 from danclaudino/pauli_parser
Browse files Browse the repository at this point in the history
Moved pauli operator parsing to regex
  • Loading branch information
danclaudino authored Oct 3, 2024
2 parents 702d8ec + 5a60e55 commit 97c1dff
Show file tree
Hide file tree
Showing 21 changed files with 280 additions and 1,846 deletions.
10 changes: 3 additions & 7 deletions quantum/observable/pauli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
set (PACKAGE_NAME "PauliOperator Parser")
set (LIBRARY_NAME xacc-pauli)

file (GLOB_RECURSE HEADERS *.hpp generated/*.hpp)
file (GLOB SRC *.cpp generated/*.cpp)
file (GLOB_RECURSE HEADERS *.hpp)
file (GLOB SRC *.cpp)

# Set up dependencies to resources to track changes
usFunctionGetResourceSource(TARGET ${LIBRARY_NAME} OUT SRC)
Expand All @@ -25,16 +25,12 @@ usFunctionGenerateBundleInit(TARGET ${LIBRARY_NAME} OUT SRC)
add_library(${LIBRARY_NAME} SHARED ${SRC})
target_include_directories(${LIBRARY_NAME}
PUBLIC
. ${CMAKE_SOURCE_DIR}/tpls/antlr/runtime/src
.
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/generated
${CMAKE_SOURCE_DIR}/tpls/taocpp
${EIGEN_INCLUDE_DIR}
${CMAKE_BINARY_DIR})

add_dependencies(${LIBRARY_NAME} antlr4_shared)


set(_bundle_name xacc_pauli)
set_target_properties(${LIBRARY_NAME}
PROPERTIES # This is required for every bundle
Expand Down
61 changes: 0 additions & 61 deletions quantum/observable/pauli/PauliListenerImpl.cpp

This file was deleted.

54 changes: 0 additions & 54 deletions quantum/observable/pauli/PauliListenerImpl.hpp

This file was deleted.

121 changes: 102 additions & 19 deletions quantum/observable/pauli/PauliOperator.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 UT-Battelle, LLC.
* Copyright (c) 2024 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
Expand All @@ -9,17 +9,20 @@
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
* Daniel Claudino - measurement basis rotations, regex parser
*******************************************************************************/
#include "PauliOperator.hpp"
#include "CompositeInstruction.hpp"
#include "IRProvider.hpp"
#include "xacc.hpp"
#include "xacc_service.hpp"
#include <Eigen/Core>
#include "PauliOperatorLexer.h"
#include "PauliListenerImpl.hpp"
#include <Eigen/Sparse>
#include <complex>
#include <unsupported/Eigen/KroneckerProduct>
#include <regex>
#include <iomanip>
#include "PauliOperatorParser.hpp"

namespace xacc {
namespace quantum {
Expand Down Expand Up @@ -555,28 +558,108 @@ const std::string PauliOperator::toString() {
}

void PauliOperator::fromString(const std::string str) {
using namespace antlr4;
using namespace pauli;

ANTLRInputStream input(str);
PauliOperatorLexer lexer(&input);
lexer.removeErrorListeners();
lexer.addErrorListener(new PauliOperatorErrorListener());
std::smatch match;
std::string::const_iterator searchStart(str.cbegin());
std::complex<double> coefficient;
bool minus = 0;

CommonTokenStream tokens(&lexer);
PauliOperatorParser parser(&tokens);
parser.removeErrorListeners();
parser.addErrorListener(new PauliOperatorErrorListener());
PauliOperatorParser parser;
while (std::regex_search(searchStart, str.cend(), match, parser.matchPattern)) {

// Walk the Abstract Syntax Tree
tree::ParseTree *tree = parser.pauliSrc();
std::string pauliString;
std::map<int, std::string> pauliMap;

PauliListenerImpl listener;
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
PauliOperatorParser::MatchType type = parser.getMatchType(match);
switch (type) {
case PauliOperatorParser::MatchType::SIGN: {
std::string sign(match[1].str());
PauliOperatorParser::removeWhiteSpaces(sign);
minus = (sign == "-");
searchStart = match.suffix().first;
continue;
}

case PauliOperatorParser::MatchType::COMPLEX_AND_P:
coefficient = std::complex<double>{std::stod(match[2].str()), std::stod(match[3].str())};
if (minus) coefficient *= -1.0;
pauliString = match[4].str();
PauliOperatorParser::removeWhiteSpaces(pauliString);
parser.parsePauliString(pauliString, pauliMap);
break;

case PauliOperatorParser::MatchType::REAL_AND_P:
coefficient = std::stod(match[5].str());
if (minus) coefficient *= -1.0;
pauliString = match[6].str();
PauliOperatorParser::removeWhiteSpaces(pauliString);
parser.parsePauliString(pauliString, pauliMap);
break;

case PauliOperatorParser::MatchType::P_ONLY:
coefficient = minus ? -1.0 : 1.0;
pauliString = match[7].str();
PauliOperatorParser::removeWhiteSpaces(pauliString);
parser.parsePauliString(pauliString, pauliMap);
break;

case PauliOperatorParser::MatchType::IDENTITY_ONLY:
coefficient = minus ? -1.0 : 1.0;
break;

case PauliOperatorParser::MatchType::REAL_AND_I:
coefficient = std::stod(match[9].str());
if (minus) coefficient *= -1.0;
break;

case PauliOperatorParser::MatchType::COMPLEX_AND_I:
coefficient = std::complex<double>{std::stod(match[11].str()), std::stod(match[12].str())};
if (minus) coefficient *= -1.0;
break;

case PauliOperatorParser::MatchType::REAL_ONLY:
coefficient = std::stod(match[14].str());
if (minus) coefficient *= -1.0;
break;

case PauliOperatorParser::MatchType::COMPLEX_ONLY:
coefficient = std::complex<double>{std::stod(match[15].str()), std::stod(match[16].str())};
if (minus) coefficient *= -1.0;
break;

clear();
case PauliOperatorParser::MatchType::UNKNOWN:
default:
xacc::error("String contains unknown patterns.");
break;
}

if (std::abs(coefficient) < 1.0e-12) {
searchStart = match.suffix().first;
continue;
}

if (pauliString.empty()) {

if (!terms.insert({"I", coefficient}).second) {
terms.at("I").coeff() += coefficient;
}
if (std::abs(terms.at("I").coeff()) < 1e-12) {
terms.erase("I");
}

operator+=(listener.getOperator());
} else {

if (!terms.insert({pauliString, Term(coefficient, pauliMap)}).second) {
terms.at(pauliString).coeff() += coefficient;
}
if (std::abs(terms.at(pauliString).coeff()) < 1e-12) {
terms.erase(pauliString);
}

}

searchStart = match.suffix().first;
}
}

bool PauliOperator::contains(PauliOperator &op) {
Expand Down
62 changes: 0 additions & 62 deletions quantum/observable/pauli/PauliOperator.g4

This file was deleted.

Loading

0 comments on commit 97c1dff

Please sign in to comment.