# ITensor C++ Questions

**URL:** https://itensor.discourse.group/c/itensor-c-questions/6.md

[Latest](https://itensor.discourse.group/latest.md) · [Categories](https://itensor.discourse.group/categories.md) · [Tags](https://itensor.discourse.group/tags.md)

---

## [About the ITensor C++ Questions category](https://itensor.discourse.group/t/about-the-itensor-c-questions-category/15)

<div class="topic-metadata">

**Author:** [@miles](https://itensor.discourse.group/u/miles)\
**Replies:** 0

</div>

Questions about the C++ version of ITensor and related codes. Please read the our guide to using the forum before posting, to make it easier for us to help you! For general questions about DMRG and other numerical meth…

---

## [Memory error (bad\_alloc) with MPS/density matrix](https://itensor.discourse.group/t/memory-error-bad-alloc-with-mps-density-matrix/2499)

<div class="topic-metadata">

**Author:** [@DavideBianchin4](https://itensor.discourse.group/u/DavideBianchin4)\
**Replies:** 0\
**Last updated:** [September 22, 2025, 3:23pm UTC](https://itensor.discourse.group/t/memory-error-bad-alloc-with-mps-density-matrix/2499 "2025-09-22T15:23:25Z")

</div>

Hello all, I’m working on a Guassian Boson Sampling setup from scratch, where I apply beam splitter 2-mode gates to an MPS state, and calculate the density matrix at the end to make local measurements. I am following th…

---

## [Swap-gate, SVD applied to MPS](https://itensor.discourse.group/t/swap-gate-svd-applied-to-mps/2489)

<div class="topic-metadata">

**Author:** [@DavideBianchin4](https://itensor.discourse.group/u/DavideBianchin4)\
**Replies:** 0\
**Last updated:** [September 8, 2025, 12:28pm UTC](https://itensor.discourse.group/t/swap-gate-svd-applied-to-mps/2489 "2025-09-08T12:28:57Z")

</div>

Hello, I am building an MPS alternating physical sites with “virtual” ancillary sites (vacuum modes); the goal is to simulate Beam Splitter interactions between physical modes in a Gaussian Boson Sampling scheme, where …

---

## [Two-site gate to MPS](https://itensor.discourse.group/t/two-site-gate-to-mps/2484)

<div class="topic-metadata">

**Author:** [@DavideBianchin4](https://itensor.discourse.group/u/DavideBianchin4)\
**Replies:** 1\
**Last updated:** [September 3, 2025, 1:52pm UTC](https://itensor.discourse.group/t/two-site-gate-to-mps/2484 "2025-09-03T13:52:45Z")

</div>

Hello! I’ll start by saying I’ve never worked with MPS or ITensor before, and I’m doing so for my bachelor’s thesis now, so excuse my ignorance. I am following the official ITensor C++ documentation for applying a two-…

---

## [Contracting diagonal tensors](https://itensor.discourse.group/t/contracting-diagonal-tensors/2323)

<div class="topic-metadata">

**Author:** [@aqubit](https://itensor.discourse.group/u/aqubit)\
**Replies:** 2\
**Last updated:** [March 28, 2025, 2:23pm UTC](https://itensor.discourse.group/t/contracting-diagonal-tensors/2323 "2025-03-28T14:23:52Z")

</div>

Hi, I am using the C++ version (and don’t intend on changing that) and have encountered a slight problem. Specifically, when trying to multiply two diagonal tensors ITensor reports that doTask is not implemented for tha…

---

## [Global Phase Control in TDVP During State Evolution](https://itensor.discourse.group/t/global-phase-control-in-tdvp-during-state-evolution/2296)

<div class="topic-metadata">

**Author:** [@Sambu\_1234](https://itensor.discourse.group/u/Sambu_1234)\
**Replies:** 2\
**Last updated:** [March 11, 2025, 12:14pm UTC](https://itensor.discourse.group/t/global-phase-control-in-tdvp-during-state-evolution/2296 "2025-03-11T12:14:56Z")

</div>

Hi, I am working with two different Hamiltonians, H\_1 and H\_2, evolving two distinct quantum states, ψ\_1 and ψ\_2, separately using the Time-Dependent Variational Principle (TDVP). Under time evolution, each quantum stat…

---

## [Inner Product of two MPS Without Conjugation and QN Conservation](https://itensor.discourse.group/t/inner-product-of-two-mps-without-conjugation-and-qn-conservation/2273)

<div class="topic-metadata">

**Author:** [@Sambu\_1234](https://itensor.discourse.group/u/Sambu_1234)\
**Replies:** 10\
**Last updated:** [March 7, 2025, 8:58pm UTC](https://itensor.discourse.group/t/inner-product-of-two-mps-without-conjugation-and-qn-conservation/2273 "2025-03-07T20:58:31Z")

</div>

Hi, I attempted to compute the inner product of two MPS without the complex conjugate by removing dag(psi) from the innerC function. Below is the modified implementation I am using Cplx innerB(MPS const& psi, MPS co…

---

## [Contracting supervector](https://itensor.discourse.group/t/contracting-supervector/203)

<div class="topic-metadata">

**Author:** [@dduyvu](https://itensor.discourse.group/u/dduyvu)\
**Replies:** 11\
**Last updated:** [February 18, 2025, 5:06am UTC](https://itensor.discourse.group/t/contracting-supervector/203 "2025-02-18T05:06:29Z")

</div>

Hi, I’m writing a density matrix as a supervector, e.g. density matrix of a N-site system is described by a 2N-site array where half of the array is the bra and the other is ket. I want to compute the trace of this dens…

---

## [TDVP with Sum of MPOs in C++ ITensor, Like DMRG](https://itensor.discourse.group/t/tdvp-with-sum-of-mpos-in-c-itensor-like-dmrg/2235)

<div class="topic-metadata">

**Author:** [@Sambu\_1234](https://itensor.discourse.group/u/Sambu_1234)\
**Replies:** 2\
**Last updated:** [February 13, 2025, 10:59am UTC](https://itensor.discourse.group/t/tdvp-with-sum-of-mpos-in-c-itensor-like-dmrg/2235 "2025-02-13T10:59:25Z")

</div>

Hi, I am working with ITensor in C++ and looking to perform TDVP time evolution for a Hamiltonian expressed as a sum of multiple MPOs. In DMRG, Hamiltonians can be handled as a sum of multiple MPOs (e.g., H=H\_1+H\_2+H\_3)…

---

## [eltC function generates wrong answer when calculating inner product](https://itensor.discourse.group/t/eltc-function-generates-wrong-answer-when-calculating-inner-product/2176)

<div class="topic-metadata">

**Author:** [@StrongCC](https://itensor.discourse.group/u/StrongCC)\
**Replies:** 2\
**Last updated:** [December 19, 2024, 7:43am UTC](https://itensor.discourse.group/t/eltc-function-generates-wrong-answer-when-calculating-inner-product/2176 "2024-12-19T07:43:17Z")

</div>

Hello, I wonder why the eltC function and the innerC function generate the different results of the norm of the wave function. tJ sitesold; readFromFile("sites\_file",sitesold); MPS psi(sitesold); …

---

## [Sz(M)Sz(i) Correlation Function in a Thermal State](https://itensor.discourse.group/t/sz-m-sz-i-correlation-function-in-a-thermal-state/2148)

<div class="topic-metadata">

**Author:** [@Sambu\_1234](https://itensor.discourse.group/u/Sambu_1234)\
**Replies:** 4\
**Last updated:** [December 5, 2024, 11:46am UTC](https://itensor.discourse.group/t/sz-m-sz-i-correlation-function-in-a-thermal-state/2148 "2024-12-05T11:46:20Z")

</div>

Hi everyone, I want to calculate the S\_{z}(M)S\_{z}(i) correlation function in the thermal state for a Heisenberg spin-1/2 model. To construct the thermal state, I used the ancilla method, where each pair of sites (physi…

---

## [Issue with ITensor flux while implementing conserved parity in DMRG C++](https://itensor.discourse.group/t/issue-with-itensor-flux-while-implementing-conserved-parity-in-dmrg-c/1958)

<div class="topic-metadata">

**Author:** [@sutapa](https://itensor.discourse.group/u/sutapa)\
**Replies:** 5\
**Last updated:** [November 18, 2024, 5:17am UTC](https://itensor.discourse.group/t/issue-with-itensor-flux-while-implementing-conserved-parity-in-dmrg-c/1958 "2024-11-18T05:17:12Z")

</div>

Hi there! I am trying to implement conserved parity in ITensor C++ version. I consider one dimensional Ising chain with periodic boundary condition. The Hamiltonian is H = -g\\sum\_j \\sigma\_j^z- J\\sum\_j \\sigma\_j^x \\sigma\_…

---

## [Error when calling function toExpH](https://itensor.discourse.group/t/error-when-calling-function-toexph/1974)

<div class="topic-metadata">

**Author:** [@Yangyf](https://itensor.discourse.group/u/Yangyf)\
**Replies:** 1\
**Last updated:** [September 26, 2024, 3:28pm UTC](https://itensor.discourse.group/t/error-when-calling-function-toexph/1974 "2024-09-26T15:28:38Z")

</div>

For SpinHalfSite, the toExpH function runs correctly. For FermionSite,which is: int N=4; auto sites = Fermion(N); auto ampo = AutoMPO(sites); for(auto j : range1(N-1)) { ampo += …

---

## [How to use AutoMPO with a matrix prefactor?](https://itensor.discourse.group/t/how-to-use-autompo-with-a-matrix-prefactor/1971)

<div class="topic-metadata">

**Author:** [@zomal](https://itensor.discourse.group/u/zomal)\
**Replies:** 6\
**Last updated:** [September 19, 2024, 2:04am UTC](https://itensor.discourse.group/t/how-to-use-autompo-with-a-matrix-prefactor/1971 "2024-09-19T02:04:01Z")

</div>

Hi, I want to add a term into my mpo using the AutoMPO function, and the term is a matrix multiplied by a S=\\frac{1}{2} operator. Specifically, in my code I include it as: for (int j=1;j\<N;j++){ mpo += matrix,"Sx",j; …

---

## [Is there a sample function in c++ version?](https://itensor.discourse.group/t/is-there-a-sample-function-in-c-version/1927)

<div class="topic-metadata">

**Author:** [@cbdag](https://itensor.discourse.group/u/cbdag)\
**Replies:** 5\
**Last updated:** [August 30, 2024, 6:05pm UTC](https://itensor.discourse.group/t/is-there-a-sample-function-in-c-version/1927 "2024-08-30T18:05:55Z")

</div>

Hi, a quick search in c++ documentation did not help: is there an MPS sampling function in c++ like in julia the “sample” command? Thanks for the information! Ceren.

---

## [read from Disk - website text](https://itensor.discourse.group/t/read-from-disk-website-text/1920)

<div class="topic-metadata">

**Author:** [@marchewkowy](https://itensor.discourse.group/u/marchewkowy)\
**Replies:** 0\
**Last updated:** [August 11, 2024, 5:07am UTC](https://itensor.discourse.group/t/read-from-disk-website-text/1920 "2024-08-11T05:07:55Z")

</div>

Hi, I managed to make read/write properly in my code, but I wonder if it would be possible to extend the help page ITensor and include literary one sentence hinting that “restoring the SiteSet class originally used …

---

## [Compile error in ITensor and Tensor Network Machine Learning Project](https://itensor.discourse.group/t/compile-error-in-itensor-and-tensor-network-machine-learning-project/1891)

<div class="topic-metadata">

**Author:** [@yhhuang](https://itensor.discourse.group/u/yhhuang)\
**Replies:** 2\
**Last updated:** [August 2, 2024, 12:49am UTC](https://itensor.discourse.group/t/compile-error-in-itensor-and-tensor-network-machine-learning-project/1891 "2024-08-02T00:49:35Z")

</div>

Dear all, Lately I’ve been studying this paper: Supervised Learning with Quantum-Inspired Tensor Networks(link). The authors have provided the source code here: link. This project is called TNML and is dependent on ITen…

---

## [Summation of MPS](https://itensor.discourse.group/t/summation-of-mps/188)

<div class="topic-metadata">

**Author:** [@ljr145733](https://itensor.discourse.group/u/ljr145733)\
**Replies:** 4\
**Last updated:** [July 18, 2024, 1:55am UTC](https://itensor.discourse.group/t/summation-of-mps/188 "2024-07-18T01:55:30Z")

</div>

Hello, I am new to DMRG and ITensor package. Currently I am very interested to calculate the spectral function based on Lanzcos method as described in this article.(https://www.cond-mat.de/events/correl11/manuscript/Koch…

---

## [Llama.cpp compatability](https://itensor.discourse.group/t/llama-cpp-compatability/1786)

<div class="topic-metadata">

**Author:** [@kevinkawchak](https://itensor.discourse.group/u/kevinkawchak)\
**Replies:** 2\
**Last updated:** [June 17, 2024, 12:55am UTC](https://itensor.discourse.group/t/llama-cpp-compatability/1786 "2024-06-17T00:55:46Z")

</div>

How compatible is ITensor C++ with llama.cpp?

---

## [Complex conjugate transpose](https://itensor.discourse.group/t/complex-conjugate-transpose/1799)

<div class="topic-metadata">

**Author:** [@4psireal2](https://itensor.discourse.group/u/4psireal2)\
**Replies:** 0\
**Last updated:** [June 11, 2024, 5:57pm UTC](https://itensor.discourse.group/t/complex-conjugate-transpose/1799 "2024-06-11T17:57:24Z")

</div>

I’m trying to compute the complex conjugate transpose of a vectorized MPO where the 2 physical legs are fused. For example, I have an MPS rho0 rho0 = ITensor ord=2: (dim=4|id=79|"n=1,Site,Boson") (dim=1|id=297|"l=1,Li…

---

## [Discrepancy in Calculating Correlation Function Using iTensor TDVP](https://itensor.discourse.group/t/discrepancy-in-calculating-correlation-function-using-itensor-tdvp/1705)

<div class="topic-metadata">

**Author:** [@subhajyoti.pal](https://itensor.discourse.group/u/subhajyoti.pal)\
**Replies:** 2\
**Last updated:** [June 9, 2024, 6:45am UTC](https://itensor.discourse.group/t/discrepancy-in-calculating-correlation-function-using-itensor-tdvp/1705 "2024-06-09T06:45:18Z")

</div>

Hi, I have been utilizing the iTensor C++ version for the past three months, working on solving a t-J model with next-nearest-neighbor interaction. Specifically, the Hamiltonian I am dealing with is given by: H = \\sum\_…

---

## [apply localMPO to an Itensor](https://itensor.discourse.group/t/apply-localmpo-to-an-itensor/1761)

<div class="topic-metadata">

**Author:** [@jacopo2012](https://itensor.discourse.group/u/jacopo2012)\
**Replies:** 3\
**Last updated:** [May 28, 2024, 1:17pm UTC](https://itensor.discourse.group/t/apply-localmpo-to-an-itensor/1761 "2024-05-28T13:17:13Z")

</div>

Hello, I am trying to apply a localMPO to a local MPS Itensor. Namely auto PH = LocalMPO(WGlobal); int b=1; int numCenter=1 PH.numCenter(numCenter); PH.position(b,psi); phi1 = psi(b); phi1 = PH \* phi1; the latte…

---

## [How to define custom states in ITensor c++?](https://itensor.discourse.group/t/how-to-define-custom-states-in-itensor-c/1696)

<div class="topic-metadata">

**Author:** [@longisland-icetea](https://itensor.discourse.group/u/longisland-icetea)\
**Replies:** 1\
**Last updated:** [May 20, 2024, 9:11am UTC](https://itensor.discourse.group/t/how-to-define-custom-states-in-itensor-c/1696 "2024-05-20T09:11:58Z")

</div>

As I know we can do custom state definition in julia like: ITensors.state(::StateName"+", ::SiteType"Electron") = \[0, 1/sqrt(2), 1/sqrt(2), 0\] But I haven’t get similar method in ITensor c++. In the source code I only …

---

## [Error related to errorMPOProd, and time-evolution (TEBD)](https://itensor.discourse.group/t/error-related-to-errormpoprod-and-time-evolution-tebd/1721)

<div class="topic-metadata">

**Author:** [@subhajyoti.pal](https://itensor.discourse.group/u/subhajyoti.pal)\
**Replies:** 1\
**Last updated:** [May 17, 2024, 8:07am UTC](https://itensor.discourse.group/t/error-related-to-errormpoprod-and-time-evolution-tebd/1721 "2024-05-17T08:07:50Z")

</div>

Hello Everyone, We are facing two issues. We were checking the result of the function errorMPOProd for a two-site operator (A) on a ground state (|G\\rangle) obtained using DMRG, where the Hamiltonian (H) does not con…

---

## [Measure an operator that is a product of many local operators?](https://itensor.discourse.group/t/measure-an-operator-that-is-a-product-of-many-local-operators/1718)

<div class="topic-metadata">

**Author:** [@sysy](https://itensor.discourse.group/u/sysy)\
**Replies:** 1\
**Last updated:** [May 16, 2024, 7:41am UTC](https://itensor.discourse.group/t/measure-an-operator-that-is-a-product-of-many-local-operators/1718 "2024-05-16T07:41:45Z")

</div>

Dear itensor: I want to measure the following operator: \\prod\_{m=1}^{N\_o}{\\left( c\_{m,1}^{\\dagger}c\_{m,2}+c\_{m,2}^{\\dagger}c\_{m,1} \\right)} the operator c^\\dagger,c represents the spinless fermion creation or annihi…

---

## [how to obtain more than one eigen energy of local MPO during sweep?](https://itensor.discourse.group/t/how-to-obtain-more-than-one-eigen-energy-of-local-mpo-during-sweep/1589)

<div class="topic-metadata">

**Author:** [@sysy](https://itensor.discourse.group/u/sysy)\
**Replies:** 4\
**Last updated:** [April 1, 2024, 2:52am UTC](https://itensor.discourse.group/t/how-to-obtain-more-than-one-eigen-energy-of-local-mpo-during-sweep/1589 "2024-04-01T02:52:13Z")

</div>

Dear itensor: ITensor/itensor/mps/dmrg.h at 98fc7022eb812fe68640e662f4051dfd2348e4e9 · ITensor/ITensor (github.com) May I ask if I want to output more eigenenergy for each effective Hamiltonian (local MPO) during the D…

---

## [How to measure an operator that may locally change the quantum number?](https://itensor.discourse.group/t/how-to-measure-an-operator-that-may-locally-change-the-quantum-number/1568)

<div class="topic-metadata">

**Author:** [@sysy](https://itensor.discourse.group/u/sysy)\
**Replies:** 11\
**Last updated:** [April 1, 2024, 2:47am UTC](https://itensor.discourse.group/t/how-to-measure-an-operator-that-may-locally-change-the-quantum-number/1568 "2024-04-01T02:47:44Z")

</div>

Dear itensor, Thank you and your team for developing itensor. Recently, I have encountered some issues while using itensor. I would like to measure the following operator, \\hat{\\mathcal{O}}=\\prod\_{m=1}^{N\_s}{\\hat{o}\_…

---

## [how to obtain the effective hamiltonian defined as following MPO, and performa exact diagionalization on it?](https://itensor.discourse.group/t/how-to-obtain-the-effective-hamiltonian-defined-as-following-mpo-and-performa-exact-diagionalization-on-it/1586)

<div class="topic-metadata">

**Author:** [@sysy](https://itensor.discourse.group/u/sysy)\
**Replies:** 0\
**Last updated:** [March 26, 2024, 1:35pm UTC](https://itensor.discourse.group/t/how-to-obtain-the-effective-hamiltonian-defined-as-following-mpo-and-performa-exact-diagionalization-on-it/1586 "2024-03-26T13:35:04Z")

</div>

Dear itensor: I want to implement the algorithm mentioned in this paper PhysRevB.96.054425 (aps.org) based on itensor, I think I need to get one MPO like this and then do exact diagonalization on it during the swe…

---

## [TDVP for Hamiltonian with three-site interaction](https://itensor.discourse.group/t/tdvp-for-hamiltonian-with-three-site-interaction/1557)

<div class="topic-metadata">

**Author:** [@Federico](https://itensor.discourse.group/u/Federico)\
**Replies:** 1\
**Last updated:** [March 19, 2024, 6:53pm UTC](https://itensor.discourse.group/t/tdvp-for-hamiltonian-with-three-site-interaction/1557 "2024-03-19T18:53:51Z")

</div>

Good morning, I have a question related to ITensor c++. I was trying to implement the time evolution by exploting TDVP (present here TDVP/tdvp.h at master · ITensor/TDVP · GitHub) of of a system with three-site interac…

---

## [How to apply ITensor to a MPS](https://itensor.discourse.group/t/how-to-apply-itensor-to-a-mps/1538)

<div class="topic-metadata">

**Author:** [@debamalya](https://itensor.discourse.group/u/debamalya)\
**Replies:** 7\
**Last updated:** [March 15, 2024, 5:44am UTC](https://itensor.discourse.group/t/how-to-apply-itensor-to-a-mps/1538 "2024-03-15T05:44:36Z")

</div>

I am calculating the String Order parameter and I am trying to create a function like the following. Cplx C\_STRING(const System S, const ITensor n, const int ni\_site, const int nj\_site) { auto sites = S.Sites; auto …

[Next page](https://itensor.discourse.group/c/itensor-c-questions/6.md?page=1)
