Code covered by the BSD License  

Highlights from
QUBIT4MATLAB V4.0

from QUBIT4MATLAB V4.0 by Geza Toth
MATLAB package for quantum information science and quantum mechanics.

h=nnchainp(a,b,varargin)
%nnchainp   Defines a Hamiltonian with a(k)b(k+1) nearest-neighbor
%           and periodic boundary condition.
%   nnchainp (a,b,n) defines a a(k)b(k+1) type Hamiltonian
%   with a periodic boundary condition.
%   If argument n is omitted than the default is taking to be
%   the value of global variable N.

function h=nnchainp(a,b,varargin)
if isempty(varargin),
    global N;
else
    if length(varargin)~=1,
        error('Wrong number of input arguments.')
    end %if
    N=varargin{1};
end %if
h=zeros(2^N);
op=kron(a,b);
for n=1:N-1
    h=h+kron(kron(eye(2^(n-1)),op),eye(2^(N-n-1)));
end %for
% Periodic boundary condition
if N>1,
   h=h+kron(kron(b,eye(2^(n-1))),kron(eye(2^(N-n-1)),a));
end %for

Contact us at files@mathworks.com