def rekey_op(op: 'cirq.Operation', rep: str): """Update measurement keys in `op` to include repetition ID `rep`.""" rekeyed_op = protocols.with_key_path(op, self.parent_path + (rep, )) if rekeyed_op is NotImplemented: return op return rekeyed_op
def _with_key_path_(self, path: Tuple[str, ...]): new_gate = protocols.with_key_path(self.gate, path) if new_gate is NotImplemented: return NotImplemented if new_gate is self.gate: # As GateOperation is immutable, this can return the original. return self return new_gate.on(*self.qubits)
def mapped_circuit(self, deep: bool = False) -> 'cirq.Circuit': """Applies all maps to the contained circuit and returns the result. Args: deep: If true, this will also call mapped_circuit on any CircuitOperations this object contains. Returns: The contained circuit with all other member variables (repetitions, qubit mapping, parameterization, etc.) applied to it. This behaves like `cirq.decompose(self)`, but preserving moment structure. """ circuit = self.circuit.unfreeze() circuit = circuit.transform_qubits(lambda q: self.qubit_map.get(q, q)) if self.repetitions < 0: circuit = circuit**-1 has_measurements = protocols.is_measurement(circuit) if has_measurements: circuit = protocols.with_measurement_key_mapping( circuit, self.measurement_key_map) circuit = protocols.resolve_parameters(circuit, self.param_resolver, recursive=False) if deep: def map_deep(op: 'cirq.Operation') -> 'cirq.OP_TREE': return op.mapped_circuit( deep=True) if isinstance(op, CircuitOperation) else op if self.repetition_ids is None: return circuit.map_operations(map_deep) if not has_measurements: return circuit.map_operations(map_deep) * abs(self.repetitions) # Path must be constructed from the top down. rekeyed_circuit = circuits.Circuit( protocols.with_key_path(circuit, self.parent_path + (rep, )) for rep in self.repetition_ids) return rekeyed_circuit.map_operations(map_deep) if self.repetition_ids is None: return circuit if not has_measurements: return circuit * abs(self.repetitions) def rekey_op(op: 'cirq.Operation', rep: str): """Update measurement keys in `op` to include repetition ID `rep`.""" rekeyed_op = protocols.with_key_path(op, self.parent_path + (rep, )) if rekeyed_op is NotImplemented: return op return rekeyed_op return circuits.Circuit( circuit.map_operations(lambda op: rekey_op(op, rep)) for rep in self.repetition_ids)
def _decompose_(self) -> 'cirq.OP_TREE': result = self.circuit.unfreeze() result = result.transform_qubits(lambda q: self.qubit_map.get(q, q)) if self.repetitions < 0: result = result**-1 result = protocols.with_measurement_key_mapping( result, self.measurement_key_map) result = protocols.resolve_parameters(result, self.param_resolver, recursive=False) # repetition_ids don't need to be taken into account if the circuit has no measurements # or if repetition_ids are unset. if self.repetition_ids is None or not protocols.is_measurement(result): return list(result.all_operations()) * abs(self.repetitions) # If it's a measurement circuit with repetitions/repetition_ids, prefix the repetition_ids # to measurements. Details at https://tinyurl.com/measurement-repeated-circuitop. ops = [] # type: List[cirq.Operation] for repetition_id in self.repetition_ids: path = self.parent_path + (repetition_id, ) ops += protocols.with_key_path(result, path).all_operations() return ops
def _with_key_path_(self, path: Tuple[str, ...]): return KrausChannel(kraus_ops=self._kraus_ops, key=protocols.with_key_path(self._key, path))
def _with_key_path_(self, path: Tuple[str, ...]): return Moment( protocols.with_key_path(op, path) if protocols. is_measurement(op) else op for op in self.operations)
def _with_key_path_(self, path: Tuple[str, ...]): return MixedUnitaryChannel( mixture=self._mixture, key=protocols.with_key_path(self._key, path) )