14#include <initializer_list>
24 #include "backend/Scalar.hpp"
28 using namespace cytnx;
30 class UniTensorType_class {
38 std::string getname(
const int &ut_type);
62 class UniTensor_base :
public intrusive_ptr_base<UniTensor_base> {
70 std::vector<std::string> _labels;
71 std::vector<Bond> _bonds;
73 bool _update_braket() {
74 if (_bonds.size() == 0)
return false;
76 if (this->_bonds[0].type() != bondType::BD_REG) {
78 for (
unsigned int i = 0; i < this->_bonds.size(); i++) {
79 if (i < this->_rowrank) {
80 if (this->_bonds[i].type() != bondType::BD_KET)
return false;
82 if (this->_bonds[i].type() != bondType::BD_BRA)
return false;
92 friend class DenseUniTensor;
94 friend class BlockUniTensor;
98 _name(std::string(
"")),
99 _is_braket_form(false),
105 UniTensor_base(
const UniTensor_base &rhs);
106 UniTensor_base &operator=(UniTensor_base &rhs);
109 bool is_diag()
const {
return this->_is_diag; }
110 const bool &is_braket_form()
const {
return this->_is_braket_form; }
111 const bool &is_tag()
const {
return this->_is_tag; }
112 const std::vector<std::string> &labels()
const {
return this->_labels; }
120 std::vector<std::string> labels = this->_labels;
122 if (labels[i] == label)
return i;
126 const std::vector<Bond> &bonds()
const {
return this->_bonds; }
127 std::vector<Bond> &bonds() {
return this->_bonds; }
130 cytnx_error_msg(idx >= this->_bonds.size(),
"[ERROR][bond] index %d out of bound, total %d\n",
131 idx, this->_bonds.size());
132 return this->_bonds[idx];
135 Bond &bond_(
const std::string &label) {
136 auto res = std::find(this->_labels.begin(), this->_labels.end(), label);
137 cytnx_error_msg(res == this->_labels.end(),
"[ERROR] label %s not exists.\n", label.c_str());
138 cytnx_uint64 idx = std::distance(this->_labels.begin(), res);
140 return this->bond_(idx);
143 const std::string &name()
const {
return this->_name; }
144 cytnx_uint64 rank()
const {
return this->_labels.size(); }
145 void set_name(
const std::string &in) { this->_name = in; }
157 void set_label(
const std::string &oldlabel,
const std::string &new_label) {
159 auto res = std::find(this->_labels.begin(), this->_labels.end(), oldlabel);
160 cytnx_error_msg(res == this->_labels.end(),
"[ERROR] label %s not exists.\n",
162 idx = std::distance(this->_labels.begin(), res);
164 cytnx_error_msg(idx >= this->_labels.size(),
"[ERROR] index exceed the rank of UniTensor%s",
168 for (
cytnx_uint64 i = 0; i < this->_labels.size(); i++) {
169 if (i == idx)
continue;
170 if (new_label == this->_labels[i]) {
175 cytnx_error_msg(is_dup,
"[ERROR] alreay has a label that is the same as the input label%s",
177 this->_labels[idx] = new_label;
179 void set_label(
const cytnx_int64 &inx,
const std::string &new_label) {
181 cytnx_error_msg(inx >= this->_labels.size(),
"[ERROR] index exceed the rank of UniTensor%s",
185 for (
cytnx_uint64 i = 0; i < this->_labels.size(); i++) {
186 if (i == inx)
continue;
187 if (new_label == this->_labels[i]) {
192 cytnx_error_msg(is_dup,
"[ERROR] alreay has a label that is the same as the input label%s",
194 this->_labels[inx] = new_label;
197 void set_labels(
const std::vector<std::string> &new_labels);
198 void relabels_(
const std::vector<std::string> &new_labels);
199 void relabels_(
const std::vector<std::string> &old_labels,
200 const std::vector<std::string> &new_labels);
201 void relabel_(
const std::string &old_label,
const std::string &new_label) {
202 this->set_label(old_label, new_label);
204 void relabel_(
const cytnx_int64 &inx,
const std::string &new_label) {
205 this->set_label(inx, new_label);
208 int uten_type() {
return this->uten_type_id; }
209 std::string uten_type_str() {
return UTenType.getname(this->uten_type_id); }
214 virtual void Init(
const std::vector<Bond> &bonds,
215 const std::vector<std::string> &in_labels = {},
216 const cytnx_int64 &rowrank = -1,
const unsigned int &dtype =
Type.Double,
217 const int &device =
Device.cpu,
const bool &is_diag =
false,
218 const bool &no_alloc =
false,
const std::string &name =
"");
220 virtual void Init_by_Tensor(
const Tensor &in,
const bool &is_diag =
false,
221 const cytnx_int64 &rowrank = -1,
const std::string &name =
"");
222 virtual std::vector<cytnx_uint64> shape()
const;
223 virtual bool is_blockform()
const;
224 virtual bool is_contiguous()
const;
225 virtual void to_(
const int &device);
226 virtual boost::intrusive_ptr<UniTensor_base> to(
const int &device);
227 virtual boost::intrusive_ptr<UniTensor_base> clone()
const;
228 virtual unsigned int dtype()
const;
229 virtual int device()
const;
230 virtual std::string dtype_str()
const;
231 virtual std::string device_str()
const;
232 virtual void set_rowrank_(
const cytnx_uint64 &new_rowrank);
233 virtual boost::intrusive_ptr<UniTensor_base> set_rowrank(
const cytnx_uint64 &new_rowrank)
const;
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);
312 virtual boost::intrusive_ptr<UniTensor_base> relabels(
313 const std::vector<std::string> &old_labels,
const std::vector<std::string> &new_labels);
315 virtual boost::intrusive_ptr<UniTensor_base> relabel(
const std::string &old_label,
316 const std::string &new_label);
318 virtual boost::intrusive_ptr<UniTensor_base> relabel(
const cytnx_int64 &inx,
319 const std::string &new_label);
321 virtual std::vector<Symmetry> syms()
const;
324 virtual void Add_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
325 virtual void Add_(
const Scalar &rhs);
327 virtual void Mul_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
328 virtual void Mul_(
const Scalar &rhs);
330 virtual void Sub_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
331 virtual void Sub_(
const Scalar &rhs);
332 virtual void lSub_(
const Scalar &lhs);
334 virtual void Div_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
335 virtual void Div_(
const Scalar &rhs);
336 virtual void lDiv_(
const Scalar &lhs);
339 virtual boost::intrusive_ptr<UniTensor_base> normalize();
340 virtual void normalize_();
342 virtual boost::intrusive_ptr<UniTensor_base>
Conj();
343 virtual void Conj_();
345 virtual boost::intrusive_ptr<UniTensor_base> Transpose();
346 virtual void Transpose_();
348 virtual boost::intrusive_ptr<UniTensor_base> Dagger();
349 virtual void Dagger_();
353 virtual void truncate_(
const std::string &bond_idx,
const cytnx_uint64 &dim);
356 virtual bool elem_exists(
const std::vector<cytnx_uint64> &locator)
const;
359 virtual Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator);
360 virtual const Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator)
const;
362 virtual cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
364 virtual cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
366 virtual cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
368 virtual cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
370 virtual cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
372 virtual cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
374 virtual cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
376 virtual cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
378 virtual cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
380 virtual cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
383 virtual const cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
385 virtual const cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
387 virtual const cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
389 virtual const cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
391 virtual const cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
393 virtual const cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
395 virtual const cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
397 virtual const cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
399 virtual const cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
401 virtual const cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
404 virtual void from_(
const boost::intrusive_ptr<UniTensor_base> &rhs,
const bool &force);
406 virtual void group_basis_();
407 virtual const std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx)
const;
408 virtual std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx);
412 virtual void _save_dispatch(std::fstream &f)
const;
413 virtual void _load_dispatch(std::fstream &f);
415 virtual ~UniTensor_base(){};
421 class DenseUniTensor :
public UniTensor_base {
425 std::vector<Tensor> _interface_block;
426 DenseUniTensor *clone_meta()
const {
427 DenseUniTensor *
tmp =
new DenseUniTensor();
428 tmp->_bonds = vec_clone(this->_bonds);
429 tmp->_labels = this->_labels;
430 tmp->_is_braket_form = this->_is_braket_form;
431 tmp->_rowrank = this->_rowrank;
432 tmp->_is_diag = this->_is_diag;
433 tmp->_name = this->_name;
434 tmp->_is_tag = this->_is_tag;
439 DenseUniTensor() { this->uten_type_id =
UTenType.Dense; };
448 void Init(
const std::vector<Bond> &bonds,
const std::vector<std::string> &in_labels = {},
449 const cytnx_int64 &rowrank = -1,
const unsigned int &dtype =
Type.Double,
450 const int &device =
Device.cpu,
const bool &is_diag =
false,
451 const bool &no_alloc =
false,
const std::string &name =
"");
453 void Init_by_Tensor(
const Tensor &in_tensor,
const bool &is_diag =
false,
454 const cytnx_int64 &rowrank = -1,
const std::string &name =
"");
455 std::vector<cytnx_uint64> shape()
const {
456 if (this->_is_diag) {
457 std::vector<cytnx_uint64> shape = this->_block.
shape();
458 shape.push_back(shape[0]);
461 return this->_block.
shape();
464 bool is_blockform()
const {
return false; }
465 void to_(
const int &device) { this->_block.
to_(device); }
466 boost::intrusive_ptr<UniTensor_base> to(
const int &device) {
467 if (this->device() == device) {
468 std::vector<Tensor> _interface_block;
471 boost::intrusive_ptr<UniTensor_base> out = this->clone();
478 "[ERROR] rowrank cannot exceed the rank of UniTensor.%s",
"\n");
479 if (this->is_diag()) {
480 cytnx_error_msg(new_rowrank != 1,
"[ERROR] rowrank should be [==1] when is_diag =true!.%s",
484 this->_rowrank = new_rowrank;
487 boost::intrusive_ptr<UniTensor_base> set_rowrank(
const cytnx_uint64 &new_rowrank)
const {
488 DenseUniTensor *out_raw = this->clone_meta();
489 out_raw->_block = this->_block;
490 out_raw->set_rowrank_(new_rowrank);
491 boost::intrusive_ptr<UniTensor_base> out(out_raw);
495 boost::intrusive_ptr<UniTensor_base> clone()
const {
496 DenseUniTensor *
tmp = this->clone_meta();
498 boost::intrusive_ptr<UniTensor_base> out(tmp);
501 bool is_contiguous()
const {
return this->_block.
is_contiguous(); }
502 unsigned int dtype()
const {
return this->_block.
dtype(); }
503 int device()
const {
return this->_block.
device(); }
504 std::string dtype_str()
const {
return Type.getname(this->_block.
dtype()); }
505 std::string device_str()
const {
return Device.getname(this->_block.
device()); }
515 boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<cytnx_int64> &mapper,
517 boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<std::string> &mapper,
528 void permute_(
const std::vector<cytnx_int64> &mapper,
const cytnx_int64 &rowrank = -1);
529 void permute_(
const std::vector<std::string> &mapper,
const cytnx_int64 &rowrank = -1);
531 boost::intrusive_ptr<UniTensor_base> relabels(
const std::vector<std::string> &new_labels);
533 boost::intrusive_ptr<UniTensor_base> relabels(
const std::vector<std::string> &old_labels,
534 const std::vector<std::string> &new_labels);
546 boost::intrusive_ptr<UniTensor_base> relabel(
const std::string &old_label,
547 const std::string &new_label);
548 boost::intrusive_ptr<UniTensor_base> relabel(
const cytnx_int64 &inx,
549 const std::string &new_label);
551 boost::intrusive_ptr<UniTensor_base> astype(
const unsigned int &dtype)
const {
552 DenseUniTensor *
tmp = this->clone_meta();
553 tmp->_block = this->_block.
astype(dtype);
554 boost::intrusive_ptr<UniTensor_base> out(tmp);
558 std::vector<Symmetry> syms()
const {
559 cytnx_error_msg(
true,
"[ERROR][DenseUniTensor] dense unitensor does not have symmetry.%s",
561 return std::vector<Symmetry>();
564 boost::intrusive_ptr<UniTensor_base> contiguous_() {
566 return boost::intrusive_ptr<UniTensor_base>(
this);
568 boost::intrusive_ptr<UniTensor_base> contiguous() {
570 if (this->is_contiguous()) {
571 boost::intrusive_ptr<UniTensor_base> out(
this);
574 DenseUniTensor *
tmp = this->clone_meta();
576 boost::intrusive_ptr<UniTensor_base> out(tmp);
580 void print_diagram(
const bool &bond_info =
false);
581 void print_blocks(
const bool &full_info =
true)
const;
582 void print_block(
const cytnx_int64 &idx,
const bool &full_info =
true)
const;
585 Tensor get_block(
const std::vector<cytnx_int64> &qnum,
const bool &force)
const {
587 true,
"[ERROR][DenseUniTensor] try to get_block() using qnum on a non-symmetry UniTensor%s",
592 const Tensor &get_block_(
const std::vector<cytnx_int64> &qnum,
const bool &force)
const {
595 "[ERROR][DenseUniTensor] try to get_block_() using qnum on a non-symmetry UniTensor%s",
599 Tensor &get_block_(
const std::vector<cytnx_int64> &qnum,
const bool &force) {
602 "[ERROR][DenseUniTensor] try to get_block_() using qnum on a non-symmetry UniTensor%s",
613 std::vector<Tensor> get_blocks()
const {
614 std::vector<Tensor> out;
616 true,
"[ERROR][DenseUniTensor] cannot use get_blocks(), use get_block() instead!%s",
"\n");
619 const std::vector<Tensor> &get_blocks_(
const bool &silent =
false)
const {
621 true,
"[ERROR][DenseUniTensor] cannot use get_blocks_(), use get_block_() instead!%s",
623 return this->_interface_block;
625 std::vector<Tensor> &get_blocks_(
const bool &silent =
false) {
627 true,
"[ERROR][DenseUniTensor] cannot use get_blocks_(), use get_block_() instead!%s",
629 return this->_interface_block;
641 "[ERROR][DenseUniTensor][put_block] The input tensor device does not "
646 if (this->is_diag()) {
648 in.
shape() != this->_block.shape(),
649 "[ERROR][DenseUniTensor] put_block, the input tensor shape does not match.%s",
"\n");
650 this->_block = in.
clone();
653 in.
shape() != this->shape(),
654 "[ERROR][DenseUniTensor] put_block, the input tensor shape does not match.%s",
"\n");
655 this->_block = in.
clone();
668 "[ERROR][DenseUniTensor][put_block] The input tensor device does not "
673 if (this->is_diag()) {
675 in.
shape() != this->_block.shape(),
676 "[ERROR][DenseUniTensor] put_block, the input tensor shape does not match.%s",
"\n");
680 in.
shape() != this->shape(),
681 "[ERROR][DenseUniTensor] put_block, the input tensor shape does not match.%s",
"\n");
686 void put_block(
const Tensor &in,
const std::vector<cytnx_int64> &qnum,
const bool &force) {
688 true,
"[ERROR][DenseUniTensor] try to put_block using qnum on a non-symmetry UniTensor%s",
691 void put_block_(
Tensor &in,
const std::vector<cytnx_int64> &qnum,
const bool &force) {
693 true,
"[ERROR][DenseUniTensor] try to put_block using qnum on a non-symmetry UniTensor%s",
697 boost::intrusive_ptr<UniTensor_base> get(
const std::vector<Accessor> &accessors) {
698 boost::intrusive_ptr<UniTensor_base> out(
new DenseUniTensor());
699 out->Init_by_Tensor(this->_block.
get(accessors),
false, 0);
703 void set(
const std::vector<Accessor> &accessors,
const Tensor &rhs) {
704 this->_block.
set(accessors, rhs);
707 void reshape_(
const std::vector<cytnx_int64> &new_shape,
const cytnx_uint64 &rowrank = 0);
708 boost::intrusive_ptr<UniTensor_base> reshape(
const std::vector<cytnx_int64> &new_shape,
710 boost::intrusive_ptr<UniTensor_base> to_dense();
721 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force,
722 const bool &by_label);
723 void combineBonds(
const std::vector<std::string> &indicators,
const bool &force =
true);
724 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force =
true);
725 boost::intrusive_ptr<UniTensor_base> contract(
const boost::intrusive_ptr<UniTensor_base> &rhs,
726 const bool &mv_elem_self =
false,
727 const bool &mv_elem_rhs =
false);
728 std::vector<Bond> getTotalQnums(
const bool &physical =
false) {
730 "getTotalQnums can only operate on UniTensor with symmetry.\n");
731 return std::vector<Bond>();
734 std::vector<std::vector<cytnx_int64>> get_blocks_qnums()
const {
736 "get_blocks_qnums can only operate on UniTensor with symmetry.\n");
737 return std::vector<std::vector<cytnx_int64>>();
740 bool same_data(
const boost::intrusive_ptr<UniTensor_base> &rhs)
const {
741 if (rhs->uten_type() !=
UTenType.Dense)
return false;
743 return this->get_block_().same_data(rhs->get_block_());
749 void Add_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
750 void Add_(
const Scalar &rhs);
752 void Mul_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
753 void Mul_(
const Scalar &rhs);
755 void Sub_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
756 void Sub_(
const Scalar &rhs);
757 void lSub_(
const Scalar &lhs);
759 void Div_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
760 void Div_(
const Scalar &rhs);
761 void lDiv_(
const Scalar &lhs);
765 boost::intrusive_ptr<UniTensor_base>
Conj() {
766 boost::intrusive_ptr<UniTensor_base> out = this->clone();
771 boost::intrusive_ptr<UniTensor_base> Transpose() {
772 boost::intrusive_ptr<UniTensor_base> out = this->clone();
778 boost::intrusive_ptr<UniTensor_base> normalize() {
779 boost::intrusive_ptr<UniTensor_base> out = this->clone();
785 boost::intrusive_ptr<UniTensor_base> Dagger() {
786 boost::intrusive_ptr<UniTensor_base> out = this->
Conj();
804 void Trace_(
const std::string &a,
const std::string &b);
805 boost::intrusive_ptr<UniTensor_base>
Trace(
const std::string &a,
const std::string &b) {
806 boost::intrusive_ptr<UniTensor_base> out = this->clone();
811 boost::intrusive_ptr<UniTensor_base> out = this->clone();
818 const Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator)
const {
820 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
822 return Scalar::Sproxy();
824 const cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
827 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
831 const cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
834 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
838 const cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
841 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
845 const cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
848 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
852 const cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
855 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
859 const cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
862 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
866 const cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
869 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
873 const cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
876 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
880 const cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
883 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
887 const cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
890 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
895 Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator) {
897 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
899 return Scalar::Sproxy();
904 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
908 cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
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",
941 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
947 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
953 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
959 true,
"[ERROR][Internal] This shouldn't be called by DenseUniTensor, something wrong.%s",
964 bool elem_exists(
const std::vector<cytnx_uint64> &locator)
const {
966 true,
"[ERROR][DenseUniTensor] elem_exists can only be used on UniTensor with Symmetry.%s",
970 if (!this->is_tag()) {
971 for (
int i = 0; i < this->_rowrank; i++) {
972 this->_bonds[i].set_type(BD_KET);
974 for (
int i = this->_rowrank; i < this->_bonds.size(); i++) {
975 this->_bonds[i].set_type(BD_BRA);
977 this->_is_tag =
true;
978 this->_is_braket_form = this->_update_braket();
991 void truncate_(
const std::string &bond_idx,
const cytnx_uint64 &dim);
993 void from_(
const boost::intrusive_ptr<UniTensor_base> &rhs,
const bool &force);
995 void group_basis_() {
996 cytnx_warning_msg(
true,
"[WARNING] group basis will not have any effect on DensUniTensor.%s",
1000 void _save_dispatch(std::fstream &f)
const;
1001 void _load_dispatch(std::fstream &f);
1003 const std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx)
const {
1004 cytnx_error_msg(
true,
"[ERROR] get_qindices can only be unsed on UniTensor with Symmetry.%s",
1007 std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx) {
1008 cytnx_error_msg(
true,
"[ERROR] get_qindices can only be unsed on UniTensor with Symmetry.%s",
1013 cytnx_error_msg(
true,
"[ERROR] get_itoi can only be unsed on UniTensor with Symmetry.%s",
1017 cytnx_error_msg(
true,
"[ERROR] get_itoi can only be unsed on UniTensor with Symmetry.%s",
1027 class BlockUniTensor :
public UniTensor_base {
1030 std::vector<std::vector<cytnx_uint64>> _inner_to_outer_idx;
1031 std::vector<Tensor> _blocks;
1037 void _fx_get_total_fluxs(std::vector<cytnx_uint64> &loc,
const std::vector<Symmetry> &syms,
1038 std::vector<cytnx_int64> &total_qns) {
1039 memset(&total_qns[0], 0,
sizeof(
cytnx_int64) * total_qns.size());
1042 if (this->_bonds[0].type() == BD_BRA)
1043 total_qns[i] = syms[0].reverse_rule(this->_bonds[0]._impl->_qnums[loc[0]][i]);
1045 total_qns[i] = this->_bonds[0]._impl->_qnums[loc[0]][i];
1047 for (
auto j = 1; j < loc.size(); j++) {
1048 if (this->_bonds[j].type() == BD_BRA)
1049 total_qns[i] = syms[i].combine_rule(
1050 total_qns[i], syms[i].reverse_rule(this->_bonds[j]._impl->_qnums[loc[j]][i]));
1053 syms[i].combine_rule(total_qns[i], this->_bonds[j]._impl->_qnums[loc[j]][i]);
1059 void _fx_locate_elem(
cytnx_int64 &bidx, std::vector<cytnx_uint64> &loc_in_T,
1060 const std::vector<cytnx_uint64> &locator)
const;
1063 void _fx_group_duplicates(
const std::vector<cytnx_uint64> &dup_bond_idxs,
1064 const std::vector<std::vector<cytnx_uint64>> &idx_mappers);
1066 void set_meta(BlockUniTensor *tmp,
const bool &inner,
const bool &outer)
const {
1069 tmp->_bonds = vec_clone(this->_bonds);
1070 tmp->_labels = this->_labels;
1071 tmp->_is_braket_form = this->_is_braket_form;
1072 tmp->_rowrank = this->_rowrank;
1073 tmp->_name = this->_name;
1076 tmp->_is_diag = this->_is_diag;
1080 tmp->_inner_to_outer_idx = this->_inner_to_outer_idx;
1084 BlockUniTensor *clone_meta(
const bool &inner,
const bool &outer)
const {
1085 BlockUniTensor *
tmp =
new BlockUniTensor();
1086 this->set_meta(tmp, inner, outer);
1092 this->uten_type_id =
UTenType.Block;
1093 this->_is_tag =
true;
1102 void Init(
const std::vector<Bond> &bonds,
const std::vector<std::string> &in_labels = {},
1103 const cytnx_int64 &rowrank = -1,
const unsigned int &dtype =
Type.Double,
1104 const int &device =
Device.cpu,
const bool &is_diag =
false,
1105 const bool &no_alloc =
false,
const std::string &name =
"");
1107 void Init_by_Tensor(
const Tensor &in_tensor,
const bool &is_diag =
false,
1108 const cytnx_int64 &rowrank = -1,
const std::string &name =
"") {
1110 true,
"[ERROR][BlockUniTensor] cannot use Init_by_tensor() on a BlockUniTensor.%s",
"\n");
1113 std::vector<cytnx_uint64> shape()
const {
1114 std::vector<cytnx_uint64> out(this->_bonds.size());
1116 out[i] = this->_bonds[i].dim();
1121 bool is_blockform()
const {
return true; }
1122 bool is_contiguous()
const {
1124 for (
int i = 0; i < this->_blocks.size(); i++) {
1125 out &= this->_blocks[i].is_contiguous();
1130 cytnx_uint64 Nblocks()
const {
return this->_blocks.size(); };
1132 void to_(
const int &device) {
1133 for (
cytnx_uint64 i = 0; i < this->_blocks.size(); i++) {
1134 this->_blocks[i].to_(device);
1138 boost::intrusive_ptr<UniTensor_base> to(
const int &device) {
1139 if (this->device() == device) {
1142 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1148 boost::intrusive_ptr<UniTensor_base> clone()
const {
1149 BlockUniTensor *
tmp = this->clone_meta(
true,
true);
1150 tmp->_blocks = vec_clone(this->_blocks);
1151 boost::intrusive_ptr<UniTensor_base> out(tmp);
1155 unsigned int dtype()
const {
1157 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
1160 return this->_blocks[0].dtype();
1162 int device()
const {
1164 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
1167 return this->_blocks[0].device();
1169 std::string dtype_str()
const {
1171 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
1174 return this->_blocks[0].dtype_str();
1176 std::string device_str()
const {
1178 cytnx_error_msg(this->_blocks.size() == 0,
"[ERROR][internal] empty blocks for blockform.%s",
1181 return this->_blocks[0].device_str();
1185 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1187 return this->_blocks[idx].clone();
1191 Tensor get_block(
const std::vector<cytnx_int64> &indices,
const bool &force_return)
const {
1193 "[ERROR][get_block][BlockUniTensor] len(indices) must be the same as the "
1194 "Tensor rank (number of legs).%s",
1197 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1201 for (
cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1202 if (inds == this->_inner_to_outer_idx[i]) {
1210 return NullRefTensor;
1213 "[ERROR][get_block][BlockUniTensor] no avaliable block exists, "
1214 "force_return=false, so "
1215 "error throws. \n If you want to return an empty block without "
1216 "error when block is "
1217 "not avaliable, set force_return=True.%s",
1221 return this->_blocks[b].clone();
1226 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1228 return this->_blocks[idx];
1232 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1234 return this->_blocks[idx];
1237 const Tensor &get_block_(
const std::vector<cytnx_int64> &indices,
1238 const bool &force_return)
const {
1240 "[ERROR][get_block][BlockUniTensor] len(indices) must be the same as the "
1241 "Tensor rank (number of legs).%s",
1244 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1248 for (
cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1249 if (inds == this->_inner_to_outer_idx[i]) {
1257 return this->NullRefTensor;
1260 "[ERROR][get_block][BlockUniTensor] no avaliable block exists, "
1261 "force_return=false, so "
1262 "error throws. \n If you want to return an empty block without "
1263 "error when block is "
1264 "not avaliable, set force_return=True.%s",
1268 return this->_blocks[b];
1272 Tensor &get_block_(
const std::vector<cytnx_int64> &indices,
const bool &force_return) {
1274 "[ERROR][get_block][BlockUniTensor] len(indices) must be the same as the "
1275 "Tensor rank (number of legs).%s",
1278 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1282 for (
cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1283 if (inds == this->_inner_to_outer_idx[i]) {
1291 return this->NullRefTensor;
1294 "[ERROR][get_block][BlockUniTensor] no avaliable block exists, "
1295 "force_return=false, so "
1296 "error throws. \n If you want to return an empty block without "
1297 "error when block is "
1298 "not avaliable, set force_return=True.%s",
1302 return this->_blocks[b];
1306 std::vector<Tensor> get_blocks()
const {
return vec_clone(this->_blocks); }
1307 const std::vector<Tensor> &get_blocks_(
const bool &)
const {
return this->_blocks; }
1308 std::vector<Tensor> &get_blocks_(
const bool &) {
return this->_blocks; }
1310 bool same_data(
const boost::intrusive_ptr<UniTensor_base> &rhs)
const {
1311 if (rhs->uten_type() !=
UTenType.Block)
return false;
1312 if (rhs->get_blocks_(1).size() != this->get_blocks_(1).size())
return false;
1314 for (
int i = 0; i < rhs->get_blocks_(1).size(); i++)
1315 if (this->get_blocks_(1)[i].same_data(rhs->get_blocks_(1)[i]) ==
false)
return false;
1322 "[ERROR][BlockUniTensor] rowrank should be [>=0] and [<=UniTensor.rank].%s",
1324 if (this->is_diag()) {
1326 "[ERROR][BlockUniTensor] rowrank should be [==1] when is_diag =true!.%s",
1329 this->_rowrank = new_rowrank;
1330 this->_is_braket_form = this->_update_braket();
1333 boost::intrusive_ptr<UniTensor_base> set_rowrank(
const cytnx_uint64 &new_rowrank)
const {
1334 BlockUniTensor *
tmp = this->clone_meta(
true,
true);
1335 tmp->_blocks = this->_blocks;
1336 tmp->set_rowrank_(new_rowrank);
1337 boost::intrusive_ptr<UniTensor_base> out(tmp);
1341 boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<cytnx_int64> &mapper,
1343 boost::intrusive_ptr<UniTensor_base> permute(
const std::vector<std::string> &mapper,
1346 void permute_(
const std::vector<cytnx_int64> &mapper,
const cytnx_int64 &rowrank = -1);
1347 void permute_(
const std::vector<std::string> &mapper,
const cytnx_int64 &rowrank = -1);
1349 boost::intrusive_ptr<UniTensor_base> contiguous_() {
1350 for (
unsigned int b = 0; b < this->_blocks.size(); b++) this->_blocks[b].contiguous_();
1351 return boost::intrusive_ptr<UniTensor_base>(
this);
1354 boost::intrusive_ptr<UniTensor_base> contiguous();
1356 void print_diagram(
const bool &bond_info =
false);
1357 void print_blocks(
const bool &full_info =
true)
const;
1358 void print_block(
const cytnx_int64 &idx,
const bool &full_info =
true)
const;
1360 boost::intrusive_ptr<UniTensor_base> contract(
const boost::intrusive_ptr<UniTensor_base> &rhs,
1361 const bool &mv_elem_self =
false,
1362 const bool &mv_elem_rhs =
false);
1364 boost::intrusive_ptr<UniTensor_base> relabels(
const std::vector<std::string> &new_labels);
1366 boost::intrusive_ptr<UniTensor_base> relabels(
const std::vector<std::string> &old_labels,
1367 const std::vector<std::string> &new_labels);
1369 boost::intrusive_ptr<UniTensor_base> relabel(
const std::string &old_label,
1370 const std::string &new_label);
1371 boost::intrusive_ptr<UniTensor_base> relabel(
const cytnx_int64 &inx,
1372 const std::string &new_label);
1374 std::vector<Symmetry> syms()
const;
1376 void reshape_(
const std::vector<cytnx_int64> &new_shape,
const cytnx_uint64 &rowrank = 0) {
1377 cytnx_error_msg(
true,
"[ERROR] cannot reshape a UniTensor with symmetry.%s",
"\n");
1379 boost::intrusive_ptr<UniTensor_base> reshape(
const std::vector<cytnx_int64> &new_shape,
1381 cytnx_error_msg(
true,
"[ERROR] cannot reshape a UniTensor with symmetry.%s",
"\n");
1385 boost::intrusive_ptr<UniTensor_base> astype(
const unsigned int &dtype)
const {
1386 BlockUniTensor *
tmp = this->clone_meta(
true,
true);
1387 tmp->_blocks.resize(this->_blocks.size());
1388 for (
cytnx_int64 blk = 0; blk < this->_blocks.size(); blk++) {
1389 tmp->_blocks[blk] = this->_blocks[blk].astype(dtype);
1391 boost::intrusive_ptr<UniTensor_base> out(tmp);
1396 boost::intrusive_ptr<UniTensor_base> get(
const std::vector<Accessor> &accessors) {
1399 "[ERROR][BlockUniTensor][get] cannot use get on a UniTensor with "
1400 "Symmetry.\n suggestion: try get_block/get_block_/get_blocks/get_blocks_ first.%s",
1406 void set(
const std::vector<Accessor> &accessors,
const Tensor &rhs) {
1409 "[ERROR][BlockUniTensor][get] cannot use get on a UniTensor with "
1410 "Symmetry.\n suggestion: try get_block/get_block_/get_blocks/get_blocks_ first.%s",
1416 "[ERROR][DenseUniTensor][put_block] The input tensor dtype does not match.%s",
1419 "[ERROR][DenseUniTensor][put_block] The input tensor device does not "
1424 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1427 "[ERROR][BlockUniTensor] the shape of input tensor does not match the shape "
1428 "of block @ idx=%d\n",
1431 this->_blocks[idx] = in.
clone();
1435 "[ERROR][DenseUniTensor][put_block] The input tensor dtype does not match.%s",
1438 "[ERROR][DenseUniTensor][put_block] The input tensor device does not "
1443 cytnx_error_msg(idx >= this->_blocks.size(),
"[ERROR][BlockUniTensor] index out of range%s",
1446 "[ERROR][BlockUniTensor] the shape of input tensor does not match the shape "
1447 "of block @ idx=%d\n",
1450 this->_blocks[idx] = in;
1452 void put_block(
const Tensor &in,
const std::vector<cytnx_int64> &indices,
const bool &check) {
1454 "[ERROR][DenseUniTensor][put_block] The input tensor dtype does not match.%s",
1457 "[ERROR][DenseUniTensor][put_block] The input tensor device does not "
1463 "[ERROR][put_block][BlockUniTensor] len(indices) must be the same as the "
1464 "Tensor rank (number of legs).%s",
1467 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1471 for (
cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1472 if (inds == this->_inner_to_outer_idx[i]) {
1481 "[ERROR][put_block][BlockUniTensor] no avaliable block exists, "
1482 "check=true, so error throws. \n If you want without error when block "
1483 "is not avaliable, set check=false.%s",
1488 in.
shape() != this->_blocks[b].shape(),
1489 "[ERROR][BlockUniTensor] the shape of input tensor does not match the shape "
1490 "of block @ idx=%d\n",
1493 this->_blocks[b] = in.
clone();
1496 void put_block_(
Tensor &in,
const std::vector<cytnx_int64> &indices,
const bool &check) {
1498 "[ERROR][DenseUniTensor][put_block] The input tensor dtype does not match.%s",
1501 "[ERROR][DenseUniTensor][put_block] The input tensor device does not "
1507 "[ERROR][put_block][BlockUniTensor] len(indices) must be the same as the "
1508 "Tensor rank (number of legs).%s",
1511 std::vector<cytnx_uint64> inds(indices.begin(), indices.end());
1515 for (
cytnx_uint64 i = 0; i < this->_inner_to_outer_idx.size(); i++) {
1516 if (inds == this->_inner_to_outer_idx[i]) {
1525 "[ERROR][put_block][BlockUniTensor] no avaliable block exists, "
1526 "check=true, so error throws. \n If you want without error when block "
1527 "is not avaliable, set check=false.%s",
1532 in.
shape() != this->_blocks[b].shape(),
1533 "[ERROR][BlockUniTensor] the shape of input tensor does not match the shape "
1534 "of block @ idx=%d\n",
1536 this->_blocks[b] = in;
1544 boost::intrusive_ptr<UniTensor_base>
Conj() {
1545 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1551 for (
int i = 0; i < this->_blocks.size(); i++) {
1552 this->_blocks[i].Conj_();
1557 boost::intrusive_ptr<UniTensor_base> Transpose() {
1558 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1564 boost::intrusive_ptr<UniTensor_base> normalize() {
1565 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1570 boost::intrusive_ptr<UniTensor_base> Dagger() {
1571 boost::intrusive_ptr<UniTensor_base> out = this->
Conj();
1580 void Trace_(
const std::string &a,
const std::string &b);
1583 boost::intrusive_ptr<UniTensor_base>
Trace(
const std::string &a,
const std::string &b) {
1584 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1586 if (out->rank() == 0) {
1587 DenseUniTensor *
tmp =
new DenseUniTensor();
1588 tmp->_block = ((BlockUniTensor *)out.get())->_blocks[0];
1589 out = boost::intrusive_ptr<UniTensor_base>(tmp);
1594 boost::intrusive_ptr<UniTensor_base> out = this->clone();
1596 if (out->rank() == 0) {
1597 DenseUniTensor *
tmp =
new DenseUniTensor();
1598 tmp->_block = ((BlockUniTensor *)out.get())->_blocks[0];
1599 out = boost::intrusive_ptr<UniTensor_base>(tmp);
1606 bool elem_exists(
const std::vector<cytnx_uint64> &locator)
const;
1608 const Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator)
const;
1609 const cytnx_complex128 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1611 const cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1613 const cytnx_double &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1615 const cytnx_float &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1617 const cytnx_uint64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1619 const cytnx_int64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1621 const cytnx_uint32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1623 const cytnx_int32 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1625 const cytnx_uint16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1627 const cytnx_int16 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1630 Scalar::Sproxy at_for_sparse(
const std::vector<cytnx_uint64> &locator);
1633 cytnx_complex64 &at_for_sparse(
const std::vector<cytnx_uint64> &locator,
1644 void _save_dispatch(std::fstream &f)
const;
1645 void _load_dispatch(std::fstream &f);
1648 void truncate_(
const std::string &bond_idx,
const cytnx_uint64 &q_index);
1651 void Add_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
1652 void Add_(
const Scalar &rhs) {
1655 "[ERROR] cannot perform elementwise arithmetic '+' btwn Scalar and BlockUniTensor.\n %s "
1657 "This operation will destroy block structure. [Suggest] using get/set_block(s) to do "
1658 "operation on the block(s).");
1661 void Mul_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
1662 void Mul_(
const Scalar &rhs);
1664 void Sub_(
const boost::intrusive_ptr<UniTensor_base> &rhs);
1665 void Sub_(
const Scalar &rhs) {
1668 "[ERROR] cannot perform elementwise arithmetic '+' btwn Scalar and BlockUniTensor.\n %s "
1670 "This operation will destroy block structure. [Suggest] using get/set_block(s) to do "
1671 "operation on the block(s).");
1673 void lSub_(
const Scalar &lhs) {
1676 "[ERROR] cannot perform elementwise arithmetic '+' btwn Scalar and BlockUniTensor.\n %s "
1678 "This operation will destroy block structure. [Suggest] using get/set_block(s) to do "
1679 "operation on the block(s).");
1682 void Div_(
const boost::intrusive_ptr<UniTensor_base> &rhs) {
1685 "[ERROR] cannot perform elementwise arithmetic '+' btwn Scalar and BlockUniTensor.\n %s "
1687 "This operation will destroy block structure. [Suggest] using get/set_block(s) to do "
1688 "operation on the block(s).");
1690 void Div_(
const Scalar &rhs);
1691 void lDiv_(
const Scalar &lhs) {
1694 "[ERROR] cannot perform elementwise arithmetic '+' btwn Scalar and BlockUniTensor.\n %s "
1696 "This operation will destroy block structure. [Suggest] using get/set_block(s) to do "
1697 "operation on the block(s).");
1699 void from_(
const boost::intrusive_ptr<UniTensor_base> &rhs,
const bool &force);
1701 void group_basis_();
1703 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force =
false);
1704 void combineBonds(
const std::vector<cytnx_int64> &indicators,
const bool &force,
1705 const bool &by_label);
1706 void combineBonds(
const std::vector<std::string> &indicators,
const bool &force =
false);
1708 const std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx)
const {
1710 bidx >= this->Nblocks(),
1711 "[ERROR][BlockUniTensor] bidx out of bound! only %d blocks in current UTen.\n",
1713 return this->_inner_to_outer_idx[bidx];
1715 std::vector<cytnx_uint64> &get_qindices(
const cytnx_uint64 &bidx) {
1717 bidx >= this->Nblocks(),
1718 "[ERROR][BlockUniTensor] bidx out of bound! only %d blocks in current UTen.\n",
1720 return this->_inner_to_outer_idx[bidx];
1730 class UniTensor_options {
1737 UniTensor_options() {
1738 this->_is_diag =
false;
1739 this->_dtype =
Type.Double;
1740 this->_device =
Device.cpu;
1741 this->_rowrank = -1;
1744 UniTensor_options(
const UniTensor_options &rhs) {
1745 this->_is_diag = rhs._is_diag;
1746 this->_dtype = rhs._dtype;
1747 this->_device = rhs._device;
1748 this->_rowrank = rhs._rowrank;
1751 UniTensor_options &operator=(
const UniTensor_options &rhs) {
1752 this->_is_diag = rhs._is_diag;
1753 this->_dtype = rhs._dtype;
1754 this->_device = rhs._device;
1755 this->_rowrank = rhs._rowrank;
1759 UniTensor_options &is_diag(
const bool &in) {
1760 this->_is_diag = in;
1763 UniTensor_options &dtype(
const int &in) {
1767 UniTensor_options &device(
const int &in) {
1771 UniTensor_options &rowrank(
const int &in) {
1772 this->_rowrank = in;
1782 boost::intrusive_ptr<UniTensor_base>
_impl;
1786 this->_impl =
rhs._impl;
1820 const std::vector<std::string> &
in_labels = {},
const std::string &
name =
"")
1842 const std::vector<std::string> &
in_labels = {},
const std::string &
name =
"") {
1872 const std::string &
name =
"")
1877 "[DEBUG] message: entry for UniTensor(const std::vector<Bond> &bonds, const "
1878 "std::vector<std::string> &in_labels={}, const cytnx_int64 &rowrank=-1, const unsigned "
1880 "&dtype=Type.Double, const int &device = Device.cpu, const bool &is_diag=false)%s",
1923 const std::string &
name =
"") {
1938 "[ERROR] All the Bond when init a UniTensor with symmetric must be in "
1939 "the same format!%s",
1944 is_sym,
"[ERROR] cannot have bonds with mixing of symmetry and non-symmetry.%s",
"\n");
1950 cytnx_warning_msg(
true,
"[DEBUG] message: entry dispatch: UniTensor: symmetric%s",
"\n");
1958 "[ERROR] internal error! [legacy Sparse entry] the Bond is symmetry but "
1959 "the version is not properly determined!%s",
1963 "[ERROR] internal error! the Bond is symmetry but the version is not "
1964 "properly determined!%s",
2077 std::vector<std::string>
vs(
new_lbls.size());
2079 [](
char *x) -> std::string { return std::string(x); });
2081 this->_impl->set_labels(
vs);
2105 "[ERROR] cannot use item on UniTensor with Symmetry.\n suggestion: use "
2106 "get_block()/get_blocks() first.%s",
2110 return tmp->_block.item<
T>();
2115 "[ERROR] cannot use item on UniTensor with Symmetry.\n suggestion: use "
2116 "get_block()/get_blocks() first.%s",
2120 return tmp->_block.item();
2145 unsigned int dtype()
const {
return this->_impl->dtype(); }
2160 int device()
const {
return this->_impl->device(); }
2166 std::string
name()
const {
return this->_impl->name(); }
2173 std::string
dtype_str()
const {
return this->_impl->dtype_str(); }
2180 std::string
device_str()
const {
return this->_impl->device_str(); }
2200 bool is_diag()
const {
return this->_impl->is_diag(); }
2207 bool is_tag()
const {
return this->_impl->is_tag(); }
2214 std::vector<Symmetry>
syms()
const {
return this->_impl->syms(); }
2228 const std::vector<std::string> &
labels()
const {
return this->_impl->labels(); }
2241 const std::vector<Bond> &
bonds()
const {
return this->_impl->bonds(); }
2246 std::vector<Bond> &
bonds() {
return this->_impl->bonds(); }
2261 std::vector<cytnx_uint64>
shape()
const {
return this->_impl->shape(); }
2339 std::vector<std::string>
vs(
new_lbls.size());
2341 [](
char *x) -> std::string { return std::string(x); });
2352 std::vector<std::string>
vs(
new_lbls.size());
2354 [](
char *x) -> std::string { return std::string(x); });
2356 this->_impl->relabels_(
vs);
2368 const std::vector<std::string> &
new_labels)
const {
2390 const std::vector<std::string> &
new_labels) {
2400 const std::initializer_list<char *> &
new_labels)
const {
2402 std::vector<std::string>
vs(
new_lbls.size());
2404 [](
char *x) -> std::string { return std::string(x); });
2409 [](
char *x) -> std::string { return std::string(x); });
2419 const std::initializer_list<char *> &
new_labels) {
2421 std::vector<std::string>
vs(
new_lbls.size());
2423 [](
char *x) -> std::string { return std::string(x); });
2428 [](
char *x) -> std::string { return std::string(x); });
2507 if (this->
dtype() == dtype) {
2549 std::vector<std::string>
vs(
mprs.size());
2551 [](
char *x) -> std::string { return std::string(x); });
2650 if (this->
uten_type() == UTenType.Block) {
2653 return this->_impl->at_for_sparse(
locator,
aux);
2655 }
else if (this->
uten_type() == UTenType.Sparse) {
2656 if (this->_impl->elem_exists(
locator)) {
2658 return this->_impl->at_for_sparse(
locator,
aux);
2660 cytnx_error_msg(
true,
"[ERROR][SparseUniTensor] invalid location. break qnum block.%s",
2676 if (this->
uten_type() == UTenType.Block) {
2679 return this->_impl->at_for_sparse(
locator,
aux);
2681 }
else if (this->
uten_type() == UTenType.Sparse) {
2682 if (this->_impl->elem_exists(
locator)) {
2684 return this->_impl->at_for_sparse(
locator,
aux);
2686 cytnx_error_msg(
true,
"[ERROR][SparseUniTensor] invalid location. break qnum block.%s",
2696 const std::vector<cytnx_uint64> &
locator)
const {
2699 "[ERROR][at] length of list should be the same for label and locator.%s",
2703 "[ERROR][at] length of lists must be the same as UniTensor.rank (# of legs)%s",
"\n");
2706 for (
int i = 0;
i <
labels.size();
i++) {
2707 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
labels[
i]);
2709 "[ERROR] label:%s does not exist in current UniTensor.\n",
2711 new_loc = std::distance(this->_impl->_labels.begin(),
res);
2714 return this->
at<T>(new_locator);
2720 "[ERROR][at] length of list should be the same for label and locator.%s",
2724 "[ERROR][at] length of lists must be the same as UniTensor.rank (# of legs)%s",
"\n");
2727 for (
int i = 0;
i <
labels.size();
i++) {
2728 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
labels[
i]);
2730 "[ERROR] label:%s does not exist in current UniTensor.\n",
2732 new_loc = std::distance(this->_impl->_labels.begin(),
res);
2735 return this->
at<T>(new_locator);
2742 const Scalar::Sproxy
at(
const std::vector<cytnx_uint64> &
locator)
const {
2743 if (this->
uten_type() == UTenType.Block) {
2744 return this->_impl->at_for_sparse(
locator);
2745 }
else if (this->
uten_type() == UTenType.Sparse) {
2746 if (this->_impl->elem_exists(
locator)) {
2747 return this->_impl->at_for_sparse(
locator);
2749 cytnx_error_msg(
true,
"[ERROR][SparseUniTensor] invalid location. break qnum block.%s",
2761 Scalar::Sproxy
at(
const std::vector<cytnx_uint64> &
locator) {
2762 if (this->
uten_type() == UTenType.Block) {
2763 return this->_impl->at_for_sparse(
locator);
2764 }
else if (this->
uten_type() == UTenType.Sparse) {
2765 if (this->_impl->elem_exists(
locator)) {
2766 return this->_impl->at_for_sparse(
locator);
2768 cytnx_error_msg(
true,
"[ERROR][SparseUniTensor] invalid location. break qnum block.%s",
2776 Scalar::Sproxy
at(
const std::vector<std::string> &
labels,
2777 const std::vector<cytnx_uint64> &
locator) {
2780 "[ERROR][at] length of list should be the same for label and locator.%s",
2784 "[ERROR][at] length of lists must be the same as UniTensor.rank (# of legs)%s",
"\n");
2787 for (
int i = 0;
i <
labels.size();
i++) {
2788 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
labels[
i]);
2790 "[ERROR] label:%s does not exist in current UniTensor.\n",
2792 new_loc = std::distance(this->_impl->_labels.begin(),
res);
2795 return this->
at(new_locator);
2798 const Scalar::Sproxy
at(
const std::vector<std::string> &
labels,
2799 const std::vector<cytnx_uint64> &
locator)
const {
2802 "[ERROR][at] length of list should be the same for label and locator.%s",
2806 "[ERROR][at] length of lists must be the same as UniTensor.rank (# of legs)%s",
"\n");
2809 for (
int i = 0;
i <
labels.size();
i++) {
2810 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
labels[
i]);
2812 "[ERROR] label:%s does not exist in current UniTensor.\n",
2814 new_loc = std::distance(this->_impl->_labels.begin(),
res);
2817 return this->
at(new_locator);
2838 return this->_impl->get_block(
qidx,
force);
2842 const bool &
force =
false)
const {
2845 "[ERROR][get_block] length of lists must be the same for both lables and qnidices%s",
"\n");
2847 "[ERROR][get_block] length of lists must be the rank (# of legs)%s",
"\n");
2849 std::vector<cytnx_int64>
loc_id(this->
rank());
2854 for (
int i = 0;
i <
labels.size();
i++) {
2855 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
labels[
i]);
2857 "[ERROR][get_block] label:%s does not exists in current Tensor.\n",
2859 new_loc = std::distance(this->_impl->_labels.begin(),
res);
2873 const bool &
force =
false)
const {
2874 std::vector<cytnx_int64> tmp =
qnum;
2888 const bool &
force =
false)
const {
2899 return this->_impl->get_block_(
idx);
2917 return this->_impl->get_block_(
qidx,
force);
2937 const bool &
force =
false) {
2940 "[ERROR][get_block] length of lists must be the same for both lables and qnidices%s",
"\n");
2942 "[ERROR][get_block] length of lists must be the rank (# of legs)%s",
"\n");
2944 std::vector<cytnx_int64>
loc_id(this->
rank());
2949 for (
int i = 0;
i <
labels.size();
i++) {
2950 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
labels[
i]);
2952 "[ERROR][get_block] label:%s does not exists in current Tensor.\n",
2954 new_loc = std::distance(this->_impl->_labels.begin(),
res);
2959 if (
out.dtype() !=
Type.Void) {
2969 std::vector<cytnx_int64> tmp =
qidx;
2982 const bool &
force =
false) {
2993 const bool &
force =
false)
const {
2994 return this->_impl->get_block_(
qidx,
force);
3001 const bool &
force =
false)
const {
3002 std::vector<cytnx_int64> tmp =
qidx;
3003 return this->_impl->get_block_(tmp,
force);
3010 const bool &
force =
false)
const {
3025 std::vector<Tensor>
get_blocks()
const {
return this->_impl->get_blocks(); }
3034 return this->_impl->get_blocks_(
silent);
3042 return this->_impl->get_blocks_(
silent);
3051 this->_impl->put_block(
in,
idx);
3070 const std::vector<cytnx_int64> &
qidx,
const bool &
force =
false) {
3073 "[ERROR][put_block] length of lists must be the same for both lables and qnidices%s",
"\n");
3075 "[ERROR][put_block] length of lists must be the rank (# of legs)%s",
"\n");
3077 std::vector<cytnx_int64>
loc_id(this->
rank());
3083 for (
int i = 0;
i <
lbls.size();
i++) {
3084 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
lbls[
i]);
3086 "[ERROR][put_block] label:%s does not exists in current Tensor.\n",
3088 new_loc = std::distance(this->_impl->_labels.begin(),
res);
3118 const std::vector<cytnx_int64> &
qidx,
const bool &
force =
false) {
3121 "[ERROR][put_block_] length of lists must be the same for both lables and qnidices%s",
3124 "[ERROR][put_block_] length of lists must be the rank (# of legs)%s",
"\n");
3126 std::vector<cytnx_int64>
loc_id(this->
rank());
3132 for (
int i = 0;
i <
lbls.size();
i++) {
3133 auto res = std::find(this->_impl->_labels.begin(),
this->_impl->_labels.end(),
lbls[
i]);
3135 "[ERROR][put_block_] label:%s does not exists in current Tensor.\n",
3137 new_loc = std::distance(this->_impl->_labels.begin(),
res);
3157 "[ERROR] cannot set elements from UniTensor with symmetry. Use at() instead.%s",
"\n");
3160 "[ERROR] cannot set UniTensor. incoming UniTensor is_diag=True.%s",
"\n");
3278 return this->_impl->getTotalQnums(
physical);
3285 return this->_impl->get_blocks_qnums();
3295 if (this->_impl->uten_type() !=
rhs._impl->uten_type())
return false;
3297 return this->_impl->same_data(
rhs._impl);
3723 out._impl = this->_impl->
Conj();
3735 this->_impl->
Conj_();
3830 if (this->
uten_type() == UTenType.Block) {
3832 if (this->
rank() == 0) {
3835 this->_impl = boost::intrusive_ptr<UniTensor_base>(tmp);
3852 if (this->
uten_type() == UTenType.Block) {
3854 if (this->
rank() == 0) {
3857 this->_impl = boost::intrusive_ptr<UniTensor_base>(tmp);
3926 return this->_impl->elem_exists(
locator);
3936 return this->
at<T>(locator);
3947 this->
at(locator) =
rc;
4050 return this->_impl->get_qindices(
bidx);
4060 return this->_impl->get_qindices(
bidx);
4073 void _Load(std::fstream &f);
4074 void _Save(std::fstream &f)
const;
4078 this->_impl->from_(
rhs._impl,
force);
4096 const std::vector<std::string> &
in_labels = {},
4098 const std::string &
name =
"") {
4116 const std::vector<std::string> &
in_labels = {},
4118 const std::string &
name =
"") {
4135 const std::string &
name =
"") {
4152 const std::vector<std::string> &
in_labels = {},
4154 const std::string &
name =
"") {
4172 const std::vector<std::string> &
in_labels = {},
4173 const std::string &
name =
"") {
4195 const std::vector<std::string> &
in_labels = {},
4197 const std::string &
name =
"") {
4222 const std::vector<std::string> &
in_labels = {},
4223 const unsigned int &
dtype =
Type.Double,
4249 const std::vector<std::string> &
in_labels = {},
4250 const unsigned int &
seed = std::random_device()(),
4252 const std::string &
name =
"");
4273 const double &
std,
const std::vector<std::string> &
in_labels = {},
4274 const unsigned int &
seed = std::random_device()(),
4276 const std::string &
name =
"");
4297 const std::vector<std::string> &
in_labels = {},
4298 const unsigned int &
seed = std::random_device()(),
4299 const unsigned int &
dtype =
Type.Double,
4340 const double &
high,
const std::vector<std::string> &
in_labels = {},
4341 const unsigned int &
seed = std::random_device()(),
4342 const unsigned int &
dtype =
Type.Double,
4359 const unsigned int &
seed = std::random_device()());
4374 const unsigned int &
seed = std::random_device()());
4379 std::ostream &operator<<(std::ostream &os,
const UniTensor &in);
4395 const bool &cacheR =
false);
4410 const bool &optimal);
4413 void _resolve_CT(std::vector<UniTensor> &TNlist);
4414 template <
class... T>
4415 void _resolve_CT(std::vector<UniTensor> &TNlist,
const UniTensor &in,
const T &...args) {
4416 TNlist.push_back(in);
4417 _resolve_CT(TNlist, args...);
4432 template <
class... T>
4434 const bool &optimal) {
4435 std::vector<UniTensor> TNlist;
4436 _resolve_CT(TNlist, in, args...);
4437 return Contracts(TNlist, order, optimal);
the object contains auxiliary properties for each Tensor rank (bond)
Definition Bond.hpp:176
Bond clone() const
return a copy of the instance Bond
Definition Bond.hpp:488
an tensor (multi-dimensional array)
Definition Tensor.hpp:41
void to_(const int &device)
move the current Tensor to the device.
Definition Tensor.hpp:615
Tensor contiguous_()
Make the Tensor contiguous by coalescing the memory (storage), inplacely.
Definition Tensor.hpp:702
unsigned int dtype() const
the dtype-id of the Tensor
Definition Tensor.hpp:514
Tensor contiguous() const
Make the Tensor contiguous by coalescing the memory (storage).
Definition Tensor.hpp:682
T & at(const std::vector< cytnx_uint64 > &locator)
Get an element at specific location.
Definition Tensor.hpp:858
Tensor clone() const
return a clone of the current Tensor.
Definition Tensor.hpp:566
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:993
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:823
const bool & is_contiguous() const
return whether the Tensor is contiguous or not.
Definition Tensor.hpp:621
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:521
Tensor get(const std::vector< cytnx::Accessor > &accessors) const
get elements using Accessor (C++ API) / slices (python API)
Definition Tensor.hpp:961
const std::vector< cytnx_uint64 > & shape() const
the shape of the Tensor
Definition Tensor.hpp:541
An Enhanced tensor specifically designed for physical Tensor network simulation.
Definition UniTensor.hpp:1779
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:3061
UniTensor to(const int &device) const
move the current UniTensor to the assigned device.
Definition UniTensor.hpp:2287
UniTensor & operator*=(const UniTensor &rhs)
The multiplication assignment operator of the UniTensor.
Definition UniTensor.hpp:3649
Tensor & get_block_(const std::initializer_list< cytnx_int64 > &qidx, const bool &force=false)
Definition UniTensor.hpp:2968
std::vector< Tensor > & get_blocks_(const bool &silent=false)
Definition UniTensor.hpp:3041
Tensor & get_block_(const std::vector< cytnx_uint64 > &qidx, const bool &force=false)
Definition UniTensor.hpp:2976
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:2624
UniTensor & operator/=(const UniTensor &rhs)
The division assignment operator of the UniTensor.
Definition UniTensor.hpp:3629
T & item()
Definition UniTensor.hpp:2103
bool is_contiguous() const
To tell whether the UniTensor is contiguous.
Definition UniTensor.hpp:2194
T get_elem(const std::vector< cytnx_uint64 > &locator) const
Definition UniTensor.hpp:3935
UniTensor Div(const UniTensor &rhs) const
The division function of the UniTensor.
Tensor get_block(const std::vector< std::string > &labels, const std::vector< cytnx_int64 > &qidx, const bool &force=false) const
Definition UniTensor.hpp:2841
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:4059
UniTensor & set_label(const cytnx_int64 &idx, const char *new_label)
Definition UniTensor.hpp:2006
UniTensor & operator+=(const UniTensor &rhs)
The addition assignment operator of the UniTensor.
Definition UniTensor.hpp:3589
void to_dense_()
Convert the UniTensor to non-diagonal form, inplacely.
Definition UniTensor.hpp:3208
UniTensor reshape(const std::vector< cytnx_int64 > &new_shape, const cytnx_uint64 &rowrank=0)
Reshape the UniTensor.
Definition UniTensor.hpp:3171
std::vector< Tensor > get_blocks() const
Get all the blocks of the UniTensor.
Definition UniTensor.hpp:3025
UniTensor permute(const std::initializer_list< char * > &mapper, const cytnx_int64 &rowrank=-1) const
Definition UniTensor.hpp:2546
void normal_(const double &mean, const double &std, const unsigned int &seed=std::random_device()())
Generate a one-bond UniTensor with all elements are random numbers sampled from a normal (Gaussian) d...
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:2207
std::string uten_type_str() const
Return the UniTensor type (cytnx::UTenType) of the UniTensor in 'string' form.
Definition UniTensor.hpp:2187
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:1869
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:2837
void permute_(const std::vector< cytnx_int64 > &mapper, const cytnx_int64 &rowrank=-1)
permute the lags of the UniTensor, inplacely.
Definition UniTensor.hpp:2564
UniTensor & tag()
Set the UniTensor as a tagged UniTensor.
Definition UniTensor.hpp:3894
const Bond & bond_(const cytnx_uint64 &idx) const
Definition UniTensor.hpp:2248
void combineBonds(const std::vector< cytnx_int64 > &indicators, const bool &force=false)
Definition UniTensor.hpp:3239
UniTensor & relabel_(const std::string &old_label, const std::string &new_label)
relabel the lags in the UniTensor by a given label.
Definition UniTensor.hpp:2474
const T & at(const std::vector< std::string > &labels, const std::vector< cytnx_uint64 > &locator) const
Definition UniTensor.hpp:2695
UniTensor & relabels_(const std::initializer_list< char * > &new_labels)
Definition UniTensor.hpp:2350
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:1818
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:2916
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:1841
static UniTensor zeros(const std::vector< cytnx_uint64 > &shape, const std::vector< std::string > &in_labels={}, const unsigned int &dtype=Type.Double, const int &device=Device.cpu, const std::string &name="")
Generate a UniTensor with all elements set to zero.
Definition UniTensor.hpp:4115
UniTensor get(const std::vector< Accessor > &accessors) const
Definition UniTensor.hpp:3146
T & at(const std::vector< std::string > &labels, const std::vector< cytnx_uint64 > &locator)
Definition UniTensor.hpp:2717
UniTensor Conj() const
Apply complex conjugate on each entry of the UniTensor.
Definition UniTensor.hpp:3721
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:2021
UniTensor set_rowrank(const cytnx_uint64 &new_rowrank) const
Definition UniTensor.hpp:2096
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:3679
cytnx_uint64 rowrank() const
Return the row rank of the UniTensor.
Definition UniTensor.hpp:2138
UniTensor & relabels_(const std::vector< std::string > &old_labels, const std::vector< std::string > &new_labels)
relables part or all of the labels in UniTensor by given new labels
Definition UniTensor.hpp:2389
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:3572
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:2075
UniTensor & Sub_(const Scalar &rhs)
The subtraction function for a given scalar.
Definition UniTensor.hpp:3424
UniTensor & relabel_(const cytnx_int64 &inx, const std::string &new_label)
rebable the lags in the UniTensor by given index.
Definition UniTensor.hpp:2461
UniTensor & operator/=(const Scalar &rhs)
The division assignment operator for a given scalar.
Definition UniTensor.hpp:3694
const bool & is_braket_form() const
Check whether the UniTensor is in braket form.
Definition UniTensor.hpp:2222
void set_elem(const std::vector< cytnx_uint64 > &locator, const T2 &rc)
Definition UniTensor.hpp:3945
UniTensor Add(const Scalar &rhs) const
The addition function for a given scalar.
static UniTensor linspace(const cytnx_double &start, const cytnx_double &end, const cytnx_uint64 &Nelem, const bool &endpoint=true, const std::vector< std::string > &in_labels={}, const unsigned int &dtype=Type.Double, const 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:4220
std::vector< cytnx_uint64 > shape() const
Get the shape of the UniTensor.
Definition UniTensor.hpp:2261
UniTensor to_dense()
Convert the UniTensor to non-diagonal form.
Definition UniTensor.hpp:3198
Scalar::Sproxy at(const std::vector< std::string > &labels, const std::vector< cytnx_uint64 > &locator)
Definition UniTensor.hpp:2776
const Tensor & get_block_(const std::vector< cytnx_int64 > &qidx, const bool &force=false) const
Definition UniTensor.hpp:2992
void reshape_(const std::vector< cytnx_int64 > &new_shape, const cytnx_uint64 &rowrank=0)
Reshape the UniTensor, inplacely.
Definition UniTensor.hpp:3183
UniTensor & set_labels(const std::vector< std::string > &new_labels)
Set new labels for all the bonds.
Definition UniTensor.hpp:2066
void permute_(const std::vector< std::string > &mapper, const cytnx_int64 &rowrank=-1)
permute the lags of the UniTensor, inplacely.
Definition UniTensor.hpp:2574
std::string name() const
Return the name of the UniTensor.
Definition UniTensor.hpp:2166
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=std::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 Scalar::Sproxy at(const std::vector< std::string > &labels, const std::vector< cytnx_uint64 > &locator) const
Definition UniTensor.hpp:2798
UniTensor & Add_(const Scalar &rhs)
The addition function for a given scalar.
Definition UniTensor.hpp:3394
UniTensor & Trace_(const std::string &a, const std::string &b)
Take the partial trance to the UniTensor, inplacely.
Definition UniTensor.hpp:3828
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:4007
Scalar::Sproxy at(const std::vector< cytnx_uint64 > &locator)
Get an element at specific location.
Definition UniTensor.hpp:2761
UniTensor Trace(const std::string &a, const std::string &b) const
Take the partial trance to the UniTensor.
Definition UniTensor.hpp:3798
bool same_data(const UniTensor &rhs) const
Check whether the Blocks address are the same.
Definition UniTensor.hpp:3293
UniTensor astype(const unsigned int &dtype) const
Return a new UniTensor that cast to different data type.
Definition UniTensor.hpp:2505
static UniTensor ones(const std::vector< cytnx_uint64 > &shape, const std::vector< std::string > &in_labels={}, const unsigned int &dtype=Type.Double, const int &device=Device.cpu, const std::string &name="")
Generate a UniTensor with all elements set to one.
Definition UniTensor.hpp:4151
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:4021
UniTensor Transpose() const
Take the transpose of the UniTensor.
Definition UniTensor.hpp:3749
static UniTensor arange(const cytnx_double &start, const cytnx_double &end, const cytnx_double &step=1, const std::vector< std::string > &in_labels={}, const unsigned int &dtype=Type.Double, const int &device=Device.cpu, const std::string &name="")
Generate a UniTensor with all elements are arange from start to end.
Definition UniTensor.hpp:4193
UniTensor & set_label(const char *old_label, const std::string &new_label)
Definition UniTensor.hpp:2029
static UniTensor Load(const char *fname)
load a UniTensor from file
UniTensor & Dagger_()
Take the conjugate transpose to the UniTensor, inplacely.
Definition UniTensor.hpp:3881
void print_diagram(const bool &bond_info=false)
Plot the diagram of the UniTensor.
Definition UniTensor.hpp:2611
std::vector< Bond > & bonds()
Definition UniTensor.hpp:2246
T & at(const std::vector< cytnx_uint64 > &locator)
Get an element at specific location.
Definition UniTensor.hpp:2648
UniTensor & set_rowrank_(const cytnx_uint64 &new_rowrank)
Set the row rank of the UniTensor.
Definition UniTensor.hpp:2091
void put_block(Tensor &in, const std::vector< std::string > &lbls, const std::vector< cytnx_int64 > &qidx, const bool &force=false)
Put the block into the UniTensor with given quantum indices, will copy the input tensor.
Definition UniTensor.hpp:3069
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:1998
static UniTensor normal(const cytnx_uint64 &Nelem, const double &mean, const double &std, const std::vector< std::string > &in_labels={}, const unsigned int &seed=std::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 contiguous() const
Make the UniTensor contiguous by coalescing the memory (storage).
Definition UniTensor.hpp:2595
Tensor get_block(const cytnx_uint64 &idx=0) const
Get the block of the UniTensor for a given index.
Definition UniTensor.hpp:2826
const T & at(const std::vector< cytnx_uint64 > &locator) const
Get an element at specific location.
Definition UniTensor.hpp:2674
UniTensor & operator*=(const Scalar &rhs)
The multiplication assignment operator for a given scalar.
Definition UniTensor.hpp:3709
UniTensor relabels(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:2367
void contiguous_()
Make the UniTensor contiguous by coalescing the memory (storage), inplacely.
Definition UniTensor.hpp:2605
UniTensor normalize() const
normalize the current UniTensor instance with 2-norm.
Definition UniTensor.hpp:3772
static UniTensor ones(const cytnx_uint64 &Nelem, const std::vector< std::string > &in_labels={}, const unsigned int &dtype=Type.Double, const int &device=Device.cpu, const std::string &name="")
Generate a one-bond UniTensor with all elements set to one.
Definition UniTensor.hpp:4133
std::vector< Symmetry > syms() const
Return the symmetry type of the UniTensor.
Definition UniTensor.hpp:2214
UniTensor & Mul_(const UniTensor &rhs)
The multiplcation function of the UniTensor.
Definition UniTensor.hpp:3337
static UniTensor zeros(const cytnx_uint64 &Nelem, const std::vector< std::string > &in_labels={}, const unsigned int &dtype=Type.Double, const int &device=Device.cpu, const std::string &name="")
Generate a one-bond UniTensor with all elements set to zero.
Definition UniTensor.hpp:4095
void put_block_(Tensor &in, const cytnx_uint64 &idx=0)
Put the block into the UniTensor with given index, inplacely.
Definition UniTensor.hpp:3101
void combineBonds(const std::vector< std::string > &indicators, const bool &force=false)
Combine the sevral bonds of the UniTensor.
Definition UniTensor.hpp:3230
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:2493
UniTensor & normalize_()
normalize the UniTensor, inplacely.
Definition UniTensor.hpp:3784
bool is_diag() const
To tell whether the UniTensor is in diagonal form.
Definition UniTensor.hpp:2200
int device() const
Return the device of the UniTensor.
Definition UniTensor.hpp:2160
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:3009
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 block for the given quantum indices on given labels.
Definition UniTensor.hpp:2936
void put_block_(Tensor &in, const std::vector< std::string > &lbls, const std::vector< cytnx_int64 > &qidx, const bool &force=false)
Put the block into the UniTensor with given quantum indices, inplacely.
Definition UniTensor.hpp:3117
void set(const std::vector< Accessor > &accessors, const Tensor &rhs)
Definition UniTensor.hpp:3151
std::string dtype_str() const
Return the data type of the UniTensor in 'string' form.
Definition UniTensor.hpp:2173
UniTensor & operator+=(const Scalar &rhs)
The addition assignment operator for a given scalar.
Definition UniTensor.hpp:3664
UniTensor & Div_(const UniTensor &rhs)
The division function of the UniTensor.
Definition UniTensor.hpp:3379
UniTensor & Div_(const Scalar &rhs)
The division function for a given scalar.
Definition UniTensor.hpp:3439
Bond & bond_(const std::string &label)
Definition UniTensor.hpp:2252
cytnx_uint64 rank() const
Return the rank of the UniTensor.
Definition UniTensor.hpp:2132
Bond bond(const std::string &label) const
Definition UniTensor.hpp:2255
void group_basis_()
Group the same quantum number basis together.
Definition UniTensor.hpp:2634
void to_(const int &device)
move the current UniTensor to the assigned device (inplace).
Definition UniTensor.hpp:2276
UniTensor & Trace_(const cytnx_int64 &a=0, const cytnx_int64 &b=1)
Take the partial trance to the UniTensor, inplacely.
Definition UniTensor.hpp:3850
UniTensor & set_name(const std::string &in)
Set the name of the UniTensor.
Definition UniTensor.hpp:1983
UniTensor Trace(const cytnx_int64 &a=0, const cytnx_int64 &b=1) const
Take the partial trance to the UniTensor.
Definition UniTensor.hpp:3813
UniTensor & Add_(const UniTensor &rhs)
The addition function of the UniTensor.
Definition UniTensor.hpp:3316
UniTensor permute(const std::vector< cytnx_int64 > &mapper, const cytnx_int64 &rowrank=-1) const
permute the lags of the UniTensor
Definition UniTensor.hpp:2523
UniTensor & set_label(const std::string &old_label, const char *new_label)
Definition UniTensor.hpp:2037
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:2536
unsigned int dtype() const
Return the data type of the UniTensor.
Definition UniTensor.hpp:2145
UniTensor Pow(const double &p) const
Power function.
UniTensor & Mul_(const Scalar &rhs)
The multiplication function for a given scalar.
Definition UniTensor.hpp:3409
UniTensor & operator-=(const UniTensor &rhs)
The subtraction assignment operator of the UniTensor.
Definition UniTensor.hpp:3609
Tensor get_block_(const std::vector< std::string > &labels, const std::vector< cytnx_uint64 > &qidx, const bool &force=false)
Definition UniTensor.hpp:2981
void combineBonds(const std::vector< cytnx_int64 > &indicators, const bool &force, const bool &by_label)
Definition UniTensor.hpp:3215
UniTensor & set_label(const char *old_label, const char *new_label)
Definition UniTensor.hpp:2045
static UniTensor arange(const cytnx_int64 &Nelem, const std::vector< std::string > &in_labels={}, const std::string &name="")
Generate a one-bond UniTensor with all elements are arange from 0 to Nelem-1.
Definition UniTensor.hpp:4171
bool is_blockform() const
Check whether the UniTensor is in block form.
Definition UniTensor.hpp:2268
UniTensor group_basis() const
Definition UniTensor.hpp:2636
UniTensor Dagger() const
Take the conjugate transpose to the UniTensor.
Definition UniTensor.hpp:3869
void set(const std::vector< Accessor > &accessors, const UniTensor &rhs)
Definition UniTensor.hpp:3154
const std::vector< Tensor > & get_blocks_(const bool &silent=false) const
Get all the blocks of the UniTensor, inplacely.
Definition UniTensor.hpp:3033
UniTensor & relabels_(const std::initializer_list< char * > &old_labels, const std::initializer_list< char * > &new_labels)
Definition UniTensor.hpp:2418
const Tensor & get_block_(const std::initializer_list< cytnx_int64 > &qidx, const bool &force=false) const
Definition UniTensor.hpp:3000
bool elem_exists(const std::vector< cytnx_uint64 > &locator) const
Geiven the locator, check if the element exists.
Definition UniTensor.hpp:3925
UniTensor & Sub_(const UniTensor &rhs)
The subtraction function of the UniTensor.
Definition UniTensor.hpp:3358
UniTensor relabels(const std::initializer_list< char * > &new_labels) const
Definition UniTensor.hpp:2337
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:3261
void put_block(const Tensor &in, const cytnx_uint64 &idx=0)
Put the block into the UniTensor with given index.
Definition UniTensor.hpp:3050
const Tensor & get_block_(const cytnx_uint64 &idx=0) const
Get the shared view of block for the given index.
Definition UniTensor.hpp:2898
static UniTensor Load(const std::string &fname)
load a UniTensor from file
const Bond & bond_(const std::string &label) const
Definition UniTensor.hpp:2251
Bond & bond_(const cytnx_uint64 &idx)
Definition UniTensor.hpp:2249
UniTensor relabel(const cytnx_int64 &inx, const std::string &new_label) const
rebable the lags in the UniTensor by given index.
Definition UniTensor.hpp:2448
UniTensor Mul(const UniTensor &rhs) const
The multiplication function of the UniTensor.
UniTensor relabels(const std::initializer_list< char * > &old_labels, const std::initializer_list< char * > &new_labels) const
Definition UniTensor.hpp:2399
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:4049
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:2228
cytnx_int64 get_index(std::string label) const
Get the index of an desired label string.
Definition UniTensor.hpp:2235
const std::vector< Bond > & bonds() const
Get the bonds of the UniTensor.
Definition UniTensor.hpp:2241
Tensor get_block(const std::vector< std::string > &labels, const std::vector< cytnx_uint64 > &qidx, const bool &force=false) const
Definition UniTensor.hpp:2887
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:4036
UniTensor & Transpose_()
Take the transpose of the UniTensor, inplacely.
Definition UniTensor.hpp:3761
UniTensor clone() const
Clone (deep copy) the UniTensor.
Definition UniTensor.hpp:2297
vec2d< cytnx_uint64 > & get_itoi()
Definition UniTensor.hpp:4070
void print_blocks(const bool &full_info=true) const
Print all of the blocks in the UniTensor.
Definition UniTensor.hpp:2617
Scalar::Sproxy item() const
Definition UniTensor.hpp:2113
const vec2d< cytnx_uint64 > & get_itoi() const
get the q-indices on each leg for all the blocks
Definition UniTensor.hpp:4069
std::string device_str() const
Return the device of the UniTensor in 'string' form.
Definition UniTensor.hpp:2180
UniTensor & convert_from(const UniTensor &rhs, const bool &force=false)
Definition UniTensor.hpp:4077
UniTensor relabels(const std::vector< std::string > &new_labels) const
relables all of the labels in UniTensor.
Definition UniTensor.hpp:2328
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:3109
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:1920
UniTensor & Conj_()
Apply complex conjugate on each entry of the UniTensor.
Definition UniTensor.hpp:3734
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=std::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...
cytnx_uint64 Nblocks() const
Return the number of blocks in the UniTensor.
Definition UniTensor.hpp:2126
Tensor get_block(const std::vector< cytnx_uint64 > &qnum, const bool &force=false) const
Definition UniTensor.hpp:2882
void Save(const char *fname) const
save a UniTensor to file
void uniform_(const double &low=0, const double &high=1, const unsigned int &seed=std::random_device()())
Generate a UniTensor with all elements are random numbers sampled from a uniform distribution,...
Bond bond(const cytnx_uint64 &idx) const
Definition UniTensor.hpp:2254
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:3994
Tensor get_block(const std::initializer_list< cytnx_int64 > &qnum, const bool &force=false) const
Definition UniTensor.hpp:2872
UniTensor & relabels_(const std::vector< std::string > &new_labels)
Set new labels for all the bonds.
Definition UniTensor.hpp:2312
const Scalar::Sproxy at(const std::vector< cytnx_uint64 > &locator) const
Get an element at specific location.
Definition UniTensor.hpp:2742
int uten_type() const
Return the UniTensor type (cytnx::UTenType) of the UniTensor.
Definition UniTensor.hpp:2153
static UniTensor uniform(const cytnx_uint64 &Nelem, const double &low, const double &high, const std::vector< std::string > &in_labels={}, const unsigned int &seed=std::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...
Tensor & get_block_(const cytnx_uint64 &idx=0)
Definition UniTensor.hpp:2906
#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 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)
void Conj_(cytnx::UniTensor &UT)
Inplace elementwise conjugate of the UniTensor.
Tensor Norm(const Tensor &Tl)
Calculate the norm of a tensor.
Helper function to print vector with ODT:
Definition Accessor.hpp:12
Device_class Device
data on which devices.
double cytnx_double
Definition Type.hpp:53
Tensor linspace(const cytnx_double &start, const cytnx_double &end, const cytnx_uint64 &Nelem, const bool &endpoint=true, const unsigned int &dtype=Type.Double, const int &device=Device.cpu)
UniTensorType_class UTenType
UniTensor type.
uint32_t cytnx_uint32
Definition Type.hpp:56
std::complex< double > cytnx_complex128
Definition Type.hpp:63
float cytnx_float
Definition Type.hpp:54
int16_t cytnx_int16
Definition Type.hpp:60
std::complex< float > cytnx_complex64
Definition Type.hpp:62
int32_t cytnx_int32
Definition Type.hpp:59
Tensor arange(const cytnx_int64 &Nelem)
create an rank-1 Tensor with incremental unsigned integer elements start with [0,Nelem)
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:57
Tensor ones(const cytnx_uint64 &Nelem, const unsigned int &dtype=Type.Double, const int &device=Device.cpu)
create an rank-1 Tensor with all the elements are initialized with one.
uint64_t cytnx_uint64
Definition Type.hpp:55
int64_t cytnx_int64
Definition Type.hpp:58
std::vector< std::vector< T > > vec2d
Definition Type.hpp:51
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.
Tensor zeros(const cytnx_uint64 &Nelem, const unsigned int &dtype=Type.Double, const int &device=Device.cpu)
create an rank-1 Tensor with all the elements are initialized with zero.