1#ifndef CYTNX_UNITENSOR_H_
2#define CYTNX_UNITENSOR_H_
8#include "utils/utils.hpp"
14#include <initializer_list>
24 #include "backend/Scalar.hpp"
33 class UniTensorType_class {
35 enum :
int {
Void = -99, Dense = 0, Sparse = 1, Block = 2, BlockFermionic = 3 };
36 std::string getname(
const int &ut_type)
const;
60 class UniTensor_base :
public intrusive_ptr_base<UniTensor_base> {
68 std::vector<std::string> _labels;
69 std::vector<Bond> _bonds;
70 std::vector<Symmetry> syms_cache;
72 bool _update_braket() {
73 if (_bonds.size() == 0)
return false;
75 if (this->_bonds[0].type() != bondType::BD_REG) {
77 for (
unsigned int i = 0; i < this->_bonds.size(); i++) {
78 if (i < this->_rowrank) {
79 if (this->_bonds[i].type() != bondType::BD_KET)
return false;
81 if (this->_bonds[i].type() != bondType::BD_BRA)
return false;
90 friend class UniTensor;
91 friend class DenseUniTensor;
92 friend class BlockUniTensor;
93 friend class BlockFermionicUniTensor;
97 _name(std::string(
"")),
98 _is_braket_form(false),
104 UniTensor_base(
const UniTensor_base &rhs);
105 UniTensor_base &operator=(UniTensor_base &rhs);
107 cytnx_uint64 rowrank()
const {
return this->_rowrank; }
108 bool is_diag()
const {
return this->_is_diag; }
109 const bool &is_braket_form()
const {
return this->_is_braket_form; }
110 const bool &is_tag()
const {
return this->_is_tag; }
111 const std::vector<std::string> &labels()
const {
return this->_labels; }
118 cytnx_int64 get_index(std::string label)
const {
119 std::vector<std::string> labels = this->_labels;
120 for (cytnx_uint64 i = 0; i < labels.size(); i++) {
121 if (labels[i] == label)
return i;
125 const std::vector<Bond> &bonds()
const {
return this->_bonds; }
127 Bond &bond_(
const cytnx_uint64 &idx) {
128 cytnx_error_msg(idx >= this->rank(),
"[ERROR][bond] index %d out of bound, total %d\n", idx,
130 return this->_bonds[idx];
133 Bond &bond_(
const std::string &label) {
134 auto res = std::find(this->_labels.begin(), this->_labels.end(), label);
135 cytnx_error_msg(res == this->_labels.end(),
"[ERROR] label %s not exists.\n", label.c_str());
136 cytnx_uint64 idx = std::distance(this->_labels.begin(), res);
138 return this->bond_(idx);
141 const std::string &name()
const {
return this->_name; }
142 cytnx_uint64 rank()
const {
144 "[ERROR][UniTensor_base][rank] inconsistent metadata: labels.size()=%zu, "
145 "bonds.size()=%zu.%s",
146 this->_labels.size(), this->_bonds.size(),
"\n");
147 return this->_labels.size();
149 void set_name_(
const std::string &in) { this->_name = in; }
150 [[deprecated(
"Please use set_name_(const std::string &in) instead.")]]
void set_name(
151 const std::string &in) {
166 void set_label_(
const std::string &oldlabel,
const std::string &new_label) {
168 auto res = std::find(this->_labels.begin(), this->_labels.end(), oldlabel);
169 cytnx_error_msg(res == this->_labels.end(),
"[ERROR] label %s not exists.\n",
171 idx = std::distance(this->_labels.begin(), res);
173 cytnx_error_msg(idx >= this->_labels.size(),
"[ERROR] index exceed the rank of UniTensor%s",
177 for (cytnx_uint64 i = 0; i < this->_labels.size(); i++) {
178 if (i == idx)
continue;
179 if (new_label == this->_labels[i]) {
184 cytnx_error_msg(is_dup,
"[ERROR] alreay has a label that is the same as the input label%s",
186 this->_labels[idx] = new_label;
188 void set_label_(
const cytnx_int64 &inx,
const std::string &new_label) {
190 cytnx_error_msg(inx >= this->_labels.size(),
"[ERROR] index exceed the rank of UniTensor%s",
194 for (cytnx_uint64 i = 0; i < this->_labels.size(); i++) {
195 if (i == inx)
continue;
196 if (new_label == this->_labels[i]) {
201 cytnx_error_msg(is_dup,
"[ERROR] alreay has a label that is the same as the input label%s",
203 this->_labels[inx] = new_label;
206 "Please use set_label_(const std::string &oldlabel, const std::string "
207 "&new_label) instead.")]]
void
208 set_label(
const std::string &oldlabel,
const std::string &new_label) {
209 this->set_label_(oldlabel, new_label);
212 "Please use set_label_(const cytnx_int64 &inx, const std::string &new_label) "
214 set_label(
const cytnx_int64 &inx,
const std::string &new_label) {
215 this->set_label_(inx, new_label);
218 [[deprecated(
"Please use relabel_(const std::vector<std::string> &new_labels) instead.")]]
void
219 set_labels(
const std::vector<std::string> &new_labels);
220 void relabel_(
const std::vector<std::string> &new_labels);
221 [[deprecated(
"Please use relabel_(const std::vector<std::string> &new_labels) instead.")]]
void
222 relabels_(
const std::vector<std::string> &new_labels);
223 void relabel_(
const std::vector<std::string> &old_labels,
224 const std::vector<std::string> &new_labels);
226 "Please use relabel_(const std::vector<std::string> &old_labels, const "
227 "std::vector<std::string> &new_labels) instead.")]]
void
228 relabels_(
const std::vector<std::string> &old_labels,
229 const std::vector<std::string> &new_labels);
230 void relabel_(
const std::string &old_label,
const std::string &new_label) {
231 this->set_label_(old_label, new_label);
233 void relabel_(
const cytnx_int64 &inx,
const std::string &new_label) {
234 this->set_label_(inx, new_label);
237 int uten_type() {
return this->uten_type_id; }
238 std::string uten_type_str()
const {
return UTenType.getname(this->uten_type_id); }
243 virtual void Init(
const std::vector<Bond> &bonds,
244 const std::vector<std::string> &in_labels = {},
245 const cytnx_int64 &rowrank = -1,
const unsigned int &dtype =
Type.Double,
246 const int &device =
Device.cpu,
const bool &is_diag =
false,
247 const bool &no_alloc =
false,
const std::string &name =
"");
249 virtual void Init_by_Tensor(
const Tensor &in,
const bool &is_diag =
false,
250 const cytnx_int64 &rowrank = -1,
const std::string &name =
"");
251 virtual std::vector<cytnx_uint64> shape()
const;
252 virtual std::vector<bool> signflip()
const;
253 virtual bool is_blockform()
const;
254 virtual bool is_contiguous()
const;
255 virtual void to_(
const int &device);
256 virtual boost::intrusive_ptr<UniTensor_base> to(
const int &device);
257 virtual boost::intrusive_ptr<UniTensor_base> clone()
const;
258 virtual unsigned int dtype()
const;
259 virtual int device()
const;
260 virtual std::string dtype_str()
const;
261 virtual std::string device_str()
const;
262 virtual void set_rowrank_(
const cytnx_uint64 &new_rowrank);
263 virtual boost::intrusive_ptr<UniTensor_base> set_rowrank(
const cytnx_uint64 &new_rowrank)
const;
265 virtual boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<cytnx_int64> &mapper,
266 const cytnx_int64 &rowrank = -1);
267 virtual boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<std::string> &mapper,
268 const cytnx_int64 &rowrank = -1);
272 virtual void permute_(
const std::vector<cytnx_int64> &mapper,
const cytnx_int64 &rowrank = -1);
273 virtual void permute_(
const std::vector<std::string> &mapper,
const cytnx_int64 &rowrank = -1);
275 virtual boost::intrusive_ptr<UniTensor_base> permute_nosignflip(
276 const std::vector<cytnx_int64> &mapper,
const cytnx_int64 &rowrank = -1);
277 virtual boost::intrusive_ptr<UniTensor_base> permute_nosignflip(
278 const std::vector<std::string> &mapper,
const cytnx_int64 &rowrank = -1);
279 virtual void permute_nosignflip_(
const std::vector<cytnx_int64> &mapper,
280 const cytnx_int64 &rowrank = -1);
281 virtual void permute_nosignflip_(
const std::vector<std::string> &mapper,
282 const cytnx_int64 &rowrank = -1);
286 virtual void twist_(
const cytnx_int64 &idx);
287 virtual void twist_(
const std::string &label);
288 virtual void fermion_twists_();
290 virtual boost::intrusive_ptr<UniTensor_base> contiguous_();
291 virtual boost::intrusive_ptr<UniTensor_base> contiguous();
292 virtual boost::intrusive_ptr<UniTensor_base> apply_();
293 virtual boost::intrusive_ptr<UniTensor_base> apply();
294 virtual void print_diagram(
const bool &bond_info =
false)
const;
295 virtual void print_blocks(
const bool &full_info =
true)
const;
296 virtual void print_block(
const cytnx_int64 &idx,
const bool &full_info =
true)
const;
298 virtual boost::intrusive_ptr<UniTensor_base> astype(
const unsigned int &dtype)
const;
300 virtual cytnx_uint64 Nblocks()
const {
return 0; };
301 virtual Tensor get_block(
const cytnx_uint64 &idx = 0)
const;
302 virtual Tensor get_block(
const std::vector<cytnx_int64> &qnum,
303 const bool &force)
const;
305 virtual const Tensor &get_block_(
const cytnx_uint64 &idx = 0)
307 virtual const Tensor &get_block_(
const std::vector<cytnx_int64> &qnum,
308 const bool &force)
const;
309 virtual Tensor &get_block_(
const cytnx_uint64 &idx = 0);
311 virtual Tensor &get_block_(
const std::vector<cytnx_int64> &qnum,
313 virtual bool same_data(
const boost::intrusive_ptr<UniTensor_base> &rhs)
const;
315 virtual std::vector<Tensor> get_blocks()
const;
316 virtual const std::vector<Tensor> &get_blocks_(
const bool &)
const;
317 virtual std::vector<Tensor> &get_blocks_(
const bool &);
319 virtual void put_block(
const Tensor &in,
const cytnx_uint64 &idx = 0);
320 virtual void put_block_(Tensor &in,
const cytnx_uint64 &idx = 0);
321 virtual void put_block(
const Tensor &in,
const std::vector<cytnx_int64> &qnum);
322 virtual void put_block_(Tensor &in,
const std::vector<cytnx_int64> &qnum);
325 virtual boost::intrusive_ptr<UniTensor_base> get(
const std::vector<Accessor> &accessors);
328 virtual void set(
const std::vector<Accessor> &accessors,
const Tensor &rhs);
330 virtual void reshape_(
const std::vector<cytnx_int64> &new_shape,
331 const cytnx_uint64 &rowrank = 0);
332 virtual boost::intrusive_ptr<UniTensor_base> reshape(
const std::vector<cytnx_int64> &new_shape,
333 const cytnx_uint64 &rowrank = 0);
334 virtual boost::intrusive_ptr<UniTensor_base> to_dense();
335 virtual void to_dense_();
336 virtual void combineBond(
const std::vector<std::string> &indicators,
const bool &force =
false);
337 virtual void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force,
338 const bool &by_label);
339 virtual void combineBonds(
const std::vector<std::string> &indicators,
340 const bool &force =
false);
341 virtual void combineBonds(
const std::vector<cytnx_int64> &indicators,
342 const bool &force =
false);
343 virtual boost::intrusive_ptr<UniTensor_base> contract(
344 const boost::intrusive_ptr<UniTensor_base> &rhs,
const bool &mv_elem_self =
false,
345 const bool &mv_elem_rhs =
false);
346 virtual std::vector<Bond> getTotalQnums(
const bool &physical =
false);
347 virtual std::vector<std::vector<cytnx_int64>> get_blocks_qnums()
const;
348 virtual void Trace_(
const std::string &a,
const std::string &b);
349 virtual void Trace_(
const cytnx_int64 &a,
const cytnx_int64 &b);
351 virtual boost::intrusive_ptr<UniTensor_base>
Trace(
const std::string &a,
const std::string &b);
352 virtual boost::intrusive_ptr<UniTensor_base>
Trace(
const cytnx_int64 &a,
const cytnx_int64 &b);
354 virtual boost::intrusive_ptr<UniTensor_base> relabel(
355 const std::vector<std::string> &new_labels);
356 virtual boost::intrusive_ptr<UniTensor_base> relabels(
357 const std::vector<std::string> &new_labels);
359 virtual boost::intrusive_ptr<UniTensor_base> relabel(
360 const std::vector<std::string> &old_labels,
const std::vector<std::string> &new_labels);
361 virtual boost::intrusive_ptr<UniTensor_base> relabels(
362 const std::vector<std::string> &old_labels,
const std::vector<std::string> &new_labels);
364 virtual boost::intrusive_ptr<UniTensor_base> relabel(
const std::string &old_label,
365 const std::string &new_label);
367 virtual boost::intrusive_ptr<UniTensor_base> relabel(
const cytnx_int64 &inx,
368 const std::string &new_label);
370 virtual std::vector<Symmetry> syms()
const;
378 virtual void Add_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
379 virtual void Add_(
const Scalar &rhs);
381 virtual void Mul_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
382 virtual void Mul_(
const Scalar &rhs);
384 virtual void Sub_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
385 virtual void Sub_(
const Scalar &rhs);
386 virtual void lSub_(
const Scalar &lhs);
388 virtual void Div_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
389 virtual void Div_(
const Scalar &rhs);
390 virtual void lDiv_(
const Scalar &lhs);
392 virtual Tensor
Norm()
const;
393 virtual boost::intrusive_ptr<UniTensor_base> normalize();
394 virtual void normalize_();
396 virtual boost::intrusive_ptr<UniTensor_base>
Conj();
397 virtual void Conj_();
399 virtual boost::intrusive_ptr<UniTensor_base> Transpose();
400 virtual void Transpose_();
402 virtual boost::intrusive_ptr<UniTensor_base> Dagger();
403 virtual void Dagger_();
407 virtual void truncate_(
const std::string &label,
const cytnx_uint64 &dim);
408 virtual void truncate_(
const cytnx_int64 &bond_idx,
const cytnx_uint64 &dim);
410 virtual bool elem_exists(
const std::vector<cytnx_uint64> &locator)
const;
413 virtual Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator);
414 virtual const Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator)
const;
416 virtual cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
417 const cytnx_complex128 &aux);
418 virtual cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
419 const cytnx_complex64 &aux);
420 virtual cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
421 const cytnx_double &aux);
422 virtual cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
423 const cytnx_float &aux);
424 virtual cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
425 const cytnx_uint64 &aux);
426 virtual cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
427 const cytnx_int64 &aux);
428 virtual cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
429 const cytnx_uint32 &aux);
430 virtual cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
431 const cytnx_int32 &aux);
432 virtual cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
433 const cytnx_uint16 &aux);
434 virtual cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
435 const cytnx_int16 &aux);
437 virtual const cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
438 const cytnx_complex128 &aux)
const;
439 virtual const cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
440 const cytnx_complex64 &aux)
const;
441 virtual const cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
442 const cytnx_double &aux)
const;
443 virtual const cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
444 const cytnx_float &aux)
const;
445 virtual const cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
446 const cytnx_uint64 &aux)
const;
447 virtual const cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
448 const cytnx_int64 &aux)
const;
449 virtual const cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
450 const cytnx_uint32 &aux)
const;
451 virtual const cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
452 const cytnx_int32 &aux)
const;
453 virtual const cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
454 const cytnx_uint16 &aux)
const;
455 virtual const cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
456 const cytnx_int16 &aux)
const;
458 virtual void from_(
const boost::intrusive_ptr<UniTensor_base> &rhs,
bool force,
459 cytnx_double tol = 0.);
461 virtual void group_basis_();
462 virtual const std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx)
const;
463 virtual std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx);
464 virtual const vec2d<cytnx_uint64> &get_itoi()
const;
465 virtual vec2d<cytnx_uint64> &get_itoi();
467 virtual void _save_dispatch(std::fstream &f)
const;
468 virtual void _load_dispatch(std::fstream &f,
unsigned int version);
470 virtual ~UniTensor_base(){};
476 class DenseUniTensor :
public UniTensor_base {
480 std::vector<Tensor> _interface_block;
481 boost::intrusive_ptr<DenseUniTensor> clone_meta()
const {
482 boost::intrusive_ptr<DenseUniTensor> tmp(
new DenseUniTensor());
483 tmp->_bonds = vec_clone(this->_bonds);
484 tmp->_labels = this->_labels;
485 tmp->syms_cache = vec_clone(this->syms_cache);
486 tmp->_is_braket_form = this->_is_braket_form;
487 tmp->_rowrank = this->_rowrank;
488 tmp->_is_diag = this->_is_diag;
489 tmp->_name = this->_name;
490 tmp->_is_tag = this->_is_tag;
495 DenseUniTensor() { this->uten_type_id =
UTenType.Dense; };
496 friend class UniTensor;
504 void Init(
const std::vector<Bond> &bonds,
const std::vector<std::string> &in_labels = {},
505 const cytnx_int64 &rowrank = -1,
const unsigned int &dtype =
Type.Double,
506 const int &device =
Device.cpu,
const bool &is_diag =
false,
507 const bool &no_alloc =
false,
const std::string &name =
"");
509 void Init_by_Tensor(
const Tensor &in_tensor,
const bool &is_diag =
false,
510 const cytnx_int64 &rowrank = -1,
const std::string &name =
"");
511 std::vector<cytnx_uint64> shape()
const {
512 if (this->_is_diag) {
513 std::vector<cytnx_uint64> shape = this->_block.shape();
514 shape.push_back(shape[0]);
517 return this->_block.shape();
520 bool is_blockform()
const {
return false; }
521 void to_(
const int &device) { this->_block.to_(device); }
522 boost::intrusive_ptr<UniTensor_base> to(
const int &device) {
523 if (this->device() == device) {
524 std::vector<Tensor> _interface_block;
527 boost::intrusive_ptr<UniTensor_base> out = this->clone();
532 void set_rowrank_(
const cytnx_uint64 &new_rowrank) {
534 "[ERROR] rowrank cannot exceed the rank of UniTensor.%s",
"\n");
535 if (this->is_diag()) {
536 cytnx_error_msg(new_rowrank != 1,
"[ERROR] rowrank should be [==1] when is_diag =true!.%s",
540 this->_rowrank = new_rowrank;
543 boost::intrusive_ptr<UniTensor_base> set_rowrank(
const cytnx_uint64 &new_rowrank)
const {
544 boost::intrusive_ptr<DenseUniTensor> out_raw = this->clone_meta();
545 out_raw->_block = this->_block;
546 out_raw->set_rowrank_(new_rowrank);
550 boost::intrusive_ptr<UniTensor_base> clone()
const {
551 boost::intrusive_ptr<DenseUniTensor> tmp = this->clone_meta();
552 tmp->_block = this->_block.clone();
555 bool is_contiguous()
const {
return this->_block.is_contiguous(); }
556 unsigned int dtype()
const {
return this->_block.dtype(); }
557 int device()
const {
return this->_block.device(); }
558 std::string dtype_str()
const {
return Type.getname(this->_block.dtype()); }
559 std::string device_str()
const {
return Device.getname(this->_block.device()); }
569 boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<cytnx_int64> &mapper,
570 const cytnx_int64 &rowrank = -1);
571 boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<std::string> &mapper,
572 const cytnx_int64 &rowrank = -1);
582 void permute_(
const std::vector<cytnx_int64> &mapper,
const cytnx_int64 &rowrank = -1);
583 void permute_(
const std::vector<std::string> &mapper,
const cytnx_int64 &rowrank = -1);
585 void twist_(
const cytnx_int64 &idx)
override {
589 void twist_(
const std::string &label)
override {
594 void fermion_twists_()
override {
599 boost::intrusive_ptr<UniTensor_base> relabel(
const std::vector<std::string> &new_labels);
600 boost::intrusive_ptr<UniTensor_base> relabels(
const std::vector<std::string> &new_labels);
602 boost::intrusive_ptr<UniTensor_base> relabel(
const std::vector<std::string> &old_labels,
603 const std::vector<std::string> &new_labels);
604 boost::intrusive_ptr<UniTensor_base> relabels(
const std::vector<std::string> &old_labels,
605 const std::vector<std::string> &new_labels);
617 boost::intrusive_ptr<UniTensor_base> relabel(
const std::string &old_label,
618 const std::string &new_label);
619 boost::intrusive_ptr<UniTensor_base> relabel(
const cytnx_int64 &inx,
620 const std::string &new_label);
622 boost::intrusive_ptr<UniTensor_base> astype(
const unsigned int &dtype)
const {
623 boost::intrusive_ptr<DenseUniTensor> tmp = this->clone_meta();
624 tmp->_block = this->_block.astype(dtype);
628 std::vector<Symmetry> syms()
const {
629 cytnx_error_msg(
true,
"[ERROR][DenseUniTensor] dense unitensor does not have symmetry.%s",
631 return std::vector<Symmetry>();
634 boost::intrusive_ptr<UniTensor_base> contiguous_() {
639 this->_block = this->_block.contiguous();
640 return boost::intrusive_ptr<UniTensor_base>(
this);
642 boost::intrusive_ptr<UniTensor_base> contiguous() {
644 if (this->is_contiguous()) {
645 boost::intrusive_ptr<UniTensor_base> out(
this);
648 boost::intrusive_ptr<DenseUniTensor> tmp = this->clone_meta();
649 tmp->_block = this->_block.contiguous();
654 boost::intrusive_ptr<UniTensor_base> apply_() {
655 return boost::intrusive_ptr<UniTensor_base>(
this);
657 boost::intrusive_ptr<UniTensor_base> apply() {
659 boost::intrusive_ptr<UniTensor_base> out(
this);
663 void print_diagram(
const bool &bond_info =
false)
const;
664 void print_blocks(
const bool &full_info =
true)
const;
665 void print_block(
const cytnx_int64 &idx,
const bool &full_info =
true)
const;
666 Tensor get_block()
const {
return this->_block.clone(); }
667 Tensor get_block(
const cytnx_uint64 &idx)
const {
669 "[ERROR][DenseUniTensor] Dense tensor has only one block, block number %llu "
670 "invalid. Use get_block(0).\n",
671 (
unsigned long long)idx);
672 return this->_block.clone();
675 Tensor get_block(
const std::vector<cytnx_int64> &qnum,
const bool &force)
const {
677 true,
"[ERROR][DenseUniTensor] try to get_block() using qnum on a non-symmetry UniTensor%s",
682 const Tensor &get_block_(
const std::vector<cytnx_int64> &qnum,
const bool &force)
const {
685 "[ERROR][DenseUniTensor] try to get_block_() using qnum on a non-symmetry UniTensor%s",
689 Tensor &get_block_(
const std::vector<cytnx_int64> &qnum,
const bool &force) {
692 "[ERROR][DenseUniTensor] try to get_block_() using qnum on a non-symmetry UniTensor%s",
698 Tensor &get_block_() {
return this->_block; }
699 Tensor &get_block_(
const cytnx_uint64 &idx) {
701 "[ERROR][DenseUniTensor] Dense tensor has only one block, block number %llu "
702 "invalid. Use get_block_(0).\n",
703 (
unsigned long long)idx);
707 const Tensor &get_block_()
const {
return this->_block; }
708 const Tensor &get_block_(
const cytnx_uint64 &idx)
const {
710 "[ERROR][DenseUniTensor] Dense tensor has only one block, block number %llu "
711 "invalid. Use get_block_(0).\n",
712 (
unsigned long long)idx);
716 cytnx_uint64 Nblocks()
const {
return 1; };
717 std::vector<Tensor> get_blocks()
const {
718 std::vector<Tensor> out;
720 true,
"[ERROR][DenseUniTensor] Cannot use get_blocks(), use get_block() instead!%s",
"\n");
723 const std::vector<Tensor> &get_blocks_(
const bool &silent =
false)
const {
725 true,
"[ERROR][DenseUniTensor] Cannot use get_blocks_(), use get_block_() instead!%s",
727 return this->_interface_block;
729 std::vector<Tensor> &get_blocks_(
const bool &silent =
false) {
731 true,
"[ERROR][DenseUniTensor] Cannot use get_blocks_(), use get_block_() instead!%s",
733 return this->_interface_block;
736 void put_block(
const Tensor &in) {
745 "[ERROR][DenseUniTensor][put_block] The input tensor device does not "
750 if (this->is_diag()) {
752 in.shape() != this->_block.shape(),
753 "[ERROR][DenseUniTensor][put_block] the input tensor shape does not match.%s",
"\n");
754 this->_block = in.clone();
757 in.shape() != this->shape(),
758 "[ERROR][DenseUniTensor][put_block] the input tensor shape does not match.%s",
"\n");
759 this->_block = in.clone();
762 void put_block(
const Tensor &in,
const cytnx_uint64 &idx) {
764 "[ERROR][DenseUniTensor] Dense tensor has only one block, block number %llu "
765 "invalid. Use put_block(0).\n",
766 (
unsigned long long)idx);
770 void put_block_(Tensor &in) {
779 "[ERROR][DenseUniTensor][put_block] The input tensor device does not "
784 if (this->is_diag()) {
786 in.shape() != this->_block.shape(),
787 "[ERROR][DenseUniTensor][put_block] the input tensor shape does not match.%s",
"\n");
791 in.shape() != this->shape(),
792 "[ERROR][DenseUniTensor][put_block] the input tensor shape does not match.%s",
"\n");
796 void put_block_(Tensor &in,
const cytnx_uint64 &idx) {
798 "[ERROR][DenseUniTensor] Dense tensor has only one block, block number %llu "
799 "invalid. Use put_block_(0).\n",
800 (
unsigned long long)idx);
804 void put_block(
const Tensor &in,
const std::vector<cytnx_int64> &qnum) {
806 true,
"[ERROR][DenseUniTensor] try to put_block using qnum on a non-symmetry UniTensor%s",
809 void put_block_(Tensor &in,
const std::vector<cytnx_int64> &qnum) {
811 true,
"[ERROR][DenseUniTensor] try to put_block using qnum on a non-symmetry UniTensor%s",
815 boost::intrusive_ptr<UniTensor_base> get(
const std::vector<Accessor> &accessors);
816 void set(
const std::vector<Accessor> &accessors,
const Tensor &rhs);
818 void reshape_(
const std::vector<cytnx_int64> &new_shape,
const cytnx_uint64 &rowrank = 0);
819 boost::intrusive_ptr<UniTensor_base> reshape(
const std::vector<cytnx_int64> &new_shape,
820 const cytnx_uint64 &rowrank = 0);
821 boost::intrusive_ptr<UniTensor_base> to_dense();
823 void combineBond(
const std::vector<std::string> &indicators,
const bool &force =
true);
824 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force,
825 const bool &by_label);
826 void combineBonds(
const std::vector<std::string> &indicators,
const bool &force =
true);
827 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force =
true);
828 boost::intrusive_ptr<UniTensor_base> contract(
const boost::intrusive_ptr<UniTensor_base> &rhs,
829 const bool &mv_elem_self =
false,
830 const bool &mv_elem_rhs =
false);
831 std::vector<Bond> getTotalQnums(
const bool &physical =
false) {
833 "getTotalQnums can only operate on UniTensor with symmetry.\n");
834 return std::vector<Bond>();
837 std::vector<std::vector<cytnx_int64>> get_blocks_qnums()
const {
839 "get_blocks_qnums can only operate on UniTensor with symmetry.\n");
840 return std::vector<std::vector<cytnx_int64>>();
843 bool same_data(
const boost::intrusive_ptr<UniTensor_base> &rhs)
const {
844 if (rhs->uten_type() !=
UTenType.Dense)
return false;
846 return this->get_block_().same_data(rhs->get_block_());
852 void Add_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
853 void Add_(
const Scalar &rhs);
855 void Mul_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
856 void Mul_(
const Scalar &rhs);
858 void Sub_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
859 void Sub_(
const Scalar &rhs);
860 void lSub_(
const Scalar &lhs);
862 void Div_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
863 void Div_(
const Scalar &rhs);
864 void lDiv_(
const Scalar &lhs);
866 void Conj_() { this->_block.Conj_(); };
868 boost::intrusive_ptr<UniTensor_base>
Conj() {
869 boost::intrusive_ptr<UniTensor_base> out = this->clone();
874 boost::intrusive_ptr<UniTensor_base> Transpose() {
875 boost::intrusive_ptr<UniTensor_base> out = this->clone();
881 boost::intrusive_ptr<UniTensor_base> normalize() {
882 boost::intrusive_ptr<UniTensor_base> out = this->clone();
888 boost::intrusive_ptr<UniTensor_base> Dagger() {
889 boost::intrusive_ptr<UniTensor_base> out = this->
Conj();
906 void Trace_(
const cytnx_int64 &a,
const cytnx_int64 &b);
907 void Trace_(
const std::string &a,
const std::string &b);
908 boost::intrusive_ptr<UniTensor_base>
Trace(
const std::string &a,
const std::string &b) {
909 boost::intrusive_ptr<UniTensor_base> out = this->clone();
913 boost::intrusive_ptr<UniTensor_base>
Trace(
const cytnx_int64 &a,
const cytnx_int64 &b) {
914 boost::intrusive_ptr<UniTensor_base> out = this->clone();
921 const Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator)
const {
923 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
925 return Scalar::Sproxy();
927 const cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
928 const cytnx_complex128 &aux)
const {
930 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
932 return cytnx_complex128(0, 0);
934 const cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
935 const cytnx_complex64 &aux)
const {
937 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
939 return cytnx_complex64(0, 0);
941 const cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
942 const cytnx_double &aux)
const {
944 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
948 const cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
949 const cytnx_float &aux)
const {
951 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
955 const cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
956 const cytnx_uint64 &aux)
const {
958 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
962 const cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
963 const cytnx_int64 &aux)
const {
965 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
969 const cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
970 const cytnx_uint32 &aux)
const {
972 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
976 const cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
977 const cytnx_int32 &aux)
const {
979 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
983 const cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
984 const cytnx_uint16 &aux)
const {
986 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
990 const cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
991 const cytnx_int16 &aux)
const {
993 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
998 Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator) {
1000 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
1002 return Scalar::Sproxy();
1004 cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1005 const cytnx_complex128 &aux) {
1007 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
1009 return *(cytnx_complex128 *)
nullptr;
1011 cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1012 const cytnx_complex64 &aux) {
1014 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
1016 return *(cytnx_complex64 *)
nullptr;
1018 cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_double &aux) {
1020 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
1022 return *(cytnx_double *)
nullptr;
1024 cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_float &aux) {
1026 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
1028 return *(cytnx_float *)
nullptr;
1030 cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_uint64 &aux) {
1032 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
1034 return *(cytnx_uint64 *)
nullptr;
1036 cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_int64 &aux) {
1038 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
1040 return *(cytnx_int64 *)
nullptr;
1042 cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_uint32 &aux) {
1044 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
1046 return *(cytnx_uint32 *)
nullptr;
1048 cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_int32 &aux) {
1050 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
1052 return *(cytnx_int32 *)
nullptr;
1054 cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_uint16 &aux) {
1056 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
1058 return *(cytnx_uint16 *)
nullptr;
1060 cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_int16 &aux) {
1062 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
1064 return *(cytnx_int16 *)
nullptr;
1067 bool elem_exists(
const std::vector<cytnx_uint64> &locator)
const {
1069 true,
"[ERROR][DenseUniTensor] elem_exists can only be used on UniTensor with Symmetry.%s",
1073 if (!this->is_tag()) {
1074 for (
int i = 0; i < this->_rowrank; i++) {
1075 this->_bonds[i] = this->_bonds[i].retype(BD_KET);
1077 for (
int i = this->_rowrank; i < this->_bonds.size(); i++) {
1078 this->_bonds[i] = this->_bonds[i].retype(BD_BRA);
1080 this->_is_tag =
true;
1081 this->_is_braket_form = this->_update_braket();
1093 void truncate_(
const cytnx_int64 &bond_idx,
const cytnx_uint64 &dim);
1094 void truncate_(
const std::string &label,
const cytnx_uint64 &dim);
1096 void from_(
const boost::intrusive_ptr<UniTensor_base> &rhs,
bool force, cytnx_double tol = 0.);
1098 void group_basis_() {
1099 cytnx_warning_msg(
true,
"[WARNING] group basis will not have any effect on DensUniTensor.%s",
1103 void _save_dispatch(std::fstream &f)
const;
1104 void _load_dispatch(std::fstream &f,
unsigned int version);
1106 const std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx)
const {
1107 cytnx_error_msg(
true,
"[ERROR] get_qindices can only be unsed on UniTensor with Symmetry.%s",
1110 std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx) {
1111 cytnx_error_msg(
true,
"[ERROR] get_qindices can only be unsed on UniTensor with Symmetry.%s",
1115 const vec2d<cytnx_uint64> &get_itoi()
const {
1116 cytnx_error_msg(
true,
"[ERROR] get_itoi can only be unsed on UniTensor with Symmetry.%s",
1119 vec2d<cytnx_uint64> &get_itoi() {
1120 cytnx_error_msg(
true,
"[ERROR] get_itoi can only be unsed on UniTensor with Symmetry.%s",
1130 class BlockUniTensor :
public UniTensor_base {
1133 std::vector<std::vector<cytnx_uint64>>
1134 _inner_to_outer_idx;
1135 std::vector<Tensor> _blocks;
1136 Tensor NullRefTensor;
1141 void _fx_get_total_fluxs(std::vector<cytnx_uint64> &loc,
const std::vector<Symmetry> &syms,
1142 std::vector<cytnx_int64> &total_qns) {
1144 loc.size() != this->_bonds.size(),
1145 "[ERROR][BlockUniTensor] qnum location rank does not match the number of bonds.%s",
"\n");
1147 "[ERROR][BlockUniTensor] total_qns size does not match symmetry count.%s",
1149 std::fill(total_qns.begin(), total_qns.end(), 0);
1150 if (this->_bonds.empty())
return;
1152 for (cytnx_int32 i = 0; i < syms.size(); i++) {
1153 if (this->_bonds[0].type() == BD_BRA)
1154 total_qns[i] = syms[i].reverse_rule(this->_bonds[0]._impl->_qnums[loc[0]][i]);
1156 total_qns[i] = this->_bonds[0]._impl->_qnums[loc[0]][i];
1158 for (
auto j = 1; j < loc.size(); j++) {
1159 if (this->_bonds[j].type() == BD_BRA)
1160 total_qns[i] = syms[i].combine_rule(
1161 total_qns[i], syms[i].reverse_rule(this->_bonds[j]._impl->_qnums[loc[j]][i]));
1164 syms[i].combine_rule(total_qns[i], this->_bonds[j]._impl->_qnums[loc[j]][i]);
1170 void _fx_locate_elem(cytnx_int64 &bidx, std::vector<cytnx_uint64> &loc_in_T,
1171 const std::vector<cytnx_uint64> &locator)
const;
1174 void _fx_group_duplicates(
const std::vector<cytnx_uint64> &dup_bond_idxs,
1175 const std::vector<std::vector<cytnx_uint64>> &idx_mappers);
1177 void set_meta(BlockUniTensor *tmp,
const bool &inner,
const bool &outer)
const {
1180 tmp->_bonds = vec_clone(this->_bonds);
1181 tmp->_labels = this->_labels;
1182 tmp->syms_cache = vec_clone(this->syms_cache);
1183 tmp->_is_braket_form = this->_is_braket_form;
1184 tmp->_rowrank = this->_rowrank;
1185 tmp->_name = this->_name;
1188 tmp->_is_diag = this->_is_diag;
1192 tmp->_inner_to_outer_idx = this->_inner_to_outer_idx;
1196 boost::intrusive_ptr<BlockUniTensor> clone_meta(
const bool &inner,
const bool &outer)
const {
1197 boost::intrusive_ptr<BlockUniTensor> tmp(
new BlockUniTensor());
1198 this->set_meta(tmp.get(), inner, outer);
1202 friend class UniTensor;
1204 this->uten_type_id =
UTenType.Block;
1205 this->_is_tag =
true;
1214 void Init(
const std::vector<Bond> &bonds,
const std::vector<std::string> &in_labels = {},
1215 const cytnx_int64 &rowrank = -1,
const unsigned int &dtype =
Type.Double,
1216 const int &device =
Device.cpu,
const bool &is_diag =
false,
1217 const bool &no_alloc =
false,
const std::string &name =
"");
1219 void Init_by_Tensor(
const Tensor &in_tensor,
const bool &is_diag =
false,
1220 const cytnx_int64 &rowrank = -1,
const std::string &name =
"") {
1222 true,
"[ERROR][BlockUniTensor] Cannot use Init_by_tensor() on a BlockUniTensor.%s",
"\n");
1225 std::vector<cytnx_uint64> shape()
const {
1226 if (this->rank() == 0)
return {};
1227 std::vector<cytnx_uint64> out(this->_bonds.size());
1228 for (cytnx_uint64 i = 0; i < out.size(); i++) {
1229 out[i] = this->_bonds[i].dim();
1234 bool is_blockform()
const {
return true; }
1235 bool is_contiguous()
const {
1237 for (
int i = 0; i < this->_blocks.size(); i++) {
1238 out &= this->_blocks[i].is_contiguous();
1243 cytnx_uint64 Nblocks()
const {
return this->_blocks.size(); };
1245 void to_(
const int &device) {
1246 for (cytnx_uint64 i = 0; i < this->_blocks.size(); i++) {
1247 this->_blocks[i].to_(device);
1251 boost::intrusive_ptr<UniTensor_base> to(
const int &device) {
1252 if (this->device() == device) {
1255 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1261 boost::intrusive_ptr<UniTensor_base> clone()
const {
1262 boost::intrusive_ptr<BlockUniTensor> tmp = this->clone_meta(
true,
true);
1263 tmp->_blocks = vec_clone(this->_blocks);
1267 unsigned int dtype()
const {
1270 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
1273 return this->_blocks.size() < 1 ?
Type.Void : this->_blocks[0].dtype();
1275 int device()
const {
1278 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
1281 return this->_blocks.size() < 1 ? -404 : this->_blocks[0].device();
1283 std::string dtype_str()
const {
1286 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
1289 return this->_blocks.size() < 1 ?
"Void, no valid blocks" : this->_blocks[0].dtype_str();
1291 std::string device_str()
const {
1294 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
1297 return this->_blocks.size() < 1 ?
"None, no valid blocks" : this->_blocks[0].device_str();
1300 Tensor get_block(
const cytnx_uint64 &idx = 0)
const {
1301 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1303 return this->_blocks[idx].clone();
1307 Tensor get_block(
const std::vector<cytnx_int64> &indices,
const bool &force_return)
const {
1308 if (this->rank() == 0) {
1310 "[ERROR][get_block][BlockUniTensor] rank-0 scalar block expects no "
1313 cytnx_error_msg(this->_blocks.empty(),
"[ERROR][BlockUniTensor] index out of range%s",
1315 return this->_blocks[0].clone();
1318 "[ERROR][get_block][BlockUniTensor] len(indices) must be the same as the "
1319 "Tensor rank (number of legs).%s",
1322 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1326 for (cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1327 if (inds == this->_inner_to_outer_idx[i]) {
1335 return NullRefTensor;
1338 "[ERROR][get_block][BlockUniTensor] no avaliable block exists, "
1339 "force_return=false, so "
1340 "error throws. \n If you want to return an empty block without "
1341 "error when block is "
1342 "not avaliable, set force_return=True.%s",
1346 return this->_blocks[b].clone();
1350 const Tensor &get_block_(
const cytnx_uint64 &idx = 0)
const {
1351 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1353 return this->_blocks[idx];
1356 Tensor &get_block_(
const cytnx_uint64 &idx = 0) {
1357 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1359 return this->_blocks[idx];
1362 const Tensor &get_block_(
const std::vector<cytnx_int64> &indices,
1363 const bool &force_return)
const {
1364 if (this->rank() == 0) {
1366 "[ERROR][get_block][BlockUniTensor] rank-0 scalar block expects no "
1369 cytnx_error_msg(this->_blocks.empty(),
"[ERROR][BlockUniTensor] index out of range%s",
1371 return this->_blocks[0];
1374 "[ERROR][get_block][BlockUniTensor] len(indices) must be the same as the "
1375 "Tensor rank (number of legs).%s",
1378 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1382 for (cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1383 if (inds == this->_inner_to_outer_idx[i]) {
1391 return this->NullRefTensor;
1394 "[ERROR][get_block][BlockUniTensor] no avaliable block exists, "
1395 "force_return=false, so "
1396 "error throws. \n If you want to return an empty block without "
1397 "error when block is "
1398 "not avaliable, set force_return=True.%s",
1402 return this->_blocks[b];
1406 Tensor &get_block_(
const std::vector<cytnx_int64> &indices,
const bool &force_return) {
1407 if (this->rank() == 0) {
1409 "[ERROR][get_block][BlockUniTensor] rank-0 scalar block expects no "
1412 cytnx_error_msg(this->_blocks.empty(),
"[ERROR][BlockUniTensor] index out of range%s",
1414 return this->_blocks[0];
1417 "[ERROR][get_block][BlockUniTensor] len(indices) must be the same as the "
1418 "Tensor rank (number of legs).%s",
1421 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1425 for (cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1426 if (inds == this->_inner_to_outer_idx[i]) {
1434 return this->NullRefTensor;
1437 "[ERROR][get_block][BlockUniTensor] no avaliable block exists, "
1438 "force_return=false, so "
1439 "error throws. \n If you want to return an empty block without "
1440 "error when block is "
1441 "not avaliable, set force_return=True.%s",
1445 return this->_blocks[b];
1449 std::vector<Tensor> get_blocks()
const {
return vec_clone(this->_blocks); }
1450 const std::vector<Tensor> &get_blocks_(
const bool &)
const {
return this->_blocks; }
1451 std::vector<Tensor> &get_blocks_(
const bool &) {
return this->_blocks; }
1453 bool same_data(
const boost::intrusive_ptr<UniTensor_base> &rhs)
const {
1454 if (rhs->uten_type() !=
UTenType.Block)
return false;
1455 if (rhs->get_blocks_(1).size() != this->get_blocks_(1).size())
return false;
1457 for (
int i = 0; i < rhs->get_blocks_(1).size(); i++)
1458 if (this->get_blocks_(1)[i].same_data(rhs->get_blocks_(1)[i]) ==
false)
return false;
1463 void set_rowrank_(
const cytnx_uint64 &new_rowrank) {
1465 "[ERROR][BlockUniTensor] rowrank should be [>=0] and [<=UniTensor.rank].%s",
1467 if (this->is_diag()) {
1469 "[ERROR][BlockUniTensor] rowrank should be [==1] when is_diag =true!.%s",
1472 this->_rowrank = new_rowrank;
1473 this->_is_braket_form = this->_update_braket();
1476 boost::intrusive_ptr<UniTensor_base> set_rowrank(
const cytnx_uint64 &new_rowrank)
const {
1477 boost::intrusive_ptr<BlockUniTensor> tmp = this->clone_meta(
true,
true);
1478 tmp->_blocks = this->_blocks;
1479 tmp->set_rowrank_(new_rowrank);
1483 boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<cytnx_int64> &mapper,
1484 const cytnx_int64 &rowrank = -1);
1485 boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<std::string> &mapper,
1486 const cytnx_int64 &rowrank = -1);
1488 void permute_(
const std::vector<cytnx_int64> &mapper,
const cytnx_int64 &rowrank = -1);
1489 void permute_(
const std::vector<std::string> &mapper,
const cytnx_int64 &rowrank = -1);
1491 void twist_(
const cytnx_int64 &idx)
override {
1495 void fermion_twists_()
override {
1499 void twist_(
const std::string &label)
override {
1504 boost::intrusive_ptr<UniTensor_base> contiguous_() {
1509 for (
unsigned int b = 0; b < this->_blocks.size(); b++)
1510 this->_blocks[b] = this->_blocks[b].contiguous();
1511 return boost::intrusive_ptr<UniTensor_base>(
this);
1513 boost::intrusive_ptr<UniTensor_base> contiguous();
1515 boost::intrusive_ptr<UniTensor_base> apply_() {
1516 return boost::intrusive_ptr<UniTensor_base>(
this);
1518 boost::intrusive_ptr<UniTensor_base> apply() {
1520 boost::intrusive_ptr<UniTensor_base> out(
this);
1524 void print_diagram(
const bool &bond_info =
false)
const;
1525 void print_blocks(
const bool &full_info =
true)
const;
1526 void print_block(
const cytnx_int64 &idx,
const bool &full_info =
true)
const;
1528 boost::intrusive_ptr<UniTensor_base> contract(
const boost::intrusive_ptr<UniTensor_base> &rhs,
1529 const bool &mv_elem_self =
false,
1530 const bool &mv_elem_rhs =
false);
1532 boost::intrusive_ptr<UniTensor_base> relabel(
const std::vector<std::string> &new_labels);
1533 boost::intrusive_ptr<UniTensor_base> relabels(
const std::vector<std::string> &new_labels);
1535 boost::intrusive_ptr<UniTensor_base> relabel(
const std::vector<std::string> &old_labels,
1536 const std::vector<std::string> &new_labels);
1537 boost::intrusive_ptr<UniTensor_base> relabels(
const std::vector<std::string> &old_labels,
1538 const std::vector<std::string> &new_labels);
1540 boost::intrusive_ptr<UniTensor_base> relabel(
const std::string &old_label,
1541 const std::string &new_label);
1542 boost::intrusive_ptr<UniTensor_base> relabel(
const cytnx_int64 &inx,
1543 const std::string &new_label);
1545 std::vector<Symmetry> syms()
const;
1547 void reshape_(
const std::vector<cytnx_int64> &new_shape,
const cytnx_uint64 &rowrank = 0) {
1548 cytnx_error_msg(
true,
"[ERROR] Cannot reshape a UniTensor with symmetry.%s",
"\n");
1550 boost::intrusive_ptr<UniTensor_base> reshape(
const std::vector<cytnx_int64> &new_shape,
1551 const cytnx_uint64 &rowrank = 0) {
1552 cytnx_error_msg(
true,
"[ERROR] Cannot reshape a UniTensor with symmetry.%s",
"\n");
1556 boost::intrusive_ptr<UniTensor_base> to_dense();
1559 boost::intrusive_ptr<UniTensor_base> astype(
const unsigned int &dtype)
const {
1560 boost::intrusive_ptr<BlockUniTensor> tmp = this->clone_meta(
true,
true);
1561 tmp->_blocks.resize(this->_blocks.size());
1562 for (cytnx_int64 blk = 0; blk < this->_blocks.size(); blk++) {
1563 tmp->_blocks[blk] = this->_blocks[blk].astype(dtype);
1569 boost::intrusive_ptr<UniTensor_base> get(
const std::vector<Accessor> &accessors) {
1572 "[ERROR][BlockUniTensor][get] Cannot use get on a UniTensor with "
1573 "Symmetry.\n suggestion: try get_block/get_block_/get_blocks/get_blocks_ first.%s",
1579 void set(
const std::vector<Accessor> &accessors,
const Tensor &rhs) {
1582 "[ERROR][BlockUniTensor][get] Cannot use get on a UniTensor with "
1583 "Symmetry.\n suggestion: try get_block/get_block_/get_blocks/get_blocks_ first.%s",
1587 void put_block(
const Tensor &in,
const cytnx_uint64 &idx = 0) {
1589 "[ERROR][BlockUniTensor][put_block] The input tensor dtype does not match.%s",
1592 "[ERROR][BlockUniTensor][put_block] The input tensor device does not "
1597 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1600 "[ERROR][BlockUniTensor] the shape of input tensor does not match the shape "
1601 "of block @ idx=%d\n",
1604 this->_blocks[idx] = in.clone();
1606 void put_block_(Tensor &in,
const cytnx_uint64 &idx = 0) {
1608 "[ERROR][BlockUniTensor][put_block] The input tensor dtype does not match.%s",
1611 "[ERROR][BlockUniTensor][put_block] The input tensor device does not "
1616 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1619 "[ERROR][BlockUniTensor] the shape of input tensor does not match the shape "
1620 "of block @ idx=%d\n",
1623 this->_blocks[idx] = in;
1625 void put_block(
const Tensor &in,
const std::vector<cytnx_int64> &indices) {
1627 "[ERROR][BlockUniTensor][put_block] The input tensor dtype does not match.%s",
1630 "[ERROR][BlockUniTensor][put_block] The input tensor device does not "
1636 "[ERROR][put_block][BlockUniTensor] len(indices) must be the same as the "
1637 "Tensor rank (number of legs).%s",
1640 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1644 for (cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1645 if (inds == this->_inner_to_outer_idx[i]) {
1652 cytnx_error_msg(
true,
"[ERROR][put_block][BlockUniTensor] no avaliable block exists.%s",
1656 in.shape() != this->_blocks[b].shape(),
1657 "[ERROR][BlockUniTensor] the shape of input tensor does not match the shape "
1658 "of block @ idx=%d\n",
1661 this->_blocks[b] = in.clone();
1664 void put_block_(Tensor &in,
const std::vector<cytnx_int64> &indices) {
1666 "[ERROR][BlockUniTensor][put_block] The input tensor dtype does not match.%s",
1669 "[ERROR][BlockUniTensor][put_block] The input tensor device does not "
1675 "[ERROR][put_block][BlockUniTensor] len(indices) must be the same as the "
1676 "Tensor rank (number of legs).%s",
1679 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1683 for (cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1684 if (inds == this->_inner_to_outer_idx[i]) {
1691 cytnx_error_msg(
true,
"[ERROR][put_block][BlockUniTensor] no avaliable block exists.%s",
1695 in.shape() != this->_blocks[b].shape(),
1696 "[ERROR][BlockUniTensor] the shape of input tensor does not match the shape "
1697 "of block @ idx=%d\n",
1699 this->_blocks[b] = in;
1707 boost::intrusive_ptr<UniTensor_base>
Conj() {
1708 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1714 for (
int i = 0; i < this->_blocks.size(); i++) {
1715 this->_blocks[i].Conj_();
1720 boost::intrusive_ptr<UniTensor_base> Transpose() {
1721 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1727 boost::intrusive_ptr<UniTensor_base> normalize() {
1728 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1733 boost::intrusive_ptr<UniTensor_base> Dagger() {
1734 boost::intrusive_ptr<UniTensor_base> out = this->
Conj();
1743 void Trace_(
const std::string &a,
const std::string &b);
1744 void Trace_(
const cytnx_int64 &a,
const cytnx_int64 &b);
1746 boost::intrusive_ptr<UniTensor_base>
Trace(
const std::string &a,
const std::string &b) {
1747 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1751 boost::intrusive_ptr<UniTensor_base>
Trace(
const cytnx_int64 &a,
const cytnx_int64 &b) {
1752 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1757 Tensor
Norm()
const;
1759 bool elem_exists(
const std::vector<cytnx_uint64> &locator)
const;
1761 const Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator)
const;
1762 const cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1763 const cytnx_complex128 &aux)
const;
1764 const cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1765 const cytnx_complex64 &aux)
const;
1766 const cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1767 const cytnx_double &aux)
const;
1768 const cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1769 const cytnx_float &aux)
const;
1770 const cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1771 const cytnx_uint64 &aux)
const;
1772 const cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1773 const cytnx_int64 &aux)
const;
1774 const cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1775 const cytnx_uint32 &aux)
const;
1776 const cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1777 const cytnx_int32 &aux)
const;
1778 const cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1779 const cytnx_uint16 &aux)
const;
1780 const cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1781 const cytnx_int16 &aux)
const;
1783 Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator);
1784 cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1785 const cytnx_complex128 &aux);
1786 cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1787 const cytnx_complex64 &aux);
1788 cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_double &aux);
1789 cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_float &aux);
1790 cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_uint64 &aux);
1791 cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_int64 &aux);
1792 cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_uint32 &aux);
1793 cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_int32 &aux);
1794 cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_uint16 &aux);
1795 cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_int16 &aux);
1797 void _save_dispatch(std::fstream &f)
const;
1798 void _load_dispatch(std::fstream &f,
unsigned int version);
1801 void truncate_(
const std::string &label,
const cytnx_uint64 &q_index);
1802 void truncate_(
const cytnx_int64 &bond_idx,
const cytnx_uint64 &q_index);
1804 void Add_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
1805 void Add_(
const Scalar &rhs) {
1808 "[ERROR] Cannot perform elementwise arithmetic '+' between Scalar and BlockUniTensor.\n %s "
1810 "This operation would destroy the block structure. [Suggest] Avoid or use get/put_block(s) "
1811 "to do operation on blocks.");
1814 void Mul_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
1815 void Mul_(
const Scalar &rhs);
1817 void Sub_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
1818 void Sub_(
const Scalar &rhs) {
1821 "[ERROR] Cannot perform elementwise arithmetic '-' between Scalar and BlockUniTensor.\n %s "
1823 "This operation would destroy the block structure. [Suggest] Avoid or use get/put_block(s) "
1824 "to do operation on blocks.");
1826 void lSub_(
const Scalar &lhs) {
1829 "[ERROR] Cannot perform elementwise arithmetic '-' between Scalar and BlockUniTensor.\n %s "
1831 "This operation would destroy the block structure. [Suggest] Avoid or use get/put_block(s) "
1832 "to do operation on blocks.");
1835 void Div_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
1836 void Div_(
const Scalar &rhs);
1837 void lDiv_(
const Scalar &lhs) {
1840 "[ERROR] Cannot perform elementwise arithmetic '/' between Scalar and BlockUniTensor.\n %s "
1842 "This operation would cause division by zero on non-block elements. [Suggest] Avoid or use "
1843 "get/put_block(s) to do operation on blocks.");
1845 void from_(
const boost::intrusive_ptr<UniTensor_base> &rhs,
bool force, cytnx_double tol = 0.);
1847 void group_basis_();
1849 void combineBond(
const std::vector<std::string> &indicators,
const bool &force =
false);
1850 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force =
false);
1851 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force,
1852 const bool &by_label);
1853 void combineBonds(
const std::vector<std::string> &indicators,
const bool &force =
false);
1855 const std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx)
const {
1857 bidx >= this->Nblocks(),
1858 "[ERROR][BlockUniTensor] bidx out of bound! only %d blocks in current UTen.\n",
1860 return this->_inner_to_outer_idx[bidx];
1862 std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx) {
1864 bidx >= this->Nblocks(),
1865 "[ERROR][BlockUniTensor] bidx out of bound! only %d blocks in current UTen.\n",
1867 return this->_inner_to_outer_idx[bidx];
1870 const vec2d<cytnx_uint64> &get_itoi()
const {
return this->_inner_to_outer_idx; }
1871 vec2d<cytnx_uint64> &get_itoi() {
return this->_inner_to_outer_idx; }
1877 class BlockFermionicUniTensor :
public UniTensor_base {
1881 std::vector<std::vector<cytnx_uint64>>
1882 _inner_to_outer_idx;
1883 std::vector<Tensor> _blocks;
1884 Tensor NullRefTensor;
1889 void _fx_get_total_fluxs(std::vector<cytnx_uint64> &loc,
const std::vector<Symmetry> &syms,
1890 std::vector<cytnx_int64> &total_qns) {
1893 loc.size() != this->_bonds.size(),
1894 "[ERROR][BlockFermionicUniTensor] qnum location rank does not match the number of bonds.%s",
1897 total_qns.size() != syms.size(),
1898 "[ERROR][BlockFermionicUniTensor] total_qns size does not match symmetry count.%s",
"\n");
1899 std::fill(total_qns.begin(), total_qns.end(), 0);
1900 if (this->_bonds.empty())
return;
1902 for (cytnx_int32 i = 0; i < syms.size(); i++) {
1903 if (this->_bonds[0].type() == BD_BRA)
1904 total_qns[i] = syms[i].reverse_rule(this->_bonds[0]._impl->_qnums[loc[0]][i]);
1906 total_qns[i] = this->_bonds[0]._impl->_qnums[loc[0]][i];
1908 for (
auto j = 1; j < loc.size(); j++) {
1909 if (this->_bonds[j].type() == BD_BRA)
1910 total_qns[i] = syms[i].combine_rule(
1911 total_qns[i], syms[i].reverse_rule(this->_bonds[j]._impl->_qnums[loc[j]][i]));
1914 syms[i].combine_rule(total_qns[i], this->_bonds[j]._impl->_qnums[loc[j]][i]);
1920 void _fx_locate_elem(cytnx_int64 &bidx, std::vector<cytnx_uint64> &loc_in_T,
1921 const std::vector<cytnx_uint64> &locator)
const;
1924 void _fx_group_duplicates(
const std::vector<cytnx_uint64> &dup_bond_idxs,
1925 const std::vector<std::vector<cytnx_uint64>> &idx_mappers);
1927 void set_meta(BlockFermionicUniTensor *tmp,
const bool &inner,
const bool &outer)
const {
1931 tmp->_bonds = vec_clone(this->_bonds);
1932 tmp->_labels = this->_labels;
1933 tmp->syms_cache = vec_clone(this->syms_cache);
1934 tmp->_is_braket_form = this->_is_braket_form;
1935 tmp->_rowrank = this->_rowrank;
1936 tmp->_name = this->_name;
1938 tmp->_signflip = this->_signflip;
1941 tmp->_is_diag = this->_is_diag;
1945 tmp->_inner_to_outer_idx = this->_inner_to_outer_idx;
1949 boost::intrusive_ptr<BlockFermionicUniTensor> clone_meta(
const bool &inner,
1950 const bool &outer)
const {
1952 boost::intrusive_ptr<BlockFermionicUniTensor> tmp(
new BlockFermionicUniTensor());
1953 this->set_meta(tmp.get(), inner, outer);
1957 friend class UniTensor;
1958 BlockFermionicUniTensor() {
1960 this->uten_type_id =
UTenType.BlockFermionic;
1961 this->_is_tag =
true;
1964 void Init(
const std::vector<Bond> &bonds,
const std::vector<std::string> &in_labels = {},
1965 const cytnx_int64 &rowrank = -1,
const unsigned int &dtype =
Type.Double,
1966 const int &device =
Device.cpu,
const bool &is_diag =
false,
1967 const bool &no_alloc =
false,
const std::string &name =
"");
1969 void Init_by_Tensor(
const Tensor &in_tensor,
const bool &is_diag =
false,
1970 const cytnx_int64 &rowrank = -1,
const std::string &name =
"") {
1972 "[ERROR][BlockFermionicUniTensor] Cannot use Init_by_tensor() on a "
1973 "BlockFermionicUniTensor.%s",
1977 std::vector<cytnx_uint64> shape()
const {
1979 if (this->rank() == 0)
return {};
1980 std::vector<cytnx_uint64> out(this->_bonds.size());
1981 for (cytnx_uint64 i = 0; i < out.size(); i++) {
1982 out[i] = this->_bonds[i].dim();
1987 bool is_blockform()
const {
1992 bool is_contiguous()
const {
1995 for (
int i = 0; i < this->_blocks.size(); i++) {
1996 out &= this->_blocks[i].is_contiguous();
2001 cytnx_uint64 Nblocks()
const {
2003 return this->_blocks.size();
2006 std::vector<bool> signflip()
const override {
return this->_signflip; };
2011 void reset_signflip_() { this->_signflip.assign(this->_blocks.size(),
false); }
2017 void erase_signflip_(
const std::vector<cytnx_uint64> &positions);
2019 void to_(
const int &device) {
2021 for (cytnx_uint64 i = 0; i < this->_blocks.size(); i++) {
2022 this->_blocks[i].to_(device);
2026 boost::intrusive_ptr<UniTensor_base> to(
const int &device) {
2028 if (this->device() == device) {
2031 boost::intrusive_ptr<UniTensor_base> out = this->clone();
2037 boost::intrusive_ptr<UniTensor_base> clone()
const {
2040 boost::intrusive_ptr<BlockFermionicUniTensor> tmp = this->clone_meta(
true,
true);
2041 tmp->_blocks = vec_clone(this->_blocks);
2045 unsigned int dtype()
const {
2048 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
2051 return this->_blocks.size() < 1 ?
Type.Void : this->_blocks[0].dtype();
2053 int device()
const {
2056 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
2059 return this->_blocks.size() < 1 ? -404 : this->_blocks[0].device();
2061 std::string dtype_str()
const {
2064 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
2067 return this->_blocks.size() < 1 ?
"Void, no valid blocks" : this->_blocks[0].dtype_str();
2069 std::string device_str()
const {
2072 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
2075 return this->_blocks.size() < 1 ?
"None, no valid blocks" : this->_blocks[0].device_str();
2078 Tensor get_block(
const cytnx_uint64 &idx = 0)
const {
2081 "[ERROR][BlockFermionicUniTensor] index out of range%s",
"\n");
2082 return this->_blocks[idx].clone();
2086 Tensor get_block(
const std::vector<cytnx_int64> &indices,
const bool &force_return)
const {
2088 if (this->rank() == 0) {
2090 "[ERROR][get_block][BlockFermionicUniTensor] rank-0 scalar block expects "
2091 "no qnum indices.%s",
2094 "[ERROR][BlockFermionicUniTensor] index out of range%s",
"\n");
2095 return this->_blocks[0].clone();
2098 indices.size() != this->rank(),
2099 "[ERROR][get_block][BlockFermionicUniTensor] len(indices) must be the same as the "
2100 "Tensor rank (number of legs).%s",
2103 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
2107 for (cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
2108 if (inds == this->_inner_to_outer_idx[i]) {
2116 return NullRefTensor;
2119 "[ERROR][get_block][BlockFermionicUniTensor] no avaliable block exists, "
2120 "force_return=false, so "
2121 "error throws. \n If you want to return an empty block without "
2122 "error when block is "
2123 "not avaliable, set force_return=True.%s",
2127 return this->_blocks[b].clone();
2131 const Tensor &get_block_(
const cytnx_uint64 &idx = 0)
const {
2134 "[ERROR][BlockFermionicUniTensor] index out of range%s",
"\n");
2135 return this->_blocks[idx];
2138 Tensor &get_block_(
const cytnx_uint64 &idx = 0) {
2141 "[ERROR][BlockFermionicUniTensor] index out of range%s",
"\n");
2142 return this->_blocks[idx];
2145 const Tensor &get_block_(
const std::vector<cytnx_int64> &indices,
2146 const bool &force_return)
const {
2148 if (this->rank() == 0) {
2150 "[ERROR][get_block][BlockFermionicUniTensor] rank-0 scalar block expects "
2151 "no qnum indices.%s",
2154 "[ERROR][BlockFermionicUniTensor] index out of range%s",
"\n");
2155 return this->_blocks[0];
2158 indices.size() != this->rank(),
2159 "[ERROR][get_block][BlockFermionicUniTensor] len(indices) must be the same as the "
2160 "Tensor rank (number of legs).%s",
2163 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
2167 for (cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
2168 if (inds == this->_inner_to_outer_idx[i]) {
2176 return this->NullRefTensor;
2179 "[ERROR][get_block][BlockFermionicUniTensor] no avaliable block exists, "
2180 "force_return=false, so "
2181 "error throws. \n If you want to return an empty block without "
2182 "error when block is "
2183 "not avaliable, set force_return=True.%s",
2187 return this->_blocks[b];
2191 Tensor &get_block_(
const std::vector<cytnx_int64> &indices,
const bool &force_return) {
2193 if (this->rank() == 0) {
2195 "[ERROR][get_block][BlockFermionicUniTensor] rank-0 scalar block expects "
2196 "no qnum indices.%s",
2199 "[ERROR][BlockFermionicUniTensor] index out of range%s",
"\n");
2200 return this->_blocks[0];
2203 indices.size() != this->rank(),
2204 "[ERROR][get_block][BlockFermionicUniTensor] len(indices) must be the same as the "
2205 "Tensor rank (number of legs).%s",
2208 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
2212 for (cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
2213 if (inds == this->_inner_to_outer_idx[i]) {
2221 return this->NullRefTensor;
2224 "[ERROR][get_block][BlockFermionicUniTensor] no avaliable block exists, "
2225 "force_return=false, so "
2226 "error throws. \n If you want to return an empty block without "
2227 "error when block is "
2228 "not avaliable, set force_return=True.%s",
2232 return this->_blocks[b];
2236 std::vector<Tensor> get_blocks()
const {
2238 return vec_clone(this->_blocks);
2240 const std::vector<Tensor> &get_blocks_(
const bool &)
const {
2242 return this->_blocks;
2244 std::vector<Tensor> &get_blocks_(
const bool &) {
2246 return this->_blocks;
2249 bool same_data(
const boost::intrusive_ptr<UniTensor_base> &rhs)
const {
2251 if (rhs->uten_type() !=
UTenType.BlockFermionic)
return false;
2252 if (rhs->get_blocks_(1).size() != this->get_blocks_(1).size())
return false;
2254 for (
int i = 0; i < rhs->get_blocks_(1).size(); i++)
2255 if (this->get_blocks_(1)[i].same_data(rhs->get_blocks_(1)[i]) ==
false)
return false;
2260 void set_rowrank_(
const cytnx_uint64 &new_rowrank) {
2263 new_rowrank > this->rank(),
2264 "[ERROR][BlockFermionicUniTensor] rowrank should be [>=0] and [<=UniTensor.rank].%s",
"\n");
2265 if (this->is_diag()) {
2268 "[ERROR][BlockFermionicUniTensor] rowrank should be [==1] when is_diag =true!.%s",
"\n");
2270 this->_rowrank = new_rowrank;
2271 this->_is_braket_form = this->_update_braket();
2274 boost::intrusive_ptr<UniTensor_base> set_rowrank(
const cytnx_uint64 &new_rowrank)
const {
2277 boost::intrusive_ptr<BlockFermionicUniTensor> tmp = this->clone_meta(
true,
true);
2278 tmp->_blocks = this->_blocks;
2279 tmp->set_rowrank_(new_rowrank);
2283 boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<cytnx_int64> &mapper,
2284 const cytnx_int64 &rowrank = -1);
2285 boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<std::string> &mapper,
2286 const cytnx_int64 &rowrank = -1);
2288 void permute_(
const std::vector<cytnx_int64> &mapper,
const cytnx_int64 &rowrank = -1)
override;
2289 void permute_(
const std::vector<std::string> &mapper,
const cytnx_int64 &rowrank = -1)
override;
2291 boost::intrusive_ptr<UniTensor_base> permute_nosignflip(
2292 const std::vector<cytnx_int64> &mapper,
const cytnx_int64 &rowrank = -1)
override;
2293 boost::intrusive_ptr<UniTensor_base> permute_nosignflip(
2294 const std::vector<std::string> &mapper,
const cytnx_int64 &rowrank = -1)
override;
2295 void permute_nosignflip_(
const std::vector<cytnx_int64> &mapper,
2296 const cytnx_int64 &rowrank = -1)
override;
2297 void permute_nosignflip_(
const std::vector<std::string> &mapper,
2298 const cytnx_int64 &rowrank = -1)
override;
2300 void twist_(
const cytnx_int64 &idx)
override;
2301 void twist_(
const std::string &label)
override;
2302 void fermion_twists_()
override;
2305 std::vector<bool> _swapsigns_(
const std::vector<cytnx_int64> &mapper)
const;
2306 std::vector<bool> _lhssigns_(
const std::vector<cytnx_int64> &mapper,
2307 const cytnx_int64 contrno)
const;
2308 std::vector<bool> _swapsigns_(
const std::vector<cytnx_uint64> &mapper)
const;
2309 std::vector<bool> _lhssigns_(
const std::vector<cytnx_uint64> &mapper,
2310 const cytnx_uint64 contrno)
const;
2312 boost::intrusive_ptr<UniTensor_base> contiguous_() {
2317 for (
unsigned int b = 0; b < this->_blocks.size(); b++)
2318 this->_blocks[b] = this->_blocks[b].contiguous();
2319 return boost::intrusive_ptr<UniTensor_base>(
this);
2321 boost::intrusive_ptr<UniTensor_base> contiguous();
2323 boost::intrusive_ptr<UniTensor_base> apply_();
2324 boost::intrusive_ptr<UniTensor_base> apply();
2326 void print_diagram(
const bool &bond_info =
false)
const;
2327 void print_blocks(
const bool &full_info =
true)
const;
2328 void print_block(
const cytnx_int64 &idx,
const bool &full_info =
true)
const;
2330 boost::intrusive_ptr<UniTensor_base> contract(
const boost::intrusive_ptr<UniTensor_base> &rhs,
2331 const bool &mv_elem_self =
false,
2332 const bool &mv_elem_rhs =
false);
2334 boost::intrusive_ptr<UniTensor_base> relabel(
const std::vector<std::string> &new_labels);
2335 boost::intrusive_ptr<UniTensor_base> relabels(
const std::vector<std::string> &new_labels);
2337 boost::intrusive_ptr<UniTensor_base> relabel(
const std::vector<std::string> &old_labels,
2338 const std::vector<std::string> &new_labels);
2339 boost::intrusive_ptr<UniTensor_base> relabels(
const std::vector<std::string> &old_labels,
2340 const std::vector<std::string> &new_labels);
2342 boost::intrusive_ptr<UniTensor_base> relabel(
const std::string &old_label,
2343 const std::string &new_label);
2344 boost::intrusive_ptr<UniTensor_base> relabel(
const cytnx_int64 &inx,
2345 const std::string &new_label);
2346 std::vector<Symmetry> syms()
const;
2348 void reshape_(
const std::vector<cytnx_int64> &new_shape,
const cytnx_uint64 &rowrank = 0) {
2349 cytnx_error_msg(
true,
"[ERROR] Cannot reshape a UniTensor with symmetry.%s",
"\n");
2351 boost::intrusive_ptr<UniTensor_base> reshape(
const std::vector<cytnx_int64> &new_shape,
2352 const cytnx_uint64 &rowrank = 0) {
2353 cytnx_error_msg(
true,
"[ERROR] Cannot reshape a UniTensor with symmetry.%s",
"\n");
2357 boost::intrusive_ptr<UniTensor_base> to_dense();
2360 boost::intrusive_ptr<UniTensor_base> astype(
const unsigned int &dtype)
const {
2362 boost::intrusive_ptr<BlockFermionicUniTensor> tmp = this->clone_meta(
true,
true);
2363 tmp->_blocks.resize(this->_blocks.size());
2364 for (cytnx_int64 blk = 0; blk < this->_blocks.size(); blk++) {
2365 tmp->_blocks[blk] = this->_blocks[blk].astype(dtype);
2371 boost::intrusive_ptr<UniTensor_base> get(
const std::vector<Accessor> &accessors) {
2374 "[ERROR][BlockFermionicUniTensor][get] Cannot use get on a UniTensor with "
2375 "Symmetry.\n suggestion: try get_block/get_block_/get_blocks/get_blocks_ first.%s",
2381 void set(
const std::vector<Accessor> &accessors,
const Tensor &rhs) {
2385 "[ERROR][BlockFermionicUniTensor][get] Cannot use get on a UniTensor with "
2386 "Symmetry.\n suggestion: try get_block/get_block_/get_blocks/get_blocks_ first.%s",
2390 void put_block(
const Tensor &in,
const cytnx_uint64 &idx = 0) {
2393 in.dtype() != this->dtype(),
2394 "[ERROR][BlockFermionicUniTensor][put_block] The input tensor dtype does not match.%s",
2397 in.device() != this->device(),
2398 "[ERROR][BlockFermionicUniTensor][put_block] The input tensor device does not "
2404 "[ERROR][BlockFermionicUniTensor] index out of range%s",
"\n");
2406 in.shape() != this->_blocks[idx].shape(),
2407 "[ERROR][BlockFermionicUniTensor] the shape of input tensor does not match the shape "
2408 "of block @ idx=%d\n",
2411 this->_blocks[idx] = in.clone();
2413 void put_block_(Tensor &in,
const cytnx_uint64 &idx = 0) {
2416 in.dtype() != this->dtype(),
2417 "[ERROR][BlockFermionicUniTensor][put_block] The input tensor dtype does not match.%s",
2420 in.device() != this->device(),
2421 "[ERROR][BlockFermionicUniTensor][put_block] The input tensor device does not "
2427 "[ERROR][BlockFermionicUniTensor] index out of range%s",
"\n");
2429 in.shape() != this->_blocks[idx].shape(),
2430 "[ERROR][BlockFermionicUniTensor] the shape of input tensor does not match the shape "
2431 "of block @ idx=%d\n",
2434 this->_blocks[idx] = in;
2436 void put_block(
const Tensor &in,
const std::vector<cytnx_int64> &indices) {
2439 in.dtype() != this->dtype(),
2440 "[ERROR][BlockFermionicUniTensor][put_block] The input tensor dtype does not match.%s",
2443 in.device() != this->device(),
2444 "[ERROR][BlockFermionicUniTensor][put_block] The input tensor device does not "
2450 indices.size() != this->rank(),
2451 "[ERROR][put_block][BlockFermionicUniTensor] len(indices) must be the same as the "
2452 "Tensor rank (number of legs).%s",
2455 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
2459 for (cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
2460 if (inds == this->_inner_to_outer_idx[i]) {
2468 true,
"[ERROR][put_block][BlockFermionicUniTensor] no avaliable block exists.%s",
"\n");
2471 in.shape() != this->_blocks[b].shape(),
2472 "[ERROR][BlockFermionicUniTensor] the shape of input tensor does not match the shape "
2473 "of block @ idx=%d\n",
2476 this->_blocks[b] = in.clone();
2479 void put_block_(Tensor &in,
const std::vector<cytnx_int64> &indices) {
2482 in.dtype() != this->dtype(),
2483 "[ERROR][BlockFermionicUniTensor][put_block] The input tensor dtype does not match.%s",
2486 in.device() != this->device(),
2487 "[ERROR][BlockFermionicUniTensor][put_block] The input tensor device does not "
2493 indices.size() != this->rank(),
2494 "[ERROR][put_block][BlockFermionicUniTensor] len(indices) must be the same as the "
2495 "Tensor rank (number of legs).%s",
2498 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
2502 for (cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
2503 if (inds == this->_inner_to_outer_idx[i]) {
2511 true,
"[ERROR][put_block][BlockFermionicUniTensor] no avaliable block exists.%s",
"\n");
2514 in.shape() != this->_blocks[b].shape(),
2515 "[ERROR][BlockFermionicUniTensor] the shape of input tensor does not match the shape "
2516 "of block @ idx=%d\n",
2518 this->_blocks[b] = in;
2526 boost::intrusive_ptr<UniTensor_base>
Conj() {
2528 boost::intrusive_ptr<UniTensor_base> out = this->clone();
2535 for (
int i = 0; i < this->_blocks.size(); i++) {
2536 this->_blocks[i].Conj_();
2543 boost::intrusive_ptr<UniTensor_base> Transpose() {
2545 boost::intrusive_ptr<UniTensor_base> out = this->clone();
2551 boost::intrusive_ptr<UniTensor_base> normalize() {
2553 boost::intrusive_ptr<UniTensor_base> out = this->clone();
2558 boost::intrusive_ptr<UniTensor_base> Dagger() {
2560 boost::intrusive_ptr<UniTensor_base> out = this->
Conj();
2570 void Trace_(
const std::string &a,
const std::string &b);
2571 void Trace_(
const cytnx_int64 &a,
const cytnx_int64 &b);
2573 boost::intrusive_ptr<UniTensor_base>
Trace(
const std::string &a,
const std::string &b) {
2575 boost::intrusive_ptr<UniTensor_base> out = this->clone();
2579 boost::intrusive_ptr<UniTensor_base>
Trace(
const cytnx_int64 &a,
const cytnx_int64 &b) {
2581 boost::intrusive_ptr<UniTensor_base> out = this->clone();
2586 Tensor
Norm()
const;
2588 bool elem_exists(
const std::vector<cytnx_uint64> &locator)
const;
2590 const Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator)
const;
2591 const cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
2592 const cytnx_complex128 &aux)
const;
2593 const cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
2594 const cytnx_complex64 &aux)
const;
2595 const cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
2596 const cytnx_double &aux)
const;
2597 const cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
2598 const cytnx_float &aux)
const;
2599 const cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
2600 const cytnx_uint64 &aux)
const;
2601 const cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
2602 const cytnx_int64 &aux)
const;
2603 const cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
2604 const cytnx_uint32 &aux)
const;
2605 const cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
2606 const cytnx_int32 &aux)
const;
2607 const cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
2608 const cytnx_uint16 &aux)
const;
2609 const cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
2610 const cytnx_int16 &aux)
const;
2612 Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator);
2613 cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
2614 const cytnx_complex128 &aux);
2615 cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
2616 const cytnx_complex64 &aux);
2617 cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_double &aux);
2618 cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_float &aux);
2619 cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_uint64 &aux);
2620 cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_int64 &aux);
2621 cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_uint32 &aux);
2622 cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_int32 &aux);
2623 cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_uint16 &aux);
2624 cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
const cytnx_int16 &aux);
2626 void _save_dispatch(std::fstream &f)
const;
2627 void _load_dispatch(std::fstream &f,
unsigned int version);
2630 void truncate_(
const std::string &label,
const cytnx_uint64 &q_index);
2631 void truncate_(
const cytnx_int64 &bond_idx,
const cytnx_uint64 &q_index);
2633 void Add_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
2634 void Add_(
const Scalar &rhs) {
2636 "[ERROR] Cannot perform elementwise arithmetic '+' between Scalar and "
2637 "BlockFermionicUniTensor.\n %s "
2639 "This operation would destroy the block structure. [Suggest] Avoid or use "
2640 "get/put_block(s) to do operation on blocks.");
2643 void Mul_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
2644 void Mul_(
const Scalar &rhs);
2646 void Sub_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
2647 void Sub_(
const Scalar &rhs) {
2649 "[ERROR] Cannot perform elementwise arithmetic '-' between Scalar and "
2650 "BlockFermionicUniTensor.\n %s "
2652 "This operation would destroy the block structure. [Suggest] Avoid or use "
2653 "get/put_block(s) to do operation on blocks.");
2655 void lSub_(
const Scalar &lhs) {
2657 "[ERROR] Cannot perform elementwise arithmetic '-' between Scalar and "
2658 "BlockFermionicUniTensor.\n %s "
2660 "This operation would destroy the block structure. [Suggest] Avoid or use "
2661 "get/put_block(s) to do operation on blocks.");
2664 void Div_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
2665 void Div_(
const Scalar &rhs);
2666 void lDiv_(
const Scalar &lhs) {
2668 "[ERROR] Cannot perform elementwise arithmetic '/' between Scalar and "
2669 "BlockFermionicUniTensor.\n %s "
2671 "This operation would cause division by zero on non-block elements. "
2672 "[Suggest] Avoid or use get/put_block(s) to do operation on blocks.");
2674 void from_(
const boost::intrusive_ptr<UniTensor_base> &rhs,
bool force, cytnx_double tol = 0.);
2676 void group_basis_();
2678 void combineBond(
const std::vector<std::string> &indicators,
const bool &force =
false);
2679 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force =
false);
2680 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force,
2681 const bool &by_label);
2682 void combineBonds(
const std::vector<std::string> &indicators,
const bool &force =
false);
2684 const std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx)
const {
2687 bidx >= this->Nblocks(),
2688 "[ERROR][BlockFermionicUniTensor] bidx out of bound! only %d blocks in current UTen.\n",
2690 return this->_inner_to_outer_idx[bidx];
2692 std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx) {
2695 bidx >= this->Nblocks(),
2696 "[ERROR][BlockFermionicUniTensor] bidx out of bound! only %d blocks in current UTen.\n",
2698 return this->_inner_to_outer_idx[bidx];
2701 const vec2d<cytnx_uint64> &get_itoi()
const {
2703 return this->_inner_to_outer_idx;
2705 vec2d<cytnx_uint64> &get_itoi() {
2707 return this->_inner_to_outer_idx;
2710 void beauty_print_block(std::ostream &os,
const cytnx_uint64 &Nin,
const cytnx_uint64 &Nout,
2711 const std::vector<cytnx_uint64> &qn_indices,
2712 const std::vector<Bond> &bonds,
const Tensor &block)
const;
2719 std::vector<cytnx_bool> _signflip;
2726 class UniTensor_options {
2733 UniTensor_options() {
2734 this->_is_diag =
false;
2735 this->_dtype =
Type.Double;
2736 this->_device =
Device.cpu;
2737 this->_rowrank = -1;
2740 UniTensor_options(
const UniTensor_options &rhs) {
2741 this->_is_diag = rhs._is_diag;
2742 this->_dtype = rhs._dtype;
2743 this->_device = rhs._device;
2744 this->_rowrank = rhs._rowrank;
2747 UniTensor_options &operator=(
const UniTensor_options &rhs) {
2748 this->_is_diag = rhs._is_diag;
2749 this->_dtype = rhs._dtype;
2750 this->_device = rhs._device;
2751 this->_rowrank = rhs._rowrank;
2755 UniTensor_options &is_diag(
const bool &in) {
2756 this->_is_diag = in;
2759 UniTensor_options &dtype(
const int &in) {
2763 UniTensor_options &device(
const int &in) {
2767 UniTensor_options &rowrank(
const int &in) {
2768 this->_rowrank = in;
2778 boost::intrusive_ptr<UniTensor_base>
_impl;
2782 this->_impl =
rhs._impl;
2816 const std::vector<std::string> &
in_labels = {},
const std::string &
name =
"")
2838 const std::vector<std::string> &
in_labels = {},
const std::string &
name =
"") {
2867 const std::string &
name =
"")
2872 "[DEBUG] message: entry for UniTensor(const std::vector<Bond> &bonds, const "
2873 "std::vector<std::string> &in_labels={}, const cytnx_int64 &rowrank=-1, const unsigned "
2875 "&dtype=Type.Double, const int &device = Device.cpu, const bool &is_diag=false)%s",
2914 const std::string &
name =
"") {
2928 "[ERROR] When initializing a UniTensor with symmetries, all Bonds must "
2929 "be in the same format!%s",
2940 is_sym,
"[ERROR] Cannot have bonds with mixing of symmetry and non-symmetry.%s",
"\n");
2946 cytnx_warning_msg(
true,
"[DEBUG] message: entry dispatch: UniTensor: symmetric%s",
"\n");
2950 "[ERROR] Symmetric tensor, but no degeneracies given. The UniTensor seems broken.%s",
2982 const std::string &
in) {
3054 "Please use set_label_(const cytnx_int64 &idx, const std::string &new_label) "
3064 "Please use set_label_(const cytnx_int64 &idx, const char *new_label) "
3076 "Please use set_label_(const std::string &old_label, const std::string "
3086 "Please use set_label_(const char *old_label, const std::string &new_label) "
3096 "Please use set_label_(const std::string &old_label, const char *new_label) "
3106 "Please use set_label_(const char *old_label, const char *new_label) "
3126 "UniTensor &relabel_(const std::vector<std::string> &new_labels) "
3140 "UniTensor &relabel_(const std::initializer_list<char *> &new_labels) "
3144 std::vector<std::string>
vs(
new_lbls.size());
3146 [](
char *
x) -> std::string { return std::string(x); });
3148 this->_impl->set_labels(
vs);
3174 "[ERROR] Cannot use item on a non-scalar UniTensor with Symmetry.\n suggestion: use "
3175 "get_block()/get_blocks() first.%s",
3177 return this->_impl->get_block_(0).item<
T>();
3181 return tmp->_block.item<
T>();
3188 "[ERROR] Cannot use item on a non-scalar UniTensor with Symmetry.\n suggestion: use "
3189 "get_block()/get_blocks() first.%s",
3191 return this->_impl->get_block_(0).item();
3195 return tmp->_block.item();
3220 unsigned int dtype()
const {
return this->_impl->dtype(); }
3249 if (this->
is_void())
return 0;
3265 int device()
const {
return this->_impl->device(); }
3271 std::string
name()
const {
return this->_impl->name(); }
3278 std::string
dtype_str()
const {
return this->_impl->dtype_str(); }
3285 std::string
device_str()
const {
return this->_impl->device_str(); }
3305 bool is_diag()
const {
return this->_impl->is_diag(); }
3312 bool is_tag()
const {
return this->_impl->is_tag(); }
3319 std::vector<Symmetry>
syms()
const {
return this->_impl->syms(); }
3333 const std::vector<std::string> &
labels()
const {
return this->_impl->labels(); }
3346 const std::vector<Bond> &
bonds()
const {
return this->_impl->bonds(); }
3361 std::vector<cytnx_uint64>
shape()
const {
return this->_impl->shape(); }
3369 std::vector<bool>
signflip()
const {
return this->_impl->signflip(); }
3434 "UniTensor &relabel_(const std::vector<std::string> &new_labels) "
3464 "UniTensor relabel(const std::vector<std::string> &new_labels) const "
3478 std::vector<std::string>
vs(
new_lbls.size());
3480 [](
char *
x) -> std::string { return std::string(x); });
3493 "UniTensor relabel(const std::initializer_list<char *> &new_labels) const "
3497 std::vector<std::string>
vs(
new_lbls.size());
3499 [](
char *
x) -> std::string { return std::string(x); });
3510 std::vector<std::string>
vs(
new_lbls.size());
3512 [](
char *
x) -> std::string { return std::string(x); });
3514 this->_impl->relabel_(
vs);
3524 "UniTensor &relabel_(const std::initializer_list<char *> &new_labels) "
3528 std::vector<std::string>
vs(
new_lbls.size());
3530 [](
char *
x) -> std::string { return std::string(x); });
3532 this->_impl->relabels_(
vs);
3544 const std::vector<std::string> &
new_labels)
const {
3556 "UniTensor relabel(const std::vector<std::string> &old_labels, const "
3557 "std::vector<std::string> &new_labels) const "
3560 const std::vector<std::string> &
new_labels)
const {
3582 const std::vector<std::string> &
new_labels) {
3593 "UniTensor &relabel_(const std::vector<std::string> &old_labels, const "
3594 "std::vector<std::string> &new_labels) "
3597 const std::vector<std::string> &
new_labels) {
3607 const std::initializer_list<char *> &
new_labels)
const {
3609 std::vector<std::string>
vs(
new_lbls.size());
3611 [](
char *
x) -> std::string { return std::string(x); });
3616 [](
char *
x) -> std::string { return std::string(x); });
3628 "UniTensor relabel(const std::initializer_list<char *> &old_labels, const "
3629 "std::initializer_list<char *> &new_labels) const "
3632 const std::initializer_list<char *> &
new_labels)
const {
3634 std::vector<std::string>
vs(
new_lbls.size());
3636 [](
char *
x) -> std::string { return std::string(x); });
3641 [](
char *
x) -> std::string { return std::string(x); });
3651 const std::initializer_list<char *> &
new_labels) {
3653 std::vector<std::string>
vs(
new_lbls.size());
3655 [](
char *
x) -> std::string { return std::string(x); });
3660 [](
char *
x) -> std::string { return std::string(x); });
3672 "UniTensor &relabel_(const std::initializer_list<char *> &old_labels, const "
3673 "std::initializer_list<char *> &new_labels) "
3676 const std::initializer_list<char *> &
new_labels) {
3678 std::vector<std::string>
vs(
new_lbls.size());
3680 [](
char *
x) -> std::string { return std::string(x); });
3685 [](
char *
x) -> std::string { return std::string(x); });
3764 if (this->
dtype() == dtype) {
3808 std::vector<std::string>
vs(
mprs.size());
3810 [](
char *
x) -> std::string { return std::string(x); });
3884 std::vector<std::string>
vs(
mprs.size());
3886 [](
char *
x) -> std::string { return std::string(x); });
4041 this->_impl = this->_impl->
apply_();
4098 return this->_impl->at_for_sparse(
locator,
aux);
4099 }
else if (this->
uten_type() == UTenType.Dense) {
4103 "[ERROR] UniTensor is not initialized and of type Void.%s",
"\n");
4106 "[ERROR] SparseUniTensor is deprecated. Use BlockUniTensor or LinOp instead.%s",
"\n");
4125 return this->_impl->at_for_sparse(
locator,
aux);
4126 }
else if (this->
uten_type() == UTenType.Dense) {
4130 "[ERROR] UniTensor is not initialized and of type Void.%s",
"\n");
4133 "[ERROR] SparseUniTensor is deprecated. Use BlockUniTensor or LinOp instead.%s",
"\n");
4141 const std::vector<cytnx_uint64> &
locator)
const {
4144 "[ERROR][at] length of list should be the same for label and locator.%s",
4148 "[ERROR][at] length of lists must be the same as UniTensor.rank (# of legs)%s",
"\n");
4151 for (
int i = 0;
i <
labels.size();
i++) {
4152 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
labels[
i]);
4154 "[ERROR] label:%s does not exist in current UniTensor.\n",
4156 new_loc = std::distance(this->_impl->_labels.begin(),
res);
4159 return this->
at<T>(new_locator);
4165 "[ERROR][at] length of list should be the same for label and locator.%s",
4169 "[ERROR][at] length of lists must be the same as UniTensor.rank (# of legs)%s",
"\n");
4172 for (
int i = 0;
i <
labels.size();
i++) {
4173 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
labels[
i]);
4175 "[ERROR] label:%s does not exist in current UniTensor.\n",
4177 new_loc = std::distance(this->_impl->_labels.begin(),
res);
4180 return this->
at<T>(new_locator);
4190 const Scalar::Sproxy
at(
const std::vector<cytnx_uint64> &
locator)
const {
4192 return this->_impl->at_for_sparse(
locator);
4193 }
else if (this->
uten_type() == UTenType.Dense) {
4197 "[ERROR] UniTensor is not initialized and of type Void.%s",
"\n");
4200 "[ERROR] SparseUniTensor is deprecated. Use BlockUniTensor or LinOp instead.%s",
"\n");
4213 Scalar::Sproxy
at(
const std::vector<cytnx_uint64> &
locator) {
4215 return this->_impl->at_for_sparse(
locator);
4216 }
else if (this->
uten_type() == UTenType.Dense) {
4220 "[ERROR] UniTensor is not initialized and of type Void.%s",
"\n");
4223 "[ERROR] SparseUniTensor is deprecated. Use BlockUniTensor or LinOp instead.%s",
"\n");
4229 Scalar::Sproxy
at(
const std::vector<std::string> &
labels,
4230 const std::vector<cytnx_uint64> &
locator) {
4233 "[ERROR][at] length of list should be the same for label and locator.%s",
4237 "[ERROR][at] length of lists must be the same as UniTensor.rank (# of legs)%s",
"\n");
4240 for (
int i = 0;
i <
labels.size();
i++) {
4241 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
labels[
i]);
4243 "[ERROR] label:%s does not exist in current UniTensor.\n",
4245 new_loc = std::distance(this->_impl->_labels.begin(),
res);
4248 return this->
at(new_locator);
4251 const Scalar::Sproxy
at(
const std::vector<std::string> &
labels,
4252 const std::vector<cytnx_uint64> &
locator)
const {
4255 "[ERROR][at] length of list should be the same for label and locator.%s",
4259 "[ERROR][at] length of lists must be the same as UniTensor.rank (# of legs)%s",
"\n");
4262 for (
int i = 0;
i <
labels.size();
i++) {
4263 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
labels[
i]);
4265 "[ERROR] label:%s does not exist in current UniTensor.\n",
4267 new_loc = std::distance(this->_impl->_labels.begin(),
res);
4270 return this->
at(new_locator);
4297 return this->_impl->get_block(
qidx,
force);
4301 const bool &
force =
false)
const {
4304 "[ERROR][get_block] length of lists must be the same for both lables and qnidices%s",
"\n");
4306 "[ERROR][get_block] length of lists must be the rank (# of legs)%s",
"\n");
4308 std::vector<cytnx_int64>
loc_id(this->
rank());
4313 for (
int i = 0;
i <
labels.size();
i++) {
4314 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
labels[
i]);
4316 "[ERROR][get_block] label:%s does not exists in current Tensor.\n",
4318 new_loc = std::distance(this->_impl->_labels.begin(),
res);
4332 const bool &
force =
false)
const {
4333 std::vector<cytnx_int64>
tmp =
qnum;
4347 const bool &
force =
false)
const {
4361 return this->_impl->get_block_(
idx);
4382 return this->_impl->get_block_(
qidx,
force);
4404 const bool &
force =
false) {
4407 "[ERROR][get_block] length of lists must be the same for both lables and qnidices%s",
"\n");
4409 "[ERROR][get_block] length of lists must be the rank (# of legs)%s",
"\n");
4411 std::vector<cytnx_int64>
loc_id(this->
rank());
4416 for (
int i = 0;
i <
labels.size();
i++) {
4417 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
labels[
i]);
4419 "[ERROR][get_block] label:%s does not exists in current Tensor.\n",
4421 new_loc = std::distance(this->_impl->_labels.begin(),
res);
4426 if (
out.dtype() !=
Type.Void) {
4436 std::vector<cytnx_int64>
tmp =
qidx;
4449 const bool &
force =
false) {
4460 const bool &
force =
false)
const {
4461 return this->_impl->get_block_(
qidx,
force);
4468 const bool &
force =
false)
const {
4469 std::vector<cytnx_int64>
tmp =
qidx;
4470 return this->_impl->get_block_(
tmp,
force);
4477 const bool &
force =
false)
const {
4495 std::vector<Tensor>
get_blocks()
const {
return this->_impl->get_blocks(); }
4507 return this->_impl->get_blocks_(
silent);
4515 return this->_impl->get_blocks_(
silent);
4539 "UniTensor &put_block(const Tensor &in, const cytnx_uint64 &idx) "
4567 "UniTensor &put_block(const Tensor &in_tens, const std::vector<cytnx_int64> &qidx) "
4583 const std::vector<cytnx_int64> &
qidx) {
4586 "[ERROR][put_block] length of lists must be the same for both lables and qnidices%s",
"\n");
4588 "[ERROR][put_block] length of lists must be the rank (# of legs)%s",
"\n");
4590 std::vector<cytnx_int64>
loc_id(this->
rank());
4596 for (
int i = 0;
i <
lbls.size();
i++) {
4597 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
lbls[
i]);
4599 "[ERROR][put_block] label:%s does not exists in current Tensor.\n",
4601 new_loc = std::distance(this->_impl->_labels.begin(),
res);
4617 "UniTensor &put_block(Tensor &in, const std::vector<std::string> &lbls, const "
4618 "std::vector<cytnx_int64> &qidx) "
4621 const std::vector<cytnx_int64> &
qidx,
const bool &
force) {
4660 "UniTensor &put_block_(Tensor &in, const std::vector<cytnx_int64> &qidx) "
4676 const std::vector<cytnx_int64> &
qidx) {
4679 "[ERROR][put_block_] length of lists must be the same for both lables and qnidices%s",
4682 "[ERROR][put_block_] length of lists must be the rank (# of legs)%s",
"\n");
4684 std::vector<cytnx_int64>
loc_id(this->
rank());
4690 for (
int i = 0;
i <
lbls.size();
i++) {
4691 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
lbls[
i]);
4693 "[ERROR][put_block_] label:%s does not exists in current Tensor.\n",
4695 new_loc = std::distance(this->_impl->_labels.begin(),
res);
4713 "UniTensor &put_block_(Tensor &in, const std::vector<std::string> &lbls, const "
4714 "std::vector<cytnx_int64> &qidx) "
4717 const std::vector<cytnx_int64> &
qidx,
const bool &
force) {
4753 return this->
get(acc_in);
4756 std::vector<cytnx::Accessor>
acc_in;
4760 return this->
get(acc_in);
4790 "[ERROR] Cannot set elements from UniTensor with symmetry. Use at() instead.%s",
"\n");
4793 "[ERROR] Cannot set UniTensor. incoming UniTensor is_diag=True.%s",
"\n");
4856 "combineBond_(const std::vector<std::string> &indicators, const bool &force) "
4870 "combineBond_(const std::vector<std::string> &indicators, const bool &force) "
4883 "combineBond_(const std::vector<std::string> &indicators, const bool &force) "
4919 const bool &
force =
false)
const {
4960 return this->_impl->getTotalQnums(
physical);
4967 return this->_impl->get_blocks_qnums();
4977 if (this->_impl->uten_type() !=
rhs._impl->uten_type())
return false;
4979 return this->_impl->same_data(
rhs._impl);
5279 [[deprecated(
"use norm() (returns Scalar) instead")]]
Tensor Norm()
const {
5280 return this->_impl->
Norm();
5496 out._impl = this->_impl->
Conj();
5508 this->_impl->
Conj_();
5660 this->_impl->
tag_();
5710 return this->_impl->elem_exists(
locator);
5719 [[deprecated(
"Use at() instead.")]]
T get_elem(
const std::vector<cytnx_uint64> &
locator)
const {
5720 return this->
at<T>(locator);
5730 const std::vector<cytnx_uint64> &
locator,
const T2 &
rc) {
5732 this->
at(locator) =
rc;
5836 return this->_impl->get_qindices(
bidx);
5846 return this->_impl->get_qindices(
bidx);
5859 void _Load(std::fstream &
f);
5860 void _Save(std::fstream &
f)
const;
5888 "Please use convert_from_(const UniTensor &rhs, bool force, cytnx_double tol) "
5909 const std::vector<std::string> &
in_labels = {},
5911 const std::string &
name =
"") {
5915 const std::vector<std::string> &
in_labels = {},
5917 const std::string &
name =
"") {
5978 const std::vector<std::string> &
in_labels = {},
5980 const std::string &
name =
"") {
5984 const std::vector<std::string> &
in_labels = {},
5986 const std::string &
name =
"") {
6005 const std::string &
name =
"") {
6027 const std::vector<std::string> &
in_labels = {},
6029 const std::string &
name =
"") {
6055 const std::string &
name =
"") {
6080 const std::vector<std::string> &
in_labels = {},
6083 const std::string &
name =
"");
6104 const double &std,
const std::vector<std::string> &
in_labels = {},
6107 const std::string &
name =
"");
6128 const std::vector<std::string> &
in_labels = {},
6130 const unsigned int &
dtype =
Type.Double,
6171 const double &
high,
const std::vector<std::string> &
in_labels = {},
6173 const unsigned int &
dtype =
Type.Double,
6210 std::ostream &operator<<(std::ostream &os,
const UniTensor &in);
6226 const bool &cacheR =
false);
6241 const bool &optimal);
6250 "UniTensor Contract(const std::vector<UniTensor> &TNs, const std::string &order, const bool "
6253 Contracts(
const std::vector<UniTensor> &TNs,
const std::string &order,
const bool &optimal);
6256 void _resolve_CT(std::vector<UniTensor> &TNlist);
6257 template <
class... T>
6258 void _resolve_CT(std::vector<UniTensor> &TNlist,
const UniTensor &in,
const T &...args) {
6259 TNlist.push_back(in);
6260 _resolve_CT(TNlist, args...);
6275 template <
class... T>
6277 const bool &optimal) {
6278 std::vector<UniTensor> TNlist;
6279 _resolve_CT(TNlist, in, args...);
6280 return Contract(TNlist, order, optimal);
6288 template <
class... T>
6291 "UniTensor Contract(const UniTensor &in, const T &...args, const std::string &order, const "
6293 "instead.")]] UniTensor
6295 const bool &optimal) {
6296 std::vector<UniTensor> TNlist;
6297 _resolve_CT(TNlist, in, args...);
6298 return Contracts(TNlist, order, optimal);
constexpr Type_class Type
data type
Definition Type.hpp:555
object that mimic the python slice to access elements in C++ [this is for c++ API only].
Definition Accessor.hpp:17
the object contains auxiliary properties for each Tensor rank (bond)
Definition Bond.hpp:178
Bond clone() const
return a copy of the instance Bond
Definition Bond.hpp:451
an tensor (multi-dimensional array)
Definition Tensor.hpp:41
T & at(const std::vector< cytnx_uint64 > &locator)
Get an element at specific location.
Definition Tensor.hpp:965
Tensor Norm() const
the Norm member function. Same as linalg::Norm(const Tensor &Tin), where Tin is the current Tensor.
An Enhanced tensor specifically designed for physical Tensor network simulation.
Definition UniTensor.hpp:2775
UniTensor to(const int &device) const
move the current UniTensor to the assigned device.
Definition UniTensor.hpp:3398
UniTensor & operator*=(const UniTensor &rhs)
The multiplication assignment operator of the UniTensor.
Definition UniTensor.hpp:5370
Tensor & get_block_(const std::initializer_list< cytnx_int64 > &qidx, const bool &force=false)
Definition UniTensor.hpp:4435
std::vector< Tensor > & get_blocks_(const bool &silent=false)
Definition UniTensor.hpp:4514
Tensor & get_block_(const std::vector< cytnx_uint64 > &qidx, const bool &force=false)
Definition UniTensor.hpp:4443
void print_block(const cytnx_int64 &idx, const bool &full_info=true) const
Print out the block of the UniTensor with a given block index number.
Definition UniTensor.hpp:4064
UniTensor & operator/=(const UniTensor &rhs)
The division assignment operator of the UniTensor.
Definition UniTensor.hpp:5350
UniTensor & set_elem(const std::vector< cytnx_uint64 > &locator, const T2 &rc)
Definition UniTensor.hpp:5729
T & item()
Definition UniTensor.hpp:3170
UniTensor & tag_()
Set the UniTensor as a tagged UniTensor, in-place.
Definition UniTensor.hpp:5659
UniTensor & put_block_(Tensor &in, const std::vector< cytnx_int64 > &qidx)
Put the block into the UniTensor with given quantum indices, inplacely.
Definition UniTensor.hpp:4648
UniTensor & set_label_(const std::string &old_label, const std::string &new_label)
set a new label for bond to replace one of the current label, in-place.
Definition UniTensor.hpp:3019
bool is_contiguous() const
To tell whether the UniTensor is contiguous.
Definition UniTensor.hpp:3299
static UniTensor eye(cytnx_uint64 dim, const std::vector< std::string > &in_labels={}, cytnx_bool is_diag=false, unsigned int dtype=Type.Double, int device=Device.cpu, const std::string &name="")
Generate a 2-bond identity UniTensor.
Definition UniTensor.hpp:5959
T get_elem(const std::vector< cytnx_uint64 > &locator) const
Definition UniTensor.hpp:5719
UniTensor Div(const UniTensor &rhs) const
The division function of the UniTensor.
UniTensor & convert_from(const UniTensor &rhs, bool force=false, cytnx_double tol=0.)
Definition UniTensor.hpp:5890
Tensor get_block(const std::vector< std::string > &labels, const std::vector< cytnx_int64 > &qidx, const bool &force=false) const
Definition UniTensor.hpp:4300
UniTensor & put_block(const Tensor &in, const cytnx_uint64 &idx, const bool &force)
Definition UniTensor.hpp:4541
std::vector< cytnx_uint64 > & get_qindices(const cytnx_uint64 &bidx)
get the q-indices on each leg for the [bidx]-th block
Definition UniTensor.hpp:5845
std::vector< bool > signflip() const
Get the sign information of a fermionic UniTensor.
Definition UniTensor.hpp:3369
UniTensor & set_label_(const std::string &old_label, const char *new_label)
Definition UniTensor.hpp:3035
UniTensor & set_label(const cytnx_int64 &idx, const char *new_label)
Definition UniTensor.hpp:3066
UniTensor & operator+=(const UniTensor &rhs)
The addition assignment operator of the UniTensor.
Definition UniTensor.hpp:5310
UniTensor reshape(const std::vector< cytnx_int64 > &new_shape, const cytnx_uint64 &rowrank=0)
Reshape the UniTensor.
Definition UniTensor.hpp:4806
std::vector< Tensor > get_blocks() const
Get all the blocks of the UniTensor.
Definition UniTensor.hpp:4495
UniTensor permute(const std::initializer_list< char * > &mapper, const cytnx_int64 &rowrank=-1) const
Definition UniTensor.hpp:3805
bool is_tag() const
To tell whether the UniTensor is tagged. That is, all of the Bond in the UniTensor is directional (al...
Definition UniTensor.hpp:3312
std::string uten_type_str() const
Return the UniTensor type (cytnx::UTenType) of the UniTensor in 'string' form.
Definition UniTensor.hpp:3292
UniTensor permute_nosignflip(const std::vector< std::string > &mapper, const cytnx_int64 &rowrank=-1) const
permute the legs of the UniTensor by labels without sign flips
Definition UniTensor.hpp:3869
UniTensor(const std::vector< Bond > &bonds, const std::vector< std::string > &in_labels={}, const cytnx_int64 &rowrank=-1, const unsigned int &dtype=Type.Double, const int &device=Device.cpu, const bool &is_diag=false, const std::string &name="")
Construct a UniTensor.
Definition UniTensor.hpp:2864
bool is_void() const
Return whether the UniTensor is uninitialized.
Definition UniTensor.hpp:3234
Tensor get_block(const std::vector< cytnx_int64 > &qidx, const bool &force=false) const
Get the block of the UniTensor for the given quantun indices.
Definition UniTensor.hpp:4296
UniTensor & contiguous_()
Make the UniTensor contiguous by coalescing the memory (storage), inplacely.
Definition UniTensor.hpp:4016
UniTensor & tag()
Definition UniTensor.hpp:5667
const Bond & bond_(const cytnx_uint64 &idx) const
Definition UniTensor.hpp:3348
bool is_scalar() const
whether the UniTensor is an initialized rank-0 scalar UniTensor
Definition UniTensor.hpp:3240
void combineBonds(const std::vector< cytnx_int64 > &indicators, const bool &force=false)
Definition UniTensor.hpp:4885
UniTensor & set_label_(const char *old_label, const char *new_label)
Definition UniTensor.hpp:3043
UniTensor & relabel_(const std::string &old_label, const std::string &new_label)
relabel the legs in the UniTensor by a given label.
Definition UniTensor.hpp:3731
const T & at(const std::vector< std::string > &labels, const std::vector< cytnx_uint64 > &locator) const
Definition UniTensor.hpp:4140
UniTensor & relabels_(const std::initializer_list< char * > &new_labels)
Definition UniTensor.hpp:3526
UniTensor(const Tensor &in_tensor, const bool &is_diag=false, const cytnx_int64 &rowrank=-1, const std::vector< std::string > &in_labels={}, const std::string &name="")
Construct a UniTensor from a cytnx::Tensor.
Definition UniTensor.hpp:2814
Tensor & get_block_(const std::vector< cytnx_int64 > &qidx, const bool &force=false)
Get the shared view of the block for the given quantum indices.
Definition UniTensor.hpp:4381
void Init(const Tensor &in_tensor, const bool &is_diag=false, const cytnx_int64 &rowrank=-1, const std::vector< std::string > &in_labels={}, const std::string &name="")
Initialize a UniTensor from a cytnx::Tensor.
Definition UniTensor.hpp:2837
UniTensor get(const std::vector< Accessor > &accessors) const
get elements using Accessor (C++ API) / slices (python API)
Definition UniTensor.hpp:4736
UniTensor & permute_nosignflip_(const std::vector< cytnx_int64 > &mapper, const cytnx_int64 &rowrank=-1)
permute the legs of the UniTensor without fermionic sign flips, inplacely.
Definition UniTensor.hpp:3903
T & at(const std::vector< std::string > &labels, const std::vector< cytnx_uint64 > &locator)
Definition UniTensor.hpp:4162
UniTensor Conj() const
Apply complex conjugate on each entry of the UniTensor.
Definition UniTensor.hpp:5494
UniTensor & set_label(const std::string &old_label, const std::string &new_label)
Definition UniTensor.hpp:3078
UniTensor set_rowrank(const cytnx_uint64 &new_rowrank) const
Definition UniTensor.hpp:3163
UniTensor & set(const std::vector< Accessor > &accessors, const UniTensor &rhs)
Definition UniTensor.hpp:4787
void Save(const std::string &fname) const
save a UniTensor to file
UniTensor & operator-=(const Scalar &rhs)
The subtraction assignment operator for a given scalar.
Definition UniTensor.hpp:5400
cytnx_uint64 rowrank() const
Return the row rank of the UniTensor.
Definition UniTensor.hpp:3213
UniTensor & put_block(const Tensor &in_tens, const std::vector< cytnx_int64 > &qidx)
Put the block into the UniTensor with given quantum number.
Definition UniTensor.hpp:4555
UniTensor & relabels_(const std::vector< std::string > &old_labels, const std::vector< std::string > &new_labels)
Definition UniTensor.hpp:3596
UniTensor Sub(const Scalar &rhs) const
The subtraction function for a given scalar.
Tensor Norm() const
Return the norm of the UniTensor.
Definition UniTensor.hpp:5279
UniTensor Mul(const Scalar &rhs) const
The multiplication function for a given scalar.
UniTensor & twist_(const std::string &label)
Inline version.
Definition UniTensor.hpp:3957
UniTensor twist(const std::string &label) const
Apply a twist (or braids/self-swap) operation to a given bond; No effect for bosonic tensors; for a f...
Definition UniTensor.hpp:3934
UniTensor & set_labels(const std::initializer_list< char * > &new_labels)
Definition UniTensor.hpp:3142
UniTensor & relabel_(const std::initializer_list< char * > &new_labels)
Definition UniTensor.hpp:3508
UniTensor & Sub_(const Scalar &rhs)
The subtraction function for a given scalar.
Definition UniTensor.hpp:5118
UniTensor & relabel_(const cytnx_int64 &inx, const std::string &new_label)
rebable the legs in the UniTensor by given index.
Definition UniTensor.hpp:3718
UniTensor & operator/=(const Scalar &rhs)
The division assignment operator for a given scalar.
Definition UniTensor.hpp:5415
const bool & is_braket_form() const
Check whether the UniTensor is in braket form.
Definition UniTensor.hpp:3327
UniTensor Add(const Scalar &rhs) const
The addition function for a given scalar.
UniTensor operator[](const std::vector< cytnx::Accessor > &accessors) const
get elements using Accessor (C++ API) / slices (python API)
Definition UniTensor.hpp:4746
UniTensor & set_label_(const char *old_label, const std::string &new_label)
Definition UniTensor.hpp:3027
std::vector< cytnx_uint64 > shape() const
Get the shape of the UniTensor.
Definition UniTensor.hpp:3361
UniTensor to_dense()
Convert the UniTensor to non-diagonal form.
Definition UniTensor.hpp:4834
UniTensor & permute_(const std::vector< cytnx_int64 > &mapper, const cytnx_int64 &rowrank=-1)
permute the legs of the UniTensor, inplacely.
Definition UniTensor.hpp:3822
Scalar::Sproxy at(const std::vector< std::string > &labels, const std::vector< cytnx_uint64 > &locator)
Definition UniTensor.hpp:4229
static UniTensor normal(const std::vector< cytnx_uint64 > &shape, const double &mean, const double &std, const std::vector< std::string > &in_labels={}, const unsigned int &seed=cytnx::random::__static_random_device(), const unsigned int &dtype=Type.Double, const int &device=Device.cpu, const std::string &name="")
Generate a UniTensor with all elements are random numbers sampled from a normal (Gaussian) distributi...
const Tensor & get_block_(const std::vector< cytnx_int64 > &qidx, const bool &force=false) const
Definition UniTensor.hpp:4459
UniTensor & set_name_(const std::string &in)
Set the name of a UniTensor, in-place.
Definition UniTensor.hpp:2973
UniTensor & set_labels(const std::vector< std::string > &new_labels)
Definition UniTensor.hpp:3128
std::string name() const
Return the name of the UniTensor.
Definition UniTensor.hpp:3271
const Scalar::Sproxy at(const std::vector< std::string > &labels, const std::vector< cytnx_uint64 > &locator) const
Definition UniTensor.hpp:4251
UniTensor & put_block(Tensor &in, const std::vector< std::string > &lbls, const std::vector< cytnx_int64 > &qidx)
Put the block into the UniTensor with given quantum indices, will copy the input tensor.
Definition UniTensor.hpp:4582
static UniTensor zeros(const std::vector< cytnx_uint64 > &shape, const std::vector< std::string > &in_labels={}, unsigned int dtype=Type.Double, int device=Device.cpu, const std::string &name="")
Generate a UniTensor with all elements set to zero.
Definition UniTensor.hpp:5908
UniTensor combineBond(const std::vector< std::string > &indicators, const bool &force=false) const
Combine the sevral bonds of the UniTensor, out-of-place.
Definition UniTensor.hpp:4918
UniTensor & Add_(const Scalar &rhs)
The addition function for a given scalar.
Definition UniTensor.hpp:5088
UniTensor & set(const std::vector< Accessor > &accessors, const Tensor &rhs)
set elements using Accessor (C++ API) / slices (python API)
Definition UniTensor.hpp:4783
UniTensor & Trace_(const std::string &a, const std::string &b)
Take the partial trace of the UniTensor, inplacely.
Definition UniTensor.hpp:5605
UniTensor & truncate_(const cytnx_int64 &bond_idx, const cytnx_uint64 &dim)
truncate bond dimension of the UniTensor by the given bond index and dimension.
Definition UniTensor.hpp:5793
UniTensor & twist_(const cytnx_int64 &idx)
Inline version.
Definition UniTensor.hpp:3966
Scalar::Sproxy at(const std::vector< cytnx_uint64 > &locator)
Get an element at a specific location.
Definition UniTensor.hpp:4213
UniTensor Trace(const std::string &a, const std::string &b) const
Take the partial trace of the UniTensor.
Definition UniTensor.hpp:5575
UniTensor & set_label_(const cytnx_int64 &idx, const char *new_label)
Definition UniTensor.hpp:3004
UniTensor & Init(const std::vector< Bond > &bonds, const std::vector< std::string > &in_labels={}, const cytnx_int64 &rowrank=-1, const unsigned int &dtype=Type.Double, const int &device=Device.cpu, const bool &is_diag=false, const std::string &name="")
Initialize the UniTensor with the given arguments.
Definition UniTensor.hpp:2911
bool same_data(const UniTensor &rhs) const
Check whether the Blocks address are the same.
Definition UniTensor.hpp:4975
UniTensor astype(const unsigned int &dtype) const
Return a new UniTensor whose elements are casted to a different data type.
Definition UniTensor.hpp:3762
UniTensor truncate(const std::string &label, const cytnx_uint64 &dim) const
truncate bond dimension of the UniTensor by the given bond label and dimension.
Definition UniTensor.hpp:5807
UniTensor Transpose() const
Take the transpose of the UniTensor.
Definition UniTensor.hpp:5525
UniTensor operator[](const std::initializer_list< cytnx_int64 > &accessors) const
Definition UniTensor.hpp:4762
UniTensor & set_label(const char *old_label, const std::string &new_label)
Definition UniTensor.hpp:3088
static UniTensor Load(const char *fname)
load a UniTensor from file
static UniTensor arange(cytnx_int64 Nelem, const std::vector< std::string > &in_labels={}, const std::string &name="")
Create a rank-1 UniTensor with incremental unsigned integer elements in the range [0,...
Definition UniTensor.hpp:6004
UniTensor & Dagger_()
Take the conjugate transpose to the UniTensor, inplacely.
Definition UniTensor.hpp:5646
T & at(const std::vector< cytnx_uint64 > &locator)
Get an element at a specific location.
Definition UniTensor.hpp:4094
UniTensor & set_rowrank_(const cytnx_uint64 &new_rowrank)
Set the rowrank of the UniTensor.
Definition UniTensor.hpp:3158
UniTensor & uniform_(const double &low=0, const double &high=1, const unsigned int &seed=cytnx::random::__static_random_device())
Generate a UniTensor with all elements are random numbers sampled from a uniform distribution,...
UniTensor & normal_(const double &mean, const double &std, const unsigned int &seed=cytnx::random::__static_random_device())
Generate a one-bond UniTensor with all elements are random numbers sampled from a normal (Gaussian) d...
UniTensor operator[](const std::initializer_list< cytnx::Accessor > &accessors) const
Definition UniTensor.hpp:4751
UniTensor & put_block_(Tensor &in, const std::vector< std::string > &lbls, const std::vector< cytnx_int64 > &qidx)
Put the block into the UniTensor with given quantum indices, inplacely.
Definition UniTensor.hpp:4675
UniTensor & set_label(const cytnx_int64 &idx, const std::string &new_label)
Definition UniTensor.hpp:3056
UniTensor permute_nosignflip(const std::vector< cytnx_int64 > &mapper, const cytnx_int64 &rowrank=-1) const
permute the legs of the UniTensor without sign flips
Definition UniTensor.hpp:3851
UniTensor & fermion_twists_()
Inline version.
Definition UniTensor.hpp:3995
UniTensor & to_dense_()
Convert the UniTensor to non-diagonal form, inplacely.
Definition UniTensor.hpp:4844
UniTensor contiguous() const
Make the UniTensor contiguous by coalescing the memory (storage).
Definition UniTensor.hpp:4006
UniTensor & put_block_(Tensor &in, const cytnx_uint64 &idx=0)
Put the block into the UniTensor with given index, inplacely.
Definition UniTensor.hpp:4634
UniTensor & combineBond_(const std::vector< std::string > &indicators, const bool &force=false)
Combine the sevral bonds of the UniTensor, in-place.
Definition UniTensor.hpp:4901
UniTensor & apply_()
Apply fermionic signflips to the UniTensor, inplacely. Subsequently, all elements returned by signfli...
Definition UniTensor.hpp:4040
Tensor get_block(const cytnx_uint64 &idx=0) const
Get the block of the UniTensor for a given block index.
Definition UniTensor.hpp:4282
const T & at(const std::vector< cytnx_uint64 > &locator) const
Get an element at a specific location.
Definition UniTensor.hpp:4121
UniTensor & operator*=(const Scalar &rhs)
The multiplication assignment operator for a given scalar.
Definition UniTensor.hpp:5430
UniTensor relabels(const std::vector< std::string > &old_labels, const std::vector< std::string > &new_labels) const
Definition UniTensor.hpp:3559
UniTensor & convert_from_(const UniTensor &rhs, bool force=false, cytnx_double tol=0.)
Copy data from a UniTensor of different type, in-place.
Definition UniTensor.hpp:5877
UniTensor normalize() const
normalize the current UniTensor instance with 2-norm.
Definition UniTensor.hpp:5549
UniTensor & relabel_(const std::vector< std::string > &new_labels)
Set new labels for all the bonds.
Definition UniTensor.hpp:3423
std::vector< Symmetry > syms() const
Return the symmetry type of the UniTensor.
Definition UniTensor.hpp:3319
UniTensor relabel(const std::vector< std::string > &new_labels) const
relabel all of the labels in UniTensor.
Definition UniTensor.hpp:3452
UniTensor & Mul_(const UniTensor &rhs)
The multiplcation function of the UniTensor.
Definition UniTensor.hpp:5025
static UniTensor linspace(cytnx_double start, cytnx_double end, cytnx_uint64 Nelem, bool endpoint=true, const std::vector< std::string > &in_labels={}, unsigned int dtype=Type.Double, int device=Device.cpu, const std::string &name="")
Generate a one-bond UniTensor with all elements are evenly spaced numbers over a specified interval.
Definition UniTensor.hpp:6052
UniTensor & permute_nosignflip_(const std::vector< std::string > &mapper, const cytnx_int64 &rowrank=-1)
permute the legs of the UniTensor without fermionic sign flips, inplacely.
Definition UniTensor.hpp:3920
void combineBonds(const std::vector< std::string > &indicators, const bool &force=false)
Definition UniTensor.hpp:4872
UniTensor relabel(const std::string &old_label, const std::string &new_label) const
relabel the legs in the UniTensor by a given label.
Definition UniTensor.hpp:3750
UniTensor & group_basis_()
Group the same quantum number basis together.
Definition UniTensor.hpp:4074
UniTensor & normalize_()
normalize the UniTensor, inplacely.
Definition UniTensor.hpp:5561
UniTensor & Inv_(double clip=-1.)
Element-wise (pseudo-)inverse, inplacely.
bool is_diag() const
To tell whether the UniTensor is in diagonal form.
Definition UniTensor.hpp:3305
int device() const
Return the device of the UniTensor.
Definition UniTensor.hpp:3265
UniTensor & Pow_(const double &p)
Take the power p of all elements, inplacely.
const Tensor & get_block_(const std::vector< cytnx_uint64 > &qidx, const bool &force=false) const
Definition UniTensor.hpp:4476
Tensor get_block_(const std::vector< std::string > &labels, const std::vector< cytnx_int64 > &qidx, const bool &force=false)
Get the shared (data) view of the block for the given quantum indices on given labels.
Definition UniTensor.hpp:4403
std::string dtype_str() const
Return the data type of the UniTensor in 'string' form.
Definition UniTensor.hpp:3278
UniTensor & operator+=(const Scalar &rhs)
The addition assignment operator for a given scalar.
Definition UniTensor.hpp:5385
UniTensor & Div_(const UniTensor &rhs)
The division function of the UniTensor.
Definition UniTensor.hpp:5073
UniTensor & Div_(const Scalar &rhs)
The division function for a given scalar.
Definition UniTensor.hpp:5133
UniTensor & put_block_(Tensor &in, const std::vector< cytnx_int64 > &qidx, const bool &force)
Definition UniTensor.hpp:4662
Bond & bond_(const std::string &label)
Definition UniTensor.hpp:3352
cytnx_uint64 rank() const
Return the rank of the UniTensor.
Definition UniTensor.hpp:3207
Bond bond(const std::string &label) const
Definition UniTensor.hpp:3355
UniTensor fermion_twists() const
Apply twists to all right bonds (>= rowrank) with bond type BD_KET.
Definition UniTensor.hpp:3985
UniTensor permute_nosignflip(const std::initializer_list< char * > &mapper, const cytnx_int64 &rowrank=-1) const
Definition UniTensor.hpp:3881
UniTensor & Trace_(const cytnx_int64 &a=0, const cytnx_int64 &b=1)
Take the partial trace of the UniTensor, inplacely.
Definition UniTensor.hpp:5619
UniTensor relabel(const std::vector< std::string > &old_labels, const std::vector< std::string > &new_labels) const
replace part or all labels by given new labels for the bonds.
Definition UniTensor.hpp:3543
UniTensor & put_block(const Tensor &in, const cytnx_uint64 &idx=0)
Put the block into the UniTensor with given index.
Definition UniTensor.hpp:4527
UniTensor & put_block(const Tensor &in_tens, const std::vector< cytnx_int64 > &qidx, const bool &force)
Definition UniTensor.hpp:4569
UniTensor & set_name(const std::string &in)
Definition UniTensor.hpp:2981
UniTensor Trace(const cytnx_int64 &a=0, const cytnx_int64 &b=1) const
Take the partial trace of the UniTensor.
Definition UniTensor.hpp:5590
UniTensor & Add_(const UniTensor &rhs)
The addition function of the UniTensor.
Definition UniTensor.hpp:5001
UniTensor permute(const std::vector< cytnx_int64 > &mapper, const cytnx_int64 &rowrank=-1) const
permute the legs of the UniTensor
Definition UniTensor.hpp:3780
UniTensor & set_label(const std::string &old_label, const char *new_label)
Definition UniTensor.hpp:3098
UniTensor permute(const std::vector< std::string > &mapper, const cytnx_int64 &rowrank=-1) const
permute the legs of the UniTensor by labels
Definition UniTensor.hpp:3793
unsigned int dtype() const
Return the data type of the UniTensor.
Definition UniTensor.hpp:3220
UniTensor & set_label_(const cytnx_int64 &idx, const std::string &new_label)
Set a new label for bond at the assigned index, in-place.
Definition UniTensor.hpp:2996
UniTensor Pow(const double &p) const
Take the power p of all elements.
UniTensor & Mul_(const Scalar &rhs)
The multiplication function for a given scalar.
Definition UniTensor.hpp:5103
UniTensor & operator-=(const UniTensor &rhs)
The subtraction assignment operator of the UniTensor.
Definition UniTensor.hpp:5330
static UniTensor ones(std::initializer_list< cytnx_uint64 > shape, const std::vector< std::string > &in_labels={}, unsigned int dtype=Type.Double, int device=Device.cpu, const std::string &name="")
Definition UniTensor.hpp:5983
Tensor get_block_(const std::vector< std::string > &labels, const std::vector< cytnx_uint64 > &qidx, const bool &force=false)
Definition UniTensor.hpp:4448
void combineBonds(const std::vector< cytnx_int64 > &indicators, const bool &force, const bool &by_label)
Definition UniTensor.hpp:4858
UniTensor & set_label(const char *old_label, const char *new_label)
Definition UniTensor.hpp:3108
bool is_blockform() const
Check whether the UniTensor is in block form.
Definition UniTensor.hpp:3376
UniTensor group_basis() const
Definition UniTensor.hpp:4079
UniTensor twist(const cytnx_int64 &idx) const
Apply a twist (or braids/self-swap) operation to a given bond; No effect for bosonic tensors; for a f...
Definition UniTensor.hpp:3947
UniTensor apply() const
Apply fermionic signflips to the UniTensor, such that all elements calling signflip() on the output t...
Definition UniTensor.hpp:4029
UniTensor Dagger() const
Take the conjugate transpose to the UniTensor.
Definition UniTensor.hpp:5632
static UniTensor uniform(const std::vector< cytnx_uint64 > &shape, const double &low, const double &high, const std::vector< std::string > &in_labels={}, const unsigned int &seed=cytnx::random::__static_random_device(), const unsigned int &dtype=Type.Double, const int &device=Device.cpu, const std::string &name="")
Generate a UniTensor with all elements are random numbers sampled from a uniform distribution.
const std::vector< Tensor > & get_blocks_(const bool &silent=false) const
Get all the blocks of the UniTensor, inplacely.
Definition UniTensor.hpp:4506
UniTensor & relabels_(const std::initializer_list< char * > &old_labels, const std::initializer_list< char * > &new_labels)
Definition UniTensor.hpp:3675
const Tensor & get_block_(const std::initializer_list< cytnx_int64 > &qidx, const bool &force=false) const
Definition UniTensor.hpp:4467
bool elem_exists(const std::vector< cytnx_uint64 > &locator) const
Given the locator, check if the element exists.
Definition UniTensor.hpp:5709
UniTensor & Sub_(const UniTensor &rhs)
The subtraction function of the UniTensor.
Definition UniTensor.hpp:5049
UniTensor relabels(const std::initializer_list< char * > &new_labels) const
Definition UniTensor.hpp:3495
Scalar norm() const
Return the norm of the UniTensor.
Definition UniTensor.hpp:5293
UniTensor contract(const UniTensor &inR, const bool &mv_elem_self=false, const bool &mv_elem_rhs=false) const
Contract the UniTensor with common labels.
Definition UniTensor.hpp:4943
const Tensor & get_block_(const cytnx_uint64 &idx=0) const
Get the shared view of the block for the given block index.
Definition UniTensor.hpp:4360
UniTensor & relabel_(const std::vector< std::string > &old_labels, const std::vector< std::string > &new_labels)
relabel part or all of the labels in UniTensor by given new labels
Definition UniTensor.hpp:3581
static UniTensor identity(cytnx_uint64 dim, const std::vector< std::string > &in_labels={}, cytnx_bool is_diag=false, unsigned int dtype=Type.Double, int device=Device.cpu, const std::string &name="")
Generate an identity UniTensor with two bonds.
Definition UniTensor.hpp:5934
static UniTensor Load(const std::string &fname)
load a UniTensor from file
UniTensor & permute_(const std::vector< std::string > &mapper, const cytnx_int64 &rowrank=-1)
permute the legs of the UniTensor, inplacely.
Definition UniTensor.hpp:3833
const Bond & bond_(const std::string &label) const
Definition UniTensor.hpp:3351
Bond & bond_(const cytnx_uint64 &idx)
Definition UniTensor.hpp:3349
cytnx_uint64 size() const
Return the total number of logical elements in the UniTensor.
Definition UniTensor.hpp:3248
static UniTensor normal(const cytnx_uint64 &Nelem, const double &mean, const double &std, const std::vector< std::string > &in_labels={}, const unsigned int &seed=cytnx::random::__static_random_device(), const unsigned int &dtype=Type.Double, const int &device=Device.cpu, const std::string &name="")
Generate a one-bond UniTensor with all elements are random numbers sampled from a normal (Gaussian) d...
UniTensor Inv(double clip=-1.) const
Element-wise (pseudo-)inverse.
UniTensor relabel(const cytnx_int64 &inx, const std::string &new_label) const
rebabel the legs in the UniTensor by given index.
Definition UniTensor.hpp:3705
UniTensor Mul(const UniTensor &rhs) const
The multiplication function of the UniTensor.
UniTensor & reshape_(const std::vector< cytnx_int64 > &new_shape, const cytnx_uint64 &rowrank=0)
Reshape the UniTensor, inplacely.
Definition UniTensor.hpp:4818
bool is_empty() const
whether the UniTensor is initialized and has no logical elements
Definition UniTensor.hpp:3258
void print_diagram(const bool &bond_info=false) const
Plot the diagram of the UniTensor.
Definition UniTensor.hpp:4049
UniTensor relabels(const std::initializer_list< char * > &old_labels, const std::initializer_list< char * > &new_labels) const
Definition UniTensor.hpp:3631
const std::vector< cytnx_uint64 > & get_qindices(const cytnx_uint64 &bidx) const
get the q-indices on each leg for the [bidx]-th block
Definition UniTensor.hpp:5835
UniTensor Div(const Scalar &rhs) const
The division function for a given scalar.
const std::vector< std::string > & labels() const
Return the labels of the UniTensor.
Definition UniTensor.hpp:3333
UniTensor & relabel_(const std::initializer_list< char * > &old_labels, const std::initializer_list< char * > &new_labels)
Definition UniTensor.hpp:3650
cytnx_int64 get_index(std::string label) const
Get the index of an desired label string.
Definition UniTensor.hpp:3340
UniTensor & to_(const int &device)
move the current UniTensor to the assigned device (inplace).
Definition UniTensor.hpp:3384
const std::vector< Bond > & bonds() const
Get the bonds of the UniTensor.
Definition UniTensor.hpp:3346
Tensor get_block(const std::vector< std::string > &labels, const std::vector< cytnx_uint64 > &qidx, const bool &force=false) const
Definition UniTensor.hpp:4346
UniTensor truncate(const cytnx_int64 &bond_idx, const cytnx_uint64 &dim) const
truncate bond dimension of the UniTensor by the given bond index and dimension.
Definition UniTensor.hpp:5822
UniTensor & Transpose_()
Take the transpose of the UniTensor, inplacely.
Definition UniTensor.hpp:5538
static UniTensor uniform(const cytnx_uint64 &Nelem, const double &low, const double &high, const std::vector< std::string > &in_labels={}, const unsigned int &seed=cytnx::random::__static_random_device(), const unsigned int &dtype=Type.Double, const int &device=Device.cpu, const std::string &name="")
Generate a one-bond UniTensor with all elements are random numbers sampled from a uniform distributio...
UniTensor & put_block(Tensor &in, const std::vector< std::string > &lbls, const std::vector< cytnx_int64 > &qidx, const bool &force)
Definition UniTensor.hpp:4620
UniTensor clone() const
Clone (deep copy) the UniTensor.
Definition UniTensor.hpp:3408
vec2d< cytnx_uint64 > & get_itoi()
Definition UniTensor.hpp:5856
void print_blocks(const bool &full_info=true) const
Print all blocks of the UniTensor.
Definition UniTensor.hpp:4057
Scalar::Sproxy item() const
Definition UniTensor.hpp:3184
const vec2d< cytnx_uint64 > & get_itoi() const
get the q-indices on each leg for all the blocks
Definition UniTensor.hpp:5855
std::string device_str() const
Return the device of the UniTensor in 'string' form.
Definition UniTensor.hpp:3285
UniTensor & put_block_(Tensor &in, const std::vector< std::string > &lbls, const std::vector< cytnx_int64 > &qidx, const bool &force)
Definition UniTensor.hpp:4716
UniTensor relabels(const std::vector< std::string > &new_labels) const
Definition UniTensor.hpp:3466
static UniTensor ones(const std::vector< cytnx_uint64 > &shape, const std::vector< std::string > &in_labels={}, unsigned int dtype=Type.Double, int device=Device.cpu, const std::string &name="")
Generate a UniTensor with all elements set to one.
Definition UniTensor.hpp:5977
UniTensor relabel(const std::initializer_list< char * > &old_labels, const std::initializer_list< char * > &new_labels) const
Definition UniTensor.hpp:3606
UniTensor Sub(const UniTensor &rhs) const
The subtraction function of the UniTensor.
UniTensor & Conj_()
Apply complex conjugate on each entry of the UniTensor.
Definition UniTensor.hpp:5507
static UniTensor arange(cytnx_double start, cytnx_double end, cytnx_double step=1, const std::vector< std::string > &in_labels={}, unsigned int dtype=Type.Double, int device=Device.cpu, const std::string &name="")
Create a rank-1 UniTensor with incremental elements in the range [start,end) with given step-size ste...
Definition UniTensor.hpp:6026
UniTensor operator[](const std::vector< cytnx_int64 > &accessors) const
Definition UniTensor.hpp:4755
UniTensor relabel(const std::initializer_list< char * > &new_labels) const
relables all of the labels in UniTensor.
Definition UniTensor.hpp:3476
cytnx_uint64 Nblocks() const
Return the number of blocks in the UniTensor.
Definition UniTensor.hpp:3201
Tensor get_block(const std::vector< cytnx_uint64 > &qnum, const bool &force=false) const
Definition UniTensor.hpp:4341
void Save(const char *fname) const
save a UniTensor to file
Bond bond(const cytnx_uint64 &idx) const
Definition UniTensor.hpp:3354
static UniTensor zeros(std::initializer_list< cytnx_uint64 > shape, const std::vector< std::string > &in_labels={}, unsigned int dtype=Type.Double, int device=Device.cpu, const std::string &name="")
Definition UniTensor.hpp:5914
UniTensor Add(const UniTensor &rhs) const
The addition function of the UniTensor.
UniTensor & truncate_(const std::string &label, const cytnx_uint64 &dim)
truncate bond dimension of the UniTensor by the given bond label and dimension.
Definition UniTensor.hpp:5780
Tensor get_block(const std::initializer_list< cytnx_int64 > &qnum, const bool &force=false) const
Definition UniTensor.hpp:4331
UniTensor & relabels_(const std::vector< std::string > &new_labels)
Definition UniTensor.hpp:3436
const Scalar::Sproxy at(const std::vector< cytnx_uint64 > &locator) const
Get an element at a specific location.
Definition UniTensor.hpp:4190
int uten_type() const
Return the UniTensor type (cytnx::UTenType) of the UniTensor.
Definition UniTensor.hpp:3228
Tensor & get_block_(const cytnx_uint64 &idx=0)
Definition UniTensor.hpp:4368
#define cytnx_warning_msg(is_true, format,...)
Definition cytnx_error.hpp:122
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:115
cytnx::UniTensor Conj(const cytnx::UniTensor &UT)
Elementwise conjugate of the UniTensor.
cytnx::UniTensor Trace(const cytnx::UniTensor &Tin, const cytnx_int64 &a=0, const cytnx_int64 &b=1)
Perform the trace over two legs of a UniTensor.
void Conj_(cytnx::UniTensor &UT)
Inplace elementwise conjugate of the UniTensor.
Tensor Norm(const Tensor &Tl)
Calculate the norm of a tensor.
std::random_device __static_random_device
Definition UniTensor.hpp:29
Definition Accessor.hpp:12
Device_class Device
data on which devices.
UniTensorType_class UTenType
UniTensor type.
@ Void
Definition Symmetry.hpp:30
Tensor zeros(const std::vector< cytnx_uint64 > &shape, unsigned int dtype=Type.Double, int device=Device.cpu)
Create a Tensor with all elements initialized to zero.
UniTensor Contract(const UniTensor &inL, const UniTensor &inR, const bool &cacheL=false, const bool &cacheR=false)
Contract two UniTensor by tracing the ranks with common labels.
Tensor linspace(cytnx_double start, cytnx_double end, cytnx_uint64 Nelem, bool endpoint=true, unsigned int dtype=Type.Double, int device=Device.cpu)
Tensor ones(const std::vector< cytnx_uint64 > &shape, unsigned int dtype=Type.Double, int device=Device.cpu)
Create a Tensor with all elements initialized to one.
UniTensor Contracts(const std::vector< UniTensor > &TNs, const std::string &order, const bool &optimal)
Tensor arange(cytnx_int64 Nelem)
Create a rank-1 Tensor with incremental unsigned integer elements in the range [0,...
Tensor identity(cytnx_uint64 Dim, unsigned int dtype=Type.Double, int device=Device.cpu)
Create a square rank-2 Tensor with the diagonal initialized to one and all other elements set to zero...