rigetti / pyquil

A Python library for quantum programming using Quil.
http://docs.rigetti.com
Apache License 2.0
1.4k stars 341 forks source link

Parsing of pi from strings #1664

Open bramathon opened 11 months ago

bramathon commented 11 months ago

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```
MarquessV commented 11 months ago

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 commented 11 months ago

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.

On Fri, Sept 22, 2023, 18:07 Marquess Valdez @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/rigetti/pyquil/issues/1664#issuecomment-1731758241, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEWA7RPAFT5IJJSXNMETU3X3XAVHANCNFSM6AAAAAA5DAHBSU . You are receiving this because you authored the thread.Message ID: @.***>

jselig-rigetti commented 10 months ago

Isn't pi literal always converted into a float per https://github.com/rigetti/quil-rs/pull/240 ?

kalzoo commented 9 months ago

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.