1 /**
2 * This code is part of Qiskit.
3 *
4 * (C) Copyright IBM 2018, 2019.
5 *
6 * This code is licensed under the Apache License, Version 2.0. You may
7 * obtain a copy of this license in the LICENSE.txt file in the root directory
8 * of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
9 *
10 * Any modifications or derivative works of this code must retain this
11 * copyright notice, and modified files need to carry a notice indicating
12 * that they have been altered from the originals.
13 */
14
15 #ifndef _TEST_HELPERS_HPP
16 #define _TEST_HELPERS_HPP
17
18 #include <csignal>
19 #include <unordered_map>
20 #include <vector>
21 #include <complex>
22 #include <Python.h>
23 #include <iostream>
24 #include <numpy/arrayobject.h>
25 #include "python_to_cpp.hpp"
26
27 // TODO: Test QuantumObj
28 // TODO: Test Hamiltonian
29
cpp_test_py_list_to_cpp_vec(PyObject * val)30 bool cpp_test_py_list_to_cpp_vec(PyObject * val){
31 // val = [1., 2., 3.]
32 auto vec = get_value<std::vector<double>>(val);
33 auto expected = std::vector<double>{1., 2., 3.};
34 return vec == expected;
35 }
36
cpp_test_py_list_of_lists_to_cpp_vector_of_vectors(PyObject * val)37 bool cpp_test_py_list_of_lists_to_cpp_vector_of_vectors(PyObject * val){
38 // val = [[1., 2., 3.]]
39 auto vec = get_value<std::vector<std::vector<double>>>(val);
40 auto expected = std::vector<std::vector<double>>{{1., 2., 3.}};
41 return vec == expected;
42 }
43
cpp_test_py_dict_string_numeric_to_cpp_map_string_numeric(PyObject * val)44 bool cpp_test_py_dict_string_numeric_to_cpp_map_string_numeric(PyObject * val){
45 // val = {"key": 1}
46 auto map = get_value<std::unordered_map<std::string, long>>(val);
47 auto expected = std::unordered_map<std::string, long>{{"key", 1}};
48 return map == expected;
49
50 }
51
cpp_test_py_dict_string_list_of_list_of_doubles_to_cpp_map_string_vec_of_vecs_of_doubles(PyObject * val)52 bool cpp_test_py_dict_string_list_of_list_of_doubles_to_cpp_map_string_vec_of_vecs_of_doubles(PyObject * val){
53 // val = {"key": [[1., 2., 3.]]}
54 auto map = get_value<std::unordered_map<std::string, std::vector<std::vector<double>>>>(val);
55 auto expected = std::unordered_map<std::string, std::vector<std::vector<double>>>{{"key", {{1., 2., 3.}}}};
56 return map == expected;
57 }
58
cpp_test_np_array_of_doubles(PyArrayObject * val)59 bool cpp_test_np_array_of_doubles(PyArrayObject * val){
60 // val = np.array([0., 1., 2., 3.])
61 auto vec = get_value<NpArray<double>>(val);
62 if(vec[0] != 0. || vec[1] != 1. || vec[2] != 2. || vec[3] != 3.)
63 return false;
64
65 return true;
66 }
67
cpp_test_evaluate_hamiltonians(PyObject * val)68 bool cpp_test_evaluate_hamiltonians(PyObject * val){
69 // TODO: Add tests!
70 return false;
71 }
72
cpp_test_py_ordered_map(PyObject * val)73 bool cpp_test_py_ordered_map(PyObject * val){
74 // Ordered map should guarantee insertion order.
75 // val = {"D0": 1, "U0": 2, "D1": 3, "U1": 4}
76 std::vector<std::string> order = {"D0", "U0", "D1", "U1"};
77 auto ordered = get_value<ordered_map<std::string, long>>(val);
78 size_t i = 0;
79 for(const auto& elem: ordered) {
80 auto key = elem.first;
81 if(key != order[i++])
82 return false;
83 }
84 return true;
85 }
86
87 #endif // _TEST_HELPERS_HPP