From e9c03b8df431592c45728db95019d44bd6569229 Mon Sep 17 00:00:00 2001 From: Alec Edgington Date: Thu, 3 Mar 2022 15:04:30 +0000 Subject: [PATCH 1/3] Handle non-real non-symbolic expressions in eval_expr(). --- tket/src/Utils/Expression.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tket/src/Utils/Expression.cpp b/tket/src/Utils/Expression.cpp index e24263b098..2a165915b2 100644 --- a/tket/src/Utils/Expression.cpp +++ b/tket/src/Utils/Expression.cpp @@ -13,9 +13,11 @@ // limitations under the License. #include "Expression.hpp" +#include #include "Constants.hpp" #include "Symbols.hpp" +#include "symengine/symengine_exception.h" namespace tket { @@ -57,7 +59,11 @@ std::optional 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; + } } } From c61f896ca77cea53cc79502175a83aac6ca7970b Mon Sep 17 00:00:00 2001 From: Alec Edgington Date: Thu, 3 Mar 2022 15:13:36 +0000 Subject: [PATCH 2/3] Add test. --- tket/tests/Circuit/test_Boxes.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tket/tests/Circuit/test_Boxes.cpp b/tket/tests/Circuit/test_Boxes.cpp index 54903436c8..149be93811 100644 --- a/tket/tests/Circuit/test_Boxes.cpp +++ b/tket/tests/Circuit/test_Boxes.cpp @@ -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]") { From 02bad3dde86a09e1aef5c2400ccfdbe8b45d168c Mon Sep 17 00:00:00 2001 From: Alec Edgington Date: Thu, 3 Mar 2022 16:55:38 +0000 Subject: [PATCH 3/3] clang-format --- tket/src/Utils/Expression.cpp | 1 + tket/tests/Circuit/test_Boxes.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tket/src/Utils/Expression.cpp b/tket/src/Utils/Expression.cpp index 2a165915b2..d81ca4617c 100644 --- a/tket/src/Utils/Expression.cpp +++ b/tket/src/Utils/Expression.cpp @@ -13,6 +13,7 @@ // limitations under the License. #include "Expression.hpp" + #include #include "Constants.hpp" diff --git a/tket/tests/Circuit/test_Boxes.cpp b/tket/tests/Circuit/test_Boxes.cpp index 149be93811..9d75f9efeb 100644 --- a/tket/tests/Circuit/test_Boxes.cpp +++ b/tket/tests/Circuit/test_Boxes.cpp @@ -381,7 +381,7 @@ SCENARIO("Pauli gadgets", "[boxes]") { REQUIRE((u - Eigen::Matrix4cd::Identity()).cwiseAbs().sum() < ERR_EPS); } GIVEN("complex coefficient") { - Expr ei {SymEngine::I}; + Expr ei{SymEngine::I}; PauliExpBox pebox({Pauli::Z}, ei); Expr p = pebox.get_phase(); REQUIRE(p == ei);