Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ RUN tar -xzf cmake-3.15.2-Linux-x86_64.tar.gz -C /usr/local/ --strip-components=

# Fetch and install the Intel MKL libraries required for building the Intel-QS simulator.
WORKDIR swpkgs/mkl
RUN wget "https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB"
RUN apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
RUN rm GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
RUN sh -c 'echo deb https://apt.repos.intel.com/mkl all main > /etc/apt/sources.list.d/intel-mkl.list'
RUN apt-get install -y gpg
RUN wget -qO - "https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB" | gpg --dearmor -o /usr/share/keyrings/intel-sw-products.gpg
RUN sh -c 'echo "deb [signed-by=/usr/share/keyrings/intel-sw-products.gpg] https://apt.repos.intel.com/mkl all main" > /etc/apt/sources.list.d/intel-mkl.list'
RUN apt-get update
RUN apt-get install -y intel-mkl-64bit-2019.2-057
# Set the (global) environment variable MKLROOT to facilitate the build process.
Expand Down
13 changes: 9 additions & 4 deletions include/tinymatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cassert>
#include <initializer_list>
#include <iostream>
#include <stdexcept>

/// \addtogroup util
/// @{
Expand Down Expand Up @@ -141,8 +142,10 @@ class TinyMatrix
/// \pre i<numRows() & j<numCols()
value_type operator()(size_type i, size_type j) const
{
assert(i < this->numRows() && "Row index out of range");
assert(j < this->numCols() && "Column index out of range");
if (i >= this->numRows())
throw std::out_of_range("TinyMatrix row index out of range");
if (j >= this->numCols())
throw std::out_of_range("TinyMatrix column index out of range");
return data_[i][j];
}

Expand All @@ -154,8 +157,10 @@ class TinyMatrix
/// \pre i<numRows() & j<numCols()
reference operator()(size_type i, size_type j)
{
assert(i < this->numRows() && "Row index out of range");
assert(j < this->numCols() && "Column index out of range");
if (i >= this->numRows())
throw std::out_of_range("TinyMatrix row index out of range");
if (j >= this->numCols())
throw std::out_of_range("TinyMatrix column index out of range");
return data_[i][j];
}

Expand Down
21 changes: 10 additions & 11 deletions pybind11/intelqs_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,15 @@ PYBIND11_MODULE(intelqs_py, m)
.def(py::init<>())
.def(py::init<>())
// Access element:
.def("__getitem__", [](const iqs::ChiMatrix<ComplexDP,4,32> &a, std::pair<py::ssize_t, py::ssize_t> i, int column) {
if (i.first > 4) throw py::index_error();
if (i.second > 4) throw py::index_error();
std::cout << "ciao\n";
.def("__getitem__", [](const iqs::ChiMatrix<ComplexDP,4,32> &a, std::pair<py::ssize_t, py::ssize_t> i) {
if (i.first < 0 || i.first >= 4) throw py::index_error();
if (i.second < 0 || i.second >= 4) throw py::index_error();
return a(i.first, i.second);
}, py::is_operator())
// Set element:
.def("__setitem__", [](iqs::ChiMatrix<ComplexDP,4,32> &a, std::pair<py::ssize_t, py::ssize_t> i, ComplexDP value) {
if (i.first > 4) throw py::index_error();
if (i.second > 4) throw py::index_error();
if (i.first < 0 || i.first >= 4) throw py::index_error();
if (i.second < 0 || i.second >= 4) throw py::index_error();
a(i.first, i.second) = value;
}, py::is_operator())
#if 0
Expand Down Expand Up @@ -147,15 +146,15 @@ std::cout << "ciao\n";
.def(py::init<>())
.def(py::init<>())
// Access element:
.def("__getitem__", [](const iqs::ChiMatrix<ComplexDP,16,32> &a, std::pair<py::ssize_t, py::ssize_t> i, int column) {
if (i.first > 16) throw py::index_error();
if (i.second > 16) throw py::index_error();
.def("__getitem__", [](const iqs::ChiMatrix<ComplexDP,16,32> &a, std::pair<py::ssize_t, py::ssize_t> i) {
if (i.first < 0 || i.first >= 16) throw py::index_error();
if (i.second < 0 || i.second >= 16) throw py::index_error();
return a(i.first, i.second);
}, py::is_operator())
// Set element:
.def("__setitem__", [](iqs::ChiMatrix<ComplexDP,16,32> &a, std::pair<py::ssize_t, py::ssize_t> i, ComplexDP value) {
if (i.first > 16) throw py::index_error();
if (i.second > 16) throw py::index_error();
if (i.first < 0 || i.first >= 16) throw py::index_error();
if (i.second < 0 || i.second >= 16) throw py::index_error();
a(i.first, i.second) = value;
}, py::is_operator())
.def("SolveEigenSystem", &iqs::ChiMatrix<ComplexDP,16,32>::SolveEigenSystem)
Expand Down
19 changes: 19 additions & 0 deletions unit_test/import_iqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@
sys.path.insert(0, "../build/lib/")
import intelqs_py as iqs


def assert_index_error(operation):
try:
operation()
except IndexError:
return
raise AssertionError("Invalid matrix index did not raise IndexError")


for matrix_type, dimension in ((iqs.CM4x4, 4), (iqs.CM16x16, 16)):
matrix = matrix_type()
matrix[dimension - 1, dimension - 1] = 1 + 2j
assert matrix[dimension - 1, dimension - 1] == 1 + 2j

for invalid_index in ((-1, 0), (0, -1), (dimension, 0), (0, dimension)):
assert_index_error(lambda index=invalid_index: matrix[index])
assert_index_error(lambda index=invalid_index: matrix.__setitem__(index, 0j))


iqs.EnvInit()
rank = iqs.MPIEnvironment.GetRank()

Expand Down
13 changes: 13 additions & 0 deletions unit_test/include/tinymatrix_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,17 @@ TEST_F(TinyMatrixTest, ComplexDP)

//////////////////////////////////////////////////////////////////////////////

TEST_F(TinyMatrixTest, OutOfRangeAccess)
{
iqs::TinyMatrix<double, 2, 3> mat;
const iqs::TinyMatrix<double, 2, 3>& const_mat = mat;

ASSERT_THROW(mat(2, 0), std::out_of_range);
ASSERT_THROW(mat(0, 3), std::out_of_range);
ASSERT_THROW(const_mat(2, 0), std::out_of_range);
ASSERT_THROW(const_mat(0, 3), std::out_of_range);
}

//////////////////////////////////////////////////////////////////////////////

#endif // header guard TINYMATRIX_TEST_HPP