12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- from typing import Any , Dict , Optional , Tuple , Sequence , TYPE_CHECKING , Union
15
+ from typing import Any , Dict , Iterable , Optional , Tuple , Sequence , TYPE_CHECKING , Union
16
16
17
17
import numpy as np
18
18
19
19
from cirq import protocols , value
20
- from cirq .ops import raw_types , gate_operation
20
+ from cirq .ops import raw_types
21
21
22
22
if TYPE_CHECKING :
23
23
import cirq
@@ -80,21 +80,12 @@ def key(self, key: Union[str, value.MeasurementKey]):
80
80
else :
81
81
self .mkey = value .MeasurementKey (name = key )
82
82
83
- def on (self , * qubits : raw_types .Qid ) -> raw_types .Operation :
84
- """Returns an application of this gate to the given qubits.
85
-
86
- Args:
87
- *qubits: The collection of qubits to potentially apply the gate to.
88
- """
89
- maybe_rekeyed_gate = self .with_key (self .mkey .with_qubits (qubits ))
90
- return gate_operation .GateOperation (maybe_rekeyed_gate , list (qubits ))
91
-
92
83
def _qid_shape_ (self ) -> Tuple [int , ...]:
93
84
return self ._qid_shape
94
85
95
86
def with_key (self , key : Union [str , value .MeasurementKey ]) -> 'MeasurementGate' :
96
87
"""Creates a measurement gate with a new key but otherwise identical."""
97
- if isinstance ( key , value . MeasurementKey ) and key == self .mkey :
88
+ if key == self .key :
98
89
return self
99
90
return MeasurementGate (
100
91
self .num_qubits (), key = key , invert_mask = self .invert_mask , qid_shape = self ._qid_shape
@@ -114,7 +105,7 @@ def with_bits_flipped(self, *bit_positions: int) -> 'MeasurementGate':
114
105
for b in bit_positions :
115
106
new_mask [b ] = not new_mask [b ]
116
107
return MeasurementGate (
117
- self .num_qubits (), key = self .mkey , invert_mask = tuple (new_mask ), qid_shape = self ._qid_shape
108
+ self .num_qubits (), key = self .key , invert_mask = tuple (new_mask ), qid_shape = self ._qid_shape
118
109
)
119
110
120
111
def full_invert_mask (self ):
@@ -161,8 +152,8 @@ def _circuit_diagram_info_(
161
152
if b :
162
153
symbols [i ] = '!M'
163
154
164
- # Mention the measurement key if it is non-trivial or there are no known qubits .
165
- if self . mkey . name or self .mkey . path or not args .known_qubits :
155
+ # Mention the measurement key.
156
+ if not args . known_qubits or self .key != _default_measurement_key ( args .known_qubits ) :
166
157
symbols [0 ] += f"('{ self .key } ')"
167
158
168
159
return protocols .CircuitDiagramInfo (tuple (symbols ))
@@ -200,13 +191,8 @@ def _quil_(
200
191
201
192
def _op_repr_ (self , qubits : Sequence ['cirq.Qid' ]) -> str :
202
193
args = list (repr (q ) for q in qubits )
203
- if self .mkey .name or self .mkey .path :
204
- if self .mkey == self .mkey .name :
205
- args .append (f'key={ self .mkey .name !r} ' )
206
- else :
207
- # Remove qubits from the `MeasurementKey` representation since we already have
208
- # qubits from the op.
209
- args .append (f'key={ self .mkey .with_qubits (tuple ())!r} ' )
194
+ if self .key != _default_measurement_key (qubits ):
195
+ args .append (f'key={ self .key !r} ' )
210
196
if self .invert_mask :
211
197
args .append (f'invert_mask={ self .invert_mask !r} ' )
212
198
arg_list = ', ' .join (args )
@@ -219,13 +205,13 @@ def __repr__(self):
219
205
return (
220
206
f'cirq.MeasurementGate('
221
207
f'{ self .num_qubits ()!r} , '
222
- f'{ self .mkey . name if self . mkey == self . mkey . name else self . mkey !r} , '
208
+ f'{ self .key !r} , '
223
209
f'{ self .invert_mask } '
224
210
f'{ qid_shape_arg } )'
225
211
)
226
212
227
213
def _value_equality_values_ (self ) -> Any :
228
- return self .mkey , self .invert_mask , self ._qid_shape
214
+ return self .key , self .invert_mask , self ._qid_shape
229
215
230
216
def _json_dict_ (self ) -> Dict [str , Any ]:
231
217
other = {}
@@ -234,7 +220,7 @@ def _json_dict_(self) -> Dict[str, Any]:
234
220
return {
235
221
'cirq_type' : self .__class__ .__name__ ,
236
222
'num_qubits' : len (self ._qid_shape ),
237
- 'key' : self .mkey ,
223
+ 'key' : self .key ,
238
224
'invert_mask' : self .invert_mask ,
239
225
** other ,
240
226
}
@@ -243,7 +229,7 @@ def _json_dict_(self) -> Dict[str, Any]:
243
229
def _from_json_dict_ (cls , num_qubits , key , invert_mask , qid_shape = None , ** kwargs ):
244
230
return cls (
245
231
num_qubits = num_qubits ,
246
- key = value .MeasurementKey .parse_serialized (key ) if isinstance ( key , str ) else key ,
232
+ key = value .MeasurementKey .parse_serialized (key ),
247
233
invert_mask = tuple (invert_mask ),
248
234
qid_shape = None if qid_shape is None else tuple (qid_shape ),
249
235
)
@@ -254,3 +240,7 @@ def _has_stabilizer_effect_(self) -> Optional[bool]:
254
240
def _act_on_ (self , args : 'cirq.ActOnArgs' , qubits : Sequence ['cirq.Qid' ]) -> bool :
255
241
args .measure (qubits , self .key , self .full_invert_mask ())
256
242
return True
243
+
244
+
245
+ def _default_measurement_key (qubits : Iterable [raw_types .Qid ]) -> str :
246
+ return ',' .join (str (q ) for q in qubits )
0 commit comments