16#include <initializer_list>
24 using namespace cytnx;
26 class UniTensorType_class {
34 std::string getname(
const int &ut_type);
58 class UniTensor_base :
public intrusive_ptr_base<UniTensor_base> {
66 std::vector<std::string> _labels;
67 std::vector<Bond> _bonds;
69 bool _update_braket() {
70 if (_bonds.size() == 0)
return false;
72 if (this->_bonds[0].type() != bondType::BD_REG) {
74 for (
unsigned int i = 0; i < this->_bonds.size(); i++) {
75 if (i < this->_rowrank) {
76 if (this->_bonds[i].type() != bondType::BD_KET)
return false;
78 if (this->_bonds[i].type() != bondType::BD_BRA)
return false;
88 friend class DenseUniTensor;
90 friend class BlockUniTensor;
94 _name(std::string(
"")),
95 _is_braket_form(false),
101 UniTensor_base(
const UniTensor_base &rhs);
102 UniTensor_base &operator=(UniTensor_base &rhs);
105 bool is_diag()
const {
return this->_is_diag; }
106 const bool &is_braket_form()
const {
return this->_is_braket_form; }
107 const bool &is_tag()
const {
return this->_is_tag; }
108 const std::vector<std::string> &labels()
const {
return this->_labels; }
116 std::vector<std::string> lbls = this->_labels;
118 if (lbls[i] == lbl)
return i;
122 const std::vector<Bond> &bonds()
const {
return this->_bonds; }
123 std::vector<Bond> &bonds() {
return this->_bonds; }
124 const std::string &name()
const {
return this->_name; }
125 cytnx_uint64 rank()
const {
return this->_labels.size(); }
126 void set_name(
const std::string &in) { this->_name = in; }
138 void set_label(
const std::string &oldlbl,
const std::string &new_label) {
140 auto res = std::find(this->_labels.begin(), this->_labels.end(), oldlbl);
141 cytnx_error_msg(res == this->_labels.end(),
"[ERROR] label %s not exists.\n", oldlbl.c_str());
142 idx = std::distance(this->_labels.begin(), res);
144 cytnx_error_msg(idx >= this->_labels.size(),
"[ERROR] index exceed the rank of UniTensor%s",
148 for (
cytnx_uint64 i = 0; i < this->_labels.size(); i++) {
149 if (i == idx)
continue;
150 if (new_label == this->_labels[i]) {
155 cytnx_error_msg(is_dup,
"[ERROR] alreay has a label that is the same as the input label%s",
157 this->_labels[idx] = new_label;
159 void set_label(
const cytnx_int64 &inx,
const std::string &new_label) {
161 cytnx_error_msg(inx >= this->_labels.size(),
"[ERROR] index exceed the rank of UniTensor%s",
165 for (
cytnx_uint64 i = 0; i < this->_labels.size(); i++) {
166 if (i == inx)
continue;
167 if (new_label == this->_labels[i]) {
172 cytnx_error_msg(is_dup,
"[ERROR] alreay has a label that is the same as the input label%s",
174 this->_labels[inx] = new_label;
177 void set_labels(
const std::vector<std::string> &new_labels);
209 int uten_type() {
return this->uten_type_id; }
210 std::string uten_type_str() {
return UTenType.getname(this->uten_type_id); }
215 virtual void Init(
const std::vector<Bond> &bonds,
216 const std::vector<std::string> &in_labels = {},
217 const cytnx_int64 &rowrank = -1,
const unsigned int &dtype =
Type.Double,
218 const int &device =
Device.cpu,
const bool &is_diag =
false,
219 const bool &no_alloc =
false,
const std::string &name =
"");
221 virtual void Init_by_Tensor(
const Tensor &in,
const bool &is_diag =
false,
222 const cytnx_int64 &rowrank = -1,
const std::string &name =
"");
223 virtual std::vector<cytnx_uint64> shape()
const;
224 virtual bool is_blockform()
const;
225 virtual bool is_contiguous()
const;
226 virtual void to_(
const int &device);
227 virtual boost::intrusive_ptr<UniTensor_base> to(
const int &device);
228 virtual boost::intrusive_ptr<UniTensor_base> clone()
const;
229 virtual unsigned int dtype()
const;
230 virtual int device()
const;
231 virtual std::string dtype_str()
const;
232 virtual std::string device_str()
const;
233 virtual void set_rowrank(
const cytnx_uint64 &new_rowrank);
235 virtual boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<cytnx_int64> &mapper,
237 virtual boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<std::string> &mapper,
242 virtual void permute_(
const std::vector<cytnx_int64> &mapper,
const cytnx_int64 &rowrank = -1);
243 virtual void permute_(
const std::vector<std::string> &mapper,
const cytnx_int64 &rowrank = -1);
247 virtual boost::intrusive_ptr<UniTensor_base> contiguous_();
248 virtual boost::intrusive_ptr<UniTensor_base> contiguous();
249 virtual void print_diagram(
const bool &bond_info =
false);
250 virtual void print_blocks(
const bool &full_info =
true)
const;
251 virtual void print_block(
const cytnx_int64 &idx,
const bool &full_info =
true)
const;
253 virtual boost::intrusive_ptr<UniTensor_base> astype(
const unsigned int &dtype)
const;
257 virtual Tensor get_block(
const std::vector<cytnx_int64> &qnum,
258 const bool &force)
const;
262 virtual const Tensor &get_block_(
const std::vector<cytnx_int64> &qnum,
263 const bool &force)
const;
266 virtual Tensor &get_block_(
const std::vector<cytnx_int64> &qnum,
268 virtual bool same_data(
const boost::intrusive_ptr<UniTensor_base> &rhs)
const;
270 virtual std::vector<Tensor> get_blocks()
const;
271 virtual const std::vector<Tensor> &get_blocks_(
const bool &)
const;
272 virtual std::vector<Tensor> &get_blocks_(
const bool &);
276 virtual void put_block(
const Tensor &in,
const std::vector<cytnx_int64> &qnum,
278 virtual void put_block_(
Tensor &in,
const std::vector<cytnx_int64> &qnum,
const bool &force);
281 virtual boost::intrusive_ptr<UniTensor_base> get(
const std::vector<Accessor> &accessors);
284 virtual void set(
const std::vector<Accessor> &accessors,
const Tensor &rhs);
286 virtual void reshape_(
const std::vector<cytnx_int64> &new_shape,
288 virtual boost::intrusive_ptr<UniTensor_base> reshape(
const std::vector<cytnx_int64> &new_shape,
290 virtual boost::intrusive_ptr<UniTensor_base> to_dense();
291 virtual void to_dense_();
292 virtual void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force,
293 const bool &by_label);
294 virtual void combineBonds(
const std::vector<std::string> &indicators,
295 const bool &force =
false);
296 virtual void combineBonds(
const std::vector<cytnx_int64> &indicators,
297 const bool &force =
false);
298 virtual boost::intrusive_ptr<UniTensor_base> contract(
299 const boost::intrusive_ptr<UniTensor_base> &rhs,
const bool &mv_elem_self =
false,
300 const bool &mv_elem_rhs =
false);
301 virtual std::vector<Bond> getTotalQnums(
const bool &physical =
false);
302 virtual std::vector<std::vector<cytnx_int64>> get_blocks_qnums()
const;
303 virtual void Trace_(
const std::string &a,
const std::string &b);
306 virtual boost::intrusive_ptr<UniTensor_base>
Trace(
const std::string &a,
const std::string &b);
309 virtual boost::intrusive_ptr<UniTensor_base> relabels(
310 const std::vector<std::string> &new_labels);
311 virtual void relabels_(
const std::vector<std::string> &new_labels);
319 virtual boost::intrusive_ptr<UniTensor_base> relabel(
const std::string &inx,
320 const std::string &new_label);
322 virtual boost::intrusive_ptr<UniTensor_base> relabel(
const cytnx_int64 &inx,
323 const std::string &new_label);
325 virtual void relabel_(
const std::string &inx,
const std::string &new_label);
326 virtual void relabel_(
const cytnx_int64 &inx,
const std::string &new_label);
328 virtual std::vector<Symmetry> syms()
const;
331 virtual void Add_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
332 virtual void Add_(
const Scalar &rhs);
334 virtual void Mul_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
335 virtual void Mul_(
const Scalar &rhs);
337 virtual void Sub_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
338 virtual void Sub_(
const Scalar &rhs);
339 virtual void lSub_(
const Scalar &lhs);
341 virtual void Div_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
342 virtual void Div_(
const Scalar &rhs);
343 virtual void lDiv_(
const Scalar &lhs);
346 virtual boost::intrusive_ptr<UniTensor_base> normalize();
347 virtual void normalize_();
349 virtual boost::intrusive_ptr<UniTensor_base>
Conj();
350 virtual void Conj_();
352 virtual boost::intrusive_ptr<UniTensor_base> Transpose();
353 virtual void Transpose_();
355 virtual boost::intrusive_ptr<UniTensor_base> Dagger();
356 virtual void Dagger_();
360 virtual void truncate_(
const std::string &bond_idx,
const cytnx_uint64 &dim);
363 virtual bool elem_exists(
const std::vector<cytnx_uint64> &locator)
const;
366 virtual Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator);
367 virtual const Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator)
const;
369 virtual cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
371 virtual cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
373 virtual cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
375 virtual cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
377 virtual cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
379 virtual cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
381 virtual cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
383 virtual cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
385 virtual cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
387 virtual cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
390 virtual const cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
392 virtual const cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
394 virtual const cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
396 virtual const cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
398 virtual const cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
400 virtual const cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
402 virtual const cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
404 virtual const cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
406 virtual const cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
408 virtual const cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
411 virtual void group_basis_();
412 virtual const std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx)
const;
413 virtual std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx);
417 virtual void _save_dispatch(std::fstream &f)
const;
418 virtual void _load_dispatch(std::fstream &f);
420 virtual ~UniTensor_base(){};
426 class DenseUniTensor :
public UniTensor_base {
430 std::vector<Tensor> _interface_block;
431 DenseUniTensor *clone_meta()
const {
432 DenseUniTensor *
tmp =
new DenseUniTensor();
433 tmp->_bonds = vec_clone(this->_bonds);
434 tmp->_labels = this->_labels;
435 tmp->_is_braket_form = this->_is_braket_form;
436 tmp->_rowrank = this->_rowrank;
437 tmp->_is_diag = this->_is_diag;
438 tmp->_name = this->_name;
439 tmp->_is_tag = this->_is_tag;
444 DenseUniTensor() { this->uten_type_id =
UTenType.Dense; };
453 void Init(
const std::vector<Bond> &bonds,
const std::vector<std::string> &in_labels = {},
454 const cytnx_int64 &rowrank = -1,
const unsigned int &dtype =
Type.Double,
455 const int &device =
Device.cpu,
const bool &is_diag =
false,
456 const bool &no_alloc =
false,
const std::string &name =
"");
458 void Init_by_Tensor(
const Tensor &in_tensor,
const bool &is_diag =
false,
459 const cytnx_int64 &rowrank = -1,
const std::string &name =
"");
460 std::vector<cytnx_uint64> shape()
const {
461 if (this->_is_diag) {
462 std::vector<cytnx_uint64> shape = this->_block.
shape();
463 shape.push_back(shape[0]);
466 return this->_block.
shape();
469 bool is_blockform()
const {
return false; }
470 void to_(
const int &device) { this->_block.
to_(device); }
471 boost::intrusive_ptr<UniTensor_base> to(
const int &device) {
472 if (this->device() == device) {
473 std::vector<Tensor> _interface_block;
476 boost::intrusive_ptr<UniTensor_base> out = this->clone();
483 "[ERROR] rowrank cannot exceed the rank of UniTensor.%s",
"\n");
484 if (this->is_diag()) {
485 cytnx_error_msg(new_rowrank != 1,
"[ERROR] rowrank should be [==1] when is_diag =true!.%s",
489 this->_rowrank = new_rowrank;
492 boost::intrusive_ptr<UniTensor_base> clone()
const {
493 DenseUniTensor *
tmp = this->clone_meta();
495 boost::intrusive_ptr<UniTensor_base> out(tmp);
498 bool is_contiguous()
const {
return this->_block.
is_contiguous(); }
499 unsigned int dtype()
const {
return this->_block.
dtype(); }
500 int device()
const {
return this->_block.
device(); }
501 std::string dtype_str()
const {
return Type.getname(this->_block.
dtype()); }
502 std::string device_str()
const {
return Device.getname(this->_block.
device()); }
512 boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<cytnx_int64> &mapper,
514 boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<std::string> &mapper,
525 void permute_(
const std::vector<cytnx_int64> &mapper,
const cytnx_int64 &rowrank = -1);
526 void permute_(
const std::vector<std::string> &mapper,
const cytnx_int64 &rowrank = -1);
528 boost::intrusive_ptr<UniTensor_base> relabels(
const std::vector<std::string> &new_labels);
529 void relabels_(
const std::vector<std::string> &new_labels);
545 boost::intrusive_ptr<UniTensor_base> relabel(
const std::string &old_label,
546 const std::string &new_label);
547 boost::intrusive_ptr<UniTensor_base> relabel(
const cytnx_int64 &inx,
548 const std::string &new_label);
550 void relabel_(
const std::string &old_label,
const std::string &new_label);
551 void relabel_(
const cytnx_int64 &inx,
const std::string &new_label);
553 boost::intrusive_ptr<UniTensor_base> astype(
const unsigned int &dtype)
const {
554 DenseUniTensor *
tmp = this->clone_meta();
555 tmp->_block = this->_block.
astype(dtype);
556 boost::intrusive_ptr<UniTensor_base> out(tmp);
560 std::vector<Symmetry> syms()
const {
561 cytnx_error_msg(
true,
"[ERROR][DenseUniTensor] dense unitensor does not have symmetry.%s",
563 return std::vector<Symmetry>();
566 boost::intrusive_ptr<UniTensor_base> contiguous_() {
568 return boost::intrusive_ptr<UniTensor_base>(
this);
570 boost::intrusive_ptr<UniTensor_base> contiguous() {
572 if (this->is_contiguous()) {
573 boost::intrusive_ptr<UniTensor_base> out(
this);
576 DenseUniTensor *
tmp = this->clone_meta();
578 boost::intrusive_ptr<UniTensor_base> out(tmp);
582 void print_diagram(
const bool &bond_info =
false);
583 void print_blocks(
const bool &full_info =
true)
const;
584 void print_block(
const cytnx_int64 &idx,
const bool &full_info =
true)
const;
587 Tensor get_block(
const std::vector<cytnx_int64> &qnum,
const bool &force)
const {
589 true,
"[ERROR][DenseUniTensor] try to get_block() using qnum on a non-symmetry UniTensor%s",
594 const Tensor &get_block_(
const std::vector<cytnx_int64> &qnum,
const bool &force)
const {
597 "[ERROR][DenseUniTensor] try to get_block_() using qnum on a non-symmetry UniTensor%s",
601 Tensor &get_block_(
const std::vector<cytnx_int64> &qnum,
const bool &force) {
604 "[ERROR][DenseUniTensor] try to get_block_() using qnum on a non-symmetry UniTensor%s",
615 std::vector<Tensor> get_blocks()
const {
616 std::vector<Tensor> out;
618 true,
"[ERROR][DenseUniTensor] cannot use get_blocks(), use get_block() instead!%s",
"\n");
621 const std::vector<Tensor> &get_blocks_(
const bool &silent =
false)
const {
623 true,
"[ERROR][DenseUniTensor] cannot use get_blocks_(), use get_block_() instead!%s",
625 return this->_interface_block;
627 std::vector<Tensor> &get_blocks_(
const bool &silent =
false) {
629 true,
"[ERROR][DenseUniTensor] cannot use get_blocks_(), use get_block_() instead!%s",
631 return this->_interface_block;
635 if (this->is_diag()) {
637 in.
shape() != this->_block.shape(),
638 "[ERROR][DenseUniTensor] put_block, the input tensor shape does not match.%s",
"\n");
639 this->_block = in.
clone();
642 in.
shape() != this->shape(),
643 "[ERROR][DenseUniTensor] put_block, the input tensor shape does not match.%s",
"\n");
644 this->_block = in.
clone();
649 if (this->is_diag()) {
651 in.
shape() != this->_block.shape(),
652 "[ERROR][DenseUniTensor] put_block, the input tensor shape does not match.%s",
"\n");
656 in.
shape() != this->shape(),
657 "[ERROR][DenseUniTensor] put_block, the input tensor shape does not match.%s",
"\n");
662 void put_block(
const Tensor &in,
const std::vector<cytnx_int64> &qnum,
const bool &force) {
664 true,
"[ERROR][DenseUniTensor] try to put_block using qnum on a non-symmetry UniTensor%s",
667 void put_block_(
Tensor &in,
const std::vector<cytnx_int64> &qnum,
const bool &force) {
669 true,
"[ERROR][DenseUniTensor] try to put_block using qnum on a non-symmetry UniTensor%s",
673 boost::intrusive_ptr<UniTensor_base> get(
const std::vector<Accessor> &accessors) {
674 boost::intrusive_ptr<UniTensor_base> out(
new DenseUniTensor());
675 out->Init_by_Tensor(this->_block.
get(accessors),
false, 0);
679 void set(
const std::vector<Accessor> &accessors,
const Tensor &rhs) {
680 this->_block.
set(accessors, rhs);
683 void reshape_(
const std::vector<cytnx_int64> &new_shape,
const cytnx_uint64 &rowrank = 0);
684 boost::intrusive_ptr<UniTensor_base> reshape(
const std::vector<cytnx_int64> &new_shape,
686 boost::intrusive_ptr<UniTensor_base> to_dense();
697 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force,
698 const bool &by_label);
699 void combineBonds(
const std::vector<std::string> &indicators,
const bool &force =
true);
700 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force =
true);
701 boost::intrusive_ptr<UniTensor_base> contract(
const boost::intrusive_ptr<UniTensor_base> &rhs,
702 const bool &mv_elem_self =
false,
703 const bool &mv_elem_rhs =
false);
704 std::vector<Bond> getTotalQnums(
const bool &physical =
false) {
706 "getTotalQnums can only operate on UniTensor with symmetry.\n");
707 return std::vector<Bond>();
710 std::vector<std::vector<cytnx_int64>> get_blocks_qnums()
const {
712 "get_blocks_qnums can only operate on UniTensor with symmetry.\n");
713 return std::vector<std::vector<cytnx_int64>>();
716 bool same_data(
const boost::intrusive_ptr<UniTensor_base> &rhs)
const {
717 if (rhs->uten_type() !=
UTenType.Dense)
return false;
719 return this->get_block_().same_data(rhs->get_block_());
725 void Add_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
726 void Add_(
const Scalar &rhs);
728 void Mul_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
729 void Mul_(
const Scalar &rhs);
731 void Sub_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
732 void Sub_(
const Scalar &rhs);
733 void lSub_(
const Scalar &lhs);
735 void Div_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
736 void Div_(
const Scalar &rhs);
737 void lDiv_(
const Scalar &lhs);
741 boost::intrusive_ptr<UniTensor_base>
Conj() {
742 boost::intrusive_ptr<UniTensor_base> out = this->clone();
747 boost::intrusive_ptr<UniTensor_base> Transpose() {
748 boost::intrusive_ptr<UniTensor_base> out = this->clone();
754 boost::intrusive_ptr<UniTensor_base> normalize() {
755 boost::intrusive_ptr<UniTensor_base> out = this->clone();
761 boost::intrusive_ptr<UniTensor_base> Dagger() {
762 boost::intrusive_ptr<UniTensor_base> out = this->
Conj();
780 void Trace_(
const std::string &a,
const std::string &b);
781 boost::intrusive_ptr<UniTensor_base>
Trace(
const std::string &a,
const std::string &b) {
782 boost::intrusive_ptr<UniTensor_base> out = this->clone();
787 boost::intrusive_ptr<UniTensor_base> out = this->clone();
794 const Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator)
const {
796 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
798 return Scalar::Sproxy();
800 const cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
803 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
807 const cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
810 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
814 const cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
817 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
821 const cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
824 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
828 const cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
831 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
835 const cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
838 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
842 const cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
845 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
849 const cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
852 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
856 const cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
859 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
863 const cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
866 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
871 Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator) {
873 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
875 return Scalar::Sproxy();
880 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
884 cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
887 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
893 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
899 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
905 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
911 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
917 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
923 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
929 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
935 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
940 bool elem_exists(
const std::vector<cytnx_uint64> &locator)
const {
942 true,
"[ERROR][DenseUniTensor] elem_exists can only be used on UniTensor with Symmetry.%s",
946 if (!this->is_tag()) {
947 for (
int i = 0; i < this->_rowrank; i++) {
948 this->_bonds[i].set_type(BD_KET);
950 for (
int i = this->_rowrank; i < this->_bonds.size(); i++) {
951 this->_bonds[i].set_type(BD_BRA);
953 this->_is_tag =
true;
954 this->_is_braket_form = this->_update_braket();
967 void truncate_(
const std::string &bond_idx,
const cytnx_uint64 &dim);
969 void group_basis_() {
970 cytnx_warning_msg(
true,
"[WARNING] group basis will not have any effect on DensUniTensor.%s",
974 void _save_dispatch(std::fstream &f)
const;
975 void _load_dispatch(std::fstream &f);
977 const std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx)
const {
978 cytnx_error_msg(
true,
"[ERROR] get_qindices can only be unsed on UniTensor with Symmetry.%s",
981 std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx) {
982 cytnx_error_msg(
true,
"[ERROR] get_qindices can only be unsed on UniTensor with Symmetry.%s",
987 cytnx_error_msg(
true,
"[ERROR] get_itoi can only be unsed on UniTensor with Symmetry.%s",
991 cytnx_error_msg(
true,
"[ERROR] get_itoi can only be unsed on UniTensor with Symmetry.%s",
1001 class BlockUniTensor :
public UniTensor_base {
1004 std::vector<std::vector<cytnx_uint64>> _inner_to_outer_idx;
1005 std::vector<Tensor> _blocks;
1011 void _fx_get_total_fluxs(std::vector<cytnx_uint64> &loc,
const std::vector<Symmetry> &syms,
1012 std::vector<cytnx_int64> &total_qns) {
1013 memset(&total_qns[0], 0,
sizeof(
cytnx_int64) * total_qns.size());
1016 if (this->_bonds[0].type() == BD_BRA)
1017 total_qns[i] = syms[0].reverse_rule(this->_bonds[0]._impl->_qnums[loc[0]][i]);
1019 total_qns[i] = this->_bonds[0]._impl->_qnums[loc[0]][i];
1021 for (
auto j = 1; j < loc.size(); j++) {
1022 if (this->_bonds[j].type() == BD_BRA)
1023 total_qns[i] = syms[i].combine_rule(
1024 total_qns[i], syms[i].reverse_rule(this->_bonds[j]._impl->_qnums[loc[j]][i]));
1027 syms[i].combine_rule(total_qns[i], this->_bonds[j]._impl->_qnums[loc[j]][i]);
1033 void _fx_locate_elem(
cytnx_int64 &bidx, std::vector<cytnx_uint64> &loc_in_T,
1034 const std::vector<cytnx_uint64> &locator)
const;
1037 void _fx_group_duplicates(
const std::vector<cytnx_uint64> &dup_bond_idxs,
1038 const std::vector<std::vector<cytnx_uint64>> &idx_mappers);
1040 void set_meta(BlockUniTensor *tmp,
const bool &inner,
const bool &outer)
const {
1043 tmp->_bonds = vec_clone(this->_bonds);
1044 tmp->_labels = this->_labels;
1045 tmp->_is_braket_form = this->_is_braket_form;
1046 tmp->_rowrank = this->_rowrank;
1047 tmp->_name = this->_name;
1050 tmp->_is_diag = this->_is_diag;
1054 tmp->_inner_to_outer_idx = this->_inner_to_outer_idx;
1058 BlockUniTensor *clone_meta(
const bool &inner,
const bool &outer)
const {
1059 BlockUniTensor *
tmp =
new BlockUniTensor();
1060 this->set_meta(tmp, inner, outer);
1066 this->uten_type_id =
UTenType.Block;
1067 this->_is_tag =
true;
1076 void Init(
const std::vector<Bond> &bonds,
const std::vector<std::string> &in_labels = {},
1077 const cytnx_int64 &rowrank = -1,
const unsigned int &dtype =
Type.Double,
1078 const int &device =
Device.cpu,
const bool &is_diag =
false,
1079 const bool &no_alloc =
false,
const std::string &name =
"");
1081 void Init_by_Tensor(
const Tensor &in_tensor,
const bool &is_diag =
false,
1082 const cytnx_int64 &rowrank = -1,
const std::string &name =
"") {
1084 true,
"[ERROR][BlockUniTensor] cannot use Init_by_tensor() on a BlockUniTensor.%s",
"\n");
1087 std::vector<cytnx_uint64> shape()
const {
1088 std::vector<cytnx_uint64> out(this->_bonds.size());
1090 out[i] = this->_bonds[i].dim();
1095 bool is_blockform()
const {
return true; }
1096 bool is_contiguous()
const {
1098 for (
int i = 0; i < this->_blocks.size(); i++) {
1099 out &= this->_blocks[i].is_contiguous();
1104 cytnx_uint64 Nblocks()
const {
return this->_blocks.size(); };
1106 void to_(
const int &device) {
1107 for (
cytnx_uint64 i = 0; i < this->_blocks.size(); i++) {
1108 this->_blocks[i].to_(device);
1112 boost::intrusive_ptr<UniTensor_base> to(
const int &device) {
1113 if (this->device() == device) {
1116 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1122 boost::intrusive_ptr<UniTensor_base> clone()
const {
1123 BlockUniTensor *
tmp = this->clone_meta(
true,
true);
1124 tmp->_blocks = vec_clone(this->_blocks);
1125 boost::intrusive_ptr<UniTensor_base> out(tmp);
1129 unsigned int dtype()
const {
1131 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
1134 return this->_blocks[0].dtype();
1136 int device()
const {
1138 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
1141 return this->_blocks[0].device();
1143 std::string dtype_str()
const {
1145 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
1148 return this->_blocks[0].dtype_str();
1150 std::string device_str()
const {
1152 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
1155 return this->_blocks[0].device_str();
1159 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1161 return this->_blocks[idx].clone();
1165 Tensor get_block(
const std::vector<cytnx_int64> &indices,
const bool &force_return)
const {
1167 "[ERROR][get_block][BlockUniTensor] len(indices) must be the same as the "
1168 "Tensor rank (number of legs).%s",
1171 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1175 for (
cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1176 if (inds == this->_inner_to_outer_idx[i]) {
1184 return NullRefTensor;
1188 "[ERROR][get_block][BlockUniTensor] no avaliable block exists, force_return=false, so "
1189 "error throws. \n If you want to return an empty block without error when block is "
1190 "not avaliable, set force_return=True.%s",
1194 return this->_blocks[b].clone();
1199 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1201 return this->_blocks[idx];
1205 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1207 return this->_blocks[idx];
1210 const Tensor &get_block_(
const std::vector<cytnx_int64> &indices,
1211 const bool &force_return)
const {
1213 "[ERROR][get_block][BlockUniTensor] len(indices) must be the same as the "
1214 "Tensor rank (number of legs).%s",
1217 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1221 for (
cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1222 if (inds == this->_inner_to_outer_idx[i]) {
1230 return this->NullRefTensor;
1234 "[ERROR][get_block][BlockUniTensor] no avaliable block exists, force_return=false, so "
1235 "error throws. \n If you want to return an empty block without error when block is "
1236 "not avaliable, set force_return=True.%s",
1240 return this->_blocks[b];
1244 Tensor &get_block_(
const std::vector<cytnx_int64> &indices,
const bool &force_return) {
1246 "[ERROR][get_block][BlockUniTensor] len(indices) must be the same as the "
1247 "Tensor rank (number of legs).%s",
1250 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1254 for (
cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1255 if (inds == this->_inner_to_outer_idx[i]) {
1263 return this->NullRefTensor;
1267 "[ERROR][get_block][BlockUniTensor] no avaliable block exists, force_return=false, so "
1268 "error throws. \n If you want to return an empty block without error when block is "
1269 "not avaliable, set force_return=True.%s",
1273 return this->_blocks[b];
1277 std::vector<Tensor> get_blocks()
const {
return vec_clone(this->_blocks); }
1278 const std::vector<Tensor> &get_blocks_(
const bool &)
const {
return this->_blocks; }
1279 std::vector<Tensor> &get_blocks_(
const bool &) {
return this->_blocks; }
1281 bool same_data(
const boost::intrusive_ptr<UniTensor_base> &rhs)
const {
1282 if (rhs->uten_type() !=
UTenType.Block)
return false;
1283 if (rhs->get_blocks_(1).size() != this->get_blocks_(1).size())
return false;
1285 for (
int i = 0; i < rhs->get_blocks_(1).size(); i++)
1286 if (this->get_blocks_(1)[i].same_data(rhs->get_blocks_(1)[i]) ==
false)
return false;
1293 "[ERROR][BlockUniTensor] rowrank should be [>=0] and [<=UniTensor.rank].%s",
1295 if (this->is_diag()) {
1297 "[ERROR][BlockUniTensor] rowrank should be [==1] when is_diag =true!.%s",
1300 this->_rowrank = new_rowrank;
1301 this->_is_braket_form = this->_update_braket();
1304 boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<cytnx_int64> &mapper,
1306 boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<std::string> &mapper,
1309 void permute_(
const std::vector<cytnx_int64> &mapper,
const cytnx_int64 &rowrank = -1);
1310 void permute_(
const std::vector<std::string> &mapper,
const cytnx_int64 &rowrank = -1);
1312 boost::intrusive_ptr<UniTensor_base> contiguous_() {
1313 for (
unsigned int b = 0; b < this->_blocks.size(); b++) this->_blocks[b].contiguous_();
1314 return boost::intrusive_ptr<UniTensor_base>(
this);
1317 boost::intrusive_ptr<UniTensor_base> contiguous();
1319 void print_diagram(
const bool &bond_info =
false);
1320 void print_blocks(
const bool &full_info =
true)
const;
1321 void print_block(
const cytnx_int64 &idx,
const bool &full_info =
true)
const;
1323 boost::intrusive_ptr<UniTensor_base> contract(
const boost::intrusive_ptr<UniTensor_base> &rhs,
1324 const bool &mv_elem_self =
false,
1325 const bool &mv_elem_rhs =
false);
1327 boost::intrusive_ptr<UniTensor_base> relabels(
const std::vector<std::string> &new_labels);
1328 void relabels_(
const std::vector<std::string> &new_labels);
1334 boost::intrusive_ptr<UniTensor_base> relabel(
const std::string &old_label,
1335 const std::string &new_label);
1336 boost::intrusive_ptr<UniTensor_base> relabel(
const cytnx_int64 &inx,
1337 const std::string &new_label);
1339 void relabel_(
const std::string &old_label,
const std::string &new_label);
1340 void relabel_(
const cytnx_int64 &inx,
const std::string &new_label);
1342 std::vector<Symmetry> syms()
const;
1344 void reshape_(
const std::vector<cytnx_int64> &new_shape,
const cytnx_uint64 &rowrank = 0) {
1345 cytnx_error_msg(
true,
"[ERROR] cannot reshape a UniTensor with symmetry.%s",
"\n");
1347 boost::intrusive_ptr<UniTensor_base> reshape(
const std::vector<cytnx_int64> &new_shape,
1349 cytnx_error_msg(
true,
"[ERROR] cannot reshape a UniTensor with symmetry.%s",
"\n");
1353 boost::intrusive_ptr<UniTensor_base> astype(
const unsigned int &dtype)
const {
1354 BlockUniTensor *
tmp = this->clone_meta(
true,
true);
1355 tmp->_blocks.resize(this->_blocks.size());
1356 for (
cytnx_int64 blk = 0; blk < this->_blocks.size(); blk++) {
1357 tmp->_blocks[blk] = this->_blocks[blk].astype(dtype);
1359 boost::intrusive_ptr<UniTensor_base> out(tmp);
1364 boost::intrusive_ptr<UniTensor_base> get(
const std::vector<Accessor> &accessors) {
1367 "[ERROR][BlockUniTensor][get] cannot use get on a UniTensor with "
1368 "Symmetry.\n suggestion: try get_block/get_block_/get_blocks/get_blocks_ first.%s",
1374 void set(
const std::vector<Accessor> &accessors,
const Tensor &rhs) {
1377 "[ERROR][BlockUniTensor][get] cannot use get on a UniTensor with "
1378 "Symmetry.\n suggestion: try get_block/get_block_/get_blocks/get_blocks_ first.%s",
1383 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1386 "[ERROR][BlockUniTensor] the shape of input tensor does not match the shape "
1387 "of block @ idx=%d\n",
1390 this->_blocks[idx] = in.
clone();
1393 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1396 "[ERROR][BlockUniTensor] the shape of input tensor does not match the shape "
1397 "of block @ idx=%d\n",
1400 this->_blocks[idx] = in;
1402 void put_block(
const Tensor &in,
const std::vector<cytnx_int64> &indices,
const bool &check) {
1404 "[ERROR][put_block][BlockUniTensor] len(indices) must be the same as the "
1405 "Tensor rank (number of legs).%s",
1408 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1412 for (
cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1413 if (inds == this->_inner_to_outer_idx[i]) {
1422 "[ERROR][put_block][BlockUniTensor] no avaliable block exists, "
1423 "check=true, so error throws. \n If you want without error when block "
1424 "is not avaliable, set check=false.%s",
1429 in.
shape() != this->_blocks[b].shape(),
1430 "[ERROR][BlockUniTensor] the shape of input tensor does not match the shape "
1431 "of block @ idx=%d\n",
1434 this->_blocks[b] = in.
clone();
1437 void put_block_(
Tensor &in,
const std::vector<cytnx_int64> &indices,
const bool &check) {
1439 "[ERROR][put_block][BlockUniTensor] len(indices) must be the same as the "
1440 "Tensor rank (number of legs).%s",
1443 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1447 for (
cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1448 if (inds == this->_inner_to_outer_idx[i]) {
1457 "[ERROR][put_block][BlockUniTensor] no avaliable block exists, "
1458 "check=true, so error throws. \n If you want without error when block "
1459 "is not avaliable, set check=false.%s",
1464 in.
shape() != this->_blocks[b].shape(),
1465 "[ERROR][BlockUniTensor] the shape of input tensor does not match the shape "
1466 "of block @ idx=%d\n",
1468 this->_blocks[b] = in;
1476 boost::intrusive_ptr<UniTensor_base>
Conj() {
1477 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1483 for (
int i = 0; i < this->_blocks.size(); i++) {
1484 this->_blocks[i].Conj_();
1489 boost::intrusive_ptr<UniTensor_base> Transpose() {
1490 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1496 boost::intrusive_ptr<UniTensor_base> normalize() {
1497 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1502 boost::intrusive_ptr<UniTensor_base> Dagger() {
1503 boost::intrusive_ptr<UniTensor_base> out = this->
Conj();
1512 void Trace_(
const std::string &a,
const std::string &b);
1515 boost::intrusive_ptr<UniTensor_base>
Trace(
const std::string &a,
const std::string &b) {
1516 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1518 if (out->rank() == 0) {
1519 DenseUniTensor *
tmp =
new DenseUniTensor();
1520 tmp->_block = ((BlockUniTensor *)out.get())->_blocks[0];
1521 out = boost::intrusive_ptr<UniTensor_base>(tmp);
1526 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1528 if (out->rank() == 0) {
1529 DenseUniTensor *
tmp =
new DenseUniTensor();
1530 tmp->_block = ((BlockUniTensor *)out.get())->_blocks[0];
1531 out = boost::intrusive_ptr<UniTensor_base>(tmp);
1538 bool elem_exists(
const std::vector<cytnx_uint64> &locator)
const;
1540 const Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator)
const;
1541 const cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1543 const cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1545 const cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1547 const cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1549 const cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1551 const cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1553 const cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1555 const cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1557 const cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1559 const cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1562 Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator);
1565 cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1576 void _save_dispatch(std::fstream &f)
const;
1577 void _load_dispatch(std::fstream &f);
1580 void truncate_(
const std::string &bond_idx,
const cytnx_uint64 &q_index);
1583 void Add_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
1584 void Add_(
const Scalar &rhs) {
1587 "[ERROR] cannot perform elementwise arithmetic '+' btwn Scalar and BlockUniTensor.\n %s \n",
1588 "This operation will destroy block structure. [Suggest] using get/set_block(s) to do "
1589 "operation on the block(s).");
1592 void Mul_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
1593 void Mul_(
const Scalar &rhs);
1595 void Sub_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
1596 void Sub_(
const Scalar &rhs) {
1599 "[ERROR] cannot perform elementwise arithmetic '+' btwn Scalar and BlockUniTensor.\n %s \n",
1600 "This operation will destroy block structure. [Suggest] using get/set_block(s) to do "
1601 "operation on the block(s).");
1603 void lSub_(
const Scalar &lhs) {
1606 "[ERROR] cannot perform elementwise arithmetic '+' btwn Scalar and BlockUniTensor.\n %s \n",
1607 "This operation will destroy block structure. [Suggest] using get/set_block(s) to do "
1608 "operation on the block(s).");
1611 void Div_(
const boost::intrusive_ptr<UniTensor_base> &rhs) {
1614 "[ERROR] cannot perform elementwise arithmetic '+' btwn Scalar and BlockUniTensor.\n %s \n",
1615 "This operation will destroy block structure. [Suggest] using get/set_block(s) to do "
1616 "operation on the block(s).");
1618 void Div_(
const Scalar &rhs);
1619 void lDiv_(
const Scalar &lhs) {
1622 "[ERROR] cannot perform elementwise arithmetic '+' btwn Scalar and BlockUniTensor.\n %s \n",
1623 "This operation will destroy block structure. [Suggest] using get/set_block(s) to do "
1624 "operation on the block(s).");
1627 void group_basis_();
1629 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force =
false);
1630 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force,
1631 const bool &by_label);
1632 void combineBonds(
const std::vector<std::string> &indicators,
const bool &force =
false);
1634 const std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx)
const {
1636 bidx >= this->Nblocks(),
1637 "[ERROR][BlockUniTensor] bidx out of bound! only %d blocks in current UTen.\n",
1639 return this->_inner_to_outer_idx[bidx];
1641 std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx) {
1643 bidx >= this->Nblocks(),
1644 "[ERROR][BlockUniTensor] bidx out of bound! only %d blocks in current UTen.\n",
1646 return this->_inner_to_outer_idx[bidx];
1656 class UniTensor_options {
1663 UniTensor_options() {
1664 this->_is_diag =
false;
1665 this->_dtype =
Type.Double;
1666 this->_device =
Device.cpu;
1667 this->_rowrank = -1;
1670 UniTensor_options(
const UniTensor_options &rhs) {
1671 this->_is_diag = rhs._is_diag;
1672 this->_dtype = rhs._dtype;
1673 this->_device = rhs._device;
1674 this->_rowrank = rhs._rowrank;
1677 UniTensor_options &operator=(
const UniTensor_options &rhs) {
1678 this->_is_diag = rhs._is_diag;
1679 this->_dtype = rhs._dtype;
1680 this->_device = rhs._device;
1681 this->_rowrank = rhs._rowrank;
1685 UniTensor_options &is_diag(
const bool &in) {
1686 this->_is_diag = in;
1689 UniTensor_options &dtype(
const int &in) {
1693 UniTensor_options &device(
const int &in) {
1697 UniTensor_options &rowrank(
const int &in) {
1698 this->_rowrank = in;
1708 boost::intrusive_ptr<UniTensor_base> _impl;
1709 UniTensor() : _impl(
new UniTensor_base()){};
1712 this->_impl = rhs._impl;
1746 const std::vector<std::string> &in_labels = {},
const std::string &
name =
"")
1747 : _impl(new UniTensor_base()) {
1768 const std::vector<std::string> &in_labels = {},
const std::string &
name =
"") {
1770 boost::intrusive_ptr<UniTensor_base> out(
new DenseUniTensor());
1773 if (in_labels.size() != 0) this->
set_labels(in_labels);
1795 UniTensor(
const std::vector<Bond> &
bonds,
const std::vector<std::string> &in_labels = {},
1798 const std::string &
name =
"")
1799 : _impl(new UniTensor_base()) {
1803 "[DEBUG] message: entry for UniTensor(const std::vector<Bond> &bonds, const "
1804 "std::vector<std::string> &in_labels={}, const cytnx_int64 &rowrank=-1, const unsigned int "
1805 "&dtype=Type.Double, const int &device = Device.cpu, const bool &is_diag=false)%s",
1845 void Init(
const std::vector<Bond> &
bonds,
const std::vector<std::string> &in_labels = {},
1848 const std::string &
name =
"") {
1850 bool is_sym =
false;
1858 sym_fver =
bonds[i]._impl->_degs.size();
1863 "[ERROR] All the Bond when init a UniTensor with symmetric must be in "
1864 "the same format!%s",
1869 is_sym,
"[ERROR] cannot have bonds with mixing of symmetry and non-symmetry.%s",
"\n");
1875 cytnx_warning_msg(
true,
"[DEBUG] message: entry dispatch: UniTensor: symmetric%s",
"\n");
1879 if (sym_fver == 0) {
1883 "[ERROR] internal error! [legacy Sparse entry] the Bond is symmetry but "
1884 "the version is not properly determined!%s",
1886 }
else if (sym_fver == -1) {
1888 "[ERROR] internal error! the Bond is symmetry but the version is not "
1889 "properly determined!%s",
1892 boost::intrusive_ptr<UniTensor_base> out(
new BlockUniTensor());
1896 boost::intrusive_ptr<UniTensor_base> out(
new DenseUniTensor());
1932 this->_impl->
set_label(idx, std::string(new_label));
1947 this->_impl->
set_label(old_label, new_label);
1955 this->_impl->
set_label(std::string(old_label), new_label);
1963 this->_impl->
set_label(old_label, std::string(new_label));
1971 this->_impl->
set_label(std::string(old_label), std::string(new_label));
2001 std::vector<char *> new_lbls(new_labels);
2002 std::vector<std::string> vs(new_lbls.size());
2003 transform(new_lbls.begin(), new_lbls.end(), vs.begin(),
2004 [](
char *x) -> std::string { return std::string(x); });
2006 this->_impl->set_labels(vs);
2024 "[ERROR] cannot use item on UniTensor with Symmetry.\n suggestion: use "
2025 "get_block()/get_blocks() first.%s",
2028 DenseUniTensor *tmp =
static_cast<DenseUniTensor *
>(this->_impl.get());
2029 return tmp->_block.item<T>();
2034 "[ERROR] cannot use item on UniTensor with Symmetry.\n suggestion: use "
2035 "get_block()/get_blocks() first.%s",
2038 DenseUniTensor *tmp =
static_cast<DenseUniTensor *
>(this->_impl.get());
2039 return tmp->_block.item();
2064 unsigned int dtype()
const {
return this->_impl->dtype(); }
2079 int device()
const {
return this->_impl->device(); }
2085 std::string
name()
const {
return this->_impl->name(); }
2092 std::string
dtype_str()
const {
return this->_impl->dtype_str(); }
2099 std::string
device_str()
const {
return this->_impl->device_str(); }
2119 bool is_diag()
const {
return this->_impl->is_diag(); }
2126 bool is_tag()
const {
return this->_impl->is_tag(); }
2133 std::vector<Symmetry>
syms()
const {
return this->_impl->syms(); }
2147 const std::vector<std::string> &
labels()
const {
return this->_impl->labels(); }
2160 const std::vector<Bond> &
bonds()
const {
return this->_impl->bonds(); }
2165 std::vector<Bond> &
bonds() {
return this->_impl->bonds(); }
2171 std::vector<cytnx_uint64>
shape()
const {
return this->_impl->shape(); }
2199 out._impl = this->_impl->
to(
device);
2209 out._impl = this->_impl->
clone();
2222 void relabels_(
const std::vector<std::string> &new_labels)
const {
2223 this->_impl->relabels(new_labels);
2234 out._impl = this->_impl->
relabels(new_labels);
2242 std::vector<char *> new_labels(new_lbls);
2243 std::vector<std::string> vs(new_labels.size());
2244 transform(new_labels.begin(), new_labels.end(), vs.begin(),
2245 [](
char *x) -> std::string { return std::string(x); });
2250 out._impl = this->_impl->
relabels(vs);
2256 void relabels_(
const std::initializer_list<char *> &new_lbls) {
2257 std::vector<char *> new_labels(new_lbls);
2258 std::vector<std::string> vs(new_labels.size());
2259 transform(new_labels.begin(), new_labels.end(), vs.begin(),
2260 [](
char *x) -> std::string { return std::string(x); });
2264 this->_impl->relabels_(vs);
2283 out._impl = this->_impl->
relabel(inx, new_label);
2295 this->_impl->relabel_(inx, new_label);
2306 void relabel_(
const std::string &old_label,
const std::string &new_label)
const {
2307 this->_impl->relabel_(old_label, new_label);
2326 out._impl = this->_impl->
relabel(old_label, new_label);
2338 if (this->
dtype() == dtype) {
2339 out._impl = this->_impl;
2379 std::vector<char *> mprs = mapper;
2380 std::vector<std::string> vs(mprs.size());
2381 transform(mprs.begin(), mprs.end(), vs.begin(),
2382 [](
char *x) -> std::string { return std::string(x); });
2396 this->_impl->permute_(mapper,
rowrank);
2406 this->_impl->permute_(mapper,
rowrank);
2442 void print_diagram(
const bool &bond_info =
false) { this->_impl->print_diagram(bond_info); }
2448 void print_blocks(
const bool &full_info =
true)
const { this->_impl->print_blocks(full_info); }
2456 this->_impl->print_block(idx, full_info);
2479 T &
at(
const std::vector<cytnx_uint64> &locator) {
2481 if (this->
uten_type() == UTenType.Block) {
2484 return this->_impl->at_for_sparse(locator, aux);
2486 }
else if (this->
uten_type() == UTenType.Sparse) {
2487 if (this->_impl->elem_exists(locator)) {
2489 return this->_impl->at_for_sparse(locator, aux);
2491 cytnx_error_msg(
true,
"[ERROR][SparseUniTensor] invalid location. break qnum block.%s",
2505 const T &
at(
const std::vector<cytnx_uint64> &locator)
const {
2507 if (this->
uten_type() == UTenType.Block) {
2510 return this->_impl->at_for_sparse(locator, aux);
2512 }
else if (this->
uten_type() == UTenType.Sparse) {
2513 if (this->_impl->elem_exists(locator)) {
2515 return this->_impl->at_for_sparse(locator, aux);
2517 cytnx_error_msg(
true,
"[ERROR][SparseUniTensor] invalid location. break qnum block.%s",
2529 const Scalar::Sproxy
at(
const std::vector<cytnx_uint64> &locator)
const {
2530 if (this->
uten_type() == UTenType.Block) {
2531 return this->_impl->at_for_sparse(locator);
2532 }
else if (this->
uten_type() == UTenType.Sparse) {
2533 if (this->_impl->elem_exists(locator)) {
2534 return this->_impl->at_for_sparse(locator);
2536 cytnx_error_msg(
true,
"[ERROR][SparseUniTensor] invalid location. break qnum block.%s",
2548 Scalar::Sproxy
at(
const std::vector<cytnx_uint64> &locator) {
2549 if (this->
uten_type() == UTenType.Block) {
2550 return this->_impl->at_for_sparse(locator);
2551 }
else if (this->
uten_type() == UTenType.Sparse) {
2552 if (this->_impl->elem_exists(locator)) {
2553 return this->_impl->at_for_sparse(locator);
2555 cytnx_error_msg(
true,
"[ERROR][SparseUniTensor] invalid location. break qnum block.%s",
2581 return this->_impl->get_block(qidx, force);
2589 const bool &force =
false)
const {
2590 std::vector<cytnx_int64> tmp = qnum;
2598 Tensor get_block(
const std::vector<cytnx_uint64> &qnum,
const bool &force =
false)
const {
2599 std::vector<cytnx_int64> iqnum(qnum.begin(), qnum.end());
2600 return this->_impl->get_block(iqnum, force);
2609 return this->_impl->get_block_(idx);
2627 return this->_impl->get_block_(qidx, force);
2633 Tensor &
get_block_(
const std::initializer_list<cytnx_int64> &qidx,
const bool &force =
false) {
2634 std::vector<cytnx_int64> tmp = qidx;
2642 std::vector<cytnx_int64> iqidx(qidx.begin(), qidx.end());
2652 const bool &force =
false)
const {
2653 return this->_impl->get_block_(qidx, force);
2660 const bool &force =
false)
const {
2661 std::vector<cytnx_int64> tmp = qidx;
2662 return this->_impl->get_block_(tmp, force);
2669 const bool &force =
false)
const {
2670 std::vector<cytnx_int64> iqidx(qidx.begin(), qidx.end());
2684 std::vector<Tensor>
get_blocks()
const {
return this->_impl->get_blocks(); }
2692 const std::vector<Tensor> &
get_blocks_(
const bool &silent =
false)
const {
2693 return this->_impl->get_blocks_(silent);
2701 return this->_impl->get_blocks_(silent);
2711 this->_impl->put_block(in, idx);
2722 void put_block(
const Tensor &in_tens,
const std::vector<cytnx_int64> &qidx,
const bool &force) {
2723 this->_impl->put_block(in_tens, qidx, force);
2740 this->_impl->put_block_(in, qidx, force);
2744 out._impl = this->_impl->
get(accessors);
2747 void set(
const std::vector<Accessor> &accessors,
const Tensor &rhs) {
2748 this->_impl->set(accessors, rhs);
2771 this->_impl->reshape_(new_shape,
rowrank);
2787 out._impl = this->_impl->
to_dense();
2802 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force,
2803 const bool &by_label) {
2804 this->_impl->combineBonds(indicators, force, by_label);
2817 void combineBonds(
const std::vector<std::string> &indicators,
const bool &force =
false) {
2818 this->_impl->combineBonds(indicators, force);
2826 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force =
false) {
2827 this->_impl->combineBonds(indicators, force);
2849 const bool &mv_elem_rhs =
false)
const {
2851 out._impl = this->_impl->
contract(inR._impl, mv_elem_self, mv_elem_rhs);
2864 std::vector<Bond> getTotalQnums(
const bool physical =
false)
const {
2865 return this->_impl->getTotalQnums(physical);
2871 std::vector<std::vector<cytnx_int64>> get_blocks_qnums()
const {
2872 return this->_impl->get_blocks_qnums();
2882 if (this->_impl->uten_type() != rhs._impl->
uten_type())
return false;
2884 return this->_impl->same_data(rhs._impl);
2904 this->_impl->
Add_(rhs._impl);
2925 this->_impl->
Mul_(rhs._impl);
2946 this->_impl->
Sub_(rhs._impl);
2967 this->_impl->
Div_(rhs._impl);
2982 this->_impl->
Add_(rhs);
2997 this->_impl->
Mul_(rhs);
3012 this->_impl->
Sub_(rhs);
3027 this->_impl->
Div_(rhs);
3310 out._impl = this->_impl->
Conj();
3322 this->_impl->
Conj_();
3387 out._impl = this->_impl->
Trace(a, b);
3402 out._impl = this->_impl->
Trace(a, b);
3416 this->_impl->
Trace_(a, b);
3417 if (this->
uten_type() == UTenType.Block) {
3419 if (this->
rank() == 0) {
3420 DenseUniTensor *tmp =
new DenseUniTensor();
3422 this->_impl = boost::intrusive_ptr<UniTensor_base>(tmp);
3438 this->_impl->
Trace_(a, b);
3439 if (this->
uten_type() == UTenType.Block) {
3441 if (this->
rank() == 0) {
3442 DenseUniTensor *tmp =
new DenseUniTensor();
3444 this->_impl = boost::intrusive_ptr<UniTensor_base>(tmp);
3458 out._impl = this->_impl->
Dagger();
3512 return this->_impl->elem_exists(locator);
3521 T
get_elem(
const std::vector<cytnx_uint64> &locator)
const {
3522 return this->at<T>(locator);
3531 void set_elem(
const std::vector<cytnx_uint64> &locator,
const T2 &rc) {
3533 this->
at(locator) = rc;
3542 void Save(
const std::string &fname)
const;
3550 void Save(
const char *fname)
const;
3636 return this->_impl->get_qindices(bidx);
3646 return this->_impl->get_qindices(bidx);
3659 void _Load(std::fstream &f);
3660 void _Save(std::fstream &f)
const;
3666 std::ostream &operator<<(std::ostream &os,
const UniTensor &in);
3682 const bool &cacheR =
false);
3697 const bool &optimal);
3700 void _resolve_CT(std::vector<UniTensor> &TNlist);
3701 template <
class... T>
3702 void _resolve_CT(std::vector<UniTensor> &TNlist,
const UniTensor &in,
const T &...args) {
3703 TNlist.push_back(in);
3704 _resolve_CT(TNlist, args...);
3719 template <
class... T>
3721 const bool &optimal) {
3722 std::vector<UniTensor> TNlist;
3723 _resolve_CT(TNlist, in, args...);
3724 return Contracts(TNlist, order, optimal);
A class to represent a scalar.
Definition Scalar.hpp:2470
an tensor (multi-dimensional array)
Definition Tensor.hpp:345
void to_(const int &device)
move the current Tensor to the device.
Definition Tensor.hpp:912
Tensor contiguous_()
Make the Tensor contiguous by coalescing the memory (storage), inplacely.
Definition Tensor.hpp:999
unsigned int dtype() const
the dtype-id of the Tensor
Definition Tensor.hpp:811
Tensor contiguous() const
Make the Tensor contiguous by coalescing the memory (storage).
Definition Tensor.hpp:979
T & at(const std::vector< cytnx_uint64 > &locator)
Get an element at specific location.
Definition Tensor.hpp:1155
Tensor clone() const
return a clone of the current Tensor.
Definition Tensor.hpp:863
void set(const std::vector< cytnx::Accessor > &accessors, const Tensor &rhs)
set elements with the input Tensor using Accessor (C++ API) / slices (python API)
Definition Tensor.hpp:1290
Tensor Norm() const
the Norm member function. Same as linalg::Norm(const Tensor &Tin), where Tin is the current Tensor.
Tensor astype(const int &new_type) const
return a new Tensor that cast to different dtype.
Definition Tensor.hpp:1120
const bool & is_contiguous() const
return whether the Tensor is contiguous or not.
Definition Tensor.hpp:918
Tensor & Conj_()
the Conj_ member function. Same as cytnx::linalg::Conj_(Tensor &Tin), where Tin is the current Tensor...
int device() const
the device-id of the Tensor
Definition Tensor.hpp:818
Tensor get(const std::vector< cytnx::Accessor > &accessors) const
get elements using Accessor (C++ API) / slices (python API)
Definition Tensor.hpp:1258
const std::vector< cytnx_uint64 > & shape() const
the shape of the Tensor
Definition Tensor.hpp:838
An Enhanced tensor specifically designed for physical Tensor network simulation.
Definition UniTensor.hpp:1705
void put_block(const Tensor &in_tens, const std::vector< cytnx_int64 > &qidx, const bool &force)
Put the block into the UniTensor with given quantum number.
Definition UniTensor.hpp:2722
UniTensor to(const int &device) const
move the current UniTensor to the assigned device.
Definition UniTensor.hpp:2197
UniTensor & operator*=(const UniTensor &rhs)
The multiplication assignment operator of the UniTensor.
Definition UniTensor.hpp:3236
Tensor & get_block_(const std::initializer_list< cytnx_int64 > &qidx, const bool &force=false)
Definition UniTensor.hpp:2633
std::vector< Tensor > & get_blocks_(const bool &silent=false)
Definition UniTensor.hpp:2700
Tensor & get_block_(const std::vector< cytnx_uint64 > &qidx, const bool &force=false)
Definition UniTensor.hpp:2641
void print_block(const cytnx_int64 &idx, const bool &full_info=true) const
Given a index and print out the corresponding block of the UniTensor.
Definition UniTensor.hpp:2455
UniTensor & operator/=(const UniTensor &rhs)
The division assignment operator of the UniTensor.
Definition UniTensor.hpp:3216
T & item()
Definition UniTensor.hpp:2022
bool is_contiguous() const
To tell whether the UniTensor is contiguous.
Definition UniTensor.hpp:2113
T get_elem(const std::vector< cytnx_uint64 > &locator) const
Definition UniTensor.hpp:3521
UniTensor Div(const UniTensor &rhs) const
The division function of the UniTensor.
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:3645
UniTensor & set_label(const cytnx_int64 &idx, const char *new_label)
Definition UniTensor.hpp:1931
UniTensor & operator+=(const UniTensor &rhs)
The addition assignment operator of the UniTensor.
Definition UniTensor.hpp:3176
void to_dense_()
Convert the UniTensor to non-diagonal form, inplacely.
Definition UniTensor.hpp:2795
UniTensor reshape(const std::vector< cytnx_int64 > &new_shape, const cytnx_uint64 &rowrank=0)
Reshape the UniTensor.
Definition UniTensor.hpp:2758
std::vector< Tensor > get_blocks() const
Get all the blocks of the UniTensor.
Definition UniTensor.hpp:2684
UniTensor permute(const std::initializer_list< char * > &mapper, const cytnx_int64 &rowrank=-1) const
Definition UniTensor.hpp:2377
void relabel_(const cytnx_int64 &inx, const std::string &new_label) const
rebable the lags in the UniTensor by given index.
Definition UniTensor.hpp:2294
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:2126
cytnx_int64 get_index(std::string lbl) const
Get the index of an desired label string.
Definition UniTensor.hpp:2154
std::string uten_type_str() const
Return the UniTensor type (cytnx::UTenType) of the UniTensor in 'string' form.
Definition UniTensor.hpp:2106
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:1795
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:2580
void permute_(const std::vector< cytnx_int64 > &mapper, const cytnx_int64 &rowrank=-1)
permute the lags of the UniTensor, inplacely.
Definition UniTensor.hpp:2395
UniTensor & tag()
Set the UniTensor as a tagged UniTensor.
Definition UniTensor.hpp:3480
void combineBonds(const std::vector< cytnx_int64 > &indicators, const bool &force=false)
Definition UniTensor.hpp:2826
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:1744
Tensor & get_block_(const std::vector< cytnx_int64 > &qidx, const bool &force=false)
Get the shared view of block for the given quantum indices.
Definition UniTensor.hpp:2626
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 with cytnx::Tensor.
Definition UniTensor.hpp:1767
UniTensor get(const std::vector< Accessor > &accessors) const
Definition UniTensor.hpp:2742
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.
Definition UniTensor.hpp:1946
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:3266
cytnx_uint64 rowrank() const
Return the row rank of the UniTensor.
Definition UniTensor.hpp:2057
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:3159
UniTensor Mul(const Scalar &rhs) const
The multiplication function for a given scalar.
UniTensor & set_labels(const std::initializer_list< char * > &new_labels)
Definition UniTensor.hpp:2000
UniTensor & Sub_(const Scalar &rhs)
The subtraction function for a given scalar.
Definition UniTensor.hpp:3011
UniTensor & operator/=(const Scalar &rhs)
The division assignment operator for a given scalar.
Definition UniTensor.hpp:3281
const bool & is_braket_form() const
Check whether the UniTensor is in braket form.
Definition UniTensor.hpp:2141
void set_elem(const std::vector< cytnx_uint64 > &locator, const T2 &rc)
Definition UniTensor.hpp:3531
UniTensor Add(const Scalar &rhs) const
The addition function for a given scalar.
std::vector< cytnx_uint64 > shape() const
Get the shape of the UniTensor.
Definition UniTensor.hpp:2171
UniTensor to_dense()
Convert the UniTensor to non-diagonal form.
Definition UniTensor.hpp:2785
UniTensor relabels(const std::initializer_list< char * > &new_lbls) const
Definition UniTensor.hpp:2241
const Tensor & get_block_(const std::vector< cytnx_int64 > &qidx, const bool &force=false) const
Definition UniTensor.hpp:2651
void reshape_(const std::vector< cytnx_int64 > &new_shape, const cytnx_uint64 &rowrank=0)
Reshape the UniTensor, inplacely.
Definition UniTensor.hpp:2770
UniTensor & set_labels(const std::vector< std::string > &new_labels)
Set new labels for all the bonds.
Definition UniTensor.hpp:1991
void permute_(const std::vector< std::string > &mapper, const cytnx_int64 &rowrank=-1)
permute the lags of the UniTensor, inplacely.
Definition UniTensor.hpp:2405
std::string name() const
Return the name of the UniTensor.
Definition UniTensor.hpp:2085
UniTensor & Add_(const Scalar &rhs)
The addition function for a given scalar.
Definition UniTensor.hpp:2981
UniTensor & Trace_(const std::string &a, const std::string &b)
Take the partial trance to the UniTensor, inplacely.
Definition UniTensor.hpp:3415
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:3593
Scalar::Sproxy at(const std::vector< cytnx_uint64 > &locator)
Get an element at specific location.
Definition UniTensor.hpp:2548
UniTensor Trace(const std::string &a, const std::string &b) const
Take the partial trance to the UniTensor.
Definition UniTensor.hpp:3385
bool same_data(const UniTensor &rhs) const
Check whether the Blocks address are the same.
Definition UniTensor.hpp:2880
UniTensor astype(const unsigned int &dtype) const
Return a new UniTensor that cast to different data type.
Definition UniTensor.hpp:2336
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:3607
UniTensor Transpose() const
Take the transpose of the UniTensor.
Definition UniTensor.hpp:3336
UniTensor & set_label(const char *old_label, const std::string &new_label)
Definition UniTensor.hpp:1954
static UniTensor Load(const char *fname)
load a UniTensor from file
UniTensor & Dagger_()
Take the conjugate transpose to the UniTensor, inplacely.
Definition UniTensor.hpp:3468
void print_diagram(const bool &bond_info=false)
Plot the diagram of the UniTensor.
Definition UniTensor.hpp:2442
std::vector< Bond > & bonds()
Definition UniTensor.hpp:2165
T & at(const std::vector< cytnx_uint64 > &locator)
Get an element at specific location.
Definition UniTensor.hpp:2479
UniTensor & set_label(const cytnx_int64 &idx, const std::string &new_label)
Set a new label for bond at the assigned index.
Definition UniTensor.hpp:1923
UniTensor contiguous() const
Make the UniTensor contiguous by coalescing the memory (storage).
Definition UniTensor.hpp:2426
Tensor get_block(const cytnx_uint64 &idx=0) const
Get the block of the UniTensor for a given index.
Definition UniTensor.hpp:2569
const T & at(const std::vector< cytnx_uint64 > &locator) const
Get an element at specific location.
Definition UniTensor.hpp:2505
UniTensor & operator*=(const Scalar &rhs)
The multiplication assignment operator for a given scalar.
Definition UniTensor.hpp:3296
void contiguous_()
Make the UniTensor contiguous by coalescing the memory (storage), inplacely.
Definition UniTensor.hpp:2436
UniTensor normalize() const
normalize the current UniTensor instance with 2-norm.
Definition UniTensor.hpp:3359
std::vector< Symmetry > syms() const
Return the symmetry type of the UniTensor.
Definition UniTensor.hpp:2133
UniTensor & Mul_(const UniTensor &rhs)
The multiplcation function of the UniTensor.
Definition UniTensor.hpp:2924
void put_block_(Tensor &in, const cytnx_uint64 &idx=0)
Put the block into the UniTensor with given index, inplacely.
Definition UniTensor.hpp:2731
void combineBonds(const std::vector< std::string > &indicators, const bool &force=false)
Combine the sevral bonds of the UniTensor.
Definition UniTensor.hpp:2817
UniTensor relabel(const std::string &old_label, const std::string &new_label) const
relabel the lags in the UniTensor by a given label.
Definition UniTensor.hpp:2324
UniTensor & normalize_()
normalize the UniTensor, inplacely.
Definition UniTensor.hpp:3371
bool is_diag() const
To tell whether the UniTensor is in diagonal form.
Definition UniTensor.hpp:2119
int device() const
Return the device of the UniTensor.
Definition UniTensor.hpp:2079
UniTensor & Pow_(const double &p)
Power function.
const Tensor & get_block_(const std::vector< cytnx_uint64 > &qidx, const bool &force=false) const
Definition UniTensor.hpp:2668
void set(const std::vector< Accessor > &accessors, const Tensor &rhs)
Definition UniTensor.hpp:2747
std::string dtype_str() const
Return the data type of the UniTensor in 'string' form.
Definition UniTensor.hpp:2092
UniTensor & operator+=(const Scalar &rhs)
The addition assignment operator for a given scalar.
Definition UniTensor.hpp:3251
UniTensor & Div_(const UniTensor &rhs)
The division function of the UniTensor.
Definition UniTensor.hpp:2966
UniTensor & Div_(const Scalar &rhs)
The division function for a given scalar.
Definition UniTensor.hpp:3026
cytnx_uint64 rank() const
Return the rank of the UniTensor.
Definition UniTensor.hpp:2051
void group_basis_()
Group the same quantum number basis together.
Definition UniTensor.hpp:2465
void to_(const int &device)
move the current UniTensor to the assigned device (inplace).
Definition UniTensor.hpp:2186
UniTensor & Trace_(const cytnx_int64 &a=0, const cytnx_int64 &b=1)
Take the partial trance to the UniTensor, inplacely.
Definition UniTensor.hpp:3437
UniTensor & set_name(const std::string &in)
Set the name of the UniTensor.
Definition UniTensor.hpp:1908
UniTensor Trace(const cytnx_int64 &a=0, const cytnx_int64 &b=1) const
Take the partial trance to the UniTensor.
Definition UniTensor.hpp:3400
UniTensor & Add_(const UniTensor &rhs)
The addition function of the UniTensor.
Definition UniTensor.hpp:2903
UniTensor permute(const std::vector< cytnx_int64 > &mapper, const cytnx_int64 &rowrank=-1) const
permute the lags of the UniTensor
Definition UniTensor.hpp:2354
UniTensor & set_label(const std::string &old_label, const char *new_label)
Definition UniTensor.hpp:1962
UniTensor permute(const std::vector< std::string > &mapper, const cytnx_int64 &rowrank=-1) const
permute the lags of the UniTensor by labels
Definition UniTensor.hpp:2367
unsigned int dtype() const
Return the data type of the UniTensor.
Definition UniTensor.hpp:2064
UniTensor Pow(const double &p) const
Power function.
UniTensor & Mul_(const Scalar &rhs)
The multiplication function for a given scalar.
Definition UniTensor.hpp:2996
UniTensor & operator-=(const UniTensor &rhs)
The subtraction assignment operator of the UniTensor.
Definition UniTensor.hpp:3196
void combineBonds(const std::vector< cytnx_int64 > &indicators, const bool &force, const bool &by_label)
Definition UniTensor.hpp:2802
UniTensor & set_label(const char *old_label, const char *new_label)
Definition UniTensor.hpp:1970
bool is_blockform() const
Check whether the UniTensor is in block form.
Definition UniTensor.hpp:2178
UniTensor group_basis() const
Definition UniTensor.hpp:2467
UniTensor Dagger() const
Take the conjugate transpose to the UniTensor.
Definition UniTensor.hpp:3456
const std::vector< Tensor > & get_blocks_(const bool &silent=false) const
Get all the blocks of the UniTensor, inplacely.
Definition UniTensor.hpp:2692
const Tensor & get_block_(const std::initializer_list< cytnx_int64 > &qidx, const bool &force=false) const
Definition UniTensor.hpp:2659
bool elem_exists(const std::vector< cytnx_uint64 > &locator) const
Geiven the locator, check if the element exists.
Definition UniTensor.hpp:3511
UniTensor & Sub_(const UniTensor &rhs)
The subtraction function of the UniTensor.
Definition UniTensor.hpp:2945
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:2848
void put_block(const Tensor &in, const cytnx_uint64 &idx=0)
Put the block into the UniTensor with given index.
Definition UniTensor.hpp:2710
const Tensor & get_block_(const cytnx_uint64 &idx=0) const
Get the shared view of block for the given index.
Definition UniTensor.hpp:2608
static UniTensor Load(const std::string &fname)
load a UniTensor from file
UniTensor relabel(const cytnx_int64 &inx, const std::string &new_label) const
rebable the lags in the UniTensor by given index.
Definition UniTensor.hpp:2281
UniTensor Mul(const UniTensor &rhs) const
The multiplication function of the UniTensor.
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:3635
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:2147
const std::vector< Bond > & bonds() const
Get the bonds of the UniTensor.
Definition UniTensor.hpp:2160
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:3622
UniTensor & Transpose_()
Take the transpose of the UniTensor, inplacely.
Definition UniTensor.hpp:3348
UniTensor clone() const
Clone (deep copy) the UniTensor.
Definition UniTensor.hpp:2207
vec2d< cytnx_uint64 > & get_itoi()
Definition UniTensor.hpp:3656
void print_blocks(const bool &full_info=true) const
Print all of the blocks in the UniTensor.
Definition UniTensor.hpp:2448
void relabel_(const std::string &old_label, const std::string &new_label) const
relabel the lags in the UniTensor by a given label.
Definition UniTensor.hpp:2306
Scalar::Sproxy item() const
Definition UniTensor.hpp:2032
const vec2d< cytnx_uint64 > & get_itoi() const
get the q-indices on each leg for all the blocks
Definition UniTensor.hpp:3655
std::string device_str() const
Return the device of the UniTensor in 'string' form.
Definition UniTensor.hpp:2099
UniTensor relabels(const std::vector< std::string > &new_labels) const
rebables all of the labels in UniTensor.
Definition UniTensor.hpp:2232
void put_block_(Tensor &in, const std::vector< cytnx_int64 > &qidx, const bool &force)
Put the block into the UniTensor with given quantum indices, inplacely.
Definition UniTensor.hpp:2739
void relabels_(const std::initializer_list< char * > &new_lbls)
Definition UniTensor.hpp:2256
void relabels_(const std::vector< std::string > &new_labels) const
Set new labels for all the bonds.
Definition UniTensor.hpp:2222
UniTensor Sub(const UniTensor &rhs) const
The subtraction function of the UniTensor.
void 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:1845
UniTensor & Conj_()
Apply complex conjugate on each entry of the UniTensor.
Definition UniTensor.hpp:3321
cytnx_uint64 Nblocks() const
Return the number of blocks in the UniTensor.
Definition UniTensor.hpp:2045
Tensor get_block(const std::vector< cytnx_uint64 > &qnum, const bool &force=false) const
Definition UniTensor.hpp:2598
void Save(const char *fname) const
save a UniTensor to file
UniTensor Conj()
Apply complex conjugate on each entry of the UniTensor.
Definition UniTensor.hpp:3308
UniTensor Add(const UniTensor &rhs) const
The addition function of the UniTensor.
UniTensor & set_rowrank(const cytnx_uint64 &new_rowrank)
Set the row rank of the UniTensor.
Definition UniTensor.hpp:2016
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:3580
Tensor get_block(const std::initializer_list< cytnx_int64 > &qnum, const bool &force=false) const
Definition UniTensor.hpp:2588
const Scalar::Sproxy at(const std::vector< cytnx_uint64 > &locator) const
Get an element at specific location.
Definition UniTensor.hpp:2529
int uten_type() const
Return the UniTensor type (cytnx::UTenType) of the UniTensor.
Definition UniTensor.hpp:2072
Tensor & get_block_(const cytnx_uint64 &idx=0)
Definition UniTensor.hpp:2616
#define cytnx_warning_msg(is_true, format,...)
Definition cytnx_error.hpp:40
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
cytnx::UniTensor Trace(const cytnx::UniTensor &Tin, const cytnx_int64 &a=0, const cytnx_int64 &b=1)
Tensor Conj(const Tensor &Tin)
Conjugate all the element in Tensor.
Tensor Norm(const Tensor &Tl)
Calculate the norm of a tensor.
void Conj_(Tensor &Tin)
inplace perform Conjugate on all the element in Tensor.
Definition Accessor.hpp:12
Device_class Device
data on which devices.
double cytnx_double
Definition Type.hpp:43
UniTensorType_class UTenType
UniTensor type.
uint32_t cytnx_uint32
Definition Type.hpp:46
std::complex< double > cytnx_complex128
Definition Type.hpp:53
float cytnx_float
Definition Type.hpp:44
int16_t cytnx_int16
Definition Type.hpp:50
std::complex< float > cytnx_complex64
Definition Type.hpp:52
int32_t cytnx_int32
Definition Type.hpp:49
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.
uint16_t cytnx_uint16
Definition Type.hpp:47
uint64_t cytnx_uint64
Definition Type.hpp:45
int64_t cytnx_int64
Definition Type.hpp:48
std::vector< std::vector< T > > vec2d
Definition Type.hpp:41
UniTensor Contracts(const std::vector< UniTensor > &TNs, const std::string &order, const bool &optimal)
Contract multiple UniTensor by tracing the ranks with common labels with pairwise operation.