Exemple #1
0
    def test_init(self):
        with pytest.raises(AssertionError):
            SiteID(site=[0, 0, 0, 0])
        with pytest.raises(AssertionError):
            SiteID(site=[0, "a"])

        site_id = SiteID(site=[1.2, 2.3])
        assert np.all(site_id.site == (1.2, 2.3))
        assert np.all(site_id.coordinate == (1.2, 2.3))
        assert repr(site_id) == "SiteID(site=(1.2, 2.3))"
Exemple #2
0
    def test_matrix_repr(self):
        sites = np.array([[0, 0], [1, 1]])
        site_indices_table = IndexTable(SiteID(site=site) for site in sites)
        SX = np.array([[0, 0.5], [0.5, 0]])
        SY = np.array([[0, -0.5j], [0.5j, 0]])
        SZ = np.array([[0.5, 0], [0, -0.5]])
        I = np.array([[1, 0], [0, 1]])

        for otype, SMatrix in zip(["x", "y", "z"], [SX, SY, SZ]):
            s0 = SpinOperator(otype, site=sites[0])
            s1 = SpinOperator(otype, site=sites[1])
            M = SpinInteraction(
                (s0, s1)).matrix_repr(site_indices_table).toarray()
            assert np.all(M == np.kron(SMatrix, SMatrix))

        sx0 = SpinOperator("x", site=sites[0])
        sy0 = SpinOperator("y", site=sites[0])
        sz0 = SpinOperator("z", site=sites[0])

        M = SpinInteraction(
            (sx0, sy0)).matrix_repr(site_indices_table).toarray()
        assert np.all(M == np.kron(I, np.dot(SX, SY)))

        M = SpinInteraction(
            (sy0, sz0)).matrix_repr(site_indices_table).toarray()
        assert np.all(M == np.kron(I, np.dot(SY, SZ)))

        M = SpinInteraction(
            (sx0, sz0)).matrix_repr(site_indices_table).toarray()
        assert np.all(M == np.kron(I, np.dot(SX, SZ)))
Exemple #3
0
def test_set_float_point_precision():
    with pytest.raises(AssertionError):
        set_float_point_precision(1.0)
    with pytest.raises(AssertionError):
        set_float_point_precision(-1)

    set_float_point_precision(precision=4)
    site_id0 = SiteID(site=[1.2, 2.3])
    assert site_id0._tuple_form == (12000, 23000)

    set_float_point_precision(precision=6)
    site_id1 = SiteID(site=[np.sqrt(2), np.sqrt(3)])
    # Instance that already exists does not affected;
    # Newly created instance use the new precision.
    assert site_id0._tuple_form == (12000, 23000)
    assert site_id1._tuple_form == (1414213, 1732050)

    # Restore the default value
    set_float_point_precision()
Exemple #4
0
    def test_init(self):
        site = (1.2, 2.3)
        with pytest.raises(AssertionError, match="Invalid operator type"):
            SpinOperator(otype="X", site=site)

        sx = SpinOperator(otype="x", site=site)
        assert sx.otype == "x"
        assert sx.site_id == SiteID(site=site)
        assert sx.coordinate == site
        assert np.all(sx.site == site)
        assert repr(sx) == 'SpinOperator(otype="x", site=(1.2, 2.3))'
Exemple #5
0
    def test_comparison(self):
        sites = np.array([[0, 0], [0.25, np.sqrt(3) / 4], [0.5, 0]])
        site_id0 = SiteID(site=sites[0])
        site_id1 = SiteID(site=sites[1])
        site_id2 = SiteID(site=sites[2])

        with pytest.raises(TypeError, match="'<' not supported"):
            trash = site_id0 < 0
        with pytest.raises(TypeError, match="'<=' not supported"):
            trash = site_id1 <= 0
        with pytest.raises(TypeError, match="'>' not supported"):
            trash = site_id2 > 0
        with pytest.raises(TypeError, match="'>=' not supported"):
            trash = site_id0 >= 0

        assert not (site_id0 == 0)
        assert site_id1 != 0

        assert site_id0 < site_id1 < site_id2
        assert site_id2 > site_id1 > site_id0
        assert site_id0 == SiteID(site=[1E-8, 1E-7])
        assert site_id0 != site_id1
Exemple #6
0
    def test_matrix_repr(self):
        sites = np.array([[0, 0], [1, 1]])
        site_indices_table = IndexTable(SiteID(site=site) for site in sites)

        sx = SpinOperator("x", site=sites[0])
        M = sx.matrix_repr(site_indices_table).toarray()
        M_ref = np.array([[0, 0.5, 0, 0], [0.5, 0, 0, 0], [0, 0, 0, 0.5],
                          [0, 0, 0.5, 0]])
        assert np.all(M == M_ref)

        sy = SpinOperator("y", site=sites[1])
        M = sy.matrix_repr(site_indices_table).toarray()
        M_ref = np.array([[0, 0, -0.5j, 0], [0, 0, 0, -0.5j], [0.5j, 0, 0, 0],
                          [0, 0.5j, 0, 0]])
        assert np.all(M == M_ref)
Exemple #7
0
    def __init__(self, otype, site):
        """
        Customize the newly created instance.

        Parameters
        ----------
        otype : {"x", "y", "z", "p" or "m"}
            The type of this spin operator.
        site : list, tuple or 1D np.ndarray
            The coordinates of the lattice site on which the spin operator is
            defined. The `site` parameter should be 1D array with length 1,
            2 or 3.
        """

        assert otype in SPIN_OTYPES, "Invalid operator type"

        site_id = SiteID(site=site)
        self._otype = otype
        self._site_id = site_id
        # The tuple form of this instance
        # It is a tuple: (otype, site) and site itself is a tuple with length
        # 1, 2 or 3.
        self._tuple_form = (otype, site_id._tuple_form)
Exemple #8
0
    def test_hash(self):
        site0 = [0, 0]
        site1 = [1E-6, 1E-6]
        site2 = [1E-4, 1E-4]

        site_id0 = SiteID(site=site0)
        site_id1 = SiteID(site=site1)
        site_id2 = SiteID(site=site2)
        assert hash(site_id0) == hash(site_id1)
        assert hash(site_id0) != hash(site_id2)
        assert hash(site_id1) != hash(site_id2)

        set_float_point_precision(precision=6)
        site_id0 = SiteID(site=site0)
        site_id1 = SiteID(site=site1)
        site_id2 = SiteID(site=site2)
        assert hash(site_id0) != hash(site_id1)
        assert hash(site_id0) != hash(site_id2)
        assert hash(site_id1) != hash(site_id2)

        # Restore the default value
        set_float_point_precision()
Exemple #9
0
 def test_getIndex(self):
     sites = np.random.random((10, 3))
     site_indices_table = IndexTable(SiteID(site=site) for site in sites)
     for index, site_id in site_indices_table:
         assert index == site_id.getIndex(site_indices_table)