def optimization_at( self, circuit: 'cirq.Circuit', index: int, op: 'cirq.Operation') -> Optional['cirq.PointOptimizationSummary']: end = index + 1 new_ops = [cirq.X(q) for q in op.qubits] done = False while not done: n = circuit.next_moment_operating_on(op.qubits, end) if n is None: break next_ops: Set[Optional[Operation]] = { circuit.operation_at(q, n) for q in op.qubits } next_ops_list: List[Operation] = [e for e in next_ops if e] next_ops_sorted = sorted(next_ops_list, key=lambda e: str(e.qubits)) for next_op in next_ops_sorted: if next_op: if set(next_op.qubits).issubset(op.qubits): end = n + 1 new_ops.extend(cirq.X(q) for q in next_op.qubits) else: done = True return PointOptimizationSummary(clear_span=end - index, clear_qubits=op.qubits, new_operations=new_ops)
def test_equality(): a = cirq.NamedQubit('a') b = cirq.NamedQubit('b') xa = cirq.X(a) ya = cirq.Y(a) eq = EqualsTester() eq.make_equality_group(lambda: PointOptimizationSummary( clear_span=0, clear_qubits=[], new_operations=[])) eq.add_equality_group( PointOptimizationSummary(clear_span=1, clear_qubits=[a], new_operations=[])) eq.add_equality_group( PointOptimizationSummary(clear_span=1, clear_qubits=[a], new_operations=[xa])) eq.add_equality_group( PointOptimizationSummary(clear_span=1, clear_qubits=[a, b], new_operations=[xa])) eq.add_equality_group( PointOptimizationSummary(clear_span=2, clear_qubits=[a], new_operations=[xa])) eq.add_equality_group( PointOptimizationSummary(clear_span=1, clear_qubits=[a], new_operations=[ya])) eq.add_equality_group( PointOptimizationSummary(clear_span=1, clear_qubits=[a], new_operations=[xa, xa]))
def optimization_at(self, circuit, index, op): end = index + 1 new_ops = [cirq.X(q) for q in op.qubits] done = False while not done: n = circuit.next_moment_operating_on(op.qubits, end) if n is None: break next_ops = {circuit.operation_at(q, n) for q in op.qubits} next_ops = [e for e in next_ops if e] next_ops = sorted(next_ops, key=lambda e: str(e.qubits)) for next_op in next_ops: if next_op: if set(next_op.qubits).issubset(op.qubits): end = n + 1 new_ops.extend(cirq.X(q) for q in next_op.qubits) else: done = True return PointOptimizationSummary(clear_span=end - index, clear_qubits=op.qubits, new_operations=new_ops)