def test_identity(): hi = nk.hilbert.Fock(n_max=3) I = nk.operator.LocalOperator(hi, constant=1) assert_same_matrices(I, np.eye(hi.n_states)) X = bcreate(hi, 0) assert_same_matrices(I @ X, X)
def test_inhomogeneous_hilb_issue_1192(): # Issue #1192 # https://github.com/netket/netket/issues/1192 hi = nk.hilbert.Fock(n_max=3) * nk.hilbert.Spin( 1 / 2) * nk.hilbert.Fock(n_max=2) c0 = bcreate(hi, 0) d2 = bdestroy(hi, 2) assert_same_matrices(c0 @ d2, c0.to_dense() @ d2.to_dense())
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 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()
def test_add_transpose(): hi = nk.hilbert.Fock(n_max=3) c0 = bcreate(hi, 0) assert_same_matrices(c0 + c0.H, c0.to_dense() + c0.H.to_dense())