Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace _PhasedFSimConverter in cirq_google with new transformer primitives #5095

Merged
merged 6 commits into from
Mar 17, 2022
47 changes: 15 additions & 32 deletions cirq-google/cirq_google/calibration/engine_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,38 +487,21 @@ def _create_step_result(
raise NotImplementedError()


class _PhasedFSimConverter(cirq.PointOptimizer):
def __init__(self, simulator: PhasedFSimEngineSimulator) -> None:
super().__init__()
self._simulator = simulator

def optimization_at(
self, circuit: cirq.Circuit, index: int, op: cirq.Operation
) -> Optional[cirq.PointOptimizationSummary]:

if isinstance(op.gate, (cirq.MeasurementGate, cirq.SingleQubitGate, cirq.WaitGate)):
new_op = op
else:
if op.gate is None:
raise IncompatibleMomentError(f'Operation {op} has a missing gate')
translated = self._simulator.gates_translator(op.gate)
if translated is None:
raise IncompatibleMomentError(
f'Moment contains non-single qubit operation ' f'{op} with unsupported gate'
)

a, b = op.qubits
new_op = self._simulator.create_gate_with_drift(a, b, translated).on(a, b)

return cirq.PointOptimizationSummary(
clear_span=1, clear_qubits=op.qubits, new_operations=new_op
)


def _convert_to_circuit_with_drift(
simulator: PhasedFSimEngineSimulator, circuit: cirq.AbstractCircuit
) -> cirq.Circuit:
circuit_with_drift = cirq.Circuit(circuit)
converter = _PhasedFSimConverter(simulator)
converter.optimize_circuit(circuit_with_drift)
return circuit_with_drift
def map_func(op: cirq.Operation, _) -> cirq.Operation:
if isinstance(op.gate, (cirq.MeasurementGate, cirq.SingleQubitGate, cirq.WaitGate)):
return op
if op.gate is None:
raise IncompatibleMomentError(f'Operation {op} has a missing gate')
translated = simulator.gates_translator(op.gate)
if translated is None:
raise IncompatibleMomentError(
f'Moment contains non-single qubit operation ' f'{op} with unsupported gate'
)

a, b = op.qubits
return simulator.create_gate_with_drift(a, b, translated).on(a, b)

return cirq.map_operations(circuit, map_func)