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

Support equality between commutative expressions #27

Closed
dbanty opened this issue Aug 12, 2021 · 3 comments · Fixed by #29
Closed

Support equality between commutative expressions #27

dbanty opened this issue Aug 12, 2021 · 3 comments · Fixed by #29

Comments

@dbanty
Copy link
Contributor

dbanty commented Aug 12, 2021

Expressions which are equal by the commutative property should evaluate to equal and hash the same. As an example, an expression 1 + theta should be equivalent to theta + 1 both when compared directly (e.g. assert_eq!) and when hashed (e.g. added to a HashSet).

Per this comment we have some internal code in the QCS backend which creates a "canonical" or "normal" form of expressions which we can likely reuse.

@genos
Copy link
Contributor

genos commented Aug 13, 2021

Trying to dive into this, I naïvely thought, "No problem, we just want to sort the operands when dealing with a commutative infix operator!" completely forgetting that the operands are themselves Expressions, which would mean I'd need to impl Ord

@genos
Copy link
Contributor

genos commented Oct 12, 2021

I think this one is covered by #29 & #25; what say you @dbanty (et. al.)?

@dbanty
Copy link
Contributor Author

dbanty commented Oct 12, 2021

Yup, I think you're right! Closing.

@dbanty dbanty closed this as completed Oct 12, 2021
genos pushed a commit that referenced this issue Aug 10, 2023
BREAKING: Expressions no longer use hashing for implementing equality
BREAKING: Expression equality no longer takes commutativity into account

In #276, @Shadow53 noted

> One thing that may be useful enough to pull into its own PR is the
> change to not use hashing in the implementation of `PartialEq` for
> `Expression`, which also helps speed things up.

We originally put this together in #27 to ensure that equality held in
the face of commutativity, e.g., `1 + x == x + 1`. In addition to the
performance benefits of decoupling the hashing and equality
implementations, it makes sense to remove any special status for
commutative operations in light of all the work we're doing on
expression simplification. If we wished to ensure expressions are
`Eq` if and only if they represent the same mathematical expression,
we'd have to have equality contingent upon simplification, which would
be even more costly.
genos added a commit that referenced this issue Aug 10, 2023
* feat!: Decouple expression hashing and equality

BREAKING: Expressions no longer use hashing for implementing equality
BREAKING: Expression equality no longer takes commutativity into account

In #276, @Shadow53 noted

> One thing that may be useful enough to pull into its own PR is the
> change to not use hashing in the implementation of `PartialEq` for
> `Expression`, which also helps speed things up.

We originally put this together in #27 to ensure that equality held in
the face of commutativity, e.g., `1 + x == x + 1`. In addition to the
performance benefits of decoupling the hashing and equality
implementations, it makes sense to remove any special status for
commutative operations in light of all the work we're doing on
expression simplification. If we wished to ensure expressions are
`Eq` if and only if they represent the same mathematical expression,
we'd have to have equality contingent upon simplification, which would
be even more costly.

* fix: Use Self::
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants