Skip to content

Commit

Permalink
fix: Handle quil_rs FunctionCallExpressions
Browse files Browse the repository at this point in the history
  • Loading branch information
MarquessV committed Sep 17, 2023
1 parent 79989c6 commit ab551d5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
13 changes: 13 additions & 0 deletions pyquil/quilatom.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,19 @@ def _convert_to_py_expression(
return BinaryExp._from_rs_infix_expression(expression.to_infix())
if expression.is_address():
return MemoryReference._from_rs_memory_reference(expression.to_address())
if expression.is_function_call():
fc = expression.to_function_call()
parameter = _convert_to_py_expression(fc.expression)
if fc.function == quil_rs_expr.ExpressionFunction.Cis:
return quil_cis(parameter)
if fc.function == quil_rs_expr.ExpressionFunction.Cosine:
return quil_cos(parameter)
if fc.function == quil_rs_expr.ExpressionFunction.Exponent:
return quil_exp(parameter)
if fc.function == quil_rs_expr.ExpressionFunction.Sine:
return quil_sin(parameter)
if fc.function == quil_rs_expr.ExpressionFunction.SquareRoot:
return quil_sqrt(parameter)
if expression.is_prefix():
prefix = expression.to_prefix()
py_expression = _convert_to_py_expression(prefix.expression)
Expand Down
16 changes: 8 additions & 8 deletions test/unit/__snapshots__/test_quilbase.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@
# name: TestDefGate.test_out[Params]
'''
DEFGATE ParameterizedGate(%X) AS MATRIX:
%X, 0, 0, 0
0, %X, 0, 0
0, 0, %X, 0
0, 0, 0, %X
cos(%X), 0, 0, 0
0, cos(%X), 0, 0
0, 0, cos(%X), 0
0, 0, 0, cos(%X)

'''
# ---
Expand All @@ -260,10 +260,10 @@
# name: TestDefGate.test_str[Params]
'''
DEFGATE ParameterizedGate(%X) AS MATRIX:
%X, 0, 0, 0
0, %X, 0, 0
0, 0, %X, 0
0, 0, 0, %X
cos(%X), 0, 0, 0
0, cos(%X), 0, 0
0, 0, cos(%X), 0
0, 0, 0, cos(%X)

'''
# ---
Expand Down
3 changes: 2 additions & 1 deletion test/unit/test_quilbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest
from syrupy.assertion import SnapshotAssertion

from pyquil.quilatom import quil_cos
from pyquil.gates import X
from pyquil.quil import Program
from pyquil.quilbase import (
Expand Down Expand Up @@ -162,7 +163,7 @@ def test_compile(self, program: Program, compiler: QPUCompiler):
("name", "matrix", "parameters"),
[
("NoParamGate", np.eye(4), []),
("ParameterizedGate", np.diag([Parameter("X")] * 4), [Parameter("X")]),
("ParameterizedGate", np.diag([quil_cos(Parameter("X"))] * 4), [Parameter("X")]),
],
ids=("No-Params", "Params"),
)
Expand Down

0 comments on commit ab551d5

Please sign in to comment.