Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/quartz/parser/qasm_parser.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "qasm_parser.h"

#include <cctype>

namespace quartz {

void find_and_replace_all(std::string &data, const std::string &tofind,
Expand Down Expand Up @@ -274,6 +276,11 @@ int ParamParser::parse_expr(std::stringstream &ss, bool is_halved) {
if (token[pos - 1] == '*') {
// Case: t*-e (negative sign), ignored here
pos = token.find_last_of("+-", pos - 1);
} else if ((token[pos - 1] == 'e' || token[pos - 1] == 'E') && pos > 1 &&
(std::isdigit((unsigned char)token[pos - 2]) ||
token[pos - 2] == '.')) {
// Case: 1.5e-05 (sign of an exponent), ignored here
pos = token.find_last_of("+-", pos - 1);
} else {
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ file(GLOB_RECURSE TEST_CREATE_GRAPHXFER_FROM_QASM "test_create_graphXfer_from_qa
file(GLOB_RECURSE TEST_PARTITION "test_partition.cpp")
file(GLOB_RECURSE TEST_MULT "test_mult.cpp")
file(GLOB_RECURSE TEST_PI "test_pi.cpp")
file(GLOB_RECURSE TEST_PARSER_EXPONENT "test_parser_exponent.cpp")
file(GLOB_RECURSE TEST_CONSTANTS "test_constants.cpp")
file(GLOB_RECURSE TEST_QASM_PARSER "test_qasm_parser.cpp" )
file(GLOB_RECURSE VERIFY_OPENQASM "verify_openqasm.cpp" )
Expand Down Expand Up @@ -73,6 +74,7 @@ add_executable(test_create_graphXfer_from_qasm ${TEST_CREATE_GRAPHXFER_FROM_QASM
add_executable(test_partition ${TEST_PARTITION} )
add_executable(test_mult ${TEST_MULT} )
add_executable(test_pi ${TEST_PI} )
add_executable(test_parser_exponent ${TEST_PARSER_EXPONENT} )
add_executable(test_constants ${TEST_CONSTANTS} )
add_executable(test_qasm_parser ${TEST_QASM_PARSER} )
add_executable(verify_openqasm ${VERIFY_OPENQASM} )
Expand Down
64 changes: 64 additions & 0 deletions src/test/test_parser_exponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "quartz/circuitseq/circuitseq.h"
#include "quartz/context/context.h"
#include "quartz/parser/qasm_parser.h"

#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace quartz;

// Rotation angles written in scientific notation (e.g. 9.5e-05, as printed by
// many tools) must not be split at the sign of their exponent: each angle must
// parse to the same circuit as the angle written as a plain decimal.
int main() {
ParamInfo param_info;
Context ctx({GateType::input_qubit, GateType::input_param, GateType::add,
GateType::rz},
/*num_qubits=*/1, &param_info);
auto parse = [&](const std::string &angle) {
QASMParser parser(&ctx);
CircuitSeq *seq = nullptr;
const bool ok = parser.load_qasm_str(
"OPENQASM 2.0;\ninclude \"qelib1.inc\";\nqreg q[1];\nrz(" + angle +
") q[0];\n",
seq);
return ok ? seq : nullptr;
};
const std::vector<std::pair<std::string, std::string>> cases = {
{"9.58737992428526e-05", "0.0000958737992428526"},
{"-4.79e-05", "-0.0000479"},
{"3.451511e-09", "0.000000003451511"},
{"1.5E+2", "150"},
{"2e-3", "0.002"},
{"0.5-1e-3", "0.5-0.001"},
{"pi*-0.25", "-pi/4"},
};
bool all_ok = true;
for (const auto &[angle, plain] : cases) {
CircuitSeq *seq1 = parse(angle);
CircuitSeq *seq2 = parse(plain);
bool ok = seq1 != nullptr && seq2 != nullptr;
if (ok) {
const auto mat1 = seq1->get_matrix(&ctx);
const auto mat2 = seq2->get_matrix(&ctx);
for (int i = 0; i < (int)mat1.size(); i++) {
for (int j = 0; j < (int)mat1[i].size(); j++) {
ok = ok && std::abs(mat1[i][j] - mat2[i][j]) < 1e-12;
}
}
}
if (!ok) {
std::cout << "rz(" << angle << ") is not parsed as rz(" << plain << ")"
<< std::endl;
all_ok = false;
}
delete seq1;
delete seq2;
}
std::cout << (all_ok ? "All angles parsed correctly." : "FAILED")
<< std::endl;
return all_ok ? 0 : 1;
}
Loading