Skip to content

πŸ› ZX PiExpression arithmetic not working properlyΒ #486

@burgholzer

Description

@burgholzer

Environment information

Any version of the ZX library.

Description

The arithmetic for PiExpressions in the ZX package seems to be quite buggy in certain aspects.
For example

const auto beta = zx::PiRational(1, 2);

std::cout << beta * 2 << "\n";
std::cout << beta * 2. << "\n";

const auto betaExpr = zx::PiExpression{beta};
std::cout << betaExpr * 2. << "\n";
// std::cout << betaExpr * 2 << "\n"; // does not compile due to missing overload

yields

1/1
1/1
159154943/500000000

The last entry is completely unexpected.

So that's the one thing. This also means something like betaExpr / 2. will result in complete garbage.
Now, #482 adds support for divisions by integers (which allows to write the above as betaExpr / 2).
However, that revealed quite a problem in the way arithmetic expressions are handled.
Namely, that

const auto beta = zx::PiRational(-1, 2);
std::cout << (beta + beta) / 2 << "\n";  // and, if the above is fixed most likely also `(beta+beta)/2.` which currently returns garbage

prints

1/2

instead of the desired result of -1/2.
The reason for that is beta+beta evaluating to -1 which is 1 mod pi.

Expected behavior

I would expect division and multiplication by floating point numbers for PiExpression to work exactly as they do for PiRational.
Furthermore, I would expect moderately complex arithmetic expressions to produce the correct result.

How to Reproduce

Run the examples from above

Metadata

Metadata

Assignees

Labels

ZXAnything related to the ZX packagebugSomething isn't workingc++Anything related to C++ code

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions