Ejemplo n.º 1
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) == expected
    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) == expected
    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) == expected
    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) == expected
    assert (
        try_convert_sqrt_iswap_to_fsim(cirq.PhasedISwapPowGate(exponent=-0.5, phase_exponent=0.1))
        is None
    )

    assert try_convert_sqrt_iswap_to_fsim(cirq.CZ) is None
Ejemplo n.º 2
0
def test_phased_iswap_pow():
    gate1 = cirq.PhasedISwapPowGate(phase_exponent=0.1, exponent=0.25)
    gate2 = cirq.PhasedISwapPowGate(phase_exponent=0.1, exponent=0.5)
    assert gate1**2 == gate2

    u1 = cirq.unitary(gate1)
    u2 = cirq.unitary(gate2)
    assert np.allclose(u1 @ u1, u2)
Ejemplo n.º 3
0
 def _convert_to_phased_iswap(
         self, g: POSSIBLE_FSIM_GATES) -> Optional[cirq.PhasedISwapPowGate]:
     if isinstance(g, cirq.PhasedISwapPowGate):
         return g
     if isinstance(g, cirq.PhasedFSimGate) and self._approx_eq_or_symbol(
         (g.zeta, g.gamma, g.phi), (0, 0, 0)):
         return cirq.PhasedISwapPowGate(exponent=-2 * g.theta / np.pi,
                                        phase_exponent=g.chi / (2 * np.pi))
     cg = self._convert_to_iswap(g)
     return (None if cg is None else cirq.PhasedISwapPowGate(
         exponent=cg.exponent, phase_exponent=0))
Ejemplo n.º 4
0
def test_phased_iswap_equality():
    eq = cirq.testing.EqualsTester()
    eq.add_equality_group(
        cirq.PhasedISwapPowGate(phase_exponent=0, exponent=0.4),
        cirq.ISWAP**0.4)
    eq.add_equality_group(
        cirq.PhasedISwapPowGate(phase_exponent=0,
                                exponent=0.4,
                                global_shift=0.3),
        cirq.ISwapPowGate(global_shift=0.3)**0.4,
    )
Ejemplo n.º 5
0
def get_match_circuit() -> cirq.Circuit:
    qubits = [cirq.LineQubit(i) for i in range(9)]

    g = cirq.CZPowGate(exponent=0.1)
    zz = cirq.ZZPowGate(exponent=0.3)
    px = cirq.PhasedXPowGate(phase_exponent=0.6, exponent=0.2)
    circ = cirq.Circuit(
        [
            cirq.H(qubits[0]),
            cirq.X(qubits[1]),
            cirq.Y(qubits[2]),
            cirq.Z(qubits[3]),
            cirq.S(qubits[4]),
            cirq.CNOT(qubits[1], qubits[4]),
            cirq.T(qubits[3]),
            cirq.CNOT(qubits[6], qubits[8]),
            cirq.I(qubits[5]),
            cirq.XPowGate(exponent=0.1)(qubits[5]),
            cirq.YPowGate(exponent=0.1)(qubits[6]),
            cirq.ZPowGate(exponent=0.1)(qubits[7]),
            g(qubits[2], qubits[3]),
            zz(qubits[3], qubits[4]),
            px(qubits[6]),
            cirq.CZ(qubits[2], qubits[3]),
            cirq.ISWAP(qubits[4], qubits[5]),
            cirq.FSimGate(1.4, 0.7)(qubits[6], qubits[7]),
            cirq.google.SYC(qubits[3], qubits[0]),
            cirq.PhasedISwapPowGate(phase_exponent=0.7, exponent=0.8)(
                qubits[3], qubits[4]),
            cirq.GlobalPhaseOperation(1j),
            cirq.measure_each(*qubits[3:-2]),
        ],
        strategy=InsertStrategy.EARLIEST,
    )
    return circ
Ejemplo n.º 6
0
def test_diagram():
    q0, q1 = cirq.LineQubit.range(2)
    c = cirq.Circuit(
        cirq.PhasedISwapPowGate(phase_exponent=sympy.Symbol('p'),
                                exponent=sympy.Symbol('t')).on(q0, q1),
        cirq.PhasedISwapPowGate(phase_exponent=2 * sympy.Symbol('p'),
                                exponent=1 + sympy.Symbol('t')).on(q0, q1),
        cirq.PhasedISwapPowGate(phase_exponent=0.2, exponent=1).on(q0, q1),
        cirq.PhasedISwapPowGate(phase_exponent=0.3, exponent=0.4).on(q0, q1),
    )
    cirq.testing.assert_has_diagram(
        c, """
0: ───PhISwap(p)─────PhISwap(2*p)───────────PhISwap(0.2)───PhISwap(0.3)───────
      │              │                      │              │
1: ───PhISwap(p)^t───PhISwap(2*p)^(t + 1)───PhISwap(0.2)───PhISwap(0.3)^0.4───
""")
Ejemplo n.º 7
0
def test_cirq_to_circuit_0_7() -> None:
    q0 = cq.LineQubit(0)
    q1 = cq.LineQubit(1)
    gate = cirq_to_circuit(cq.Circuit(cq.rx(0.5).on(q0)))[0]
    assert isinstance(gate, qf.XPow)
    assert gate.param("t") == 0.5 / pi

    gate = cirq_to_circuit(cq.Circuit(cq.ry(0.5).on(q0)))[0]
    assert isinstance(gate, qf.YPow)
    assert gate.param("t") == 0.5 / pi

    gate = cirq_to_circuit(cq.Circuit(cq.rz(0.5).on(q0)))[0]
    assert isinstance(gate, qf.ZPow)
    assert gate.param("t") == 0.5 / pi

    # gate = cirq_to_circuit(cq.Circuit(cq.IdentityGate(2).on(q0, q1)))[0]

    op = (cq.PhasedISwapPowGate()**0.5).on(q0, q1)
    circ = cirq_to_circuit(cq.Circuit(op))
    assert qf.gates_close(circ.asgate(), qf.Givens(0.5 * pi / 2, 0, 1))

    op = cq.PhasedXZGate(x_exponent=0.125,
                         z_exponent=0.25,
                         axis_phase_exponent=0.375).on(q0)
    circ = cirq_to_circuit(cq.Circuit(op))
    assert len(circ) == 3
Ejemplo n.º 8
0
def test_phased_iswap_has_consistent_protocols(phase_exponent, exponent,
                                               global_shift):
    cirq.testing.assert_implements_consistent_protocols(
        cirq.PhasedISwapPowGate(phase_exponent=phase_exponent,
                                exponent=exponent,
                                global_shift=global_shift),
        ignoring_global_phase=False,
    )
def test_unsupported_phased_iswap():
    """Tests that a Phased ISwap with a provided phase_exponent and exponent is
    not supported."""
    q0 = cirq.LineQubit(0)
    q1 = cirq.LineQubit(1)
    circuit = cirq.Circuit(cirq.PhasedISwapPowGate(exponent=0.5, phase_exponent=0.33)(q0, q1))
    with pytest.raises(ValueError, match='phase_exponent of .25 OR an exponent of 1'):
        cgoc.ConvertToSycamoreGates().optimize_circuit(circuit)
Ejemplo n.º 10
0
def test_repr():
    p = -0.25
    t = 0.75
    s = 0.3
    gate = cirq.PhasedISwapPowGate(phase_exponent=p,
                                   exponent=t,
                                   global_shift=s)
    cirq.testing.assert_equivalent_repr(gate)
Ejemplo n.º 11
0
def test_create_circuits_initial_trapping(layout_type: type) -> None:
    layout = layout_type(size=4)
    parameters = _create_test_parameters(
        layout,
        initial_state=IndependentChainsInitialState(
            up=UniformTrappingPotential(particles=2),
            down=GaussianTrappingPotential(particles=2,
                                           center=0.2,
                                           sigma=0.3,
                                           scale=0.4)))

    initial, _, _ = create_circuits(parameters, trotter_steps=0)

    up1, up2, up3, up4 = layout.up_qubits
    down1, down2, down3, down4 = layout.down_qubits

    expected = cirq.Circuit([
        cirq.Moment(cirq.X(up1), cirq.X(up2), cirq.X(down1), cirq.X(down2)),
        cirq.Moment(
            cirq.PhasedISwapPowGate(phase_exponent=0.25,
                                    exponent=-0.7322795271987699).on(up2, up3),
            cirq.PhasedISwapPowGate(phase_exponent=0.25,
                                    exponent=-0.7502896835888355).on(
                                        down2, down3),
        ),
        cirq.Moment((cirq.Z**0.0).on(up3), (cirq.Z**0.0).on(down3)),
        cirq.Moment(
            cirq.PhasedISwapPowGate(phase_exponent=0.25,
                                    exponent=-0.564094216848975).on(up1, up2),
            cirq.PhasedISwapPowGate(phase_exponent=0.25,
                                    exponent=-0.5768514417132005).on(
                                        down1, down2),
            cirq.PhasedISwapPowGate(phase_exponent=0.25,
                                    exponent=-0.5640942168489748).on(up3, up4),
            cirq.PhasedISwapPowGate(phase_exponent=0.25,
                                    exponent=-0.5973464819433905).on(
                                        down3, down4),
        ),
        cirq.Moment(
            (cirq.Z**0.0).on(up2),
            (cirq.Z**0.0).on(down2),
            (cirq.Z**0.0).on(up4),
            (cirq.Z**0.0).on(down4),
        ),
        cirq.Moment(
            cirq.PhasedISwapPowGate(phase_exponent=0.25,
                                    exponent=-0.26772047280123007).on(
                                        up2, up3),
            cirq.PhasedISwapPowGate(phase_exponent=0.25,
                                    exponent=-0.2994619470606122).on(
                                        down2, down3),
        ),
        cirq.Moment((cirq.Z**0.0).on(up3), (cirq.Z**0.0).on(down3)),
    ])

    assert cirq.approx_eq(initial, expected)
 def one_and_two_body_interaction(p, q, a, b) -> cirq.OP_TREE:
     t_symbol = LetterWithSubscripts('T', p, q, i)
     w_symbol = LetterWithSubscripts('W', p, q, i)
     v_symbol = LetterWithSubscripts('V', p, q, i)
     if t_symbol in param_set:
         yield cirq.ISwapPowGate(exponent=-t_symbol).on(a, b)
     if w_symbol in param_set:
         yield cirq.PhasedISwapPowGate(exponent=w_symbol).on(a, b)
     if v_symbol in param_set:
         yield cirq.CZPowGate(exponent=v_symbol).on(a, b)
Ejemplo n.º 13
0
def test_phased_iswap_init():
    p = -0.25
    t = 0.75
    s = 0.5
    gate = cirq.PhasedISwapPowGate(phase_exponent=p,
                                   exponent=t,
                                   global_shift=s)
    assert gate.phase_exponent == p
    assert gate.exponent == t
    assert gate.global_shift == s
def test_unsupported_phased_iswap():
    """Tests that a Phased ISwap with a provided phase_exponent and exponent is
    not supported."""
    q0 = cirq.LineQubit(0)
    q1 = cirq.LineQubit(1)
    circuit = cirq.Circuit(cirq.PhasedISwapPowGate(exponent=0.5, phase_exponent=0.33)(q0, q1))
    converted_circuit = circuit.copy()
    with cirq.testing.assert_deprecated("Use cirq.optimize_for_target_gateset", deadline='v1.0'):
        cgoc.ConvertToSycamoreGates().optimize_circuit(converted_circuit)
    cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent(
        circuit, converted_circuit, atol=1e-8
    )
Ejemplo n.º 15
0
def test_phased_iswap_equivalent_circuit():
    p = 0.7
    t = -0.4
    gate = cirq.PhasedISwapPowGate(phase_exponent=p, exponent=t)
    q0, q1 = cirq.LineQubit.range(2)
    equivalent_circuit = cirq.Circuit([
        cirq.Z(q0)**p,
        cirq.Z(q1)**-p,
        cirq.ISWAP(q0, q1)**t,
        cirq.Z(q0)**-p,
        cirq.Z(q1)**p,
    ])
    assert np.allclose(cirq.unitary(gate), cirq.unitary(equivalent_circuit))
Ejemplo n.º 16
0
def test_try_convert_syc_or_sqrt_iswap_to_fsim():
    def check_converts(gate: cirq.Gate):
        result = try_convert_syc_or_sqrt_iswap_to_fsim(gate)
        assert np.allclose(cirq.unitary(gate), cirq.unitary(result))

    def check_none(gate: cirq.Gate):
        assert try_convert_syc_or_sqrt_iswap_to_fsim(gate) is None

    check_converts(cirq_google.ops.SYC)
    check_converts(cirq.FSimGate(np.pi / 2, np.pi / 6))
    check_none(cirq.FSimGate(0, np.pi))
    check_converts(cirq.FSimGate(np.pi / 4, 0.0))
    check_none(cirq.FSimGate(0.2, 0.3))
    check_converts(cirq.ISwapPowGate(exponent=0.5))
    check_converts(cirq.ISwapPowGate(exponent=-0.5))
    check_none(cirq.ISwapPowGate(exponent=0.3))
    check_converts(cirq.PhasedFSimGate(theta=np.pi / 4, phi=0.0, chi=0.7))
    check_none(cirq.PhasedFSimGate(theta=0.3, phi=0.4))
    check_converts(cirq.PhasedISwapPowGate(exponent=0.5, phase_exponent=0.75))
    check_none(cirq.PhasedISwapPowGate(exponent=0.4, phase_exponent=0.75))
    check_none(cirq.ops.CZPowGate(exponent=1.0))
    check_none(cirq.CX)
Ejemplo n.º 17
0
def test_gate_translators_are_consistent():
    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

    check(cirq.FSimGate(theta=np.pi / 4, phi=0))
    check(cirq.FSimGate(theta=-np.pi / 4, phi=0))
    check(cirq.FSimGate(theta=7 * np.pi / 4, phi=0))
    check(cirq.PhasedFSimGate(theta=np.pi / 4, phi=0))
    check(cirq.ISwapPowGate(exponent=0.5))
    check(cirq.ISwapPowGate(exponent=-0.5))
    check(cirq.PhasedISwapPowGate(exponent=0.5, phase_exponent=-0.5))
Ejemplo n.º 18
0
def test_fsim_gate_family_convert_rejects():
    # Non compatible, random 1/2/3 qubit gates.
    for gate in [cirq.rx(np.pi / 2), cirq.CNOT, cirq.CCNOT]:
        assert cirq_google.FSimGateFamily().convert(gate, cirq.PhasedFSimGate) is None
        assert gate not in cirq_google.FSimGateFamily(gates_to_accept=[cirq.PhasedFSimGate])
    # Custom gate with an overriden `_value_equality_values_cls_`.
    assert UnequalSycGate() not in cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC])
    assert UnequalSycGate(is_parameterized=True) not in cirq_google.FSimGateFamily(
        gates_to_accept=[cirq_google.SYC], allow_symbols=True
    )
    # Partially paramaterized incompatible gate.
    assert cirq.FSimGate(THETA, np.pi / 2) not in cirq_google.FSimGateFamily(
        gates_to_accept=[cirq.PhasedISwapPowGate(exponent=0.5, phase_exponent=0.1), cirq.CZPowGate]
    )
Ejemplo n.º 19
0
def test_phased_iswap_unitary():
    p = 0.3
    t = 0.4
    actual = cirq.unitary(cirq.PhasedISwapPowGate(phase_exponent=p,
                                                  exponent=t))
    c = np.cos(np.pi * t / 2)
    s = np.sin(np.pi * t / 2) * 1j
    f = np.exp(2j * np.pi * p)
    # yapf: disable
    expected = np.array([[1, 0, 0, 0],
                         [0, c, s * f, 0],
                         [0, s * f.conjugate(), c, 0],
                         [0, 0, 0, 1]])
    # yapf: enable
    assert np.allclose(actual, expected)
 def one_and_two_body_interaction(p, q, a, b) -> cirq.OP_TREE:
     tv_symbol = LetterWithSubscripts('Tv', i)
     t_symbol = LetterWithSubscripts('T', p, q, i)
     w_symbol = LetterWithSubscripts('W', p, q, i)
     v_symbol = LetterWithSubscripts('V', p, q, i)
     if custom_is_vertical_edge(p, q,
                                self.hubbard.lattice.x_dimension,
                                self.hubbard.lattice.y_dimension,
                                self.hubbard.lattice.periodic):
         yield cirq.ISwapPowGate(exponent=-tv_symbol).on(a, b)
     if t_symbol in param_set:
         yield cirq.ISwapPowGate(exponent=-t_symbol).on(a, b)
     if w_symbol in param_set:
         yield cirq.PhasedISwapPowGate(exponent=w_symbol).on(a, b)
     if v_symbol in param_set:
         yield cirq.CZPowGate(exponent=v_symbol).on(a, b)
Ejemplo n.º 21
0
def test_create_circuits_initial_single_particle(layout_type: type) -> None:
    layout = layout_type(size=4)
    parameters = _create_test_parameters(
        layout,
        initial_state=IndependentChainsInitialState(
            up=UniformSingleParticle(),
            down=PhasedGaussianSingleParticle(k=0.2, sigma=0.4, position=0.6)))

    initial, _, _ = create_circuits(parameters, trotter_steps=0)

    up1, up2, up3, up4 = layout.up_qubits
    down1, down2, down3, down4 = layout.down_qubits

    expected = cirq.Circuit([
        cirq.Moment(
            cirq.X(up2),
            cirq.X(down2),
        ),
        cirq.Moment(
            cirq.PhasedISwapPowGate(phase_exponent=0.25,
                                    exponent=-0.5).on(up2, up3),
            cirq.PhasedISwapPowGate(phase_exponent=0.25,
                                    exponent=-0.5550026943884815).on(
                                        down2, down3),
        ),
        cirq.Moment(
            cirq.PhasedISwapPowGate(phase_exponent=0.25,
                                    exponent=-0.5).on(up3, up4),
            cirq.PhasedISwapPowGate(phase_exponent=0.25,
                                    exponent=0.5).on(up1, up2),
            cirq.PhasedISwapPowGate(phase_exponent=0.25,
                                    exponent=-0.42338370832577876).on(
                                        down3, down4),
            cirq.PhasedISwapPowGate(phase_exponent=0.25,
                                    exponent=0.3609629722553269).on(
                                        down1, down2),
        ),
        cirq.Moment(
            (cirq.Z**0.0).on(up3),
            (cirq.Z**0.0).on(up4),
            (cirq.Z**0.0).on(up1),
            (cirq.Z**0.0).on(up2),
            (cirq.Z**0.004244131815783875).on(down3),
            (cirq.Z**0.02546479089470326).on(down4),
            (cirq.Z**-0.03819718634205488).on(down1),
            (cirq.Z**-0.016976527263135508).on(down2),
        ),
    ])

    assert cirq.approx_eq(initial, expected)
Ejemplo n.º 22
0
    def test_cirq_qsim_all_supported_gates(self):
        q0 = cirq.GridQubit(1, 1)
        q1 = cirq.GridQubit(1, 0)
        q2 = cirq.GridQubit(0, 1)
        q3 = cirq.GridQubit(0, 0)

        circuit = cirq.Circuit(
            cirq.Moment([
                cirq.H(q0),
                cirq.H(q1),
                cirq.H(q2),
                cirq.H(q3),
            ]),
            cirq.Moment([
                cirq.T(q0),
                cirq.T(q1),
                cirq.T(q2),
                cirq.T(q3),
            ]),
            cirq.Moment([
                cirq.CZPowGate(exponent=0.7, global_shift=0.2)(q0, q1),
                cirq.CXPowGate(exponent=1.2, global_shift=0.4)(q2, q3),
            ]),
            cirq.Moment([
                cirq.XPowGate(exponent=0.3, global_shift=1.1)(q0),
                cirq.YPowGate(exponent=0.4, global_shift=1)(q1),
                cirq.ZPowGate(exponent=0.5, global_shift=0.9)(q2),
                cirq.HPowGate(exponent=0.6, global_shift=0.8)(q3),
            ]),
            cirq.Moment([
                cirq.CX(q0, q2),
                cirq.CZ(q1, q3),
            ]),
            cirq.Moment([
                cirq.X(q0),
                cirq.Y(q1),
                cirq.Z(q2),
                cirq.S(q3),
            ]),
            cirq.Moment([
                cirq.XXPowGate(exponent=0.4, global_shift=0.7)(q0, q1),
                cirq.YYPowGate(exponent=0.8, global_shift=0.5)(q2, q3),
            ]),
            cirq.Moment([cirq.I(q0),
                         cirq.I(q1),
                         cirq.IdentityGate(2)(q2, q3)]),
            cirq.Moment([
                cirq.rx(0.7)(q0),
                cirq.ry(0.2)(q1),
                cirq.rz(0.4)(q2),
                cirq.PhasedXPowGate(phase_exponent=0.8,
                                    exponent=0.6,
                                    global_shift=0.3)(q3),
            ]),
            cirq.Moment([
                cirq.ZZPowGate(exponent=0.3, global_shift=1.3)(q0, q2),
                cirq.ISwapPowGate(exponent=0.6, global_shift=1.2)(q1, q3),
            ]),
            cirq.Moment([
                cirq.XPowGate(exponent=0.1, global_shift=0.9)(q0),
                cirq.YPowGate(exponent=0.2, global_shift=1)(q1),
                cirq.ZPowGate(exponent=0.3, global_shift=1.1)(q2),
                cirq.HPowGate(exponent=0.4, global_shift=1.2)(q3),
            ]),
            cirq.Moment([
                cirq.SwapPowGate(exponent=0.2, global_shift=0.9)(q0, q1),
                cirq.PhasedISwapPowGate(phase_exponent=0.8, exponent=0.6)(q2,
                                                                          q3),
            ]),
            cirq.Moment([
                cirq.PhasedXZGate(x_exponent=0.2,
                                  z_exponent=0.3,
                                  axis_phase_exponent=1.4)(q0),
                cirq.T(q1),
                cirq.H(q2),
                cirq.S(q3),
            ]),
            cirq.Moment([
                cirq.SWAP(q0, q2),
                cirq.XX(q1, q3),
            ]),
            cirq.Moment([
                cirq.rx(0.8)(q0),
                cirq.ry(0.9)(q1),
                cirq.rz(1.2)(q2),
                cirq.T(q3),
            ]),
            cirq.Moment([
                cirq.YY(q0, q1),
                cirq.ISWAP(q2, q3),
            ]),
            cirq.Moment([
                cirq.T(q0),
                cirq.Z(q1),
                cirq.Y(q2),
                cirq.X(q3),
            ]),
            cirq.Moment([
                cirq.FSimGate(0.3, 1.7)(q0, q2),
                cirq.ZZ(q1, q3),
            ]),
            cirq.Moment([
                cirq.ry(1.3)(q0),
                cirq.rz(0.4)(q1),
                cirq.rx(0.7)(q2),
                cirq.S(q3),
            ]),
            cirq.Moment([
                cirq.MatrixGate(
                    np.array([[0, -0.5 - 0.5j, -0.5 - 0.5j, 0],
                              [0.5 - 0.5j, 0, 0, -0.5 + 0.5j],
                              [0.5 - 0.5j, 0, 0, 0.5 - 0.5j],
                              [0, -0.5 - 0.5j, 0.5 + 0.5j, 0]]))(q0, q1),
                cirq.MatrixGate(
                    np.array([[0.5 - 0.5j, 0, 0, -0.5 + 0.5j],
                              [0, 0.5 - 0.5j, -0.5 + 0.5j, 0],
                              [0, -0.5 + 0.5j, -0.5 + 0.5j, 0],
                              [0.5 - 0.5j, 0, 0, 0.5 - 0.5j]]))(q2, q3),
            ]),
            cirq.Moment([
                cirq.MatrixGate(np.array([[1, 0], [0, 1j]]))(q0),
                cirq.MatrixGate(np.array([[0, -1j], [1j, 0]]))(q1),
                cirq.MatrixGate(np.array([[0, 1], [1, 0]]))(q2),
                cirq.MatrixGate(np.array([[1, 0], [0, -1]]))(q3),
            ]),
            cirq.Moment([
                cirq.riswap(0.7)(q0, q1),
                cirq.givens(1.2)(q2, q3),
            ]),
            cirq.Moment([
                cirq.H(q0),
                cirq.H(q1),
                cirq.H(q2),
                cirq.H(q3),
            ]),
        )

        simulator = cirq.Simulator()
        cirq_result = simulator.simulate(circuit)

        qsim_simulator = qsimcirq.QSimSimulator()
        qsim_result = qsim_simulator.simulate(circuit)

        assert cirq.linalg.allclose_up_to_global_phase(
            qsim_result.state_vector(), cirq_result.state_vector())
Ejemplo n.º 23
0
def test_phased_iswap_str():
    assert str(cirq.PhasedISwapPowGate(exponent=1)) == 'PhasedISWAP'
    assert str(cirq.PhasedISwapPowGate(exponent=0.5)) == 'PhasedISWAP**0.5'
    assert (str(cirq.PhasedISwapPowGate(
        exponent=0.5,
        global_shift=0.5)) == 'PhasedISWAP(exponent=0.5, global_shift=0.5)')
Ejemplo n.º 24
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
Ejemplo n.º 25
0
def test_serialize_not_serializable():
    q0, q1 = cirq.LineQubit.range(2)
    serializer = ionq.Serializer()
    circuit = cirq.Circuit(cirq.PhasedISwapPowGate()(q0, q1))
    with pytest.raises(ValueError, match='PhasedISWAP'):
        _ = serializer.serialize(circuit)
Ejemplo n.º 26
0
def test_phased_iswap_init():
    p = -0.25
    t = 0.75
    gate = cirq.PhasedISwapPowGate(phase_exponent=p, exponent=t)
    assert gate.phase_exponent == p
    assert gate.exponent == t
Ejemplo n.º 27
0
def test_phased_iswap_equality():
    assert (cirq.PhasedISwapPowGate(phase_exponent=0,
                                    exponent=0.4) == cirq.ISWAP**0.4)
Ejemplo n.º 28
0
def test_decompose_invalid_qubits():
    qs = cirq.LineQubit.range(3)
    with pytest.raises(ValueError):
        cirq.protocols.decompose_once_with_qubits(cirq.PhasedISwapPowGate(),
                                                  qs)
Ejemplo n.º 29
0
def test_repr():
    p = -0.25
    t = 0.75
    gate = cirq.PhasedISwapPowGate(phase_exponent=p, exponent=t)
    cirq.testing.assert_equivalent_repr(gate)
Ejemplo n.º 30
0
def test_phased_iswap_str():
    assert str(cirq.PhasedISwapPowGate(exponent=1)) == 'PhasedISWAP'
    assert str(cirq.PhasedISwapPowGate(exponent=0.5)) == 'PhasedISWAP**0.5'