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

fix: The DefGate.matrix property will no longer raise an exception when the matrix contains a mix of atomic and object types. #1685

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyquil/quilbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
MarquessV marked this conversation as resolved.
Show resolved Hide resolved
return to_py_matrix(np.asarray(super().specification.to_matrix())) # type: ignore[no-any-return]

@matrix.setter
Expand Down
19 changes: 19 additions & 0 deletions test/unit/__snapshots__/test_quilbase.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions test/unit/test_quilbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
Loading