Skip to content

Commit

Permalink
馃悰 Fix PiExpression division and multiplication arithmetic (#549)
Browse files Browse the repository at this point in the history
## Description

Fix Arithmetic Issues when dividing or multiplying a `PiExpression` by a
`double`.

Partially solves #486

## 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.
  • Loading branch information
pehamTom committed Feb 12, 2024
1 parent 55752ce commit af9d20a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/mqt-core/operations/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ class Expression {
return *this;
}
std::for_each(terms.begin(), terms.end(), [&](auto& term) { term *= rhs; });
constant *= U{rhs};
constant = U{double{constant} * double{rhs}};
return *this;
}

Expand All @@ -275,7 +275,7 @@ class Expression {
throw std::runtime_error("Trying to divide expression by 0!");
}
std::for_each(terms.begin(), terms.end(), [&](auto& term) { term /= rhs; });
constant /= U{rhs};
constant = U{double{constant} / double{rhs}};
return *this;
}

Expand Down
12 changes: 12 additions & 0 deletions test/zx/test_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,15 @@ TEST_F(ExpressionTest, Instantiation) {
EXPECT_THROW([[maybe_unused]] const auto h = e.evaluate(assignment),
sym::SymbolicException);
}

TEST_F(ExpressionTest, Arithmetic) {
const auto beta = zx::PiRational(1, 2);

EXPECT_EQ(beta * 2, zx::PiRational(1, 1));
EXPECT_EQ(beta * 2., zx::PiRational(1, 1));

const auto betaExpr = zx::PiExpression{beta};
EXPECT_EQ(betaExpr * 2., zx::PiExpression{zx::PiRational(1, 1)});

EXPECT_EQ(betaExpr / .5, zx::PiExpression{zx::PiRational(1, 1)});
}

0 comments on commit af9d20a

Please sign in to comment.