Пример #1
0
def test_z_proto_dict_convert():
    gate = cg.ExpZGate(half_turns=cirq.Symbol('k'))
    proto_dict = {
        'exp_z': {
            'target': {
                'row': 2,
                'col': 3
            },
            'half_turns': {
                'parameter_key': 'k'
            }
        }
    }
    assert_proto_dict_convert(cg.ExpZGate, gate, proto_dict,
                              cirq.GridQubit(2, 3))

    gate = cg.ExpZGate(half_turns=0.5)
    proto_dict = {
        'exp_z': {
            'target': {
                'row': 2,
                'col': 3
            },
            'half_turns': {
                'raw': 0.5
            }
        }
    }
    assert_proto_dict_convert(cg.ExpZGate, gate, proto_dict,
                              cirq.GridQubit(2, 3))
Пример #2
0
def test_z_to_proto():
    assert proto_matches_text(
        cg.ExpZGate(half_turns=Symbol('k')).to_proto(GridQubit(2, 3)), """
        exp_z {
            target {
                row: 2
                col: 3
            }
            half_turns {
                parameter_key: "k"
            }
        }
        """)

    assert proto_matches_text(
        cg.ExpZGate(half_turns=0.5).to_proto(GridQubit(2, 3)), """
        exp_z {
            target {
                row: 2
                col: 3
            }
            half_turns {
                raw: 0.5
            }
        }
        """)
Пример #3
0
def test_z_parameterize():
    parameterized_gate = cg.ExpZGate(half_turns=cirq.Symbol('a'))
    assert parameterized_gate.is_parameterized()
    assert cirq.unitary(parameterized_gate, None) is None
    resolver = cirq.ParamResolver({'a': 0.1})
    resolved_gate = parameterized_gate.with_parameters_resolved_by(resolver)
    assert resolved_gate == cg.ExpZGate(half_turns=0.1)
Пример #4
0
def test_z_parameterize():
    parameterized_gate = cg.ExpZGate(half_turns=Symbol('a'))
    assert parameterized_gate.is_parameterized()
    with pytest.raises(ValueError):
        _ = parameterized_gate.matrix()
    resolver = ParamResolver({'a': 0.1})
    resolved_gate = parameterized_gate.with_parameters_resolved_by(resolver)
    assert resolved_gate == cg.ExpZGate(half_turns=0.1)
Пример #5
0
def test_z_matrix():
    assert np.allclose(cirq.unitary(cg.ExpZGate(half_turns=1)),
                       np.array([[-1j, 0], [0, 1j]]))
    assert np.allclose(cirq.unitary(cg.ExpZGate(half_turns=0.5)),
                       np.array([[1 - 1j, 0], [0, 1 + 1j]]) / np.sqrt(2))
    assert np.allclose(cirq.unitary(cg.ExpZGate(half_turns=0)),
                       np.array([[1, 0], [0, 1]]))
    assert np.allclose(cirq.unitary(cg.ExpZGate(half_turns=-0.5)),
                       np.array([[1 + 1j, 0], [0, 1 - 1j]]) / np.sqrt(2))
Пример #6
0
def test_symbols_block():
    q = cirq.NamedQubit('q')
    assert_optimizes(before=cirq.Circuit([
        cirq.Moment([cg.ExpZGate(half_turns=1)(q)]),
        cirq.Moment([cg.ExpZGate(half_turns=cirq.Symbol('a'))(q)]),
        cirq.Moment([cg.ExpZGate(half_turns=0.25)(q)]),
    ]),
                     expected=cirq.Circuit([
                         cirq.Moment(
                             [cg.ExpZGate(half_turns=cirq.Symbol('a'))(q)]),
                         cirq.Moment([cg.ExpZGate(half_turns=1.25)(q)]),
                     ]))
Пример #7
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)],
        ))
Пример #8
0
def test_z_eq():
    eq = cirq.testing.EqualsTester()
    eq.make_equality_group(lambda: cg.ExpZGate(half_turns=0))
    eq.add_equality_group(cg.ExpZGate(), cg.ExpZGate(half_turns=1),
                          cg.ExpZGate(degs=180), cg.ExpZGate(rads=np.pi))
    eq.make_equality_group(lambda: cg.ExpZGate(half_turns=cirq.Symbol('a')))
    eq.make_equality_group(lambda: cg.ExpZGate(half_turns=cirq.Symbol('b')))
    eq.add_equality_group(cg.ExpZGate(half_turns=-1.5),
                          cg.ExpZGate(half_turns=10.5))
Пример #9
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),
            cg.ExpZGate(half_turns=0.5).on(a),
            cg.ExpZGate(half_turns=0.1).on(b), cirq.measure(a, b)),
        atol=1e-8)
Пример #10
0
def test_ignores_czs_separated_by_parameterized():
    a, b = cirq.LineQubit.range(2)
    assert_optimizes(before=cirq.Circuit([
        cirq.Moment([cirq.CZ(a, b)]),
        cirq.Moment([cg.ExpZGate(half_turns=cirq.Symbol('boo'))(a)]),
        cirq.Moment([cirq.CZ(a, b)]),
    ]),
                     expected=cirq.Circuit([
                         cirq.Moment([cirq.CZ(a, b)]),
                         cirq.Moment(
                             [cg.ExpZGate(half_turns=cirq.Symbol('boo'))(a)]),
                         cirq.Moment([cirq.CZ(a, b)]),
                     ]))
Пример #11
0
def test_validate_operation_existing_qubits():
    d = square_device(3, 3, holes=[cirq.GridQubit(1, 1)])

    d.validate_operation(cirq.GateOperation(
        cg.Exp11Gate(),
        (cirq.GridQubit(0, 0), cirq.GridQubit(1, 0))))
    d.validate_operation(cg.ExpZGate().on(cirq.GridQubit(0, 0)))

    with pytest.raises(ValueError):
        d.validate_operation(
            cg.Exp11Gate().on(cirq.GridQubit(0, 0), cirq.GridQubit(-1, 0)))
    with pytest.raises(ValueError):
        d.validate_operation(cg.ExpZGate().on(cirq.GridQubit(-1, 0)))
    with pytest.raises(ValueError):
        d.validate_operation(
            cg.Exp11Gate().on(cirq.GridQubit(1, 0), cirq.GridQubit(1, 1)))
Пример #12
0
def test_expz_matrix(half_turns):
    if (version[0] == 0 and version[1] <= 3):
        nptest.assert_array_almost_equal(xmon_gates.ExpZGate(half_turns=half_turns).matrix,
                                google.ExpZGate(half_turns=half_turns).matrix())
    else:
        nptest.assert_array_almost_equal(xmon_gates.ExpZGate(half_turns=half_turns).matrix,
                                unitary(ops.ZPowGate(exponent=half_turns,
                                                     global_shift=half_turns)))
Пример #13
0
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]])
Пример #14
0
def test_handedness_of_xmon_exp_z_gate():
    circuit = cirq.Circuit.from_ops(cirq.H(Q1),
                                    cg.ExpZGate(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)
Пример #15
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))
Пример #16
0
def test_param_resolver_exp_z_half_turns():
    exp_z = cg.ExpZGate(half_turns=cirq.Symbol('a'))
    circuit = cirq.Circuit()
    circuit.append(exp_z(Q1))
    resolver = cirq.ParamResolver({'a': -0.5})
    result = compute_gate(circuit, resolver)
    np.testing.assert_almost_equal(
        result,
        np.array([[cmath.exp(1j * math.pi * 0.25), 0],
                  [0, cmath.exp(-1j * math.pi * 0.25)]]))
Пример #17
0
def test_cancels_other_full_w():
    q = cirq.NamedQubit('q')

    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate(axis_half_turns=0.25).on(q)],
            [cg.ExpWGate(axis_half_turns=0.25).on(q)],
        ),
        expected=quick_circuit(
            [],
            [],
        ))

    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate(axis_half_turns=0.25).on(q)],
            [cg.ExpWGate(axis_half_turns=0.125).on(q)],
        ),
        expected=quick_circuit(
            [],
            [cg.ExpZGate(half_turns=-0.25).on(q)],
        ))

    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate().on(q)],
            [cg.ExpWGate(axis_half_turns=0.25).on(q)],
        ),
        expected=quick_circuit(
            [],
            [cg.ExpZGate(half_turns=0.5).on(q)],
        ))

    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate(axis_half_turns=0.25).on(q)],
            [cg.ExpWGate().on(q)],
        ),
        expected=quick_circuit(
            [],
            [cg.ExpZGate(half_turns=-0.5).on(q)],
        ))
Пример #18
0
def test_circuit_device_validation_fails(build):
    circuit = cirq.Circuit(device=cg.Foxtail)

    # Purposefully create an invalid Circuit by fiddling with internal bits.
    # This simulates a failure in the incremental checks.
    circuit._moments.append(
        cirq.Moment([cg.ExpZGate().on(cirq.NamedQubit("dorothy"))]))

    with pytest.raises(ValueError, match='Unsupported qubit type'):
        cg.Engine(api_key="key").run(program=circuit,
                                     job_config=cg.JobConfig('project-id'))
Пример #19
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
Пример #20
0
def test_blocked_by_unknown_and_symbols():
    a = cirq.NamedQubit('a')
    b = cirq.NamedQubit('b')

    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate().on(a)],
            [cirq.SWAP(a, b)],
            [cg.ExpWGate().on(a)],
        ),
        expected=quick_circuit(
            [cg.ExpWGate().on(a)],
            [cirq.SWAP(a, b)],
            [cg.ExpWGate().on(a)],
        ))

    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate().on(a)],
            [cg.ExpZGate(half_turns=cirq.Symbol('z')).on(a)],
            [cg.ExpWGate().on(a)],
        ),
        expected=quick_circuit(
            [cg.ExpWGate().on(a)],
            [cg.ExpZGate(half_turns=cirq.Symbol('z')).on(a)],
            [cg.ExpWGate().on(a)],
        ),
        compare_unitaries=False)

    assert_optimizes(
        before=quick_circuit(
            [cg.ExpWGate().on(a)],
            [cg.Exp11Gate(half_turns=cirq.Symbol('z')).on(a, b)],
            [cg.ExpWGate().on(a)],
        ),
        expected=quick_circuit(
            [cg.ExpWGate().on(a)],
            [cg.Exp11Gate(half_turns=cirq.Symbol('z')).on(a, b)],
            [cg.ExpWGate().on(a)],
        ),
        compare_unitaries=False)
Пример #21
0
def test_validate_scheduled_operation_adjacent_exp_11_exp_z():
    d = square_device(3, 3, holes=[cirq.GridQubit(1, 1)])
    q0 = cirq.GridQubit(0, 0)
    q1 = cirq.GridQubit(1, 0)
    q2 = cirq.GridQubit(2, 0)
    s = cirq.Schedule(d, [
        cirq.ScheduledOperation.op_at_on(
            cg.ExpZGate().on(q0), cirq.Timestamp(), d),
        cirq.ScheduledOperation.op_at_on(
            cg.Exp11Gate().on(q1, q2), cirq.Timestamp(), d),
    ])
    d.validate_schedule(s)
Пример #22
0
def test_validate_operation_supported_gate():
    d = square_device(3, 3)

    class MyGate(cirq.Gate):
        pass

    d.validate_operation(cirq.GateOperation(cg.ExpZGate(),
                                            [cirq.GridQubit(0, 0)]))
    with pytest.raises(ValueError):
        d.validate_operation(cirq.GateOperation(MyGate, [cirq.GridQubit(0, 0)]))
    with pytest.raises(ValueError):
        d.validate_operation(NotImplementedOperation())
Пример #23
0
def test_unknown_operation_blocks():
    q = cirq.NamedQubit('q')

    class UnknownOp(cirq.Operation):
        @property
        def qubits(self):
            return [q]

        def with_qubits(self, *new_qubits):
            raise NotImplementedError()

    u = UnknownOp()

    assert_optimizes(before=cirq.Circuit([
        cirq.Moment([cg.ExpZGate(half_turns=1)(q)]),
        cirq.Moment([u]),
    ]),
                     expected=cirq.Circuit([
                         cirq.Moment([cg.ExpZGate(half_turns=1)(q)]),
                         cirq.Moment([u]),
                     ]))
Пример #24
0
def test_cirq_symbol_diagrams():
    q00 = cirq.GridQubit(0, 0)
    q01 = cirq.GridQubit(0, 1)
    c = cirq.Circuit.from_ops(
        cg.ExpWGate(axis_half_turns=cirq.Symbol('a'),
                    half_turns=cirq.Symbol('b')).on(q00),
        cg.ExpZGate(half_turns=cirq.Symbol('c')).on(q01),
        cg.Exp11Gate(half_turns=cirq.Symbol('d')).on(q00, q01),
    )
    assert c.to_text_diagram() == """
(0, 0): ───W(a)^b───@─────
                    │
(0, 1): ───Z^c──────@^d───
    """.strip()
Пример #25
0
def test_init():
    d = square_device(2, 2, holes=[cirq.GridQubit(1, 1)])
    ns = cirq.Duration(nanos=1)
    q00 = cirq.GridQubit(0, 0)
    q01 = cirq.GridQubit(0, 1)
    q10 = cirq.GridQubit(1, 0)

    assert d.qubits == {q00, q01, q10}
    assert d.duration_of(cg.ExpZGate().on(q00)) == 0 * ns
    assert d.duration_of(cirq.measure(q00)) == ns
    assert d.duration_of(cirq.measure(q00, q01)) == ns
    assert d.duration_of(cg.ExpWGate().on(q00)) == 2 * ns
    assert d.duration_of(cg.Exp11Gate().on(q00, q01)) == 3 * ns
    with pytest.raises(ValueError):
        _ = d.duration_of(cirq.Gate().on(q00))
Пример #26
0
def test_z_diagram_chars():
    q = cirq.GridQubit(0, 1)
    c = cirq.Circuit.from_ops(
        cg.ExpZGate().on(q),
        cg.ExpZGate(half_turns=0.5).on(q),
        cg.ExpZGate(half_turns=0.25).on(q),
        cg.ExpZGate(half_turns=0.125).on(q),
        cg.ExpZGate(half_turns=-0.5).on(q),
        cg.ExpZGate(half_turns=-0.25).on(q),
    )
    assert c.to_text_diagram() == """
(0, 1): ───Z───S───T───Z^0.125───S^-1───T^-1───
    """.strip()
Пример #27
0
def test_z_diagram_chars():
    q = cirq.GridQubit(0, 1)
    c = cirq.Circuit.from_ops(
        cg.ExpZGate().on(q),
        cg.ExpZGate(half_turns=0.5).on(q),
        cg.ExpZGate(half_turns=0.25).on(q),
        cg.ExpZGate(half_turns=0.125).on(q),
        cg.ExpZGate(half_turns=-0.5).on(q),
        cg.ExpZGate(half_turns=-0.25).on(q),
    )
    cirq.testing.assert_has_diagram(
        c, """
(0, 1): ───Z───S───T───Z^0.125───S^-1───T^-1───
""")
Пример #28
0
def test_trace_bound():
    assert cg.ExpZGate(half_turns=.001).trace_distance_bound() < 0.01
    assert cg.ExpWGate(half_turns=.001).trace_distance_bound() < 0.01
    assert cg.ExpZGate(half_turns=cirq.Symbol('a')).trace_distance_bound() >= 1
    assert cg.ExpWGate(half_turns=cirq.Symbol('a')).trace_distance_bound() >= 1
Пример #29
0
def test_z_repr():
    gate = cg.ExpZGate(half_turns=0.1)
    assert repr(gate) == 'ExpZGate(half_turns=0.1)'
Пример #30
0
def test_has_inverse():
    assert cg.ExpZGate(half_turns=.1).has_inverse()
    assert cg.ExpWGate(half_turns=.1).has_inverse()
    assert not cg.ExpZGate(half_turns=cirq.Symbol('a')).has_inverse()
    assert not cg.ExpWGate(half_turns=cirq.Symbol('a')).has_inverse()