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 optimize_circuit(self, circuit: circuits.Circuit): circuit._moments = [ *transformers.eject_phased_paulis( circuit, atol=self.tolerance, eject_parameterized=self.eject_parameterized) ]
def merge_single_qubit_gates_into_phxz(circuit: circuits.Circuit, atol: float = 1e-8) -> None: """Canonicalizes runs of single-qubit rotations in a circuit. Specifically, any run of non-parameterized single-qubit gates will be replaced by an optional PhasedXZ operation. Args: circuit: The circuit to rewrite. This value is mutated in-place. atol: Absolute tolerance to angle error. Larger values allow more negligible gates to be dropped, smaller values increase accuracy. """ circuit._moments = [ *transformers.merge_single_qubit_gates_to_phxz(circuit, atol=atol) ]
def display_mapping(circuit: circuits.Circuit, initial_mapping: LogicalMapping) -> None: """Inserts display gates between moments to indicate the mapping throughout the circuit.""" qubits = sorted(circuit.all_qubits()) mapping = initial_mapping.copy() old_moments = circuit._moments gate = MappingDisplayGate(mapping.get(q) for q in qubits) new_moments = [ops.Moment([gate(*qubits)])] for moment in old_moments: new_moments.append(moment) update_mapping(mapping, moment) gate = MappingDisplayGate(mapping.get(q) for q in qubits) new_moments.append(ops.Moment([gate(*qubits)])) circuit._moments = new_moments