From d57cce9edf6ac19a5329fbf54dd2e863014fa7ec Mon Sep 17 00:00:00 2001 From: Marquess Valdez Date: Tue, 31 Oct 2023 09:41:08 -0700 Subject: [PATCH] fix: The `DefGate.matrix` property will no longer raise an exception when the matrix contains a mix of atomic and object types. (#1685) --- pyquil/quilbase.py | 4 ++-- test/unit/__snapshots__/test_quilbase.ambr | 19 +++++++++++++++++++ test/unit/test_numpy.py | 2 +- test/unit/test_quilbase.py | 14 ++++++++++++-- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/pyquil/quilbase.py b/pyquil/quilbase.py index c73728dee..287a9e900 100644 --- a/pyquil/quilbase.py +++ b/pyquil/quilbase.py @@ -627,7 +627,7 @@ def _from_rs_gate_definition(cls, gate_definition: quil_rs.GateDefinition) -> Se def _convert_to_matrix_specification( matrix: Union[List[List[Expression]], np.ndarray, np.matrix] ) -> quil_rs.GateSpecification: - to_rs_matrix = np.vectorize(_convert_to_rs_expression) + to_rs_matrix = np.vectorize(_convert_to_rs_expression, otypes=["O"]) return quil_rs.GateSpecification.from_matrix(to_rs_matrix(np.asarray(matrix))) @staticmethod @@ -678,7 +678,7 @@ def num_args(self) -> int: @property def matrix(self) -> np.ndarray: - to_py_matrix = np.vectorize(_convert_to_py_expression) + to_py_matrix = np.vectorize(_convert_to_py_expression, otypes=["O"]) return to_py_matrix(np.asarray(super().specification.to_matrix())) # type: ignore[no-any-return] @matrix.setter diff --git a/test/unit/__snapshots__/test_quilbase.ambr b/test/unit/__snapshots__/test_quilbase.ambr index a73922f78..1074e677a 100644 --- a/test/unit/__snapshots__/test_quilbase.ambr +++ b/test/unit/__snapshots__/test_quilbase.ambr @@ -221,12 +221,23 @@ '\tSAMPLE-RATE: 44.1', }) # --- +# name: TestDefGate.test_get_constructor[MixedTypes] + 'MixedTypes(%theta) 123' +# --- # name: TestDefGate.test_get_constructor[No-Params] 'NoParamGate 123' # --- # name: TestDefGate.test_get_constructor[Params] 'ParameterizedGate(%theta) 123' # --- +# name: TestDefGate.test_out[MixedTypes] + ''' + DEFGATE MixedTypes(%X) AS MATRIX: + 0, sin(%X) + 0, 0 + + ''' +# --- # name: TestDefGate.test_out[No-Params] ''' DEFGATE NoParamGate AS MATRIX: @@ -247,6 +258,14 @@ ''' # --- +# name: TestDefGate.test_str[MixedTypes] + ''' + DEFGATE MixedTypes(%X) AS MATRIX: + 0, sin(%X) + 0, 0 + + ''' +# --- # name: TestDefGate.test_str[No-Params] ''' DEFGATE NoParamGate AS MATRIX: diff --git a/test/unit/test_numpy.py b/test/unit/test_numpy.py index 4980d4141..c9d789edd 100644 --- a/test/unit/test_numpy.py +++ b/test/unit/test_numpy.py @@ -220,7 +220,7 @@ def test_defgate(): p += U_test(1, 0) qam = PyQVM(n_qubits=2, quantum_simulator_type=NumpyWavefunctionSimulator) qam.execute(p) - wf1 = qam.wf_simulator.wf + wf1 = qam.wf_simulator.wf.astype(np.complex128) should_be = np.zeros((2, 2), dtype=np.complex128) one_over_sqrt2 = 1 / np.sqrt(2) should_be[0, 1] = one_over_sqrt2 diff --git a/test/unit/test_quilbase.py b/test/unit/test_quilbase.py index 028c3ff27..14f2f1e65 100644 --- a/test/unit/test_quilbase.py +++ b/test/unit/test_quilbase.py @@ -6,7 +6,7 @@ import pytest from syrupy.assertion import SnapshotAssertion -from pyquil.quilatom import quil_cos +from pyquil.quilatom import quil_cos, quil_sin from pyquil.gates import X from pyquil.quil import Program from pyquil.quilbase import ( @@ -171,8 +171,18 @@ def test_compile(self, program: Program, compiler: QPUCompiler): [ ("NoParamGate", np.eye(4), []), ("ParameterizedGate", np.diag([quil_cos(Parameter("X"))] * 4), [Parameter("X")]), + ( + "MixedTypes", + np.array( + [ + [0, quil_sin(Parameter("X"))], + [0, 0], + ] + ), + [Parameter("X")], + ), ], - ids=("No-Params", "Params"), + ids=("No-Params", "Params", "MixedTypes"), ) class TestDefGate: @pytest.fixture