12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
"""A no-qubit global phase operation."""
15
- from typing import Any , Dict , Tuple , TYPE_CHECKING
15
+ from typing import Any , Dict , Sequence , Tuple , TYPE_CHECKING
16
16
17
17
import numpy as np
18
18
19
19
from cirq import value , protocols
20
- from cirq .ops import raw_types
20
+ from cirq ._compat import deprecated_class
21
+ from cirq .ops import gate_operation , raw_types
21
22
22
23
if TYPE_CHECKING :
23
24
import cirq
24
25
25
26
26
27
@value .value_equality (approximate = True )
27
- class GlobalPhaseOperation (raw_types .Operation ):
28
+ @deprecated_class (deadline = 'v0.16' , fix = 'Use cirq.global_phase_operation' )
29
+ class GlobalPhaseOperation (gate_operation .GateOperation ):
28
30
def __init__ (self , coefficient : value .Scalar , atol : float = 1e-8 ) -> None :
29
- if abs (1 - abs (coefficient )) > atol :
30
- raise ValueError (f'Coefficient is not unitary: { coefficient !r} ' )
31
- self .coefficient = coefficient
32
-
33
- @property
34
- def qubits (self ) -> Tuple ['cirq.Qid' , ...]:
35
- return ()
31
+ gate = GlobalPhaseGate (coefficient , atol )
32
+ super ().__init__ (gate , [])
36
33
37
34
def with_qubits (self , * new_qubits ) -> 'GlobalPhaseOperation' :
38
35
if new_qubits :
39
36
raise ValueError (f'{ self !r} applies to 0 qubits but new_qubits={ new_qubits !r} .' )
40
37
return self
41
38
39
+ @property
40
+ def coefficient (self ) -> value .Scalar :
41
+ return self .gate .coefficient # type: ignore
42
+
43
+ @coefficient .setter
44
+ def coefficient (self , coefficient : value .Scalar ):
45
+ # coverage: ignore
46
+ self .gate ._coefficient = coefficient # type: ignore
47
+
48
+ def __str__ (self ) -> str :
49
+ return str (self .coefficient )
50
+
51
+ def __repr__ (self ) -> str :
52
+ return f'cirq.GlobalPhaseOperation({ self .coefficient !r} )'
53
+
54
+ def _json_dict_ (self ) -> Dict [str , Any ]:
55
+ return protocols .obj_to_dict_helper (self , ['coefficient' ])
56
+
57
+
58
+ @value .value_equality (approximate = True )
59
+ class GlobalPhaseGate (raw_types .Gate ):
60
+ def __init__ (self , coefficient : value .Scalar , atol : float = 1e-8 ) -> None :
61
+ if abs (1 - abs (coefficient )) > atol :
62
+ raise ValueError (f'Coefficient is not unitary: { coefficient !r} ' )
63
+ self ._coefficient = coefficient
64
+
65
+ @property
66
+ def coefficient (self ) -> value .Scalar :
67
+ return self ._coefficient
68
+
42
69
def _value_equality_values_ (self ) -> Any :
43
70
return self .coefficient
44
71
45
72
def _has_unitary_ (self ) -> bool :
46
73
return True
47
74
48
- def __pow__ (self , power ):
75
+ def __pow__ (self , power ) -> 'cirq.GlobalPhaseGate' :
49
76
if isinstance (power , (int , float )):
50
- return GlobalPhaseOperation (self .coefficient ** power )
77
+ return GlobalPhaseGate (self .coefficient ** power )
51
78
return NotImplemented
52
79
53
80
def _unitary_ (self ) -> np .ndarray :
@@ -60,7 +87,7 @@ def _apply_unitary_(self, args) -> np.ndarray:
60
87
def _has_stabilizer_effect_ (self ) -> bool :
61
88
return True
62
89
63
- def _act_on_ (self , args : 'cirq.ActOnArgs' ):
90
+ def _act_on_ (self , args : 'cirq.ActOnArgs' , qubits ):
64
91
from cirq .sim import clifford
65
92
66
93
if isinstance (args , clifford .ActOnCliffordTableauArgs ):
@@ -78,7 +105,17 @@ def __str__(self) -> str:
78
105
return str (self .coefficient )
79
106
80
107
def __repr__ (self ) -> str :
81
- return f'cirq.GlobalPhaseOperation({ self .coefficient !r} )'
108
+ return f'cirq.GlobalPhaseGate({ self .coefficient !r} )'
109
+
110
+ def _op_repr_ (self , qubits : Sequence ['cirq.Qid' ]) -> str :
111
+ return f'cirq.global_phase_operation({ self .coefficient !r} )'
82
112
83
113
def _json_dict_ (self ) -> Dict [str , Any ]:
84
114
return protocols .obj_to_dict_helper (self , ['coefficient' ])
115
+
116
+ def _qid_shape_ (self ) -> Tuple [int , ...]:
117
+ return tuple ()
118
+
119
+
120
+ def global_phase_operation (coefficient : value .Scalar , atol : float = 1e-8 ) -> 'cirq.GateOperation' :
121
+ return GlobalPhaseGate (coefficient , atol )()
0 commit comments