def test_phase_calibrated_fsim_gate_compensated(phase_exponent: float): a, b = cirq.LineQubit.range(2) ideal_gate = cirq.FSimGate(theta=np.pi / 4, phi=0.0) characterized_gate = cirq.PhasedFSimGate(theta=ideal_gate.theta, zeta=0.1, chi=0.2, gamma=0.3, phi=ideal_gate.phi) parameters = PhasedFSimCharacterization( theta=ideal_gate.theta, zeta=characterized_gate.zeta, chi=characterized_gate.chi, gamma=characterized_gate.gamma, phi=ideal_gate.phi, ) calibrated = PhaseCalibratedFSimGate(ideal_gate, phase_exponent=phase_exponent) # Passing characterized_gate as engine_gate simulates the hardware execution. operations = calibrated.with_zeta_chi_gamma_compensated( (a, b), parameters, engine_gate=characterized_gate) cirq.testing.assert_allclose_up_to_global_phase( cirq.unitary(cirq.Circuit(operations)), cirq.unitary( cirq.Circuit([ [cirq.Z(a)**-phase_exponent, cirq.Z(b)**phase_exponent], ideal_gate.on(a, b), [cirq.Z(a)**phase_exponent, cirq.Z(b)**-phase_exponent], ])), atol=1e-8, )
def test_phase_calibrated_fsim_gate_as_characterized_phased_fsim_gate( phase_exponent: float): a, b = cirq.LineQubit.range(2) ideal_gate = cirq.FSimGate(theta=np.pi / 4, phi=0.0) characterized_gate = cirq.PhasedFSimGate(theta=ideal_gate.theta, zeta=0.1, chi=0.2, gamma=0.3, phi=ideal_gate.phi) parameters = PhasedFSimCharacterization( theta=ideal_gate.theta, zeta=characterized_gate.zeta, chi=characterized_gate.chi, gamma=characterized_gate.gamma, phi=ideal_gate.phi, ) calibrated = PhaseCalibratedFSimGate(ideal_gate, phase_exponent=phase_exponent) phased_gate = calibrated.as_characterized_phased_fsim_gate(parameters).on( a, b) assert np.allclose( cirq.unitary(phased_gate), cirq.unitary( cirq.Circuit([ [cirq.Z(a)**-phase_exponent, cirq.Z(b)**phase_exponent], characterized_gate.on(a, b), [cirq.Z(a)**phase_exponent, cirq.Z(b)**-phase_exponent], ])), )
def test_phase_corrected_fsim_operations_with_phase_exponent( theta: float, zeta: float, chi: float, gamma: float, phi: float) -> None: a, b = cirq.LineQubit.range(2) phase_exponent = 0.5 # Theta is negated to match the phase exponent of 0.5. expected_gate = cirq.PhasedFSimGate(theta=-theta, zeta=-zeta, chi=-chi, gamma=-gamma, phi=phi) expected = cirq.unitary(expected_gate) corrected = workflow.FSimPhaseCorrections.from_characterization( (a, b), PhaseCalibratedFSimGate(cirq.FSimGate(theta=theta, phi=phi), phase_exponent), cirq_google.PhasedFSimCharacterization(theta=theta, zeta=zeta, chi=chi, gamma=gamma, phi=phi), characterization_index=5, ) actual = cirq.unitary(corrected.as_circuit()) assert cirq.equal_up_to_global_phase(actual, expected) assert corrected.moment_to_calibration == [None, 5, None]
def test_try_convert_sqrt_iswap_to_fsim_converts_correctly(): expected = cirq.FSimGate(theta=np.pi / 4, phi=0) expected_unitary = cirq.unitary(expected) fsim = cirq.FSimGate(theta=np.pi / 4, phi=0) assert np.allclose(cirq.unitary(fsim), expected_unitary) assert try_convert_sqrt_iswap_to_fsim(fsim) == PhaseCalibratedFSimGate( expected, 0.0) assert try_convert_sqrt_iswap_to_fsim( cirq.FSimGate(theta=np.pi / 4, phi=0.1)) is None assert try_convert_sqrt_iswap_to_fsim(cirq.FSimGate(theta=np.pi / 3, phi=0)) is None phased_fsim = cirq.PhasedFSimGate(theta=np.pi / 4, phi=0) assert np.allclose(cirq.unitary(phased_fsim), expected_unitary) assert try_convert_sqrt_iswap_to_fsim( phased_fsim) == PhaseCalibratedFSimGate(expected, 0.0) assert (try_convert_sqrt_iswap_to_fsim( cirq.PhasedFSimGate(theta=np.pi / 4, zeta=0.1, phi=0)) is None) iswap_pow = cirq.ISwapPowGate(exponent=-0.5) assert np.allclose(cirq.unitary(iswap_pow), expected_unitary) assert try_convert_sqrt_iswap_to_fsim( iswap_pow) == PhaseCalibratedFSimGate(expected, 0.0) assert try_convert_sqrt_iswap_to_fsim( cirq.ISwapPowGate(exponent=-0.4)) is None phased_iswap_pow = cirq.PhasedISwapPowGate(exponent=0.5, phase_exponent=-0.5) assert np.allclose(cirq.unitary(phased_iswap_pow), expected_unitary) assert try_convert_sqrt_iswap_to_fsim( phased_iswap_pow) == PhaseCalibratedFSimGate(expected, 0.0) assert (try_convert_sqrt_iswap_to_fsim( cirq.PhasedISwapPowGate(exponent=-0.6, phase_exponent=0.1)) is None) assert try_convert_sqrt_iswap_to_fsim(cirq.CZ) is None assert (try_convert_sqrt_iswap_to_fsim( cirq.FSimGate(theta=sympy.Symbol('t'), phi=sympy.Symbol('p'))) is None)
def from_characterization( cls, qubits: Tuple[Qid, Qid], gate_calibration: PhaseCalibratedFSimGate, 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_calibration: Original, imperfect gate that is supposed to run on the hardware together with phase information. parameters: The real parameters of the supplied gate. characterization_index: characterization index to use at each moment with gate. """ operations = gate_calibration.with_zeta_chi_gamma_compensated( qubits, parameters) moment_to_calibration = [None, characterization_index, None] return cls(operations, moment_to_calibration)
def create_gate_with_drift( self, a: cirq.Qid, b: cirq.Qid, gate_calibration: PhaseCalibratedFSimGate ) -> cirq.PhasedFSimGate: """Generates a gate with drift for a given gate. Args: a: The first qubit. b: The second qubit. gate_calibration: Reference gate together with a phase information. Returns: A modified gate that includes the drifts induced by internal state of the simulator. """ gate = gate_calibration.engine_gate if (a, b, gate) in self._drifted_parameters: parameters = self._drifted_parameters[(a, b, gate)] elif (b, a, gate) in self._drifted_parameters: parameters = self._drifted_parameters[(b, a, gate)].parameters_for_qubits_swapped() else: parameters = self._drift_generator(a, b, gate) self._drifted_parameters[(a, b, gate)] = parameters return gate_calibration.as_characterized_phased_fsim_gate(parameters)
def _fsim_identity_converter( gate: cirq.Gate) -> Optional[PhaseCalibratedFSimGate]: if isinstance(gate, cirq.FSimGate): return PhaseCalibratedFSimGate(gate, 0.0) return None
def test_try_convert_gate_to_fsim(): def check(gate: cirq.Gate, expected: PhaseCalibratedFSimGate): assert np.allclose(cirq.unitary(gate), cirq.unitary(expected)) assert try_convert_gate_to_fsim(gate) == expected check( cirq.FSimGate(theta=0.3, phi=0.5), PhaseCalibratedFSimGate(cirq.FSimGate(theta=0.3, phi=0.5), 0.0), ) check( cirq.FSimGate(7 * np.pi / 4, 0.0), PhaseCalibratedFSimGate(cirq.FSimGate(np.pi / 4, 0.0), 0.5), ) check( cirq.ISwapPowGate(exponent=-0.5), PhaseCalibratedFSimGate(cirq.FSimGate(theta=0.25 * np.pi, phi=0.0), 0.0), ) check( cirq.ops.ISwapPowGate(exponent=0.8, global_shift=2.5), PhaseCalibratedFSimGate(cirq.FSimGate(theta=0.4 * np.pi, phi=0.0), 0.5), ) gate = cirq.ops.ISwapPowGate(exponent=0.3, global_shift=0.4) assert try_convert_gate_to_fsim(gate) is None check( cirq.PhasedFSimGate(theta=0.2, phi=0.5, chi=1.5 * np.pi), PhaseCalibratedFSimGate(cirq.FSimGate(theta=0.2, phi=0.5), 0.25), ) gate = cirq.PhasedFSimGate(theta=0.2, phi=0.5, zeta=1.5 * np.pi) assert try_convert_gate_to_fsim(gate) is None check( cirq.PhasedISwapPowGate(exponent=-0.5, phase_exponent=0.75), PhaseCalibratedFSimGate(cirq.FSimGate(theta=0.25 * np.pi, phi=0.0), 0.25), ) check(cirq.CZ, PhaseCalibratedFSimGate(cirq.FSimGate(theta=0.0, phi=np.pi), 0.0)) check( cirq.ops.CZPowGate(exponent=0.3), PhaseCalibratedFSimGate(cirq.FSimGate(theta=0.0, phi=-0.3 * np.pi), 0.0), ) check( cirq.ops.CZPowGate(exponent=0.8, global_shift=2.5), PhaseCalibratedFSimGate(cirq.FSimGate(theta=0.0, phi=-0.8 * np.pi), 0.0), ) gate = cirq.ops.CZPowGate(exponent=0.3, global_shift=0.4) assert try_convert_gate_to_fsim(gate) is None check( cirq_google.ops.SYC, PhaseCalibratedFSimGate(cirq.FSimGate(phi=np.pi / 6, theta=np.pi / 2), 0.0), ) assert try_convert_gate_to_fsim(cirq.CX) is None # Parameterized gates are not supported. x = sympy.Symbol('x') assert try_convert_gate_to_fsim(cirq.ops.ISwapPowGate(exponent=x)) is None assert try_convert_gate_to_fsim(cirq.PhasedFSimGate(theta=x)) is None assert try_convert_gate_to_fsim( cirq.PhasedISwapPowGate(exponent=x)) is None assert try_convert_gate_to_fsim(cirq.CZPowGate(exponent=x)) is None