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

Parsing of pi from strings #1664

Open
bramathon opened this issue Sep 22, 2023 · 4 comments
Open

Parsing of pi from strings #1664

bramathon opened this issue Sep 22, 2023 · 4 comments
Labels
bug 🐛 An issue that needs fixing.

Comments

@bramathon
Copy link
Contributor

Issue Description

If I construct a program from a string, parameters such as "pi/2" are preserved and output that way. However, if I construct the same program in python, the parameter is output as 1.5707963267948966.

The two programs below are entirely equivalent and should compare as equal, but they do not.

How to Reproduce

Code Snippet

from numpy import pi
from pyquil.quil import Program
from pyquil.gates import RX, RZ, H, CNOT

program_from_string = Program("""
H 4
H 5
H 6
CNOT 4 5
CNOT 5 6
RX(pi/2) 6
RZ(pi/2) 6
RX(pi/2) 5
RZ(pi/2) 5
""")

print(program)

program = Program()
program += H(4)
program += H(5)
program += H(6)
program += CNOT(4, 5)
program += CNOT(5, 6)
program += RX(pi/2, 6)
program += RZ(pi/2, 6)
program += RX(pi/2, 5)
program += RZ(pi/2, 5)
print(program)

program_from_string == program```
@bramathon bramathon added the bug 🐛 An issue that needs fixing. label Sep 22, 2023
@MarquessV
Copy link
Contributor

This will require some design work in quil-rs to resolve. The issue is that, when parsing a program, the string pi is parsed as the Quil keyword pi. However, when constructing a gate in Python, np.pi is just a float. quil-rs would either need to rewrite expressions containing pi before storing them, or do so during comparison checks to make this work.

@bramathon
Copy link
Contributor Author

bramathon commented Sep 22, 2023 via email

@jselig-rigetti
Copy link
Contributor

Isn't pi literal always converted into a float per rigetti/quil-rs#240 ?

@kalzoo
Copy link
Contributor

kalzoo commented Dec 19, 2023

I would just like it to be one way or the other. I think the simplest way
is when parsing a problem, replace pi with 3.14159...

This is ugly for program readers, but at least consistent.

Consistent with np.pi (itself a float), perhaps - but inconsistent with the input program, because it would no longer round-trip to the same string, where, syntactically, pi != 3.14.... IMO this current behavior seems reasonable - np.pi is a number, but Quil pi is a special identifier to Quil itself, described in the Quil spec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An issue that needs fixing.
Projects
None yet
Development

No branches or pull requests

4 participants