Skip to content

Commit

Permalink
update spin API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jue-Xu committed Jul 21, 2024
1 parent ada55be commit a2b9f4b
Show file tree
Hide file tree
Showing 7 changed files with 481 additions and 135 deletions.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
122 changes: 122 additions & 0 deletions quantum_simulation_recipe/spin.py
Original file line number Diff line number Diff line change
@@ -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()
130 changes: 0 additions & 130 deletions quantum_simulation_recipe/spin_ham.py

This file was deleted.

Loading

0 comments on commit a2b9f4b

Please sign in to comment.