예제 #1
0
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]
예제 #2
0
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,
    )
예제 #3
0
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],
            ])),
    )
예제 #4
0
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)
예제 #5
0
def _fsim_identity_converter(
        gate: cirq.Gate) -> Optional[PhaseCalibratedFSimGate]:
    if isinstance(gate, cirq.FSimGate):
        return PhaseCalibratedFSimGate(gate, 0.0)
    return None
예제 #6
0
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