Comments (4)
I'm interested in this because I have found an expedient to bypass the arnoldi issue in #360.
For those who need more matrix operations, you can compile ITensor together with other linear algebra packages such as armadillo, and use the matrix functions therein. Below is an example of constructing an armadillo matrix from a rank-2 ITensor using the above v
int di = 3;
int dj = 3;
auto i = Index(di, "i");
auto j = Index(dj, "j");
auto T = ITensor(i, j);
T.randomize();
auto extractReal = [](Dense<Real> const& d)
{
return d.store;
};
auto v = applyFunc(extractReal,T.store());
arma::mat m(&v[0], di, dj, false);
from itensor.
ITensor storage makes use of an std::vector
with a custom allocator that allows for allocating uninitialized memory (since std::vector
always initializes memory as zeros, which we found to be a pretty significant cost throughout ITensor). You can see that here, for example. You can see the definition here.
Perhaps you can use:
vector_no_init<Real> vec = v;
though I'm not sure if that will "play nice" with your application. Otherwise, you should be able to copy the data over to an std::vector
using one of the many std::vector
constructors or by copying the data over element by element, if you really need an std::vector
.
from itensor.
Hi Matthew! Thanks a lot!
I've read through the source codes of dense.h and vector_no_init.h and I can understand the implementation now.
from itensor.
After looking into the ITData
class I've found a conceptually simpler and generally much faster (in debug mode) method to extract dense data to a std::vector
. This is done in merely two lines.
vector_no_init<Real>& dvec = (*((ITWrap<Dense<Real>>*) & (*T.store()))).d.store;
std::vector<Real> vec(dvec.begin(), dvec.end());
The first line may look like a mess, but it's actually not very difficult to understand.
T.store()
is a shared pointer to ITData
, dereference it and take address again gives a bare pointer to ITData
. The bare pointer is then converted to a pointer to the template class ITWrap
which is derived from ITData
. For a Real dense ITensor this template parameter is the class Dense<Real>
. Dereference this pointer again gives an ITWrap
class, this is the type of the part inside the outermost bracket, so (...)
has type ITWrap
.
The actual ITensor data is stored in a Dense<Real>
, the d
member of ITWrap
. Finally (...).d.store
is a std::vector
with a custom allocator, this is exactly the data vector that we want.
A standard vector can then be initialized by setting the correct iterators. This method is, in debug mode and on my laptop, around 70 times faster than the above applyFunc
method, however, there's no performance difference in release mode.
And there're more advantages since the data vector dvec
is now completely exposed to us, for example it's easier to retrieve other information or even manipulate the data with STL tools, and not limited to ITensor::visit
and ITensor::apply
.
from itensor.
Related Issues (20)
- error in calling arnoldi function HOT 1
- range1 not working like python HOT 2
- (no)prime not working appropriately with <keyword arguments> HOT 1
- Add stracktraces to error messages
- configure/cmake HOT 3
- Introduce `apply` and `contract` MPS/MPO functions
- .position() of MPS does not preserve the link index of the MPS HOT 1
- Typo in sample/Makefile
- subscript out of range in itensor/util/autovector.h HOT 2
- Reading input from a .tns file HOT 2
- Should QDense constructor error if zero blocks? HOT 6
- Two issues in installing ITensor c++ on Windows HOT 10
- fatal error: itensor/config.h: No such file or directory HOT 3
- Performing multiple HDF5 operations "captures" cout,cerr HOT 9
- compile error when using OpenBLAS in Ubuntu 22.04 HOT 1
- Losing QN information when reading from HDF5 HOT 1
- Bug: unexpected returning order of indexes HOT 3
- Unexpected behaviour: elt(T) doesn't raise out-of range error HOT 5
- Failure to compile with gcc-13: call of overloaded 'format(..)' is ambiguous HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from itensor.