Skip to content

Commit

Permalink
[bugfix] Handle non-real non-symbolic expressions in eval_expr() (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
cqc-alec authored Mar 3, 2022
1 parent 6783400 commit 18b888d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion tket/src/Utils/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

#include "Expression.hpp"

#include <optional>

#include "Constants.hpp"
#include "Symbols.hpp"
#include "symengine/symengine_exception.h"

namespace tket {

Expand Down Expand Up @@ -57,7 +60,11 @@ std::optional<double> eval_expr(const Expr& e) {
if (!SymEngine::free_symbols(e).empty()) {
return std::nullopt;
} else {
return SymEngine::eval_double(e);
try {
return SymEngine::eval_double(e);
} catch (SymEngine::NotImplementedError&) {
return std::nullopt;
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions tket/tests/Circuit/test_Boxes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,12 @@ SCENARIO("Pauli gadgets", "[boxes]") {
Eigen::MatrixXcd u = tket_sim::get_unitary(c);
REQUIRE((u - Eigen::Matrix4cd::Identity()).cwiseAbs().sum() < ERR_EPS);
}
GIVEN("complex coefficient") {
Expr ei{SymEngine::I};
PauliExpBox pebox({Pauli::Z}, ei);
Expr p = pebox.get_phase();
REQUIRE(p == ei);
}
}

SCENARIO("box daggers", "[boxes]") {
Expand Down

0 comments on commit 18b888d

Please sign in to comment.