Skip to content

Commit

Permalink
🐛 Fix scientific notation in QASM import (cda-tum#560)
Browse files Browse the repository at this point in the history
## Description

This PR fixes a small bug in the QASM importer that would prevent it
from properly importing floating point numbers in scientific notation.

## Checklist:

<!---
This checklist serves as a reminder of a couple of things that ensure
your pull request will be merged swiftly.
-->

- [x] The pull request only contains commits that are related to it.
- [x] I have added appropriate tests and documentation.
- [x] I have made sure that all CI jobs on GitHub pass.
- [x] The pull request introduces no new warnings and follows the
project's style guidelines.

---------

Signed-off-by: burgholzer <[email protected]>
  • Loading branch information
burgholzer authored Mar 12, 2024
1 parent a392c48 commit 414fe24
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
30 changes: 25 additions & 5 deletions src/parsers/qasm3_parser/Scanner.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#include "parsers/qasm3_parser/Scanner.hpp"

#include <cstdint>
#include <istream>
#include <optional>
#include <regex>
#include <sstream>
#include <stdexcept>
#include <string>

namespace qasm3 {
char Scanner::readUtf8Codepoint(std::istream* in) {
Expand Down Expand Up @@ -186,12 +192,26 @@ Token Scanner::consumeNumberLiteral() {
error("Float literals are only allowed in base 10");
}

char const sep = ch;
nextCh();
auto valAfterDecimalSeparator = consumeNumberLiteral(base);
std::stringstream ss{};
ss << valBeforeDecimalSeparator;

if (ch == '.') {
ss << ch;
nextCh();
const auto valAfterDecimalSeparator = consumeNumberLiteral(base);
ss << valAfterDecimalSeparator;
}

std::stringstream ss;
ss << valBeforeDecimalSeparator << sep << valAfterDecimalSeparator;
if (ch == 'e' || ch == 'E') {
ss << ch;
nextCh();
if (ch == '+' || ch == '-') {
ss << ch;
nextCh();
}
const auto valAfterExponent = consumeNumberLiteral(base);
ss << valAfterExponent;
}

try {
t.valReal = std::stod(ss.str());
Expand Down
4 changes: 3 additions & 1 deletion test/unittests/test_qasm3_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,14 +743,16 @@ TEST_F(Qasm3ParserTest, ImportQasm3CPrefix) {
TEST_F(Qasm3ParserTest, ImportQasmScanner) {
std::stringstream ss{};
const std::string testfile =
"$1 : . .5 -1. -= += ++ *= **= ** /= % %= |= || | &= "
"$1 : . .5 -1. 1.25e-3 1e3 -= += ++ *= **= ** /= % %= |= || | &= "
"&& & ^= ^ ~= ~ ! <= <<= << < >= >>= >> >";
const auto tokens = std::vector{
qasm3::Token::Kind::HardwareQubit,
qasm3::Token::Kind::Colon,
qasm3::Token::Kind::Dot,
qasm3::Token::Kind::FloatLiteral,
qasm3::Token::Kind::FloatLiteral,
qasm3::Token::Kind::FloatLiteral,
qasm3::Token::Kind::FloatLiteral,
qasm3::Token::Kind::MinusEquals,
qasm3::Token::Kind::PlusEquals,
qasm3::Token::Kind::DoublePlus,
Expand Down

0 comments on commit 414fe24

Please sign in to comment.