Ejemplo n.º 1
0
 def check(gate: cirq.Gate, expected: PhaseCalibratedFSimGate):
     assert np.allclose(cirq.unitary(gate), cirq.unitary(expected))
     assert try_convert_gate_to_fsim(gate) == expected
Ejemplo n.º 2
0
 def check(gate):
     result1 = try_convert_gate_to_fsim(gate)
     result2 = try_convert_sqrt_iswap_to_fsim(gate)
     assert result1 == result2
     assert result1 is not None
Ejemplo n.º 3
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