@@ -55,6 +55,7 @@ def __init__(
55
55
* ,
56
56
name : str = None ,
57
57
qid_shape : Optional [Iterable [int ]] = None ,
58
+ unitary_check : bool = True ,
58
59
unitary_check_rtol : float = 1e-5 ,
59
60
unitary_check_atol : float = 1e-8 ,
60
61
) -> None :
@@ -66,6 +67,10 @@ def __init__(
66
67
qid_shape: The shape of state tensor that the matrix applies to.
67
68
If not specified, this value is inferred by assuming that the
68
69
matrix is supposed to apply to qubits.
70
+ unitary_check: If True, check that the supplied matrix is unitary up to the
71
+ given tolerances. This should only be disabled if the matrix has already been
72
+ checked for unitarity, in which case we get a slight performance improvement by
73
+ not checking again.
69
74
unitary_check_rtol: The relative tolerance for checking whether the supplied matrix
70
75
is unitary. See `cirq.is_unitary`.
71
76
unitary_check_atol: The absolute tolerance for checking whether the supplied matrix
@@ -99,8 +104,14 @@ def __init__(
99
104
f'qid_shape: { self ._qid_shape } \n '
100
105
)
101
106
102
- if not linalg .is_unitary (matrix , rtol = unitary_check_rtol , atol = unitary_check_atol ):
103
- raise ValueError (f'Not a unitary matrix: { self ._matrix } ' )
107
+ if unitary_check and not linalg .is_unitary (
108
+ matrix , rtol = unitary_check_rtol , atol = unitary_check_atol
109
+ ):
110
+ raise ValueError (f'Not a unitary matrix: { matrix } ' )
111
+
112
+ def with_name (self , name : str ) -> 'MatrixGate' :
113
+ """Creates a new MatrixGate with the same matrix and a new name."""
114
+ return MatrixGate (self ._matrix , name = name , qid_shape = self ._qid_shape , unitary_check = False )
104
115
105
116
def _json_dict_ (self ) -> Dict [str , Any ]:
106
117
return {'matrix' : self ._matrix .tolist (), 'qid_shape' : self ._qid_shape }
0 commit comments