def try_convert_sqrt_iswap_to_fsim(gate: Gate) -> Optional[FSimGate]: """Converts an equivalent gate to FSimGate(theta=π/4, phi=0) if possible. Args: gate: Gate to verify. Returns: FSimGate(theta=π/4, phi=0) if provided gate either FSimGate, ISWapPowGate, PhasedFSimGate or PhasedISwapPowGate that is equivalent to FSimGate(theta=π/4, phi=0). None otherwise. """ if isinstance(gate, FSimGate): if not np.isclose(gate.phi, 0.0): return None angle = gate.theta elif isinstance(gate, ISwapPowGate): angle = -gate.exponent * np.pi / 2 elif isinstance(gate, PhasedFSimGate): if (not np.isclose(gate.zeta, 0.0) or not np.isclose(gate.chi, 0.0) or not np.isclose(gate.gamma, 0.0) or not np.isclose(gate.phi, 0.0)): return None angle = gate.theta elif isinstance(gate, PhasedISwapPowGate): if not np.isclose(-gate.phase_exponent - 0.5, 0.0): return None angle = gate.exponent * np.pi / 2 else: return None if np.isclose(angle, np.pi / 4): return FSimGate(theta=np.pi / 4, phi=0.0) return None
def try_convert_sqrt_iswap_to_fsim(gate: Gate) -> Optional[PhaseCalibratedFSimGate]: """Converts an equivalent gate to FSimGate(theta=π/4, phi=0) if possible. Args: gate: Gate to verify. Returns: FSimGateCalibration with engine_gate FSimGate(theta=π/4, phi=0) if the provided gate is either FSimGate, ISWapPowGate, PhasedFSimGate or PhasedISwapPowGate that is equivalent to FSimGate(theta=±π/4, phi=0). None otherwise. """ if isinstance(gate, FSimGate): if not np.isclose(gate.phi, 0.0): return None angle = gate.theta elif isinstance(gate, ISwapPowGate): angle = -gate.exponent * np.pi / 2 elif isinstance(gate, PhasedFSimGate): if ( not np.isclose(gate.zeta, 0.0) or not np.isclose(gate.chi, 0.0) or not np.isclose(gate.gamma, 0.0) or not np.isclose(gate.phi, 0.0) ): return None angle = gate.theta elif isinstance(gate, PhasedISwapPowGate): if not np.isclose(-gate.phase_exponent - 0.5, 0.0): return None angle = gate.exponent * np.pi / 2 else: return None angle_canonical = angle % (2 * np.pi) if np.isclose(angle_canonical, np.pi / 4): return PhaseCalibratedFSimGate(FSimGate(theta=np.pi / 4, phi=0.0), 0.0) elif np.isclose(angle_canonical, 7 * np.pi / 4): return PhaseCalibratedFSimGate(FSimGate(theta=np.pi / 4, phi=0.0), 0.5) return None
def from_characterization( cls, qubits: Tuple[Qid, Qid], gate: FSimGate, parameters: PhasedFSimCharacterization, characterization_index: Optional[int], ) -> 'FSimPhaseCorrections': """Creates an operation that compensates for zeta, chi and gamma angles of the supplied gate and characterization. Args: qubits: Qubits that the gate should act on. gate: Original, imperfect gate that is supposed to run on the hardware. parameters: The real parameters of the supplied gate. characterization_index: characterization index to use at each moment with gate. """ assert parameters.zeta is not None, "Zeta value must not be None" zeta = parameters.zeta assert parameters.gamma is not None, "Gamma value must not be None" gamma = parameters.gamma assert parameters.chi is not None, "Chi value must not be None" chi = parameters.chi a, b = qubits alpha = 0.5 * (zeta + chi) beta = 0.5 * (zeta - chi) operations = ( (rz(0.5 * gamma - alpha).on(a), rz(0.5 * gamma + alpha).on(b)), (gate.on(a, b),), (rz(0.5 * gamma - beta).on(a), rz(0.5 * gamma + beta).on(b)), ) moment_to_calibration = [None, characterization_index, None] return cls(operations, moment_to_calibration)