@@ -139,7 +139,7 @@ def nth_operation( # type: ignore[override]
139139 yield cirq .CZ (* accumulator , target [target_idx ])
140140
141141 def on_classical_vals (self , ** vals ) -> Dict [str , 'ClassicalValT' ]:
142- if self .target_gate != cirq .X :
142+ if self .target_gate != cirq .X and self . target_gate != cirq . Z :
143143 return NotImplemented
144144 if len (self .control_registers ) > 1 or len (self .selection_registers ) > 1 :
145145 return NotImplemented
@@ -148,13 +148,17 @@ def on_classical_vals(self, **vals) -> Dict[str, 'ClassicalValT']:
148148 selection_name = self .selection_registers [0 ].name
149149 selection = vals [selection_name ]
150150 target = vals ['target' ]
151- if control :
151+
152+ # When target_gate == cirq.X, the action is (modulo phase) a single bitflip.
153+ if control and self .target_gate == cirq .X :
152154 max_selection = self .selection_registers [0 ].dtype .iteration_length_or_zero () - 1
153155 target = (2 ** (max_selection - selection )) ^ target
156+ # When target_gate == cirq.Z, the action is only in the phase.
157+
154158 return {control_name : control , selection_name : selection , 'target' : target }
155159
156160 def basis_state_phase (self , ** vals ) -> Union [complex , None ]:
157- if self .target_gate != cirq .X :
161+ if self .target_gate != cirq .X and self . target_gate != cirq . Z :
158162 return None
159163 if len (self .control_registers ) > 1 or len (self .selection_registers ) > 1 :
160164 return None
@@ -165,7 +169,10 @@ def basis_state_phase(self, **vals) -> Union[complex, None]:
165169 target = vals ['target' ]
166170 if control :
167171 max_selection = self .selection_registers [0 ].dtype .iteration_length_or_zero () - 1
168- num_phases = (target >> (max_selection - selection + 1 )).bit_count ()
172+ if self .target_gate == cirq .X :
173+ num_phases = (target >> (max_selection - selection + 1 )).bit_count ()
174+ else :
175+ num_phases = (target >> (max_selection - selection )).bit_count ()
169176 return 1 if (num_phases % 2 ) == 0 else - 1
170177 return 1
171178
0 commit comments