Constructing operators

Start by importing PauliStrings :

using PauliStrings
import PauliStrings as ps

To construct an operator we first need to declare an empty operator of $N$ qubits :

H = Operator(N)

For the moment, PauliStrings.jl supports a maximum of 64 qubits.

We can add a term of the form $J X_i$ by doing

H += J, "X", i

and a term of the form $J X_iX_j$ by doing

H += J, "X", i, "X", j

Similarly, we add a term of the form $J X_iX_jX_k$ by doing

H += J, "X", i, "X", j, "X", k

etc.

1D transverse ising model

Lets construct the Hamiltonian of a 1D transverse ising model $H=-J(\sum_{<i,j>}Z_i Z_j +g \sum_i X_i)$

function ising1D(N, J, g)
    H = Operator(N)
    for j in 1:(N - 1)
        H += "Z",j,"Z",j+1
    end
    H += "Z",1,"Z",N #periodic boundary condition
    for j in 1:N
        H += g,"X",j
    end
    return -J*H
end

Note that the first qubit starts at index 1, following Julia's 1-based index.

Operators can be printed in strings format with the println function:

julia> println(ising1D(3, 1, 0.5))
(-1.0 + 0.0im) Z1Z
(-1.0 + 0.0im) 1ZZ
(-0.5 + 0.0im) 1X1
(-0.5 + 0.0im) X11
(-1.0 + 0.0im) ZZ1
(-0.5 + 0.0im) 11X

2D transverse ising model

Here we construct a 2D ising model on a square lattice of L*L sites, with no periodic boundary conditions.

function ising2D(L, J, g)
    H = ps.Operator(L*L)
    for x in 1:L-1
        for y in 1:L
            # convert x,y to qubit index
            i = L*(y-1)+x
            j = L*(y-1)+(x+1)
            # horizontal interaction terms
            H += ('Z',i,'Z',j)
            # convert x,y to qubit index
            i = L*(x-1)+y
            j = L*x+y
            # vertical interaction terms
            H += ('Z',i,'Z',j)
        end
    end
    for j in 1:L*L
        H += g,"X",j
    end
    return -J*H
end