def __call__(self, strategy: 'cirq.Circuit'): if not is_acquaintance_strategy(strategy): raise TypeError('not is_acquaintance_strategy(strategy)') expose_acquaintance_gates(strategy) strategy.device = self.execution_strategy.device super().optimize_circuit(strategy) return self.mapping.copy()
def rectify_acquaintance_strategy(circuit: circuits.Circuit, acquaint_first: bool = True) -> None: """Splits moments so that they contain either only acquaintance gates or only permutation gates. Orders resulting moments so that the first one is of the same type as the previous one. Args: circuit: The acquaintance strategy to rectify. acquaint_first: Whether to make acquaintance moment first in when splitting the first mixed moment. """ if not is_acquaintance_strategy(circuit): raise TypeError('not is_acquaintance_strategy(circuit)') rectified_moments = [] for moment in circuit: gate_type_to_ops = collections.defaultdict( list) # type: Dict[bool, List[ops.GateOperation]] for op in moment.operations: gate_type_to_ops[isinstance( op.gate, AcquaintanceOpportunityGate)].append(op) if len(gate_type_to_ops) == 1: rectified_moments.append(moment) continue for acquaint_first in sorted(gate_type_to_ops.keys(), reverse=acquaint_first): rectified_moments.append( circuits.Moment(gate_type_to_ops[acquaint_first])) circuit._moments = rectified_moments
def remove_redundant_acquaintance_opportunities( strategy: circuits.Circuit) -> None: """Removes redundant acquaintance opportunities.""" if not is_acquaintance_strategy(strategy): raise TypeError('not is_acquaintance_strategy(circuit)') qubits = tuple(strategy.all_qubits()) mapping = {q: i for i, q in enumerate(qubits)} expose_acquaintance_gates(strategy) annotated_strategy = strategy.copy() LogicalAnnotator(mapping)(annotated_strategy) new_moments = [] # type: List[ops.Moment] acquaintance_opps = set() # type: Set[FrozenSet[int]] for moment in annotated_strategy: new_moment = [] # type: List[ops.Operation] for op in moment: if isinstance(op, AcquaintanceOperation): opp = frozenset(cast(Sequence[int], op.logical_indices)) if opp not in acquaintance_opps: acquaintance_opps.add(opp) new_moment.append(acquaint(*op.qubits)) else: new_moment.append(op) new_moments.append(ops.Moment(new_moment)) strategy._moments = new_moments
def replace_acquaintance_with_swap_network( circuit: 'cirq.Circuit', qubit_order: Sequence['cirq.Qid'], acquaintance_size: Optional[int] = 0, swap_gate: 'cirq.Gate' = ops.SWAP, ) -> bool: """ Replace every moment containing acquaintance gates (after rectification) with a generalized swap network, with the partition given by the acquaintance gates in that moment (and singletons for the free qubits). Accounts for reversing effect of swap networks. Args: circuit: The acquaintance strategy. qubit_order: The qubits, in order, on which the replacing swap network gate acts on. acquaintance_size: The acquaintance size of the new swap network gate. swap_gate: The gate used to swap logical indices. Returns: Whether or not the overall effect of the inserted swap network gates is to reverse the order of the qubits, i.e. the parity of the number of swap network gates inserted. Raises: TypeError: circuit is not an acquaintance strategy. """ if not is_acquaintance_strategy(circuit): raise TypeError('not is_acquaintance_strategy(circuit)') rectify_acquaintance_strategy(circuit) reflected = False reverse_map = {q: r for q, r in zip(qubit_order, reversed(qubit_order))} for moment_index, moment in enumerate(circuit): if reflected: moment = moment.transform_qubits(reverse_map.__getitem__) if all( isinstance(op.gate, AcquaintanceOpportunityGate) for op in moment.operations): swap_network_gate = SwapNetworkGate.from_operations( qubit_order, moment.operations, acquaintance_size, swap_gate) swap_network_op = swap_network_gate(*qubit_order) moment = ops.Moment([swap_network_op]) reflected = not reflected circuit._moments[moment_index] = moment return reflected
def __call__(self, strategy: circuits.Circuit): if not is_acquaintance_strategy(strategy): raise TypeError('not is_acquaintance_strategy(strategy)') expose_acquaintance_gates(strategy) super().optimize_circuit(strategy) return self.mapping.copy()