From a2b9f4bb6851eeb7efaa2497c18cfe96f368f672 Mon Sep 17 00:00:00 2001 From: Jue Xu Date: Sun, 21 Jul 2024 15:02:41 +0800 Subject: [PATCH] update spin API --- .../09d749e2-95b2-40d4-9eb5-444ebea855d8.hdf5 | Bin 0 -> 33288 bytes .../data/hydrogen_2_1.0.hdf5 | Bin 0 -> 33288 bytes .../{fermion_ham.py => fermion.py} | 8 +- quantum_simulation_recipe/spin.py | 122 ++++++ quantum_simulation_recipe/spin_ham.py | 130 ------- quantum_simulation_recipe/test.ipynb | 354 ++++++++++++++++++ setup.py | 2 +- 7 files changed, 481 insertions(+), 135 deletions(-) create mode 100644 quantum_simulation_recipe/09d749e2-95b2-40d4-9eb5-444ebea855d8.hdf5 create mode 100644 quantum_simulation_recipe/data/hydrogen_2_1.0.hdf5 rename quantum_simulation_recipe/{fermion_ham.py => fermion.py} (98%) create mode 100644 quantum_simulation_recipe/spin.py delete mode 100644 quantum_simulation_recipe/spin_ham.py create mode 100644 quantum_simulation_recipe/test.ipynb diff --git a/quantum_simulation_recipe/09d749e2-95b2-40d4-9eb5-444ebea855d8.hdf5 b/quantum_simulation_recipe/09d749e2-95b2-40d4-9eb5-444ebea855d8.hdf5 new file mode 100644 index 0000000000000000000000000000000000000000..f170554d9f564a74189602e518723d876f6d8129 GIT binary patch literal 33288 zcmeI53v3ic7=Y*cfU`wtDQH2E69q3KS{}hh%QHMqQXW!BiqUSb*X4TjdUxDip%n^+ zm{pHEM$fDryQCVq=0(O%Ot`?(Y15d%f%JwNTo<@^8b< zKlA@PJM-=A?mx4$!+O*7nfJ#Gj$!hq*Rv=Vr-=B|1e<;xPr*3qGeOWnSOQ^11Q&En z{$nf=(&Hh2f4hYXX3Su$7sLE9gx%QrXf7o|Lh&jtBtyLVAud!x{CqAKav*McgbQU5 zzi#4!0SgZ46ljxxX~xv~a>=TpT^S*)c=7I*H(2{MMe^h3K)dAaR~NMXChNI<>#OVT zmYs6f4gqYx=};sCgeA~^u%u?MGtVPM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W19yE;&tlGN=oL0vgm-Od2zYnY{O*In9R^Mw|7ut1-N=%4tv zM>y#nJPMf;0U|&IhyW2F0z`la5CI}U1c*R)69|0o$$NOv{gu|a>8$UZ;=^4> z9~L~YeOg6lp6GQKVm!<2vB_(s=H=VHHkaLIv3V7pv<#x#Dl+N2Dv!l&bID7oD!Vw$ zc~UB44uP+<+PvPX+v$~8TXhJcU9@=Jxbmt)aJsW>Ub7t*Vs!}1@})Q~%gP-27Q1M6 z3vSVsZ};F)7|U5Ahz`+h#e!PQ4yVIrG28v}b1oO%cC$;cIlQ9P-Fz4ZN9CE=#OC8U z9ious%r5lb!MnoQW_q4$Y^#D;Y@Tf21SKwK>dwxSxhQctGl(y-*nG{I9QQF-;y&ig zV)4sgj`QUw1#_OOxgLsLwlhD=F1AXSmar5h41z?;k}rJ`Coh33htk2({KCTDQu;@p(R;t)cHw%QAV9{l?w9 z_B+(!t+55fhQQfJ1&tdc2?~h-5g-CYfCvx)BG5Al1p3h?rJ`Xp&&8;8#Q9n;=xfU% zbZB_LpuJ}g*QH8^j&Vu4`kn?*mnW#(q1RuUsBV`7YW+}kJFRPj&2yR4!Gcc;(LbU7 zN(X_GIT0WNM1Tko0U|&IhyW2F0z^Qg1VX>}?5ue%8^Yo}Fpf{)e?D0j;=_@27{@4o zRQ&|!fL+eZ>UPPXt{$jvr*&=DwX2O`(XP6V$0p(-T^!BpRqI|S9<6w64htUt12SjB AXaE2J literal 0 HcmV?d00001 diff --git a/quantum_simulation_recipe/data/hydrogen_2_1.0.hdf5 b/quantum_simulation_recipe/data/hydrogen_2_1.0.hdf5 new file mode 100644 index 0000000000000000000000000000000000000000..f170554d9f564a74189602e518723d876f6d8129 GIT binary patch literal 33288 zcmeI53v3ic7=Y*cfU`wtDQH2E69q3KS{}hh%QHMqQXW!BiqUSb*X4TjdUxDip%n^+ zm{pHEM$fDryQCVq=0(O%Ot`?(Y15d%f%JwNTo<@^8b< zKlA@PJM-=A?mx4$!+O*7nfJ#Gj$!h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I}U1c*SG69@x8I(-ah+^v3ei0QY?LVk3ZUkOxvB0vO)01+SpM1Tl% zj6mRcf1;8>@cACM`#xk9c$)t69JDpw9|rx3%8C3GcWb|5RETD_k@^)O2$7ly5CI}U z1c(3;AOby$K%gIOQYxs6;uBx|=WBE{e)s0V7?g5GMnq4U%})vX*%-pq5+m2P^Ow)p zlV(G~yr1$E^&PK*y7W19yE;&tlGN=oL0vgm-Od2zYnY{O*In9R^Mw|7ut1-N=%4tv zM>y#nJPMf;0U|&IhyW2F0z`la5CI}U1c*R)69|0o$$NOv{gu|a>8$UZ;=^4> z9~L~YeOg6lp6GQKVm!<2vB_(s=H=VHHkaLIv3V7pv<#x#Dl+N2Dv!l&bID7oD!Vw$ zc~UB44uP+<+PvPX+v$~8TXhJcU9@=Jxbmt)aJsW>Ub7t*Vs!}1@})Q~%gP-27Q1M6 z3vSVsZ};F)7|U5Ahz`+h#e!PQ4yVIrG28v}b1oO%cC$;cIlQ9P-Fz4ZN9CE=#OC8U z9ious%r5lb!MnoQW_q4$Y^#D;Y@Tf21SKwK>dwxSxhQctGl(y-*nG{I9QQF-;y&ig zV)4sgj`QUw1#_OOxgLsLwlhD=F1AXSmar5h41z?;k}rJ`Coh33htk2({KCTDQu;@p(R;t)cHw%QAV9{l?w9 z_B+(!t+55fhQQfJ1&tdc2?~h-5g-CYfCvx)BG5Al1p3h?rJ`Xp&&8;8#Q9n;=xfU% zbZB_LpuJ}g*QH8^j&Vu4`kn?*mnW#(q1RuUsBV`7YW+}kJFRPj&2yR4!Gcc;(LbU7 zN(X_GIT0WNM1Tko0U|&IhyW2F0z^Qg1VX>}?5ue%8^Yo}Fpf{)e?D0j;=_@27{@4o zRQ&|!fL+eZ>UPPXt{$jvr*&=DwX2O`(XP6V$0p(-T^!BpRqI|S9<6w64htUt12SjB AXaE2J literal 0 HcmV?d00001 diff --git a/quantum_simulation_recipe/fermion_ham.py b/quantum_simulation_recipe/fermion.py similarity index 98% rename from quantum_simulation_recipe/fermion_ham.py rename to quantum_simulation_recipe/fermion.py index 51dbd42..8bcc31a 100644 --- a/quantum_simulation_recipe/fermion_ham.py +++ b/quantum_simulation_recipe/fermion.py @@ -97,7 +97,7 @@ def op2mat(ops, n_qubits, to_array=False): return list_sparse_mat -class h2o_hamiltonian: +class H2O: def __init__(self, verbose=False): # Set molecule parameters self.use_ActiveSpace = True @@ -212,8 +212,8 @@ def pauli_commutator(pauli_str1, pauli_str2): return product, 2*(factor%2) * phase -class hydrogen_chain_hamiltonian: - def __init__(self, chain_length, bond_length, verbose=False): +class Hydrogen_Chain: + def __init__(self, chain_length: int, bond_length: float, verbose=False): # def hydrogen_chain_hamiltonian(chain_length, bond_length): self.chain_length = chain_length @@ -295,7 +295,7 @@ def __init__(self, chain_length, bond_length, verbose=False): # return hydrogen_hamiltonian_list -def LiH_hamiltonian(): +def LiH(): basis = 'sto-3g' multiplicity = 1 # Set Hamiltonian parameters. diff --git a/quantum_simulation_recipe/spin.py b/quantum_simulation_recipe/spin.py new file mode 100644 index 0000000..84fd711 --- /dev/null +++ b/quantum_simulation_recipe/spin.py @@ -0,0 +1,122 @@ +############################ +# define spin Hamiltonians +############################ + +import numpy as np +from qiskit.quantum_info import SparsePauliOp +import random + +class Nearest_Neighbour_1d: + def __init__(self, n: int, Jx=1, Jy=1, Jz=1, hx=0.2, hy=0, hz=0, pbc=False, verbose=False, rand_field=[]): + self.n = n + self.xx_tuples = [('XX', [i, i + 1], Jx) for i in range(0, n-1)] + self.yy_tuples = [('YY', [i, i + 1], Jy) for i in range(0, n-1)] + self.zz_tuples = [('ZZ', [i, i + 1], Jz) for i in range(0, n-1)] + + if len(rand_field) == 0: + self.rand_field = [0]*n + elif len(rand_field) >= n: + self.rand_field = rand_field[:n] + else: + raise ValueError(f'Length of random field should be at least {n}!') + + self.x_tuples = [('X', [i], (self.rand_field[i]+1)*hx) for i in range(0, n)] + self.y_tuples = [('Y', [i], (self.rand_field[i]+1)*hy) for i in range(0, n)] + self.z_tuples = [('Z', [i], (self.rand_field[i]+1)*hz) for i in range(0, n)] + + if pbc: + self.xx_tuples.append(('XX', [n-1, 0], Jx)) + self.yy_tuples.append(('YY', [n-1, 0], Jy)) + self.zz_tuples.append(('ZZ', [n-1, 0], Jz)) + + self.ham = SparsePauliOp.from_sparse_list([*self.xx_tuples, *self.yy_tuples, *self.zz_tuples, *self.x_tuples, *self.y_tuples, *self.z_tuples], num_qubits=n).simplify() + self.xyz_group() + self.par_group() + if verbose: + print('The Hamiltonian: \n', self.ham) + print('The xyz grouping: \n', self.ham_xyz) + print('The parity grouping: \n', self.ham_par) + + def xyz_group(self): + self.x_terms = SparsePauliOp.from_sparse_list([*self.xx_tuples, *self.x_tuples], num_qubits=self.n).simplify() + self.y_terms = SparsePauliOp.from_sparse_list([*self.yy_tuples, *self.y_tuples], num_qubits=self.n).simplify() + self.z_terms = SparsePauliOp.from_sparse_list([*self.zz_tuples, *self.z_tuples], num_qubits=self.n).simplify() + self.ham_xyz = [self.x_terms, self.y_terms, self.z_terms] + self.ham_xyz = [item for item in self.ham_xyz if not np.all(abs(item.coeffs) == 0)] + + def par_group(self): + self.even_terms = SparsePauliOp.from_sparse_list([*self.xx_tuples[::2], *self.yy_tuples[::2], *self.zz_tuples[::2], *self.x_tuples[::2], *self.y_tuples[::2], *self.z_tuples[::2]], num_qubits=self.n).simplify() + self.odd_terms = SparsePauliOp.from_sparse_list([*self.xx_tuples[1::2], *self.yy_tuples[1::2], *self.zz_tuples[1::2], *self.x_tuples[1::2], *self.y_tuples[1::2], *self.z_tuples[1::2]], num_qubits=self.n).simplify() + self.ham_par = [self.even_terms, self.odd_terms] + + # def lc_group(self, right, left, step): + # self.ham_lc = [] + + +class Power_Law: + def __init__(self, n: int, alpha=4, Jx=1, Jy=1, Jz=1, hx=0.0, hy=0.0, hz=0.2, pbc=False, verbose=False): + self.n, self.alpha = n, alpha + self.xx_tuples = [('XX', [i, j], Jx*abs(i-j)**(-alpha)) for i in range(0, n-1) for j in range(i+1, n)] + self.yy_tuples = [('YY', [i, j], Jy*abs(i-j)**(-alpha)) for i in range(0, n-1) for j in range(i+1, n)] + self.zz_tuples = [('ZZ', [i, j], Jz*abs(i-j)**(-alpha)) for i in range(0, n-1) for j in range(i+1, n)] + self.x_tuples = [('X', [i], hx) for i in range(0, n)] + self.y_tuples = [('Y', [i], hy) for i in range(0, n)] + self.z_tuples = [('Z', [i], hz) for i in range(0, n)] + if pbc: + # self.xx_tuples.append(('XX', [n-1, 0], Jx)) + # self.yy_tuples.append(('YY', [n-1, 0], Jy)) + # self.zz_tuples.append(('ZZ', [n-1, 0], Jz)) + raise ValueError(f'PBC is not defined!') + + self.ham = SparsePauliOp.from_sparse_list([*self.xx_tuples, *self.yy_tuples, *self.zz_tuples, *self.x_tuples, *self.y_tuples, *self.z_tuples], num_qubits=n).simplify() + if verbose: print('The Hamiltonian: \n', self.ham) + self.xyz_group() + + def xyz_group(self): + self.x_terms = SparsePauliOp.from_sparse_list([*self.xx_tuples, *self.x_tuples], self.n).simplify() + self.y_terms = SparsePauliOp.from_sparse_list([*self.yy_tuples, *self.y_tuples], self.n).simplify() + self.z_terms = SparsePauliOp.from_sparse_list([*self.zz_tuples, *self.z_tuples], self.n).simplify() + self.ham_xyz = [self.x_terms, self.y_terms, self.z_terms] + self.ham_xyz = [item for item in self.ham_xyz if not np.all(abs(item.coeffs) == 0)] + + # def parity_group(self): + # print('todo') + # return self.ham.to_matrix().todense() + +# class TF_Ising_1d: +# def __init__(self, n: int, J=1, h=0.2, g=0.0, pbc=False, verbose=False): +# self.n = n +# self.zz_tuples = [('ZZ', [i, i + 1], -J) for i in range(0, n-1)] +# self.x_tuples = [('X', [i], -h) for i in range(0, n)] +# self.z_tuples = [('Z', [i], -g) for i in range(0, n)] +# if pbc: self.zz_tuples.append(('ZZ', [n-1, 0], -J)) + +# self.ham = SparsePauliOp.from_sparse_list([*self.zz_tuples, *self.x_tuples, *self.z_tuples], num_qubits=n).simplify() +# if verbose: print('The Hamiltonian: \n', self.ham) +# self.parity_group() +# self.xyz_group() + +# def parity_group(self): +# # return self.ham.to_matrix().todense() +# self.ham_parity = [SparsePauliOp.from_sparse_list([*self.zz_tuples[::2], *self.x_tuples[::2], *self.z_tuples[::2]], num_qubits=self.n).simplify(), SparsePauliOp.from_sparse_list([*self.zz_tuples[1::2], *self.x_tuples[1::2], *self.z_tuples[1::2]], num_qubits=self.n).simplify()] + +# def xyz_group(self): +# self.ham_xyz = [SparsePauliOp.from_sparse_list([*self.zz_tuples, *self.z_tuples], num_qubits=self.n).simplify(), SparsePauliOp.from_sparse_list([*self.x_tuples], num_qubits=self.n)] + +# class Heisenberg_1d: +# def __init__(self, n: int, Jx=1, Jy=1, Jz=1, h=0.2, pbc=False, verbose=False): +# self.xx_tuples = [('XX', [i, i + 1], -Jx) for i in range(0, n-1)] +# self.yy_tuples = [('YY', [i, i + 1], -Jy) for i in range(0, n-1)] +# self.zz_tuples = [('ZZ', [i, i + 1], -Jz) for i in range(0, n-1)] +# self.x_tuples = [('X', [i], -h) for i in range(0, n)] +# if pbc: +# self.xx_tuples.append(('XX', [n-1, 0], -Jx)) +# self.yy_tuples.append(('YY', [n-1, 0], -Jy)) +# self.zz_tuples.append(('ZZ', [n-1, 0], -Jz)) + +# self.ham = SparsePauliOp.from_sparse_list([*self.xx_tuples, *self.yy_tuples, *self.zz_tuples, *self.x_tuples], num_qubits=n) +# if verbose: print('The Hamiltonian: \n', self.ham) + +# def parity_group(self): +# print('todo') +# return self.ham.to_matrix().todense() diff --git a/quantum_simulation_recipe/spin_ham.py b/quantum_simulation_recipe/spin_ham.py deleted file mode 100644 index 4d8be97..0000000 --- a/quantum_simulation_recipe/spin_ham.py +++ /dev/null @@ -1,130 +0,0 @@ -############################ -# define spin Hamiltonians -############################ - -import numpy as np -from qiskit.quantum_info import SparsePauliOp -import random - -class Nearest_Neighbour_1d: - def __init__(self, n: int, Jx=1, Jy=1, Jz=1, hx=0.2, hy=0, hz=0, pbc=False, verbose=False, rand_field=[]): - self.n = n - self.xx_tuples = [('XX', [i, i + 1], Jx) for i in range(0, n-1)] - self.yy_tuples = [('YY', [i, i + 1], Jy) for i in range(0, n-1)] - self.zz_tuples = [('ZZ', [i, i + 1], Jz) for i in range(0, n-1)] - - if len(rand_field) != 0: - self.rand_field = rand_field[:n] - # if len(rand_field) < n: - # self.rand_field = rand_field + [random.gauss(0, 0.1) for _ in range(n-len(rand_field))] - # else: - # self.rand_field = rand_field[:n] - # # self.rand_field = rand_field + [0.2*(random.random()-0.5) for _ in range(n-len(rand_field))] - if hx != 0: - self.x_tuples = [('X', [i], (self.rand_field[i]+1)*hx) for i in range(0, n)] - else: - self.x_tuples = [('X', [i], hx) for i in range(0, n)] - if hy != 0: - self.y_tuples = [('Y', [i], (self.rand_field[i]+1)*hy) for i in range(0, n)] - else: - self.y_tuples = [('Y', [i], hy) for i in range(0, n)] - if hz != 0: - self.z_tuples = [('Z', [i], (self.rand_field[i]+1)*hz) for i in range(0, n)] - else: - self.z_tuples = [('Z', [i], hz) for i in range(0, n)] - else: - self.x_tuples = [('X', [i], hx) for i in range(0, n)] - self.y_tuples = [('Y', [i], hy) for i in range(0, n)] - self.z_tuples = [('Z', [i], hz) for i in range(0, n)] - - if pbc: - self.xx_tuples.append(('XX', [n-1, 0], Jx)) - self.yy_tuples.append(('YY', [n-1, 0], Jy)) - self.zz_tuples.append(('ZZ', [n-1, 0], Jz)) - - self.ham = SparsePauliOp.from_sparse_list([*self.xx_tuples, *self.yy_tuples, *self.zz_tuples, *self.x_tuples, *self.y_tuples, *self.z_tuples], num_qubits=n).simplify() - self.xyz_group() - self.par_group() - if verbose: - print('The Hamiltonian: \n', self.ham) - print('The xyz grouping: \n', self.ham_xyz) - print('The parity grouping: \n', self.ham_par) - - def xyz_group(self): - self.x_terms = SparsePauliOp.from_sparse_list([*self.xx_tuples, *self.x_tuples], num_qubits=self.n).simplify() - self.y_terms = SparsePauliOp.from_sparse_list([*self.yy_tuples, *self.y_tuples], num_qubits=self.n).simplify() - self.z_terms = SparsePauliOp.from_sparse_list([*self.zz_tuples, *self.z_tuples], num_qubits=self.n).simplify() - self.ham_xyz = [self.x_terms, self.y_terms, self.z_terms] - - def par_group(self): - self.even_terms = SparsePauliOp.from_sparse_list([*self.xx_tuples[::2], *self.yy_tuples[::2], *self.zz_tuples[::2], *self.x_tuples[::2], *self.y_tuples[::2], *self.z_tuples[::2]], num_qubits=self.n).simplify() - self.odd_terms = SparsePauliOp.from_sparse_list([*self.xx_tuples[1::2], *self.yy_tuples[1::2], *self.zz_tuples[1::2], *self.x_tuples[1::2], *self.y_tuples[1::2], *self.z_tuples[1::2]], num_qubits=self.n).simplify() - self.ham_par = [self.even_terms, self.odd_terms] - - def lc_group(self, right, left, step): - self.ham_lc = [] - - -class Power_law: - def __init__(self, n: int, alpha=4, Jx=1, Jy=1, Jz=1, hx=0.0, hy=0.0, hz=0.2, pbc=False, verbose=False): - self.n, self.alpha = n, alpha - self.xx_tuples = [('XX', [i, j], Jx*abs(i-j)**(-alpha)) for i in range(0, n-1) for j in range(i+1, n)] - self.yy_tuples = [('YY', [i, j], Jy*abs(i-j)**(-alpha)) for i in range(0, n-1) for j in range(i+1, n)] - self.zz_tuples = [('ZZ', [i, j], Jz*abs(i-j)**(-alpha)) for i in range(0, n-1) for j in range(i+1, n)] - self.x_tuples = [('X', [i], hx) for i in range(0, n)] - self.y_tuples = [('Y', [i], hy) for i in range(0, n)] - self.z_tuples = [('Z', [i], hz) for i in range(0, n)] - if pbc: - # self.xx_tuples.append(('XX', [n-1, 0], Jx)) - # self.yy_tuples.append(('YY', [n-1, 0], Jy)) - # self.zz_tuples.append(('ZZ', [n-1, 0], Jz)) - raise ValueError(f'PBC is not defined!') - - self.ham = SparsePauliOp.from_sparse_list([*self.xx_tuples, *self.yy_tuples, *self.zz_tuples, *self.x_tuples, *self.y_tuples, *self.z_tuples], num_qubits=n).simplify() - if verbose: print('The Hamiltonian: \n', self.ham) - self.xyz_group() - - def xyz_group(self): - self.ham_xyz = [SparsePauliOp.from_sparse_list([*self.xx_tuples, *self.x_tuples], num_qubits=self.n).simplify(), SparsePauliOp.from_sparse_list([*self.yy_tuples, *self.y_tuples], num_qubits=self.n).simplify(), SparsePauliOp.from_sparse_list([*self.zz_tuples, *self.z_tuples], num_qubits=self.n).simplify()] - - def parity_group(self): - print('todo') - return self.ham.to_matrix().todense() - -class TF_Ising_1d: - def __init__(self, n: int, J=1, h=0.2, g=0.0, pbc=False, verbose=False): - self.n = n - self.zz_tuples = [('ZZ', [i, i + 1], -J) for i in range(0, n-1)] - self.x_tuples = [('X', [i], -h) for i in range(0, n)] - self.z_tuples = [('Z', [i], -g) for i in range(0, n)] - if pbc: self.zz_tuples.append(('ZZ', [n-1, 0], -J)) - - self.ham = SparsePauliOp.from_sparse_list([*self.zz_tuples, *self.x_tuples, *self.z_tuples], num_qubits=n).simplify() - if verbose: print('The Hamiltonian: \n', self.ham) - self.parity_group() - self.xyz_group() - - def parity_group(self): - # return self.ham.to_matrix().todense() - self.ham_parity = [SparsePauliOp.from_sparse_list([*self.zz_tuples[::2], *self.x_tuples[::2], *self.z_tuples[::2]], num_qubits=self.n).simplify(), SparsePauliOp.from_sparse_list([*self.zz_tuples[1::2], *self.x_tuples[1::2], *self.z_tuples[1::2]], num_qubits=self.n).simplify()] - - def xyz_group(self): - self.ham_xyz = [SparsePauliOp.from_sparse_list([*self.zz_tuples, *self.z_tuples], num_qubits=self.n).simplify(), SparsePauliOp.from_sparse_list([*self.x_tuples], num_qubits=self.n)] - -class Heisenberg_1d: - def __init__(self, n: int, Jx=1, Jy=1, Jz=1, h=0.2, pbc=False, verbose=False): - self.xx_tuples = [('XX', [i, i + 1], -Jx) for i in range(0, n-1)] - self.yy_tuples = [('YY', [i, i + 1], -Jy) for i in range(0, n-1)] - self.zz_tuples = [('ZZ', [i, i + 1], -Jz) for i in range(0, n-1)] - self.x_tuples = [('X', [i], -h) for i in range(0, n)] - if pbc: - self.xx_tuples.append(('XX', [n-1, 0], -Jx)) - self.yy_tuples.append(('YY', [n-1, 0], -Jy)) - self.zz_tuples.append(('ZZ', [n-1, 0], -Jz)) - - self.ham = SparsePauliOp.from_sparse_list([*self.xx_tuples, *self.yy_tuples, *self.zz_tuples, *self.x_tuples], num_qubits=n) - if verbose: print('The Hamiltonian: \n', self.ham) - - def parity_group(self): - print('todo') - return self.ham.to_matrix().todense() diff --git a/quantum_simulation_recipe/test.ipynb b/quantum_simulation_recipe/test.ipynb new file mode 100644 index 0000000..fcea94f --- /dev/null +++ b/quantum_simulation_recipe/test.ipynb @@ -0,0 +1,354 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "e385a43f", + "metadata": {}, + "source": [ + "# Test" + ] + }, + { + "cell_type": "markdown", + "id": "b318c92e", + "metadata": {}, + "source": [ + "## Hamiltonians" + ] + }, + { + "cell_type": "markdown", + "id": "2209bd76", + "metadata": {}, + "source": [ + "### Spin lattice model" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "8c03d3e4", + "metadata": {}, + "outputs": [], + "source": [ + "from spin import *\n", + "from fermion import *" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "6ae552ff", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[SparsePauliOp(['IIXX', 'XXII', 'IIYY', 'YYII', 'IIZZ', 'ZZII', 'IIIZ', 'IZII'],\n", + " coeffs=[1. +0.j, 1. +0.j, 1. +0.j, 1. +0.j, 1. +0.j, 1. +0.j, 0.2+0.j, 0.2+0.j]),\n", + " SparsePauliOp(['IXXI', 'XIIX', 'IYYI', 'YIIY', 'IZZI', 'ZIIZ', 'IIZI', 'ZIII'],\n", + " coeffs=[1. +0.j, 1. +0.j, 1. +0.j, 1. +0.j, 1. +0.j, 1. +0.j, 0.2+0.j, 0.2+0.j])]" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Test 1D Nearest Neighbour (Heisenberg) Hamiltonian with PBC\n", + "n = 4\n", + "nnh = Nearest_Neighbour_1d(4, Jx=1, Jy=1, Jz=1, hx=0, hy=0, hz=0.2, pbc=True)\n", + "nnh.ham\n", + "nnh.ham_par\n", + "# nnh.ham_xyz" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "id": "c9ffdc57", + "metadata": {}, + "outputs": [], + "source": [ + "## to matrix\n", + "h_list = [h.to_matrix() for h in nnh.ham_par]\n", + "# h_list" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "id": "37fd7d0d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[SparsePauliOp(['IIIX', 'IIXI', 'IXII', 'XIII'],\n", + " coeffs=[0.20865101+0.j, 0.18081363+0.j, 0.22731382+0.j, 0.19945575+0.j]),\n", + " SparsePauliOp(['IIZZ', 'IZZI', 'ZZII', 'ZIIZ'],\n", + " coeffs=[1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j])]" + ] + }, + "execution_count": 103, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## TF Ising with disorder field\n", + "n = 4\n", + "disorder = np.random.uniform(-0.2, 0.2, n)\n", + "tfi = Nearest_Neighbour_1d(4, Jx=0, Jy=0, Jz=1, hx=0.2, hy=0, hz=0, pbc=True, rand_field=disorder)\n", + "tfi.ham\n", + "tfi.ham_xyz" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "697472fc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[SparsePauliOp(['IXX', 'XIX', 'XXI'],\n", + " coeffs=[1. +0.j, 0.0625+0.j, 1. +0.j]),\n", + " SparsePauliOp(['IZZ', 'ZIZ', 'ZZI', 'IIZ', 'IZI', 'ZII'],\n", + " coeffs=[1. +0.j, 0.0625+0.j, 1. +0.j, 0.2 +0.j, 0.2 +0.j, 0.2 +0.j])]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Test 1D Power-law decaying Hamiltonian\n", + "plh = Power_Law(3, alpha=4, Jx=1, Jy=0, Jz=1, hx=0, hy=0, hz=0.2)\n", + "plh.ham_xyz" + ] + }, + { + "cell_type": "markdown", + "id": "d7fe9c22", + "metadata": {}, + "source": [ + "### Molecules" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "5534a0bd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "# pstr: 15, ['IIII', 'ZIII', 'IZII', 'IIZI', 'IIIZ', 'ZZII', 'YXXY', 'YYXX', 'XXYY', 'XYYX', 'ZIZI', 'ZIIZ', 'IZZI', 'IZIZ', 'IIZZ']\n", + "# groups: 2\n", + "[[{'IIII': (-0.3276081896748102+0j)}, {'ZIII': (0.13716572937099497+0j)}, {'IZII': (0.13716572937099497+0j)}, {'IIZI': (-0.13036292057109025+0j)}, {'IIIZ': (-0.13036292057109025+0j)}, {'ZZII': (0.15660062488237947+0j)}], [{'YXXY': (0.049197645871367546+0j)}, {'YYXX': (-0.049197645871367546+0j)}, {'XXYY': (-0.049197645871367546+0j)}, {'XYYX': (0.049197645871367546+0j)}, {'ZIZI': (0.10622904490856078+0j)}, {'ZIIZ': (0.15542669077992832+0j)}, {'IZZI': (0.15542669077992832+0j)}, {'IZIZ': (0.10622904490856078+0j)}, {'IIZZ': (0.16326768673564335+0j)}]]\n" + ] + } + ], + "source": [ + "h2 = Hydrogen_Chain(2, 1.0)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "011d3ad9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.52917721092 [] +\n", + "-1.110844179883727 [0^ 0] +\n", + "0.31320124976475894 [0^ 0^ 0 0] +\n", + "0.09839529174273509 [0^ 0^ 2 2] +\n", + "0.31320124976475894 [0^ 1^ 1 0] +\n", + "0.09839529174273509 [0^ 1^ 3 2] +\n", + "0.09839529174273509 [0^ 2^ 0 2] +\n", + "0.31085338155985653 [0^ 2^ 2 0] +\n", + "0.09839529174273509 [0^ 3^ 1 2] +\n", + "0.31085338155985653 [0^ 3^ 3 0] +\n", + "0.31320124976475894 [1^ 0^ 0 1] +\n", + "0.09839529174273509 [1^ 0^ 2 3] +\n", + "-1.110844179883727 [1^ 1] +\n", + "0.31320124976475894 [1^ 1^ 1 1] +\n", + "0.09839529174273509 [1^ 1^ 3 3] +\n", + "0.09839529174273509 [1^ 2^ 0 3] +\n", + "0.31085338155985653 [1^ 2^ 2 1] +\n", + "0.09839529174273509 [1^ 3^ 1 3] +\n", + "0.31085338155985653 [1^ 3^ 3 1] +\n", + "0.3108533815598567 [2^ 0^ 0 2] +\n", + "0.09839529174273509 [2^ 0^ 2 0] +\n", + "0.3108533815598567 [2^ 1^ 1 2] +\n", + "0.09839529174273509 [2^ 1^ 3 0] +\n", + "-0.5891210037060843 [2^ 2] +\n", + "0.09839529174273509 [2^ 2^ 0 0] +\n", + "0.3265353734712867 [2^ 2^ 2 2] +\n", + "0.09839529174273509 [2^ 3^ 1 0] +\n", + "0.3265353734712867 [2^ 3^ 3 2] +\n", + "0.3108533815598567 [3^ 0^ 0 3] +\n", + "0.09839529174273509 [3^ 0^ 2 1] +\n", + "0.3108533815598567 [3^ 1^ 1 3] +\n", + "0.09839529174273509 [3^ 1^ 3 1] +\n", + "0.09839529174273509 [3^ 2^ 0 1] +\n", + "0.3265353734712867 [3^ 2^ 2 3] +\n", + "-0.5891210037060843 [3^ 3] +\n", + "0.09839529174273509 [3^ 3^ 1 1] +\n", + "0.3265353734712867 [3^ 3^ 3 3]" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "h2.fermion_hamiltonian" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "90678d99", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "() 0.52917721092\n", + "((0, 1), (0, 0)) -1.110844179883727\n", + "((1, 1), (1, 0)) -1.110844179883727\n", + "((2, 1), (2, 0)) -0.5891210037060843\n", + "((3, 1), (3, 0)) -0.5891210037060843\n", + "((0, 1), (0, 1), (0, 0), (0, 0)) 0.31320124976475894\n", + "((0, 1), (0, 1), (2, 0), (2, 0)) 0.09839529174273509\n", + "((0, 1), (1, 1), (1, 0), (0, 0)) 0.31320124976475894\n", + "((0, 1), (1, 1), (3, 0), (2, 0)) 0.09839529174273509\n", + "((0, 1), (2, 1), (0, 0), (2, 0)) 0.09839529174273509\n", + "((0, 1), (2, 1), (2, 0), (0, 0)) 0.31085338155985653\n", + "((0, 1), (3, 1), (1, 0), (2, 0)) 0.09839529174273509\n", + "((0, 1), (3, 1), (3, 0), (0, 0)) 0.31085338155985653\n", + "((1, 1), (0, 1), (0, 0), (1, 0)) 0.31320124976475894\n", + "((1, 1), (0, 1), (2, 0), (3, 0)) 0.09839529174273509\n", + "((1, 1), (1, 1), (1, 0), (1, 0)) 0.31320124976475894\n", + "((1, 1), (1, 1), (3, 0), (3, 0)) 0.09839529174273509\n", + "((1, 1), (2, 1), (0, 0), (3, 0)) 0.09839529174273509\n", + "((1, 1), (2, 1), (2, 0), (1, 0)) 0.31085338155985653\n", + "((1, 1), (3, 1), (1, 0), (3, 0)) 0.09839529174273509\n", + "((1, 1), (3, 1), (3, 0), (1, 0)) 0.31085338155985653\n", + "((2, 1), (0, 1), (0, 0), (2, 0)) 0.3108533815598567\n", + "((2, 1), (0, 1), (2, 0), (0, 0)) 0.09839529174273509\n", + "((2, 1), (1, 1), (1, 0), (2, 0)) 0.3108533815598567\n", + "((2, 1), (1, 1), (3, 0), (0, 0)) 0.09839529174273509\n", + "((2, 1), (2, 1), (0, 0), (0, 0)) 0.09839529174273509\n", + "((2, 1), (2, 1), (2, 0), (2, 0)) 0.3265353734712867\n", + "((2, 1), (3, 1), (1, 0), (0, 0)) 0.09839529174273509\n", + "((2, 1), (3, 1), (3, 0), (2, 0)) 0.3265353734712867\n", + "((3, 1), (0, 1), (0, 0), (3, 0)) 0.3108533815598567\n", + "((3, 1), (0, 1), (2, 0), (1, 0)) 0.09839529174273509\n", + "((3, 1), (1, 1), (1, 0), (3, 0)) 0.3108533815598567\n", + "((3, 1), (1, 1), (3, 0), (1, 0)) 0.09839529174273509\n", + "((3, 1), (2, 1), (0, 0), (1, 0)) 0.09839529174273509\n", + "((3, 1), (2, 1), (2, 0), (3, 0)) 0.3265353734712867\n", + "((3, 1), (3, 1), (1, 0), (1, 0)) 0.09839529174273509\n", + "((3, 1), (3, 1), (3, 0), (3, 0)) 0.3265353734712867" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "h2.molecular_hamiltonian" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "87d9256d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(-0.3276081896748102+0j) [] +\n", + "(-0.049197645871367546+0j) [X0 X1 Y2 Y3] +\n", + "(0.049197645871367546+0j) [X0 Y1 Y2 X3] +\n", + "(0.049197645871367546+0j) [Y0 X1 X2 Y3] +\n", + "(-0.049197645871367546+0j) [Y0 Y1 X2 X3] +\n", + "(0.13716572937099497+0j) [Z0] +\n", + "(0.15660062488237947+0j) [Z0 Z1] +\n", + "(0.10622904490856078+0j) [Z0 Z2] +\n", + "(0.15542669077992832+0j) [Z0 Z3] +\n", + "(0.13716572937099497+0j) [Z1] +\n", + "(0.15542669077992832+0j) [Z1 Z2] +\n", + "(0.10622904490856078+0j) [Z1 Z3] +\n", + "(-0.13036292057109025+0j) [Z2] +\n", + "(0.16326768673564335+0j) [Z2 Z3] +\n", + "(-0.13036292057109025+0j) [Z3]" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "h2.jw" + ] + }, + { + "cell_type": "markdown", + "id": "f981124d", + "metadata": {}, + "source": [ + "## Misc" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5ceb9923", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.8.8 ('base')", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.14" + }, + "vscode": { + "interpreter": { + "hash": "4e8ef2f9fcac0817bca9a7ca376f64f20b4df5ea3bf7af756a50bda7d3557ea6" + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/setup.py b/setup.py index 23f99b7..41fd1bc 100644 --- a/setup.py +++ b/setup.py @@ -28,7 +28,7 @@ setuptools.setup( name="quantum-simulation-recipe", - version='0.1.1', + version='0.1.2', # version=package_version, # author="Jue XU", author_email="xujue@connect.hku.hk",