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

Don't assume expressions being substituted are mod 4 #220

Merged
merged 4 commits into from
Feb 15, 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
12 changes: 12 additions & 0 deletions pytket/tests/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ def test_symbolic_ops() -> None:
assert np.allclose(commands[1].op.params, [2.4], atol=1e-10)


def test_subst_4() -> None:
# https://github.com/CQCL/tket/issues/219
m = fresh_symbol("m")
c = Circuit(1)
a = m / 4
c.add_gate(OpType.Rx, a, [0])
c.symbol_substitution({m: 4})
angle = c.get_commands()[0].op.params[0]
print(angle)
assert np.isclose(angle, 1.0)


def test_sympy_conversion() -> None:
def get_type_tree(expr: sympy.Expr) -> str:
# Format e.g. "<class 'sympy.core.numbers.Pi'>" to "Pi"
Expand Down
2 changes: 1 addition & 1 deletion tket/src/Circuit/Circuit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void Circuit::symbol_substitution(const symbol_map_t &symbol_map) {
// This is a workaround for a symengine issue: symengine currently has poor
// handling of symbolic evaluations for atan2. However, this may not catch
// every such issue, so we should revisit it.
Comment on lines 142 to 144
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this comment still true?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think it is still true.

if (equiv_0(e, 4)) {
if (approx_0(e)) {
sub_map[s] = SymEngine::zero;
} else {
sub_map[s] = e;
Expand Down
5 changes: 0 additions & 5 deletions tket/src/Gate/Rotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@

namespace tket {

static bool approx_0(const Expr &e) {
std::optional<double> v = eval_expr(e);
return v && (std::abs(v.value()) < EPS);
}

static Expr atan2_bypi(const Expr &a, const Expr &b) {
std::optional<double> va = eval_expr(a);
std::optional<double> vb = eval_expr(b);
Expand Down
5 changes: 5 additions & 0 deletions tket/src/Utils/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

namespace tket {

bool approx_0(const Expr& e, double tol) {
std::optional<double> v = eval_expr(e);
return v && (std::abs(v.value()) < tol);
}

double fmodn(double x, unsigned n) {
x /= n;
x -= floor(x);
Expand Down
10 changes: 10 additions & 0 deletions tket/src/Utils/include/Utils/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ Expr cos_halfpi_times(const Expr& e);
*/
Expr sin_halfpi_times(const Expr& e);

/**
* Test if an expression is approximately zero
*
* @param e expression
* @param tol tolerance
*
* @return whether \p e is within \p tol of zero
*/
bool approx_0(const Expr& e, double tol = EPS);

/**
* Evaluate modulo n in the range [0,n)
*
Expand Down