Exemple #1
0
def test_inverse_transform():
    # Define family on square lattice
    s = spin_matrices(1 / 2)
    # Time reversal
    TR = PointGroupElement(
        np.eye(2), True, False,
        spin_rotation(2 * np.pi * np.array([0, 1 / 2, 0]), s))
    # Mirror symmetry
    Mx = PointGroupElement(
        np.array([[-1, 0], [0, 1]]), False, False,
        spin_rotation(2 * np.pi * np.array([1 / 2, 0, 0]), s))
    # Fourfold
    C4 = PointGroupElement(
        np.array([[0, 1], [-1, 0]]), False, False,
        spin_rotation(2 * np.pi * np.array([0, 0, 1 / 4]), s))
    symmetries = [TR, Mx, C4]

    # One site per unit cell
    norbs = OrderedDict([('A', 2)])
    # Hopping to a neighbouring atom one primitive lattice vector away
    hopping_vectors = [('A', 'A', [1, 0])]
    # Make family
    family = bloch_family(hopping_vectors, symmetries, norbs)
    fam = hamiltonian_from_family(family, tosympy=False)
    # Atomic coordinates within the unit cell
    atom_coords = [(0, 0)]
    lat_vecs = [(1, 0), (0, 1)]
    syst = model_to_builder(fam, norbs, lat_vecs, atom_coords)
    # Convert it back
    ham2 = builder_to_model(syst).tomodel(nsimplify=True)
    # Check that it's the same as the original
    assert fam == ham2

    # Check that the Hamiltonians are identical at random points in the Brillouin zone
    sysw = kwant.wraparound.wraparound(syst).finalized()
    H1 = sysw.hamiltonian_submatrix
    H2 = ham2.lambdify()
    H3 = fam.lambdify()
    coeffs = 0.5 + np.random.rand(3)
    for _ in range(20):
        kx, ky = 3 * np.pi * (np.random.rand(2) - 0.5)
        params = dict(c0=coeffs[0], c1=coeffs[1], c2=coeffs[2], k_x=kx, k_y=ky)
        assert allclose(H1(params=params), H2(**params))
        assert allclose(H1(params=params), H3(**params))
Exemple #2
0
def test_graphene_to_kwant():

    norbs = OrderedDict([('A', 1), ('B', 1)
                         ])  # A and B atom per unit cell, one orbital each
    hopping_vectors = [('A', 'B', [1, 0])
                       ]  # Hopping between neighbouring A and B atoms
    # Atomic coordinates within the unit cell
    atom_coords = [(0, 0), (1, 0)]
    # We set the interatom distance to 1, so the lattice vectors have length sqrt(3)
    lat_vecs = [(3 / 2, np.sqrt(3) / 2), (3 / 2, -np.sqrt(3) / 2)]

    # Time reversal
    TR = PointGroupElement(sympy.eye(2), True, False, np.eye(2))
    # Chiral symmetry
    C = PointGroupElement(sympy.eye(2), False, True, np.array([[1, 0], [0,
                                                                        -1]]))
    # Atom A rotates into A, B into B.
    sphi = 2 * sympy.pi / 3
    RC3 = sympy.Matrix([[sympy.cos(sphi), -sympy.sin(sphi)],
                        [sympy.sin(sphi), sympy.cos(sphi)]])
    C3 = PointGroupElement(RC3, False, False, np.eye(2))

    # Generate graphene Hamiltonian in Kwant from qsymm
    symmetries = [C, TR, C3]
    # Generate using a family
    family = bloch_family(hopping_vectors, symmetries, norbs)
    syst_from_family = model_to_builder(family,
                                        norbs,
                                        lat_vecs,
                                        atom_coords,
                                        coeffs=None)
    # Generate using a single Model object
    g = sympy.Symbol('g', real=True)
    ham = hamiltonian_from_family(family, coeffs=[g])
    ham = Model(hamiltonian=ham, momenta=family[0].momenta)
    syst_from_model = model_to_builder(ham, norbs, lat_vecs, atom_coords)

    # Make the graphene Hamiltonian using kwant only
    atoms, orbs = zip(*[(atom, norb) for atom, norb in norbs.items()])
    # Make the kwant lattice
    lat = kwant.lattice.general(lat_vecs, atom_coords, norbs=orbs)
    # Store sublattices by name
    sublattices = {
        atom: sublat
        for atom, sublat in zip(atoms, lat.sublattices)
    }

    sym = kwant.TranslationalSymmetry(*lat_vecs)
    bulk = kwant.Builder(sym)

    bulk[[sublattices['A'](0, 0), sublattices['B'](0, 0)]] = 0

    def hop(site1, site2, c0):
        return c0

    bulk[lat.neighbors()] = hop

    fsyst_family = kwant.wraparound.wraparound(syst_from_family).finalized()
    fsyst_model = kwant.wraparound.wraparound(syst_from_model).finalized()
    fsyst_kwant = kwant.wraparound.wraparound(bulk).finalized()

    # Check that the energies are identical at random points in the Brillouin zone
    coeff = 0.5 + np.random.rand()
    for _ in range(20):
        kx, ky = 3 * np.pi * (np.random.rand(2) - 0.5)
        params = dict(c0=coeff, k_x=kx, k_y=ky)
        hamiltonian1 = fsyst_kwant.hamiltonian_submatrix(params=params,
                                                         sparse=False)
        hamiltonian2 = fsyst_family.hamiltonian_submatrix(params=params,
                                                          sparse=False)
        assert allclose(hamiltonian1, hamiltonian2)
        params = dict(g=coeff, k_x=kx, k_y=ky)
        hamiltonian3 = fsyst_model.hamiltonian_submatrix(params=params,
                                                         sparse=False)
        assert allclose(hamiltonian2, hamiltonian3)

    # Include random onsites as well
    one = sympy.numbers.One()
    onsites = [
        Model({one: np.array([[1, 0], [0, 0]])}, momenta=family[0].momenta),
        Model({one: np.array([[0, 0], [0, 1]])}, momenta=family[0].momenta)
    ]
    family = family + onsites
    syst_from_family = model_to_builder(family,
                                        norbs,
                                        lat_vecs,
                                        atom_coords,
                                        coeffs=None)
    gs = list(sympy.symbols('g0:%d' % 3, real=True))
    ham = hamiltonian_from_family(family, coeffs=gs)
    ham = Model(hamiltonian=ham, momenta=family[0].momenta)
    syst_from_model = model_to_builder(ham, norbs, lat_vecs, atom_coords)

    def onsite_A(site, c1):
        return c1

    def onsite_B(site, c2):
        return c2

    bulk[[sublattices['A'](0, 0)]] = onsite_A
    bulk[[sublattices['B'](0, 0)]] = onsite_B

    fsyst_family = kwant.wraparound.wraparound(syst_from_family).finalized()
    fsyst_model = kwant.wraparound.wraparound(syst_from_model).finalized()
    fsyst_kwant = kwant.wraparound.wraparound(bulk).finalized()

    # Check equivalence of the Hamiltonian at random points in the BZ
    coeffs = 0.5 + np.random.rand(3)
    for _ in range(20):
        kx, ky = 3 * np.pi * (np.random.rand(2) - 0.5)
        params = dict(c0=coeffs[0], c1=coeffs[1], c2=coeffs[2], k_x=kx, k_y=ky)
        hamiltonian1 = fsyst_kwant.hamiltonian_submatrix(params=params,
                                                         sparse=False)
        hamiltonian2 = fsyst_family.hamiltonian_submatrix(params=params,
                                                          sparse=False)
        assert allclose(hamiltonian1, hamiltonian2)
        params = dict(g0=coeffs[0], g1=coeffs[1], g2=coeffs[2], k_x=kx, k_y=ky)
        hamiltonian3 = fsyst_model.hamiltonian_submatrix(params=params,
                                                         sparse=False)
        assert allclose(hamiltonian2, hamiltonian3)
Exemple #3
0
def model_to_builder(model, norbs, lat_vecs, atom_coords, *, coeffs=None):
    """Make a `~kwant.builder.Builder` out of qsymm.Models or qsymm.BlochModels.

    Parameters
    ----------
    model : qsymm.Model, qsymm.BlochModel, or an iterable thereof
        The Hamiltonian (or terms of the Hamiltonian) to convert to a
        Builder.
    norbs : OrderedDict or sequence of pairs
        Maps sites to the number of orbitals per site in a unit cell.
    lat_vecs : list of arrays
        Lattice vectors of the underlying tight binding lattice.
    atom_coords : list of arrays
        Positions of the sites (or atoms) within a unit cell.
        The ordering of the atoms is the same as in norbs.
    coeffs : list of sympy.Symbol, default None.
        Constant prefactors for the individual terms in model, if model
        is a list of multiple objects. If model is a single Model or BlochModel
        object, this argument is ignored. By default assigns the coefficient
        c_n to element model[n].

    Returns
    -------
    syst : `~kwant.builder.Builder`
        The unfinalized Kwant system representing the qsymm Model(s).

    Notes
    -----
    Onsite terms that are not provided in the input model are set
    to zero by default.

    The input model(s) representing the tight binding Hamiltonian in
    Bloch form should follow the convention where the difference in the real
    space atomic positions appear in the Bloch factors.
    """

    def make_int(R):
        # If close to an integer array convert to integer tinyarray, else
        # return None
        R_int = ta.array(np.round(R), int)
        if qsymm.linalg.allclose(R, R_int):
            return R_int
        else:
            return None

    def term_onsite(onsites_dict, hopping_dict, hop_mat, atoms,
                    sublattices, coords_dict):
        """Find the Kwant onsites and hoppings in a qsymm.BlochModel term
        that has no lattice translation in the Bloch factor.
        """
        for atom1, atom2 in it.product(atoms, atoms):
            # Subblock within the same sublattice is onsite
            hop = hop_mat[ranges[atom1], ranges[atom2]]
            if sublattices[atom1] == sublattices[atom2]:
                onsites_dict[atom1] += Model({coeff: hop}, momenta=momenta)
            # Blocks between sublattices are hoppings between sublattices
            # at the same position.
            # Only include nonzero hoppings
            elif not allclose(hop, 0):
                if not allclose(np.array(coords_dict[atom1]),
                                np.array(coords_dict[atom2])):
                    raise ValueError(
                        "Position of sites not compatible with qsymm model.")
                lat_basis = np.array(zer)
                hop = Model({coeff: hop}, momenta=momenta)
                hop_dir = builder.HoppingKind(-lat_basis, sublattices[atom1],
                                              sublattices[atom2])
                hopping_dict[hop_dir] += hop
        return onsites_dict, hopping_dict

    def term_hopping(hopping_dict, hop_mat, atoms,
                     sublattices, coords_dict):
        """Find Kwant hoppings in a qsymm.BlochModel term that has a lattice
        translation in the Bloch factor.
        """
        # Iterate over combinations of atoms, set hoppings between each
        for atom1, atom2 in it.product(atoms, atoms):
            # Take the block from atom1 to atom2
            hop = hop_mat[ranges[atom1], ranges[atom2]]
            # Only include nonzero hoppings
            if allclose(hop, 0):
                continue
            # Adjust hopping vector to Bloch form basis
            r_lattice = (
                r_vec
                + np.array(coords_dict[atom1])
                - np.array(coords_dict[atom2])
            )
            # Bring vector to basis of lattice vectors
            lat_basis = np.linalg.solve(np.vstack(lat_vecs).T, r_lattice)
            lat_basis = make_int(lat_basis)
            # Should only have hoppings that are integer multiples of
            # lattice vectors
            if lat_basis is not None:
                hop_dir = builder.HoppingKind(-lat_basis,
                                              sublattices[atom1],
                                              sublattices[atom2])
                # Set the hopping as the matrix times the hopping amplitude
                hopping_dict[hop_dir] += Model({coeff: hop}, momenta=momenta)
            else:
                raise RuntimeError('A nonzero hopping not matching a '
                                   'lattice vector was found.')
        return hopping_dict

    # Disambiguate single model instances from iterables thereof. Because
    # Model is itself iterable (subclasses dict) this is a bit cumbersome.
    if isinstance(model, Model):
        # BlochModel can't yet handle getting a Blochmodel as input
        if not isinstance(model, BlochModel):
            model = BlochModel(model)
    else:
        model = BlochModel(hamiltonian_from_family(
            model, coeffs=coeffs, nsimplify=False, tosympy=False))


    # 'momentum' and 'zer' are used in the closures defined above, so don't
    # move these declarations down.
    momenta = model.momenta
    if len(momenta) != len(lat_vecs):
        raise ValueError("Dimension of the lattice and number of "
                         "momenta do not match.")
    zer = [0] * len(momenta)


    # Subblocks of the Hamiltonian for different atoms.
    N = 0
    if not any([isinstance(norbs, OrderedDict), isinstance(norbs, list),
                isinstance(norbs, tuple)]):
        raise ValueError('norbs must be OrderedDict, tuple, or list.')
    else:
        norbs = OrderedDict(norbs)
    ranges = dict()
    for a, n in norbs.items():
        ranges[a] = slice(N, N + n)
        N += n

    # Extract atoms and number of orbitals per atom,
    # store the position of each atom
    atoms, orbs = zip(*norbs.items())
    coords_dict = dict(zip(atoms, atom_coords))

    # Make the kwant lattice
    lat = lattice.general(lat_vecs, atom_coords, norbs=orbs)
    # Store sublattices by name
    sublattices = dict(zip(atoms, lat.sublattices))

    # Keep track of the hoppings and onsites by storing those
    # which have already been set.
    hopping_dict = defaultdict(dict)
    onsites_dict = defaultdict(dict)

    # Iterate over all terms in the model.
    for key, hop_mat in model.items():
        # Determine whether this term is an onsite or a hopping, extract
        # overall symbolic coefficient if any, extract the exponential
        # part describing the hopping if present.
        r_vec, coeff = key
        # Onsite term; modifies onsites_dict and hopping_dict in-place
        if allclose(r_vec, 0):
            term_onsite(
                onsites_dict, hopping_dict, hop_mat,
                atoms, sublattices, coords_dict)
        # Hopping term; modifies hopping_dict in-place
        else:
            term_hopping(hopping_dict, hop_mat, atoms,
                         sublattices, coords_dict)

    # If some onsite terms are not set, we set them to zero.
    for atom in atoms:
        if atom not in onsites_dict:
            onsites_dict[atom] = Model(
                {sympy.numbers.One(): np.zeros((norbs[atom], norbs[atom]))},
                momenta=momenta)

    # Make the Kwant system, and set all onsites and hoppings.

    sym = lattice.TranslationalSymmetry(*lat_vecs)
    syst = builder.Builder(sym)

    # Iterate over all onsites and set them
    for atom, onsite in onsites_dict.items():
        syst[sublattices[atom](*zer)] = onsite.lambdify(onsite=True)

    # Finally, iterate over all the hoppings and set them
    for direction, hopping in hopping_dict.items():
        syst[direction] = hopping.lambdify(hopping=True)

    return syst