def optimize_circuit(self, circuit: circuits.Circuit): # Tracks qubit phases (in half turns; multiply by pi to get radians). qubit_phase = defaultdict(lambda: 0) # type: Dict[ops.QubitId, float] def dump_tracked_phase(qubits: Iterable[ops.QubitId], index: int) -> None: """Zeroes qubit_phase entries by emitting Z gates.""" for q in qubits: p = qubit_phase[q] if not decompositions.is_negligible_turn(p, self.tolerance): dump_op = ops.Z(q)**(p * 2) insertions.append((index, dump_op)) qubit_phase[q] = 0 deletions = [] # type: List[Tuple[int, ops.Operation]] inline_intos = [] # type: List[Tuple[int, ops.Operation]] insertions = [] # type: List[Tuple[int, ops.Operation]] for moment_index, moment in enumerate(circuit): for op in moment.operations: # Move Z gates into tracked qubit phases. h = _try_get_known_z_half_turns(op) if h is not None: q = op.qubits[0] qubit_phase[q] += h / 2 deletions.append((moment_index, op)) continue # Z gate before measurement is a no-op. Drop tracked phase. if ops.MeasurementGate.is_measurement(op): for q in op.qubits: qubit_phase[q] = 0 # If there's no tracked phase, we can move on. phases = [qubit_phase[q] for q in op.qubits] if all( decompositions.is_negligible_turn(p, self.tolerance) for p in phases): continue # Try to move the tracked phasing over the operation. phased_op = op for i, p in enumerate(phases): if not decompositions.is_negligible_turn( p, self.tolerance): phased_op = protocols.phase_by(phased_op, -p, i, default=None) if phased_op is not None: deletions.append((moment_index, op)) inline_intos.append( (moment_index, cast(ops.Operation, phased_op))) else: dump_tracked_phase(op.qubits, moment_index) dump_tracked_phase(qubit_phase.keys(), len(circuit)) circuit.batch_remove(deletions) circuit.batch_insert_into(inline_intos) circuit.batch_insert(insertions)
def _potential_cross_whole_w(moment_index: int, op: ops.Operation, tolerance: float, state: _OptimizerState) -> None: """Grabs or cancels a held W gate against an existing W gate. [Where W(a) is shorthand for PhasedX(phase_exponent=a).] Uses the following identity: ───W(a)───W(b)─── ≡ ───Z^-a───X───Z^a───Z^-b───X───Z^b─── ≡ ───Z^-a───Z^-a───Z^b───X───X───Z^b─── ≡ ───Z^-a───Z^-a───Z^b───Z^b─── ≡ ───Z^2(b-a)─── """ state.deletions.append((moment_index, op)) _, phase_exponent = cast(Tuple[float, float], _try_get_known_phased_pauli(op)) q = op.qubits[0] a = state.held_w_phases.get(q) b = phase_exponent if a is None: # Collect the gate. state.held_w_phases[q] = b else: # Cancel the gate. state.held_w_phases[q] = None t = 2 * (b - a) if not decompositions.is_negligible_turn(t / 2, tolerance): leftover_phase = ops.Z(q)**t state.inline_intos.append((moment_index, leftover_phase))
def dump_tracked_phase(qubits: Iterable[ops.Qid], index: int) -> None: """Zeroes qubit_phase entries by emitting Z gates.""" for q in qubits: p = qubit_phase[q] if not decompositions.is_negligible_turn(p, self.tolerance): dump_op = ops.Z(q)**(p * 2) insertions.append((index, dump_op)) qubit_phase[q] = 0
def optimize_circuit(self, circuit: circuits.Circuit): state = _OptimizerState() for moment_index, moment in enumerate(circuit): for op in moment.operations: affected = [q for q in op.qubits if state.held_w_phases.get(q) is not None] # Collect, phase, and merge Ws. w = _try_get_known_phased_pauli(op) if w is not None: if decompositions.is_negligible_turn( w[0] - 1, self.tolerance): _potential_cross_whole_w(moment_index, op, self.tolerance, state) else: _potential_cross_partial_w(moment_index, op, state) continue if not affected: continue # Absorb Z rotations. t = _try_get_known_z_half_turns(op) if t is not None: _absorb_z_into_w(moment_index, op, state) continue # Dump coherent flips into measurement bit flips. if ops.op_gate_of_type(op, ops.MeasurementGate): _dump_into_measurement(moment_index, op, state) # Cross CZs using kickback. if _try_get_known_cz_half_turns(op) is not None: if len(affected) == 1: _single_cross_over_cz(moment_index, op, affected[0], state) else: _double_cross_over_cz(op, state) continue # Don't know how to handle this situation. Dump the gates. _dump_held(op.qubits, moment_index, state) # Put anything that's still held at the end of the circuit. _dump_held(state.held_w_phases.keys(), len(circuit), state) circuit.batch_remove(state.deletions) circuit.batch_insert_into(state.inline_intos) circuit.batch_insert(state.insertions)
def dump_tracked_phase(qubits: Iterable[ops.Qid], index: int) -> None: """Zeroes qubit_phase entries by emitting Z gates.""" for q in qubits: p = qubit_phase[q] qubit_phase[q] = 0 if decompositions.is_negligible_turn(p, self.tolerance): continue dumped = False moment_index = circuit.prev_moment_operating_on([q], index) if moment_index is not None: op = circuit.moments[moment_index][q] if op and isinstance(op.gate, ops.PhasedXZGate): # Attach z-rotation to replacing PhasedXZ gate. idx = phased_xz_replacements[moment_index, q] _, _, repl_op = replacements[idx] gate = cast(ops.PhasedXZGate, repl_op.gate) repl_op = gate.with_z_exponent(p * 2).on(q) replacements[idx] = (moment_index, op, repl_op) dumped = True if not dumped: # Add a new Z gate dump_op = ops.Z(q)**(p * 2) insertions.append((index, dump_op))
def optimize_circuit(self, circuit: circuits.Circuit): # Tracks qubit phases (in half turns; multiply by pi to get radians). qubit_phase: Dict[ops.Qid, float] = defaultdict(lambda: 0) deletions: List[Tuple[int, ops.Operation]] = [] replacements: List[Tuple[int, ops.Operation, ops.Operation]] = [] insertions: List[Tuple[int, ops.Operation]] = [] phased_xz_replacements: Dict[Tuple[int, ops.Qid], int] = {} def dump_tracked_phase(qubits: Iterable[ops.Qid], index: int) -> None: """Zeroes qubit_phase entries by emitting Z gates.""" for q in qubits: p = qubit_phase[q] qubit_phase[q] = 0 if decompositions.is_negligible_turn(p, self.tolerance): continue dumped = False moment_index = circuit.prev_moment_operating_on([q], index) if moment_index is not None: op = circuit.moments[moment_index][q] if op and isinstance(op.gate, ops.PhasedXZGate): # Attach z-rotation to replacing PhasedXZ gate. idx = phased_xz_replacements[moment_index, q] _, _, repl_op = replacements[idx] gate = cast(ops.PhasedXZGate, repl_op.gate) repl_op = gate.with_z_exponent(p * 2).on(q) replacements[idx] = (moment_index, op, repl_op) dumped = True if not dumped: # Add a new Z gate dump_op = ops.Z(q)**(p * 2) insertions.append((index, dump_op)) for moment_index, moment in enumerate(circuit): for op in moment.operations: # Move Z gates into tracked qubit phases. h = _try_get_known_z_half_turns(op, self.eject_parameterized) if h is not None: q = op.qubits[0] qubit_phase[q] += h / 2 deletions.append((moment_index, op)) continue # Z gate before measurement is a no-op. Drop tracked phase. if isinstance(op.gate, ops.MeasurementGate): for q in op.qubits: qubit_phase[q] = 0 # If there's no tracked phase, we can move on. phases = [qubit_phase[q] for q in op.qubits] if not isinstance(op.gate, ops.PhasedXZGate) and all( decompositions.is_negligible_turn(p, self.tolerance) for p in phases): continue if _is_swaplike(op): a, b = op.qubits qubit_phase[a], qubit_phase[b] = qubit_phase[ b], qubit_phase[a] continue # Try to move the tracked phasing over the operation. phased_op = op for i, p in enumerate(phases): if not decompositions.is_negligible_turn( p, self.tolerance): phased_op = protocols.phase_by(phased_op, -p, i, default=None) if phased_op is not None: gate = phased_op.gate if isinstance(gate, ops.PhasedXZGate) and ( self.eject_parameterized or not protocols.is_parameterized(gate.z_exponent)): qubit = phased_op.qubits[0] qubit_phase[qubit] += gate.z_exponent / 2 phased_op = gate.with_z_exponent(0).on(qubit) repl_idx = len(replacements) phased_xz_replacements[moment_index, qubit] = repl_idx replacements.append((moment_index, op, phased_op)) else: dump_tracked_phase(op.qubits, moment_index) dump_tracked_phase(qubit_phase.keys(), len(circuit)) circuit.batch_remove(deletions) circuit.batch_replace(replacements) circuit.batch_insert(insertions)