def test_param_resolver_param_dict():
    exp_w = cg.ExpWGate(half_turns=cirq.Symbol('a'), axis_half_turns=0.0)
    circuit = cirq.Circuit()
    circuit.append(exp_w(Q1))
    resolver = cirq.ParamResolver({'a': 0.5})

    simulator = cg.XmonSimulator()
    result = simulator.run(circuit, resolver)
    assert result.params.param_dict == {'a': 0.5}
Exemple #2
0
def test_w_to_proto_dict():
    gate = cg.ExpWGate(exponent=cirq.Symbol('k'), phase_exponent=1)
    proto_dict = {
        'exp_w': {
            'target': {
                'row': 2,
                'col': 3
            },
            'axis_half_turns': {
                'raw': 1
            },
            'half_turns': {
                'parameter_key': 'k'
            }
        }
    }
    assert_proto_dict_convert(gate, proto_dict, cirq.GridQubit(2, 3))

    gate = cg.ExpWGate(exponent=0.5, phase_exponent=cirq.Symbol('j'))
    proto_dict = {
        'exp_w': {
            'target': {
                'row': 2,
                'col': 3
            },
            'axis_half_turns': {
                'parameter_key': 'j'
            },
            'half_turns': {
                'raw': 0.5
            }
        }
    }
    assert_proto_dict_convert(gate, proto_dict, cirq.GridQubit(2, 3))
Exemple #3
0
def test_w_str():
    assert str(cg.ExpWGate()) == 'X'
    assert str(cg.ExpWGate(axis_half_turns=0.99999,
                           half_turns=0.5)) == 'X^-0.5'
    assert str(cg.ExpWGate(axis_half_turns=0.5, half_turns=0.25)) == 'Y^0.25'
    assert str(cg.ExpWGate(axis_half_turns=0.25,
                           half_turns=0.5)) == 'W(0.25)^0.5'
Exemple #4
0
def test_w_potential_implementation():
    assert not cirq.can_cast(cirq.KnownMatrix,
                             cg.ExpWGate(half_turns=cirq.Symbol('a')))
    assert not cirq.can_cast(cirq.ReversibleEffect,
                             cg.ExpWGate(half_turns=cirq.Symbol('a')))
    assert cirq.can_cast(cirq.KnownMatrix, cg.ExpWGate())
    assert cirq.can_cast(cirq.ReversibleEffect, cg.ExpWGate())
Exemple #5
0
def test_w_to_proto():
    assert proto_matches_text(
        cg.ExpWGate(half_turns=Symbol('k'),
                    axis_half_turns=1).to_proto(GridQubit(2, 3)), """
        exp_w {
            target {
                row: 2
                col: 3
            }
            axis_half_turns {
                raw: 1
            }
            half_turns {
                parameter_key: "k"
            }
        }
        """)

    assert proto_matches_text(
        cg.ExpWGate(half_turns=0.5,
                    axis_half_turns=Symbol('j')).to_proto(GridQubit(2, 3)), """
        exp_w {
            target {
                row: 2
                col: 3
            }
            axis_half_turns {
                parameter_key: "j"
            }
            half_turns {
                raw: 0.5
            }
        }
        """)
Exemple #6
0
def bit_flip_circuit(flip0, flip1):
    q1, q2 = cirq.GridQubit(0, 0), cirq.GridQubit(0, 1)
    g1, g2 = cg.ExpWGate(exponent=flip0)(q1), cg.ExpWGate(exponent=flip1)(q2)
    m1, m2 = cirq.MeasurementGate('q1')(q1), cirq.MeasurementGate('q2')(q2)
    circuit = cirq.Circuit()
    circuit.append([g1, g2, m1, m2])
    return circuit
Exemple #7
0
def test_w_parameterize():
    parameterized_gate = cg.ExpWGate(half_turns=cirq.Symbol('a'),
                                     axis_half_turns=cirq.Symbol('b'))
    assert parameterized_gate.is_parameterized()
    assert cirq.unitary(parameterized_gate, None) is None
    resolver = cirq.ParamResolver({'a': 0.1, 'b': 0.2})
    resolved_gate = parameterized_gate.with_parameters_resolved_by(resolver)
    assert resolved_gate == cg.ExpWGate(half_turns=0.1, axis_half_turns=0.2)
Exemple #8
0
def test_w_parameterize():
    parameterized_gate = cg.ExpWGate(exponent=cirq.Symbol('a'),
                                     phase_exponent=cirq.Symbol('b'))
    assert cirq.is_parameterized(parameterized_gate)
    assert not cirq.has_unitary(parameterized_gate)
    assert cirq.unitary(parameterized_gate, None) is None
    resolver = cirq.ParamResolver({'a': 0.1, 'b': 0.2})
    resolved_gate = cirq.resolve_parameters(parameterized_gate, resolver)
    assert resolved_gate == cg.ExpWGate(exponent=0.1, phase_exponent=0.2)
def test_handedness_of_xmon_gates():
    circuit = cirq.Circuit.from_ops(
        cg.ExpWGate(half_turns=-0.5).on(Q1),
        cg.ExpZGate(half_turns=-0.5).on(Q1),
        cg.ExpWGate(axis_half_turns=0.5, half_turns=0.5).on(Q1),
        cg.XmonMeasurementGate(key='').on(Q1),
    )
    result = cg.XmonSimulator().run(circuit)
    np.testing.assert_equal(result.measurements[''], [[True]])
Exemple #10
0
def test_handedness_of_xmon_gates():
    circuit = cirq.Circuit.from_ops(
        cg.ExpWGate(exponent=-0.5).on(Q1),
        cirq.Z(Q1)**-0.5,
        cg.ExpWGate(phase_exponent=0.5, exponent=0.5).on(Q1),
        cirq.MeasurementGate(key='').on(Q1),
    )
    result = cg.XmonSimulator().run(circuit)
    np.testing.assert_equal(result.measurements[''], [[True]])
Exemple #11
0
def test_w_parameterize():
    parameterized_gate = cg.ExpWGate(half_turns=Symbol('a'),
                                     axis_half_turns=Symbol('b'))
    assert parameterized_gate.is_parameterized()
    with pytest.raises(ValueError):
        _ = parameterized_gate.matrix()
    resolver = ParamResolver({'a': 0.1, 'b': 0.2})
    resolved_gate = parameterized_gate.with_parameters_resolved_by(resolver)
    assert resolved_gate == cg.ExpWGate(half_turns=0.1, axis_half_turns=0.2)
Exemple #12
0
def test_w_diagram_chars():
    q = cirq.GridQubit(0, 1)
    c = cirq.Circuit.from_ops(
        cg.ExpWGate(phase_exponent=0).on(q),
        cg.ExpWGate(phase_exponent=0.25).on(q),
        cg.ExpWGate(phase_exponent=0.5).on(q),
        cg.ExpWGate(phase_exponent=cirq.Symbol('a')).on(q),
    )
    cirq.testing.assert_has_diagram(c, """
(0, 1): ───X───W(0.25)───Y───W(a)───
""")
Exemple #13
0
def test_w_diagram_chars():
    q = cirq.GridQubit(0, 1)
    c = cirq.Circuit.from_ops(
        cg.ExpWGate(axis_half_turns=0).on(q),
        cg.ExpWGate(axis_half_turns=0.25).on(q),
        cg.ExpWGate(axis_half_turns=0.5).on(q),
        cg.ExpWGate(axis_half_turns=cirq.Symbol('a')).on(q),
    )
    assert c.to_text_diagram() == """
(0, 1): ───X───W(0.25)───Y───W(a)───
    """.strip()
Exemple #14
0
def test_crosses_czs():
    a = cirq.NamedQubit('a')
    b = cirq.NamedQubit('b')

    # Full CZ.
    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate(axis_half_turns=0.25).on(a)],
            [cirq.CZ(a, b)],
        ),
        expected=quick_circuit(
            [cg.ExpZGate().on(b)],
            [cg.Exp11Gate().on(a, b)],
            [cg.ExpWGate(axis_half_turns=0.25).on(a)],
        ))
    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate(axis_half_turns=0.125).on(a)],
            [cirq.CZ(b, a)],
        ),
        expected=quick_circuit(
            [cg.ExpZGate().on(b)],
            [cg.Exp11Gate().on(a, b)],
            [cg.ExpWGate(axis_half_turns=0.125).on(a)],
        ))

    # Partial CZ.
    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate().on(a)],
            [cirq.CZ(a, b)**0.25],
        ),
        expected=quick_circuit(
            [cg.ExpZGate(half_turns=0.25).on(b)],
            [cg.Exp11Gate(half_turns=-0.25).on(a, b)],
            [cg.ExpWGate().on(a)],
        ))

    # Double cross.
    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate(axis_half_turns=0.125).on(a)],
            [cg.ExpWGate(axis_half_turns=0.375).on(b)],
            [cirq.CZ(a, b)**0.25],
        ),
        expected=quick_circuit(
            [],
            [],
            [cirq.CZ(a, b)**0.25],
            [cg.ExpWGate(axis_half_turns=0.25).on(a),
             cg.ExpWGate(axis_half_turns=0.5).on(b)],
        ))
Exemple #15
0
def test_known_old_failure():
    a, b = cirq.LineQubit.range(2)
    cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent(
        actual=cirq.Circuit.from_ops(
            cg.ExpWGate(half_turns=0.61351656,
                        axis_half_turns=0.8034575038876517).on(b),
            cirq.measure(a, b)),
        reference=cirq.Circuit.from_ops(
            cg.ExpWGate(half_turns=0.61351656,
                        axis_half_turns=0.8034575038876517).on(b),
            cirq.Z(a)**0.5,
            cirq.Z(b)**0.1, cirq.measure(a, b)),
        atol=1e-8)
Exemple #16
0
def test_crosses_czs():
    a = cirq.NamedQubit('a')
    b = cirq.NamedQubit('b')

    # Full CZ.
    assert_optimizes(before=quick_circuit(
        [cg.ExpWGate(phase_exponent=0.25).on(a)],
        [cirq.CZ(a, b)],
    ),
                     expected=quick_circuit(
                         [cirq.Z(b)],
                         [cirq.CZ(a, b)],
                         [cg.ExpWGate(phase_exponent=0.25).on(a)],
                     ))
    assert_optimizes(before=quick_circuit(
        [cg.ExpWGate(phase_exponent=0.125).on(a)],
        [cirq.CZ(b, a)],
    ),
                     expected=quick_circuit(
                         [cirq.Z(b)],
                         [cirq.CZ(a, b)],
                         [cg.ExpWGate(phase_exponent=0.125).on(a)],
                     ))

    # Partial CZ.
    assert_optimizes(before=quick_circuit(
        [cg.ExpWGate().on(a)],
        [cirq.CZ(a, b)**0.25],
    ),
                     expected=quick_circuit(
                         [cirq.Z(b)**0.25],
                         [cirq.CZ(a, b)**-0.25],
                         [cg.ExpWGate().on(a)],
                     ))

    # Double cross.
    assert_optimizes(before=quick_circuit(
        [cg.ExpWGate(phase_exponent=0.125).on(a)],
        [cg.ExpWGate(phase_exponent=0.375).on(b)],
        [cirq.CZ(a, b)**0.25],
    ),
                     expected=quick_circuit(
                         [],
                         [],
                         [cirq.CZ(a, b)**0.25],
                         [
                             cg.ExpWGate(phase_exponent=0.5).on(b),
                             cg.ExpWGate(phase_exponent=0.25).on(a)
                         ],
                     ))
Exemple #17
0
def test_unphaseable_causes_earlier_merge_without_size_increase():
    class UnknownGate(cirq.Gate):
        pass

    u = UnknownGate()

    # pylint: disable=not-callable
    q = cirq.NamedQubit('q')
    assert_optimizes(before=cirq.Circuit([
        cirq.Moment([cirq.Z(q)]),
        cirq.Moment([u(q)]),
        cirq.Moment([cirq.Z(q)**0.5]),
        cirq.Moment([cirq.X(q)]),
        cirq.Moment([cirq.Z(q)**0.25]),
        cirq.Moment([cirq.X(q)]),
        cirq.Moment([u(q)]),
    ]),
                     expected=cirq.Circuit([
                         cirq.Moment([cirq.Z(q)]),
                         cirq.Moment([u(q)]),
                         cirq.Moment(),
                         cirq.Moment([cirq.Y(q)]),
                         cirq.Moment([cg.ExpWGate(phase_exponent=0.25).on(q)]),
                         cirq.Moment([cirq.Z(q)**0.75]),
                         cirq.Moment([u(q)]),
                     ]))
def test_param_resolver_exp_w_axis_half_turns():
    exp_w = cg.ExpWGate(half_turns=1.0, axis_half_turns=cirq.Symbol('a'))
    circuit = cirq.Circuit()
    circuit.append(exp_w(Q1))
    resolver = cirq.ParamResolver({'a': 0.5})
    result = compute_gate(circuit, resolver)
    np.testing.assert_almost_equal(result, np.array([[0, -1], [1, 0]]))
Exemple #19
0
def test_toggles_measurements():
    a = cirq.NamedQubit('a')
    b = cirq.NamedQubit('b')

    # Single.
    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate(axis_half_turns=0.25).on(a)],
            [cirq.measure(a, b)],
        ),
        expected=quick_circuit(
            [],
            [cirq.measure(a, b, invert_mask=(True,))],
        ))
    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate(axis_half_turns=0.25).on(b)],
            [cirq.measure(a, b)],
        ),
        expected=quick_circuit(
            [],
            [cirq.measure(a, b, invert_mask=(False, True))],
        ))

    # Multiple.
    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate(axis_half_turns=0.25).on(a)],
            [cg.ExpWGate(axis_half_turns=0.25).on(b)],
            [cirq.measure(a, b)],
        ),
        expected=quick_circuit(
            [],
            [],
            [cirq.measure(a, b, invert_mask=(True, True))],
        ))

    # Xmon.
    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate(axis_half_turns=0.25).on(a)],
            [cg.XmonMeasurementGate(key='t').on(a, b)],
        ),
        expected=quick_circuit(
            [],
            [cg.XmonMeasurementGate(key='t', invert_mask=(True,)).on(a, b)],
        ))
def test_handedness_of_xmon_exp_x_gate():
    circuit = cirq.Circuit.from_ops(cg.ExpWGate(half_turns=0.5).on(Q1))
    simulator = cg.XmonSimulator()
    result = list(simulator.simulate_moment_steps(circuit))[-1]
    cirq.testing.assert_allclose_up_to_global_phase(result.state(),
                                                    np.array([1, -1j]) *
                                                    np.sqrt(0.5),
                                                    atol=1e-7)
Exemple #21
0
def _make_random_single_qubit_op_layer(
        device: cg.XmonDevice,
        randint: Callable[[int, int], int]) -> Iterable[cirq.Operation]:
    for q in device.qubits:
        angle = randint(0, 3) / 2
        axis = randint(0, 7) / 4
        if angle:
            yield cg.ExpWGate(half_turns=angle, axis_half_turns=axis).on(q)
Exemple #22
0
def test_param_resolver_exp_w_half_turns():
    exp_w = cg.ExpWGate(exponent=cirq.Symbol('a'), phase_exponent=0.0)
    circuit = cirq.Circuit()
    circuit.append(exp_w(Q1))
    resolver = cirq.ParamResolver({'a': -0.5})
    result = compute_gate(circuit, resolver)
    amp = 1.0 / math.sqrt(2)
    np.testing.assert_almost_equal(
        result, np.array([[amp, amp * 1j], [amp * 1j, amp]]))
Exemple #23
0
def test_invalid_to_proto_dict_qubit_number():
    with pytest.raises(ValueError, match='Wrong number of qubits'):
        _ = cg.gate_to_proto_dict(cirq.CZ**0.5, (cirq.GridQubit(2, 3),))
    with pytest.raises(ValueError, match='Wrong number of qubits'):
        cg.gate_to_proto_dict(cirq.Z**0.5, (cirq.GridQubit(2, 3),
                                            cirq.GridQubit(3, 4)))
    with pytest.raises(ValueError, match='Wrong number of qubits'):
        cg.ExpWGate(half_turns=0.5, axis_half_turns=0).to_proto_dict(
            cirq.GridQubit(2, 3), cirq.GridQubit(3, 4))
def test_param_resolver_exp_w_multiple_params():
    exp_w = cg.ExpWGate(half_turns=cirq.Symbol('a'),
                        axis_half_turns=cirq.Symbol('b'))
    circuit = cirq.Circuit()
    circuit.append(exp_w(Q1))
    resolver = cirq.ParamResolver({'a': -0.5, 'b': 0.5})
    result = compute_gate(circuit, resolver)
    amp = 1.0 / math.sqrt(2)
    np.testing.assert_almost_equal(result, np.array([[amp, amp], [-amp, amp]]))
Exemple #25
0
def test_expw_matrix(half_turns, axis_half_turns):
    m1 = xmon_gates.ExpWGate(half_turns=half_turns,
                                axis_half_turns=axis_half_turns).matrix
    if (version[0] == 0 and version[1] <= 3):
        m2 = google.ExpWGate(half_turns=half_turns,
                            axis_half_turns=axis_half_turns).matrix()
    else:
         m2 = unitary(ops.PhasedXPowGate(exponent=half_turns,
                            phase_exponent=axis_half_turns))
    nptest.assert_array_almost_equal(m1, m2)
Exemple #26
0
def test_invalid_to_proto_dict_qubit_number():
    with pytest.raises(ValueError):
        cg.Exp11Gate(half_turns=0.5).to_proto_dict(cirq.GridQubit(2, 3))
    with pytest.raises(ValueError):
        cg.ExpZGate(half_turns=0.5).to_proto_dict(cirq.GridQubit(2, 3),
                                                  cirq.GridQubit(3, 4))
    with pytest.raises(ValueError):
        cg.ExpWGate(half_turns=0.5,
                    axis_half_turns=0).to_proto_dict(cirq.GridQubit(2, 3),
                                                     cirq.GridQubit(3, 4))
Exemple #27
0
def large_circuit():
    np.random.seed(0)
    qubits = [cirq.GridQubit(i, 0) for i in range(10)]
    sqrt_x = cg.ExpWGate(half_turns=0.5, axis_half_turns=0.0)
    circuit = cirq.Circuit()
    for _ in range(11):
        circuit.append(
            [sqrt_x(qubit) for qubit in qubits if np.random.random() < 0.5])
        circuit.append([cirq.CZ(qubits[i], qubits[i + 1]) for i in range(9)])
    circuit.append([cirq.MeasurementGate(key='meas')(*qubits)])
    return circuit
Exemple #28
0
def test_validate_scheduled_operation_not_adjacent_exp_11_exp_w():
    d = square_device(3, 3, holes=[cirq.GridQubit(1, 1)])
    q0 = cirq.GridQubit(0, 0)
    p1 = cirq.GridQubit(1, 2)
    p2 = cirq.GridQubit(2, 2)
    s = cirq.Schedule(d, [
        cirq.ScheduledOperation.op_at_on(cg.ExpWGate().on(q0),
                                         cirq.Timestamp(), d),
        cirq.ScheduledOperation.op_at_on(cirq.CZ(p1, p2), cirq.Timestamp(), d),
    ])
    d.validate_schedule(s)
Exemple #29
0
def basic_circuit():
    sqrt_x = cg.ExpWGate(half_turns=-0.5, axis_half_turns=0.0)
    z = cg.ExpZGate()
    cz = cg.Exp11Gate()
    circuit = cirq.Circuit()
    circuit.append(
        [sqrt_x(Q1), sqrt_x(Q2),
         cz(Q1, Q2),
         sqrt_x(Q1), sqrt_x(Q2),
         z(Q1)])
    return circuit
Exemple #30
0
def test_w_decomposition():
    q = cirq.NamedQubit('q')
    cirq.testing.assert_allclose_up_to_global_phase(
        cirq.Circuit.from_ops(
            cg.ExpWGate(half_turns=0.25,
                        axis_half_turns=0.3).on(q)).to_unitary_matrix(),
        cirq.Circuit.from_ops(
            cirq.Z(q)**-0.3,
            cirq.X(q)**0.25,
            cirq.Z(q)**0.3).to_unitary_matrix(),
        atol=1e-8)