Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle non-real non-symbolic expressions in eval_expr() #273

Merged
merged 3 commits into from
Mar 3, 2022
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
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