AngouriMath icon indicating copy to clipboard operation
AngouriMath copied to clipboard

Added a simplify pattern for XOR that reduces A xor 0 to A.

Open jclapis opened this issue 4 years ago • 8 comments

This is a small PR that adds a rule regarding XOR to the common simplifier patterns:

X ⊕ 0 = X

Our group encounters this kind of pattern occasionally, so we would like to see this added to the canonical Simplify() method. All of the unit tests pass with this new rule in place, and we have added some unit tests to cover this new behavior.

jclapis avatar Aug 23 '21 19:08 jclapis

In fact, xor is not exactly a numeric operation. It's an operation on booleans, so

a xor false

would get reduced to a, and

a xor true

gets reduced to not a

WhiteBlackGoose avatar Aug 23 '21 20:08 WhiteBlackGoose

That is true for the bitwise operation, certainly. Our group is using AngouriMath for some quantum computing research, so we are using XOR to compare two quantum registers which are usually represented as variables and numbers instead of the bitwise approach. In either case, I just tested it with the following:

        [Fact] public void Xor3() => AssertSimplify(new Entity.Xorf(false, x), x);
        [Fact] public void Xor4() => AssertSimplify(new Entity.Xorf(x, false), x);

The pattern above still passes these tests, but fails the a xor true tests. I will amend this PR with patterns for that in place shortly.

jclapis avatar Aug 23 '21 20:08 jclapis

Codecov Report

Merging #498 (1b3909d) into master (e45a06c) will not change coverage. The diff coverage is n/a.

:exclamation: Current head 1b3909d differs from pull request most recent head 9e4b6b8. Consider uploading reports for the commit 9e4b6b8 to get more accurate results Impacted file tree graph

@@      Coverage Diff      @@
##   master   #498   +/-   ##
=============================
=============================


Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update e45a06c...9e4b6b8. Read the comment docs.

codecov-commenter avatar Aug 23 '21 21:08 codecov-commenter

Our group is using AngouriMath for some quantum computing research

Sounds amazing.

WhiteBlackGoose avatar Aug 24 '21 03:08 WhiteBlackGoose

As far as I can see, those patterns are already there, aren't they? (this is an F# wrapper of the library, but it's powered by the same kernel)

WhiteBlackGoose avatar Aug 24 '21 03:08 WhiteBlackGoose

So here's the thing. The most basic operations (like exclusive or against a constant, or multiplication by 0, or etc.) are covered by inner simplification than by patterns, and it seems to be covered already.

Although now I think that having it in patterns is not a bad idea too 🤔 .

WhiteBlackGoose avatar Aug 24 '21 03:08 WhiteBlackGoose

As far as I can see, those patterns are already there, aren't they?

Probably, I hadn't tested them before because we don't use Boolean as one of the arguments for Xorf. We use Integer, as in this pattern:

X ⊕ 0 = X

This is our sample code:

Entity.Variable n = "n";
Entity.Xorf xor = new Entity.Xorf(n, 0);
xor.Simplify();
Console.WriteLine(xor);

It produces the following:

n xor 0

We would like it to produce n instead of n xor 0. Clearly it works out of the box with Boolean as you have demonstrated, but it doesn't work for Integer. That's what the original incarnation of this PR aimed to add.

jclapis avatar Aug 24 '21 03:08 jclapis

Let's keep this PR open for now, because I'm not sure whether we need it for integers (it doesn't work on integers currently ; to make it work, you would need to implement all logic for boolean operators).

In your case, you can do

Entity ReplaceIntsWithBooleans(Entity expr) => expr.Substitute(0, false).Substitute(1, true);
Entity ReplaceBooleansWithInts(Entity expr) => expr.Substitute(false, 0).Substitute(true, 1);

WhiteBlackGoose avatar Aug 24 '21 03:08 WhiteBlackGoose