Skip to content
Snippets Groups Projects
Commit 90a541e5 authored by Isidora Araya's avatar Isidora Araya
Browse files

add instructions on how to save and load qsymm models

parent 212b7ec7
No related branches found
No related tags found
1 merge request!23Saving qsymm models
Checking pipeline status
......@@ -209,3 +209,24 @@ It is exactly the Hamiltonian family we started with.
For more detailed examples see :ref:`tutorial_kdotp_generator`, :ref:`tutorial_bloch_generator`
and :ref:`tutorial_kekule`.
Saving and loading Qsymm models
-------------------------------------------------
We can save and load Qsymm models.
To save we do:
.. jupyter-execute::
H2D_str = str(H2D.tosympy(nsimplify=True))
file = open("H2D.txt", "w")
file.write(H2D_str)
file.close()
To load we do:
.. jupyter-execute::
f = open('H2D.txt','r').read()
loaded_H2D = qsymm.Model(sympy.parsing.sympy_parser.parse_expr(f), momenta=['k_x', 'k_z'])
import numpy as np
import sympy
import re
import json
from astropy.utils.misc import JsonCustomEncoder
import qsymm
from icecream import ic
def bloch_model_to_json(model, name):
name = name + '.json'
model_dict = {str(key) : model[key].tolist() for key in model.keys()}
with open(name, 'w') as f:
json.dump(model_dict, f, cls = JsonCustomEncoder)
def bloch_model_from_json(name):
with open("json_models/" + name + ".json", "r") as f:
model = json.load(f, cls=JsonCustomDecoderExp)
return qsymm.Model(model, momenta=["k_x", "k_y"])
class JsonCustomDecoderExp(json.JSONDecoder):
"""
A decoder function tailored specifically to our purposes: take a dictionary of strings, output a dictionary
of symbolic keys (Mul types etc.) and arrays.
"""
def __init__(self, *args, **kwargs):
json.JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs)
def object_hook(self, obj):
new_dict = dict()
for key in obj.keys():
# back-convert the key from string to symbols
symbolic = []
# find exponents, works for arbitrary number of exponents
exp_full = re.findall('\*?e\*\*\(-?I\*k_\w\)\*?', key) # gives list with exponential terms in key
if exp_full:
coef = key
for exp in exp_full:
coef = coef.replace(exp, '') # constant coefficient
exp_power_pl = re.findall('\*?e\*\*\(I\*(k_\w)\)\*?', key) # gives list of positive exponents
exp_power_mn = re.findall('\*?e\*\*\(-I\*(k_\w)\)\*?', key) # gives list of negative exponents
if len(exp_power_pl)>=1:
sym_exp_pls = []
for exp in exp_power_pl:
sym_exp_pls.append(sympy.Pow(sympy.Symbol('e'), sympy.I*sympy.Symbol(exp)))
if len(exp_power_mn)==0: # no negative powers
symbolic_key = sympy.Mul(*sym_exp_pls, sympy.Symbol(coef))
if len(exp_power_mn)>=1:
sym_exp_mns = []
for exp in exp_power_mn:
sym_exp_mns.append(sympy.Pow(sympy.Symbol('e'), -sympy.I*sympy.Symbol(exp)))
if len(exp_power_pl)==0: # no positive powers
symbolic_key = sympy.Mul(*sym_exp_mns, sympy.Symbol(coef))
else: # both positive and negative powers
symbolic_key = sympy.Mul(*sym_exp_mns, *sym_exp_pls, sympy.Symbol(coef))
else:
symbolic_key = sympy.Symbol(key)
assert str(key)==str(symbolic_key) # make sure converter works
array = []
for line in obj[key]:
lineski = []
for entry in line:
lineski.append(entry[0] + 1j * entry[1])
array.append(lineski)
new_dict[symbolic_key] = np.array(array)
return new_dict
%% Cell type:code id:ec96970a-359f-4e6f-b2d9-529749253dab tags:
``` python
import numpy as np
import tinyarray
import qsymm
import sympy
from sympy import Matrix
import string
from convert_func import bloch_model_from_json, bloch_model_to_json
```
%% Cell type:code id:d7832d0c-1b99-47aa-9a40-00091eda980e tags:
``` python
sigma_x = tinyarray.array([[0, 1], [1, 0]])
sigma_y = tinyarray.array([[0, -1j], [1j, 0]])
sigma_z = tinyarray.array([[1, 0], [0, -1]])
#create qsymm model
ham = " cos(k_x) * sigma_z + sin(k_y) * eye(2) "
H = qsymm.Model(ham, momenta=["k_x", "k_y"])
H.tosympy()
```
%% Output
$\displaystyle \left[\begin{matrix}0.5 e^{i k_{x}} - 0.5 i e^{i k_{y}} + 0.5 i e^{- i k_{y}} + 0.5 e^{- i k_{x}} & 0\\0 & - 0.5 e^{i k_{x}} - 0.5 i e^{i k_{y}} + 0.5 i e^{- i k_{y}} - 0.5 e^{- i k_{x}}\end{matrix}\right]$
Matrix([
[0.5*e**(I*k_x) - 0.5*I*e**(I*k_y) + 0.5*I*e**(-I*k_y) + 0.5*e**(-I*k_x), 0],
[ 0, -0.5*e**(I*k_x) - 0.5*I*e**(I*k_y) + 0.5*I*e**(-I*k_y) - 0.5*e**(-I*k_x)]])
%% Cell type:code id:309fb778-7db3-4a02-a163-ab6479aa7711 tags:
``` python
#find symmetries
candidates = qsymm.groups.square()
discrete_symm, continuous_symm = qsymm.symmetries(H, candidates)
print(len(discrete_symm), len(continuous_symm))
abcd_sympy = list(sympy.symbols('a:z'))
abcd_list = list(string.ascii_lowercase)
del abcd_sympy[4]
del abcd_list[4]
norbs = [('A', 2)] # A atom per unit cell, 4 orbitals each
hopping_vectors = [('A', 'A', [0, 1]), ('A', 'A', [1, 0])]
#find all temrs compatible with 1 specific symmetry
family= qsymm.bloch_family(hopping_vectors, [discrete_symm[1]], norbs)
#add all the terms compatible with the symmetry
H = (H + sum([symbol * term for symbol, term in zip(abcd_sympy[3:], family)]))
H.tosympy(nsimplify=True)
```
%% Output
8 1
$\displaystyle \left[\begin{matrix}d + e^{i k_{x}} i + \frac{e^{i k_{x}}}{2} + e^{i k_{y}} m + i e^{i k_{y}} q - \frac{i e^{i k_{y}}}{2} + e^{- i k_{y}} m - i e^{- i k_{y}} q + \frac{i e^{- i k_{y}}}{2} + e^{- i k_{x}} i + \frac{e^{- i k_{x}}}{2} & e^{i k_{x}} j + i e^{i k_{x}} l + e^{i k_{y}} n + i e^{i k_{y}} r + f + i h + e^{- i k_{y}} o - i e^{- i k_{y}} s + e^{- i k_{x}} j + i e^{- i k_{x}} l\\e^{i k_{x}} j - i e^{i k_{x}} l + e^{i k_{y}} o + i e^{i k_{y}} s + f - i h + e^{- i k_{y}} n - i e^{- i k_{y}} r + e^{- i k_{x}} j - i e^{- i k_{x}} l & e^{i k_{x}} k - \frac{e^{i k_{x}}}{2} + e^{i k_{y}} p + i e^{i k_{y}} t - \frac{i e^{i k_{y}}}{2} + g + e^{- i k_{y}} p - i e^{- i k_{y}} t + \frac{i e^{- i k_{y}}}{2} + e^{- i k_{x}} k - \frac{e^{- i k_{x}}}{2}\end{matrix}\right]$
Matrix([
[d + e**(I*k_x)*i + e**(I*k_x)/2 + e**(I*k_y)*m + I*e**(I*k_y)*q - I*e**(I*k_y)/2 + e**(-I*k_y)*m - I*e**(-I*k_y)*q + I*e**(-I*k_y)/2 + e**(-I*k_x)*i + e**(-I*k_x)/2, e**(I*k_x)*j + I*e**(I*k_x)*l + e**(I*k_y)*n + I*e**(I*k_y)*r + f + I*h + e**(-I*k_y)*o - I*e**(-I*k_y)*s + e**(-I*k_x)*j + I*e**(-I*k_x)*l],
[ e**(I*k_x)*j - I*e**(I*k_x)*l + e**(I*k_y)*o + I*e**(I*k_y)*s + f - I*h + e**(-I*k_y)*n - I*e**(-I*k_y)*r + e**(-I*k_x)*j - I*e**(-I*k_x)*l, e**(I*k_x)*k - e**(I*k_x)/2 + e**(I*k_y)*p + I*e**(I*k_y)*t - I*e**(I*k_y)/2 + g + e**(-I*k_y)*p - I*e**(-I*k_y)*t + I*e**(-I*k_y)/2 + e**(-I*k_x)*k - e**(-I*k_x)/2]])
%% Cell type:code id:387f8212-1861-4875-8fc5-798cb5d83220 tags:
``` python
bloch_model_to_json(H, 'model')
```
%% Cell type:code id:de61fbf0-f6a6-43db-b32c-300e408fcbd3 tags:
``` python
```
%% Cell type:code id:f5cb9c98-cc14-448d-8a03-41df556ce3d2 tags:
``` python
```
{"e**(-I*k_x)": [[[0.5, 0.0], [0.0, 0.0]], [[0.0, 0.0], [-0.5, 0.0]]], "e**(-I*k_y)": [[[0.0, 0.5], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.5]]], "e**(I*k_x)": [[[0.5, 0.0], [0.0, 0.0]], [[0.0, 0.0], [-0.5, 0.0]]], "e**(I*k_y)": [[[0.0, -0.5], [0.0, 0.0]], [[0.0, 0.0], [0.0, -0.5]]], "e**(-I*k_x)*l": [[[-0.0, 0.0], [0.0, 1.0]], [[0.0, -1.0], [-0.0, 0.0]]], "e**(-I*k_x)*j": [[[0.0, 0.0], [1.0, 0.0]], [[1.0, 0.0], [0.0, 0.0]]], "e**(I*k_y)*s": [[[0.0, 0.0], [0.0, 0.0]], [[0.0, 1.0], [0.0, 0.0]]], "e**(-I*k_y)*o": [[[0.0, 0.0], [1.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], "e**(-I*k_y)*t": [[[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, -1.0]]], "e**(-I*k_y)*m": [[[1.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], "e**(I*k_y)*o": [[[0.0, 0.0], [0.0, 0.0]], [[1.0, 0.0], [0.0, 0.0]]], "e**(I*k_y)*t": [[[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 1.0]]], "e**(I*k_y)*m": [[[1.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], "e**(-I*k_y)*r": [[[0.0, 0.0], [0.0, 0.0]], [[0.0, -1.0], [0.0, 0.0]]], "e**(-I*k_y)*n": [[[0.0, 0.0], [0.0, 0.0]], [[1.0, 0.0], [0.0, 0.0]]], "e**(-I*k_x)*i": [[[1.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], "e**(I*k_x)*j": [[[0.0, 0.0], [1.0, 0.0]], [[1.0, 0.0], [0.0, 0.0]]], "e**(I*k_x)*l": [[[-0.0, 0.0], [0.0, 1.0]], [[0.0, -1.0], [-0.0, 0.0]]], "e**(-I*k_x)*k": [[[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [1.0, 0.0]]], "e**(-I*k_y)*p": [[[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [1.0, 0.0]]], "d": [[[1.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], "f": [[[0.0, 0.0], [1.0, 0.0]], [[1.0, 0.0], [0.0, 0.0]]], "e**(I*k_y)*n": [[[0.0, 0.0], [1.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], "h": [[[-0.0, 0.0], [0.0, 1.0]], [[0.0, -1.0], [-0.0, 0.0]]], "e**(I*k_y)*p": [[[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [1.0, 0.0]]], "e**(-I*k_y)*q": [[[0.0, -1.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], "e**(I*k_y)*r": [[[0.0, 0.0], [0.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]]], "g": [[[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [1.0, 0.0]]], "e**(I*k_y)*q": [[[0.0, 1.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], "e**(I*k_x)*i": [[[1.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], "e**(I*k_x)*k": [[[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [1.0, 0.0]]], "e**(-I*k_y)*s": [[[0.0, 0.0], [0.0, -1.0]], [[0.0, 0.0], [0.0, 0.0]]]}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment