Beispiel #1
0
def test_pauli_algebra(S):
    hi = nk.hilbert.Spin(S)**N

    for i in range(N):
        sx = spin.sigmax(hi, i)
        sy = spin.sigmay(hi, i)
        sz = spin.sigmaz(hi, i)

        sm = spin.sigmam(hi, i)
        sp = spin.sigmap(hi, i)

        assert_almost_equal(0.5 * (sx - 1j * sy).to_dense(), sm.to_dense())
        assert_almost_equal(0.5 * (sx + 1j * sy).to_dense(), sp.to_dense())
        assert_almost_equal(0.5 * (sx.to_dense() - 1j * sy.to_dense()),
                            sm.to_dense())
        assert_almost_equal(0.5 * (sx.to_dense() + 1j * sy.to_dense()),
                            sp.to_dense())

        if S == 1 / 2:
            Imat = np.eye(hi.n_states)

            # check that -i sx sy sz = I
            assert_almost_equal((-1j * sx @ sy @ sz).to_dense(), Imat)
            assert_almost_equal(
                (-1j * sx.to_dense() @ sy.to_dense() @ sz.to_dense()), Imat)
Beispiel #2
0
    def reset(self):
        comm = MPI.COMM_WORLD
        rank = comm.Get_rank()
        self._set_graph()
        if rank == 0:
            sys.stdout.write("Graph one the {:d} vertices.\n".format(
                self.graph.n_sites))
            sys.stdout.flush()
        self.n_spins = self.graph.n_sites
        self.hilbert = nk.hilbert.Spin(graph=self.graph, s=0.5)
        self.machine = nk.machine.RbmSpin(self.hilbert, alpha=4)
        self.machine.init_random_parameters(seed=42, sigma=1.0e-2)
        self.sampler = nk.sampler.MetropolisLocal(self.machine)
        self._set_operator()
        self.optimizer = nk.optimizer.RmsProp()

        use_cholesky = self.machine.n_par < 10000

        self.vmc = nk.Vmc(
            hamiltonian=self.hamiltonian,
            sampler=self.sampler,
            optimizer=self.optimizer,
            n_samples=max([2000, self.n_spins * 50]),
            sr=nk.optimizer.SR(
                lsq_solver="LLT",
                diag_shift=1.0e-2,
                use_iterative=not use_cholesky,
                is_holomorphic=self.sampler.machine.is_holomorphic,
            ),
        )

        if rank == 0:
            sys.stdout.write("RBM with {:d} params.\n".format(
                self.machine.n_par))
            sys.stdout.write(self.vmc.info())
            sys.stdout.write("\n")
            sys.stdout.flush()

        self.corr_operators = {}

        for i in range(self.n_spins):
            for j in range(self.n_spins):
                self.corr_operators["{:d}-{:d}".format(
                    i, j)] = sigmaz(self.hilbert, i) * sigmaz(self.hilbert, j)
        self.correlations = []
def test_simple_operators():
    L = 4
    g = nk.graph.Hypercube(L, 1)
    hi = nk.hilbert.Spin(g, 0.5)

    sx = [[0, 1], [1, 0]]
    sy = [[0, -1.0j], [1.0j, 0]]
    sz = [[1, 0], [0, -1]]
    sm = [[0, 0], [1, 0]]
    sp = [[0, 1], [0, 0]]

    print("Testing Sigma_x/y/z...")
    for i in range(L):
        sx_hat = nk.operator.LocalOperator(hi, sx, [i])
        sy_hat = nk.operator.LocalOperator(hi, sy, [i])
        sz_hat = nk.operator.LocalOperator(hi, sz, [i])

        assert (sigmax(hi, i).to_dense() == sx_hat.to_dense()).all()
        assert (sigmay(hi, i).to_dense() == sy_hat.to_dense()).all()
        assert (sigmaz(hi, i).to_dense() == sz_hat.to_dense()).all()

    print("Testing Sigma_+/-...")
    for i in range(L):
        sm_hat = nk.operator.LocalOperator(hi, sm, [i])
        sp_hat = nk.operator.LocalOperator(hi, sp, [i])

        assert (sigmam(hi, i).to_dense() == sm_hat.to_dense()).all()
        assert (sigmap(hi, i).to_dense() == sp_hat.to_dense()).all()

    print("Testing Sigma_+/- composition...")

    hi = nk.hilbert.Spin(g, 0.5)
    for i in range(L):
        sx = sigmax(hi, i)
        sy = sigmay(hi, i)

        sigmam_hat = 0.5 * (sx + (-1j) * sy)
        sigmap_hat = 0.5 * (sx + (1j) * sy)

        assert (sigmam(hi, i).to_dense() == sigmam_hat.to_dense()).all()
        assert (sigmap(hi, i).to_dense() == sigmap_hat.to_dense()).all()

    print("Testing create/destroy composition...")
    hi = nk.hilbert.Boson(g, 3)
    for i in range(L):
        a = bdestroy(hi, i)
        ad = bcreate(hi, i)
        n = bnumber(hi, i)

        assert np.allclose(n.to_dense(), (ad * a).to_dense())
        assert (ad.to_dense() == a.conjugate().transpose().to_dense()).all()
    def __init__(self, n_spins: int, j: float) -> None:
        self.n_spins: Optional[int] = n_spins * n_spins
        self.dim = n_spins
        self.j = j

        self.report: Optional[pd.DataFrame] = None
        self.graph: Optional[nk.graph.Graph] = None
        self.hilbert: Optional[nk.hilbert.Hilbert] = None
        self.sampler: Optional[nk.sampler.MetropolisExchange] = None
        self.hamiltonian: Optional[nk.operator.GraphOperator] = None
        self.optimizer: Optional[nk.optimizer.Optimizer] = None
        self.vmc: Optional[nk.Vmc] = None
        self.corr_operators: Optional[Dict[str,
                                           nk.operator.LocalOperator]] = None
        self.correlations: List[np.ndarray] = []

        self.reset()
        self.corr_operators = {}
        self.k = self.n_spins // 2 + self.dim // 2

        for i in range(self.n_spins):
            self.corr_operators["{:d}-{:d}".format(
                self.k,
                i)] = sigmaz(self.hilbert, self.k) * sigmaz(self.hilbert, i)
Beispiel #5
0
def test_simple_operators():
    L = 4
    hi = nk.hilbert.Spin(0.5) ** L

    sx = [[0, 1], [1, 0]]
    sy = [[0, -1.0j], [1.0j, 0]]
    sz = [[1, 0], [0, -1]]
    sm = [[0, 0], [1, 0]]
    sp = [[0, 1], [0, 0]]

    print("Testing Sigma_x/y/z...")
    for i in range(L):
        sx_hat = nk.operator.LocalOperator(hi, sx, [i])
        sy_hat = nk.operator.LocalOperator(hi, sy, [i])
        sz_hat = nk.operator.LocalOperator(hi, sz, [i])
        assert (sigmax(hi, i).to_dense() == sx_hat.to_dense()).all()
        assert (sigmay(hi, i).to_dense() == sy_hat.to_dense()).all()
        assert (sigmaz(hi, i).to_dense() == sz_hat.to_dense()).all()

    print("Testing Sigma_+/-...")
    for i in range(L):
        sm_hat = nk.operator.LocalOperator(hi, sm, [i])
        sp_hat = nk.operator.LocalOperator(hi, sp, [i])
        assert (sigmam(hi, i).to_dense() == sm_hat.to_dense()).all()
        assert (sigmap(hi, i).to_dense() == sp_hat.to_dense()).all()

    print("Testing Sigma_+/- composition...")

    hi = nk.hilbert.Spin(0.5, N=L)
    for i in range(L):
        sx = sigmax(hi, i)
        sy = sigmay(hi, i)
        sigmam_hat = 0.5 * (sx + (-1j) * sy)
        sigmap_hat = 0.5 * (sx + (1j) * sy)
        assert (sigmam(hi, i).to_dense() == sigmam_hat.to_dense()).all()
        assert (sigmap(hi, i).to_dense() == sigmap_hat.to_dense()).all()

    print("Testing create/destroy composition...")
    hi = nk.hilbert.Fock(3, N=L)
    for i in range(L):
        print("i=", i)
        a = bdestroy(hi, i)
        ad = bcreate(hi, i)
        n = bnumber(hi, i)

        assert np.allclose(n.to_dense(), (ad @ a).to_dense())
        assert (ad.to_dense() == a.conjugate().transpose().to_dense()).all()

    print("Testing mixed spaces...")
    L = 3
    his = nk.hilbert.Spin(0.5, N=L)
    hib = nk.hilbert.Fock(3, N=L - 1)
    hi = his * hib
    for i in range(hi.size):
        print("i=", i)
        sx = sigmax(hi, i)

        assert sx.operators[0].shape == (hi.shape[i], hi.shape[i])
        assert np.allclose(n.to_dense(), (ad @ a).to_dense())
        assert (ad.to_dense() == a.conjugate().transpose().to_dense()).all()

    for i in range(3):
        print("i=", i)
        a = bdestroy(hi, i)
        ad = bcreate(hi, i)
        n = bnumber(hi, i)
        for j in range(3, 5):
            print("j=", i)
            a = bdestroy(hi, j)
            ad = bcreate(hi, j)
            n = bnumber(hi, j)

        assert np.allclose(n.to_dense(), (ad @ a).to_dense())
        assert (ad.to_dense() == a.conjugate().transpose().to_dense()).all()
Beispiel #6
0
np.set_printoptions(linewidth=180)

# 1D Lattice
L = 4

# Hilbert space of spins on the graph
hi = nk.hilbert.Spin(s=0.5) ** L

ha = nk.operator.LocalOperator(hi, dtype=complex)
j_ops = []

for i in range(L):
    ha += spin.sigmax(hi, i)
    ha += spin.sigmay(hi, i)
    ha += spin.sigmaz(hi, i) @ spin.sigmaz(hi, (i + 1) % L)
    j_ops.append(spin.sigmam(hi, i))
    j_ops.append(1j * spin.sigmam(hi, i))


#  Create the lindbladian with
lind = nk.operator.LocalLiouvillian(ha, j_ops)


def test_lindblad_form():
    ## Construct the lindbladian by hand:
    idmat = sparse.eye(2**L)

    # Build the non hermitian matrix
    hnh_mat = ha.to_sparse()
    for j_op in j_ops: