Esempio n. 1
0
def test_gate_protocols():
    for p in [1, 1j, -1]:
        cirq.testing.assert_implements_consistent_protocols(
            cirq.GlobalPhaseGate(p))

    np.testing.assert_allclose(cirq.unitary(cirq.GlobalPhaseGate(1j)),
                               np.array([[1j]]),
                               atol=1e-8)
Esempio n. 2
0
def test_gate_init():
    gate = cirq.GlobalPhaseGate(1j)
    assert gate.coefficient == 1j
    assert isinstance(gate.on(), cirq.GateOperation)
    assert gate.on().gate == gate
    assert cirq.has_stabilizer_effect(gate)

    with pytest.raises(ValueError, match='Coefficient is not unitary'):
        _ = cirq.GlobalPhaseGate(2)
    with pytest.raises(ValueError, match='Wrong number of qubits'):
        _ = gate.on(cirq.LineQubit(0))
Esempio n. 3
0
def test_gate_act_on_ch_form(phase):
    state = cirq.StabilizerStateChForm(0)
    args = cirq.StabilizerChFormSimulationState(
        qubits=[], prng=np.random.RandomState(), initial_state=state
    )
    cirq.act_on(cirq.GlobalPhaseGate(phase), args, qubits=(), allow_decompose=False)
    assert state.state_vector() == [[phase]]
Esempio n. 4
0
def test_parameterization():
    t = sympy.Symbol('t')
    gpt = cirq.GlobalPhaseGate(coefficient=t)
    assert cirq.is_parameterized(gpt)
    assert cirq.parameter_names(gpt) == {'t'}
    assert not cirq.has_unitary(gpt)
    assert gpt.coefficient == t
    assert (gpt**2).coefficient == t**2
Esempio n. 5
0
def test_gate_act_on_tableau(phase):
    original_tableau = cirq.CliffordTableau(0)
    args = cirq.ActOnCliffordTableauArgs(original_tableau.copy(),
                                         np.random.RandomState(), {})
    cirq.act_on(cirq.GlobalPhaseGate(phase),
                args,
                qubits=(),
                allow_decompose=False)
    assert args.tableau == original_tableau
Esempio n. 6
0
def test_gate_act_on_ch_form(phase):
    state = cirq.StabilizerStateChForm(0)
    args = cirq.ActOnStabilizerCHFormArgs(
        qubits=[],
        prng=np.random.RandomState(),
        log_of_measurement_results={},
        initial_state=state,
    )
    cirq.act_on(cirq.GlobalPhaseGate(phase),
                args,
                qubits=(),
                allow_decompose=False)
    assert state.state_vector() == [[phase]]
Esempio n. 7
0
def test_resolve_error(resolve_fn):
    t = sympy.Symbol('t')
    gpt = cirq.GlobalPhaseGate(coefficient=t)
    with pytest.raises(ValueError, match='Coefficient is not unitary'):
        resolve_fn(gpt, {'t': -2})
Esempio n. 8
0
def test_resolve(resolve_fn):
    t = sympy.Symbol('t')
    gpt = cirq.GlobalPhaseGate(coefficient=t)
    assert resolve_fn(gpt, {'t': -1}) == cirq.GlobalPhaseGate(coefficient=-1)
Esempio n. 9
0
def test_gate_global_phase_op_json_dict():
    assert cirq.GlobalPhaseGate(-1j)._json_dict_() == {'coefficient': -1j}
Esempio n. 10
0
def test_gate_op_repr():
    gate = cirq.GlobalPhaseGate(1j)
    cirq.testing.assert_equivalent_repr(gate.on())
Esempio n. 11
0
def test_gate_str():
    assert str(cirq.GlobalPhaseGate(1j)) == '1j'
def test_apply_gate():
    q0, q1 = cirq.LineQubit.range(2)
    state = Mock()
    args = cirq.StabilizerSimulationState(state=state, qubits=[q0, q1])

    assert args._strat_apply_gate(cirq.X, [q0]) is True
    state.apply_x.assert_called_with(0, 1.0, 0.0)

    state.reset_mock()
    assert args._strat_apply_gate(cirq.X**2, [q0]) is True
    state.apply_x.assert_called_with(0, 2.0, 0.0)

    state.reset_mock()
    assert args._strat_apply_gate(cirq.X**sympy.Symbol('t'),
                                  [q0]) is NotImplemented
    state.apply_x.assert_not_called()

    state.reset_mock()
    assert args._strat_apply_gate(cirq.XPowGate(exponent=2, global_shift=1.3),
                                  [q1]) is True
    state.apply_x.assert_called_with(1, 2.0, 1.3)

    state.reset_mock()
    assert args._strat_apply_gate(cirq.X**1.4, [q0]) == NotImplemented
    state.apply_x.assert_not_called()

    state.reset_mock()
    assert args._strat_apply_gate(cirq.Y, [q0]) is True
    state.apply_y.assert_called_with(0, 1.0, 0.0)

    state.reset_mock()
    assert args._strat_apply_gate(cirq.Z, [q0]) is True
    state.apply_z.assert_called_with(0, 1.0, 0.0)

    state.reset_mock()
    assert args._strat_apply_gate(cirq.H, [q0]) is True
    state.apply_h.assert_called_with(0, 1.0, 0.0)

    state.reset_mock()
    assert args._strat_apply_gate(cirq.CX, [q0, q1]) is True
    state.apply_cx.assert_called_with(0, 1, 1.0, 0.0)

    state.reset_mock()
    assert args._strat_apply_gate(cirq.CX, [q1, q0]) is True
    state.apply_cx.assert_called_with(1, 0, 1.0, 0.0)

    state.reset_mock()
    assert args._strat_apply_gate(cirq.CZ, [q0, q1]) is True
    state.apply_cz.assert_called_with(0, 1, 1.0, 0.0)

    state.reset_mock()
    assert args._strat_apply_gate(cirq.GlobalPhaseGate(1j), []) is True
    state.apply_global_phase.assert_called_with(1j)

    state.reset_mock()
    assert args._strat_apply_gate(cirq.GlobalPhaseGate(sympy.Symbol('t')),
                                  []) is NotImplemented
    state.apply_global_phase.assert_not_called()

    state.reset_mock()
    assert args._strat_apply_gate(cirq.SWAP, [q0, q1]) is True
    state.apply_cx.assert_has_calls(
        [call(0, 1), call(1, 0, 1.0, 0.0),
         call(0, 1)])

    state.reset_mock()
    assert args._strat_apply_gate(
        cirq.SwapPowGate(exponent=2, global_shift=1.3), [q0, q1]) is True
    state.apply_cx.assert_has_calls(
        [call(0, 1), call(1, 0, 2.0, 1.3),
         call(0, 1)])

    state.reset_mock()
    assert args._strat_apply_gate(cirq.BitFlipChannel(0.5),
                                  [q0]) == NotImplemented
    state.apply_x.assert_not_called()