Esempio n. 1
0
def test_incorporate_result_not_view():
    tensor = np.zeros((2, 2))
    tensor2 = np.zeros((2, 2))
    buffer = np.empty_like(tensor)
    args = cirq.ApplyUnitaryArgs(tensor, buffer, [0])
    not_sub_args = cirq.ApplyUnitaryArgs(tensor2, buffer, [0])
    with pytest.raises(ValueError, match='view'):
        _incorporate_result_into_target(args, not_sub_args, tensor2)
Esempio n. 2
0
    def assert_is_swap(val: cirq.SupportsConsistentApplyUnitary) -> None:
        qid_shape = (1, 2, 4, 2)
        op_indices = [1, 3]
        state = np.arange(2 * (1 * 3 * 4 * 5), dtype=np.complex64).reshape(
            (1, 2, 1, 5, 3, 1, 4))
        expected = state.copy()
        buf = expected[..., 0, 1, :, :].copy()
        expected[..., 0, 1, :, :] = expected[..., 1, 0, :, :]
        expected[..., 1, 0, :, :] = buf
        expected[..., :2, :2, :, :] *= 1j

        args = cirq.ApplyUnitaryArgs(state, np.empty_like(state), [5, 4, 6, 3])
        sub_args = args._for_operation_with_qid_shape(
            op_indices, tuple(qid_shape[i] for i in op_indices))
        sub_result = val._apply_unitary_(sub_args)
        result = _incorporate_result_into_target(args, sub_args, sub_result)
        np.testing.assert_allclose(result, expected, atol=1e-8, verbose=True)
Esempio n. 3
0
    def assert_is_swap_simple(
            val: cirq.SupportsConsistentApplyUnitary) -> None:
        qid_shape = (2, 2)
        op_indices = [0, 1]
        state = np.arange(3 * 3, dtype=np.complex64).reshape((1, 3, 3))
        expected = state.copy()
        buf = expected[..., 0, 1].copy()
        expected[..., 0, 1] = expected[..., 1, 0]
        expected[..., 1, 0] = buf
        expected[..., :2, :2] *= 1j

        args = cirq.ApplyUnitaryArgs(state, np.empty_like(state), [1, 2])
        sub_args = args._for_operation_with_qid_shape(
            op_indices, tuple(qid_shape[i] for i in op_indices))
        sub_result = val._apply_unitary_(sub_args)
        result = _incorporate_result_into_target(args, sub_args, sub_result)
        np.testing.assert_allclose(result, expected, atol=1e-8)