Applying a Two-site Operator to an MPS

I am trying to apply a CNOT gate onto an MPS state, but I have no clue on how to create the CNOT gate.
In the guide MPS and MPO Examples · ITensors.jl
it is cited a G operator, but there is no description of how to build it

It’s a good question, because it can be a bit hard to know where to look in the documentation about this. The most relevant documentation section would be this one:
https://itensor.github.io/ITensors.jl/stable/SiteType.html

The short answer is that you can create an ITensor for that operator by doing:

s = siteinds("Qubit",N)
...
G = op("CNOT",s[i],s[j])

Note that it is a two-site operator.

Lastly, I’m realizing that we should probably update the docs to steer people to the apply function which can do what’s shown in those code examples for you, so is much more convenient. Internally it does the same steps as in those examples but saves you from having to write them all manually.

You can apply the gate G to an MPS by just doing

psi = apply(G,psi)

where psi is an MPS.

Thank you so much!
I will be working with ITensors for the next few months, I will probably ask other questions, so I’d love to pay it back by helping out with some documentation or similar if possibile.

1 Like

Can I also define a new operator by a Matrix?

Good question: yes you can. This was also not explained adequately in the docs, so I just updated the docs to better explain this and your other questions above.

Briefly, if you have an Index s and a matrix M you can make an ITensor operator out of M by calling

O = op(M,s)

Here is the updated docs page talking more about the op function:
https://itensor.github.io/ITensors.jl/dev/examples/Physics.html

and I updated the docs for gate evolution (apply function, also named product):
https://itensor.github.io/ITensors.jl/dev/MPSandMPO.html#Gate-evolution

If you see others docs that are missing or could be improved, please let us know. We’d certainly be happy to receive pull requests with updates on the docs, no matter how small. For example, the code examples showing how to apply a gate should probably now recommend using the apply function versus the “manual” way currently shown there (which could still be listed there further down as a kind of advanced version).

Is there a way to retrieve the whole state vector (or initial tensor) already built-in?

Like

inner(ψ, MPS(s, n → n == 1 || n == 3 ? “↓” : “↑”))

for every possible state?

Happy to help with this, but could you post it as a new topic? Also to clarify your question are you asking about turning an MPS into a single tensor? Thanks –