Commit 5fa767d1 authored by Joseph Weston's avatar Joseph Weston

Merge branch 'feature/deprecate-args' into 'master'

Deprecate 'args' parameter in favor of 'params'

Closes #272

See merge request !280
parents e9557cf7 d44ecee2
Pipeline #15424 passed with stages
in 42 minutes and 58 seconds
......@@ -143,7 +143,7 @@
normalized_fluxes = [flux / (2 * pi) for flux in fluxes]
data = []
for flux in fluxes:
smatrix = kwant.smatrix(syst, energy, args=[flux])
smatrix = kwant.smatrix(syst, energy, params=dict(phi=flux))
data.append(smatrix.transmission(1, 0))
- pyplot.figure()
......
......@@ -68,7 +68,7 @@
energies = []
for B in Bfields:
# Obtain the Hamiltonian as a dense matrix
ham_mat = syst.hamiltonian_submatrix(args=[B], sparse=True)
ham_mat = syst.hamiltonian_submatrix(params=dict(B=B), sparse=True)
# we only calculate the 15 lowest eigenvalues
ev = sla.eigsh(ham_mat.tocsc(), k=15, sigma=0,
......@@ -105,7 +105,7 @@
+ size = (_defs.figwidth_in, _defs.figwidth_in)
+
# Calculate the wave functions in the system.
ham_mat = syst.hamiltonian_submatrix(sparse=True, args=[B])
ham_mat = syst.hamiltonian_submatrix(sparse=True, params=dict(B=B))
evals, evecs = sorted_eigs(sla.eigsh(ham_mat.tocsc(), k=20, sigma=0))
# Plot the probability density of the 10th eigenmode.
......@@ -124,12 +124,12 @@
+ size = (_defs.figwidth_in, _defs.figwidth_in)
+
# Calculate the wave functions in the system.
ham_mat = syst.hamiltonian_submatrix(sparse=True, args=[B])
ham_mat = syst.hamiltonian_submatrix(sparse=True, params=dict(B=B))
evals, evecs = sorted_eigs(sla.eigsh(ham_mat.tocsc(), k=20, sigma=0))
# Calculate and plot the local current of the 10th eigenmode.
J = kwant.operator.Current(syst)
current = J(evecs[:, 9], args=[B])
current = J(evecs[:, 9], params=dict(B=B))
- kwant.plotter.current(syst, current, colorbar=False)
+ for extension in ('pdf', 'png'):
+ kwant.plotter.current(
......
......@@ -59,7 +59,7 @@
# Compute conductance
data = []
for welldepth in welldepths:
smatrix = kwant.smatrix(syst, energy, args=[-welldepth])
smatrix = kwant.smatrix(syst, energy, params=dict(pot=-welldepth))
data.append(smatrix.transmission(1, 0))
- pyplot.figure()
......
......@@ -170,6 +170,27 @@ parameters that the system (and its leads) expects::
This is a provisional API that may be changed in a future version of Kwant.
Passing system arguments via ``args`` is deprecated in favor of ``params``
--------------------------------------------------------------------------
It is now deprecated to pass arguments to systems by providing the
``args`` parameter (in ``kwant.smatrix`` and elsewhere). This is
error prone and requires that all value functions take the same
formal parameters, even if they do not depend on all of them. The
preferred way of passing parameters to Kwant systems is by passing
a dictionary using ``params``::
def onsite(site, magnetic_field, voltage):
return magnetic_field * sigma_z + voltage * sigma_0
syst = make_system(onsite).finalized()
kwant.smatrix(syst, params=dict(magnetic_field=0.5, voltage=0.2))
# Compare this to the deprecated 'args'
kwant.smatrix(syst, args=(0.5, 0.2))
The ability to provide ``args`` will be removed in a future Kwant version.
Interpolated density plots
--------------------------
A new function `~kwant.plotter.density` has been added that can be used to
......
......@@ -160,9 +160,9 @@ energy eigenstates:
.. image:: /code/figure/discretizer_gs.*
Note in the above that we provided the function ``V`` to
``syst.hamiltonian_submatrix`` using ``params=dict(V=potential)``, rather than
via ``args``.
Note in the above that we pass the spatially varying potential *function*
to our system via a parameter called ``V``, because the symbol $V$
was used in the intial, symbolic, definition of the Hamiltonian.
In addition, the function passed as ``V`` expects two input parameters ``x``
and ``y``, the same as in the initial continuum Hamiltonian.
......
......@@ -184,10 +184,12 @@ Finally, we compute the transmission probability:
:start-after: #HIDDEN_BEGIN_sqvr
:end-before: #HIDDEN_END_sqvr
``kwant.smatrix`` allows us to specify a list, `args`, that will be passed as
additional arguments to the functions that provide the Hamiltonian matrix
elements. In this example we are able to solve the system for different depths
of the potential well by passing the potential value. We obtain the result:
``kwant.smatrix`` allows us to specify a dictionary, `params`, that contains
the additional arguments required by the Hamiltonian matrix elements.
In this example we are able to solve the system for different depths
of the potential well by passing the potential value (remember above
we defined our `onsite` function that takes a parameter named `pot`).
We obtain the result:
.. image:: /code/figure/quantum_well_result.*
......
......@@ -12,6 +12,7 @@ import numbers
import inspect
import warnings
import importlib
import functools
from contextlib import contextmanager
__all__ = ['KwantDeprecationWarning', 'UserCodeError']
......@@ -41,6 +42,46 @@ class UserCodeError(Exception):
pass
def deprecate_parameter(parameter_name, version=None, help=None,
stacklevel=3):
"""Trigger a deprecation warning if the wrapped function is called
with the provided parameter."""
message = ("The '{}' parameter has been deprecated since version {} -- {}"
.format(parameter_name, version, help))
def warn():
warnings.warn(message, KwantDeprecationWarning,
stacklevel=stacklevel)
def wrapper(f=None):
# Instead of being used as a decorator, can be called with
# no arguments to just raise the warning.
if f is None:
warn()
return
sig = inspect.signature(f)
@functools.wraps(f)
def inner(*args, **kwargs):
# If the named argument is truthy
if sig.bind(*args, **kwargs).arguments.get(parameter_name):
warn()
return f(*args, **kwargs)
return inner
return wrapper
# Deprecation for 'args' parameter; defined once to minimize boilerplate,
# as this parameter is present all over Kwant.
deprecate_args = deprecate_parameter('args', version=1.4, help=
"Instead, provide named parameters as a dictionary via 'params'.")
def interleave(seq):
"""Return an iterator that yields pairs of elements from a sequence.
......
......@@ -16,6 +16,7 @@ import types
from .graph.core cimport CGraph, gintArraySlice
from .graph.defs cimport gint
from .graph.defs import gint_dtype
from ._common import deprecate_args
msg = ('Hopping from site {0} to site {1} does not match the '
'dimensions of onsite Hamiltonians of these sites.')
......@@ -253,6 +254,7 @@ def _check_parameters_match(expected_parameters, params):
raise TypeError(''.join(msg))
@deprecate_args
@cython.binding(True)
@cython.embedsignature(True)
def hamiltonian_submatrix(self, args=(), to_sites=None, from_sites=None,
......
......@@ -22,7 +22,7 @@ from .linalg import lll
from .operator import Density
from .physics import DiscreteSymmetry
from ._common import (ensure_isinstance, get_parameters, reraise_warnings,
interleave)
interleave, deprecate_args)
__all__ = ['Builder', 'Site', 'SiteFamily', 'SimpleSiteFamily', 'Symmetry',
......@@ -617,6 +617,7 @@ def _ensure_signature(func):
return func
# function conforming to old API: needs wrapping
@deprecate_args
def wrapper(energy, args=(), *, params=None):
return func(energy, args)
......@@ -647,6 +648,7 @@ class SelfEnergyLead(Lead):
"""Trivial finalization: the object is returned itself."""
return self
@deprecate_args
def selfenergy(self, energy, args=(), *, params=None):
return self.selfenergy_func(energy, args, params=params)
......@@ -677,9 +679,11 @@ class ModesLead(Lead):
"""Trivial finalization: the object is returned itself."""
return self
@deprecate_args
def modes(self, energy, args=(), *, params=None):
return self.modes_func(energy, args, params=params)
@deprecate_args
def selfenergy(self, energy, args=(), *, params=None):
stabilized = self.modes(energy, args, params=params)[1]
return stabilized.selfenergy()
......@@ -1894,6 +1898,7 @@ class _FinalizedBuilderMixin:
value = herm_conj(value)
return value
@deprecate_args
def discrete_symmetry(self, args=(), *, params=None):
if self._cons_law is not None:
eigvals, eigvecs = self._cons_law
......
......@@ -24,7 +24,7 @@ from .graph.core cimport EdgeIterator
from .graph.defs cimport gint
from .graph.defs import gint_dtype
from .system import InfiniteSystem
from ._common import UserCodeError, get_parameters
from ._common import UserCodeError, get_parameters, deprecate_args
################ Generic Utility functions
......@@ -499,7 +499,7 @@ cdef class _LocalOperator:
args : tuple, optional
The arguments to pass to the system. Used to evaluate
the ``onsite`` elements and, possibly, the system Hamiltonian.
Mutually exclusive with 'params'.
Deprecated in favor of 'params' (and mutually exclusive with it).
params : dict, optional
Dictionary of parameter names and their values. Mutually exclusive
with 'args'.
......@@ -514,6 +514,11 @@ cdef class _LocalOperator:
raise ValueError("Extra arguments are already bound to this "
"operator. You should call this operator "
"providing neither 'args' nor 'params'.")
if args:
# deprecate_args does not play nicely with methods of cdef classes,
# when used as a decorator, so we manually raise the
# deprecation warning here.
deprecate_args()
if args and params:
raise TypeError("'args' and 'params' are mutually exclusive.")
if bra is None:
......@@ -555,7 +560,8 @@ cdef class _LocalOperator:
Wavefunctions defined over all the orbitals of the system.
args : tuple
The extra arguments to the Hamiltonian value functions and
the operator ``onsite`` function. Mutually exclusive with 'params'.
the operator ``onsite`` function.
Deprecated in favor of 'params' (and mutually exclusive with it).
params : dict, optional
Dictionary of parameter names and their values. Mutually exclusive
with 'args'.
......@@ -568,6 +574,11 @@ cdef class _LocalOperator:
raise ValueError("Extra arguments are already bound to this "
"operator. You should call this operator "
"providing neither 'args' nor 'params'.")
if args:
# deprecate_args does not play nicely with methods of cdef classes,
# when used as a decorator, so we manually raise the
# deprecation warning here.
deprecate_args()
if args and params:
raise TypeError("'args' and 'params' are mutually exclusive.")
......@@ -588,7 +599,15 @@ cdef class _LocalOperator:
Returns a copy of this operator that does not need to be passed extra
arguments when subsequently called or when using the ``act`` method.
Providing positional arguments via 'args' is deprecated,
instead provide named parameters as a dictionary via 'params'.
"""
if args:
# deprecate_args does not play nicely with methods of cdef classes,
# when used as a decorator, so we manually raise the
# deprecation warning here.
deprecate_args()
if args and params:
raise TypeError("'args' and 'params' are mutually exclusive.")
# generic creation of new instance
......@@ -621,7 +640,8 @@ cdef class _LocalOperator:
If `op` is `ACT` then `bra` is None.
args : tuple
The extra arguments to the Hamiltonian value functions and
the operator ``onsite`` function. Mutually exclusive with 'params'.
the operator ``onsite`` function.
Deprecated in favor of 'params' (and mutually exclusive with it).
op : operation
The operation to perform.
`MAT_ELS`: calculate matrix elements between `bra` and `ket`
......@@ -786,7 +806,11 @@ cdef class Density(_LocalOperator):
@cython.cdivision(True)
@cython.embedsignature
def tocoo(self, args=(), *, params=None):
"""Convert the operator to coordinate format sparse matrix."""
"""Convert the operator to coordinate format sparse matrix.
Providing positional arguments via 'args' is deprecated,
instead provide named parameters as a dictionary via 'params'.
"""
cdef int blk, blk_size, n_blocks, n, k = 0
cdef int [:, :] offsets, shapes
cdef int [:] row, col
......@@ -794,6 +818,11 @@ cdef class Density(_LocalOperator):
raise ValueError("Extra arguments are already bound to this "
"operator. You should call this operator "
"providing neither 'args' nor 'params'.")
if args:
# deprecate_args does not play nicely with methods of cdef classes,
# when used as a decorator, so we manually raise the
# deprecation warning here.
deprecate_args()
if args and params:
raise TypeError("'args' and 'params' are mutually exclusive.")
......@@ -887,6 +916,9 @@ cdef class Current(_LocalOperator):
Returns a copy of this operator that does not need to be passed extra
arguments when subsequently called or when using the ``act`` method.
Providing positional arguments via 'args' is deprecated,
instead provide named parameters as a dictionary via 'params'.
"""
q = super().bind(args, params=params)
q._bound_hamiltonian = self._eval_hamiltonian(args, params)
......@@ -1012,6 +1044,9 @@ cdef class Source(_LocalOperator):
Returns a copy of this operator that does not need to be passed extra
arguments when subsequently called or when using the ``act`` method.
Providing positional arguments via 'args' is deprecated,
instead provide named parameters as a dictionary via 'params'.
"""
q = super().bind(args, params=params)
q._bound_hamiltonian = self._eval_hamiltonian(args, params)
......
......@@ -11,7 +11,7 @@
import math
import numpy as np
from .. import system
from .._common import ensure_isinstance
from .._common import ensure_isinstance, deprecate_args
__all__ = ['Bands']
......@@ -27,7 +27,7 @@ class Bands:
calculated.
args : tuple, defaults to empty
Positional arguments to pass to the ``hamiltonian`` method.
Mutually exclusive with 'params'.
Deprecated in favor or 'params' (and mutually exclusive with it).
params : dict, optional
Dictionary of parameter names and their values. Mutually exclusive
with 'args'.
......@@ -50,6 +50,7 @@ class Bands:
"""
_crossover_size = 8
@deprecate_args
def __init__(self, sys, args=(), *, params=None):
syst = sys
ensure_isinstance(syst, system.InfiniteSystem)
......
......@@ -27,6 +27,7 @@ from scipy import spatial, interpolate
from math import cos, sin, pi, sqrt
from . import system, builder, _common
from ._common import deprecate_args
__all__ = ['plot', 'map', 'bands', 'spectrum', 'current', 'density',
......@@ -1348,6 +1349,7 @@ def map(sys, value, colorbar=True, cmap=None, vmin=None, vmax=None, a=None,
return fig
@deprecate_args
def bands(sys, args=(), momenta=65, file=None, show=True, dpi=None,
fig_size=None, ax=None, *, params=None):
"""Plot band structure of a translationally invariant 1D system.
......@@ -1358,7 +1360,7 @@ def bands(sys, args=(), momenta=65, file=None, show=True, dpi=None,
A system bands of which are to be plotted.
args : tuple, defaults to empty
Positional arguments to pass to the ``hamiltonian`` method.
Mutally exclusive with 'params'.
Deprecated in favor of 'params' (and mutually exclusive with it).
momenta : int or 1D array-like
Either a number of sampling points on the interval [-pi, pi], or an
array of points at which the band structure has to be evaluated.
......
......@@ -14,7 +14,7 @@ import abc
from numbers import Integral
import numpy as np
import scipy.sparse as sp
from .._common import ensure_isinstance
from .._common import ensure_isinstance, deprecate_args
from .. import system
from functools import reduce
......@@ -113,7 +113,7 @@ class SparseSolver(metaclass=abc.ABCMeta):
Excitation energy at which to solve the scattering problem.
args : tuple, defaults to empty
Positional arguments to pass to the ``hamiltonian`` method.
Mutually exclusive with 'params'.
Deprecated in favor of 'params' (and mutually exclusive with it).
check_hermiticity : bool
Check if Hamiltonian matrices are in fact Hermitian.
Enables deduction of missing transmission coefficients.
......@@ -296,6 +296,7 @@ class SparseSolver(metaclass=abc.ABCMeta):
return LinearSys(lhs, rhs, indices, num_orb), lead_info
@deprecate_args
def smatrix(self, sys, energy=0, args=(),
out_leads=None, in_leads=None, check_hermiticity=True,
*, params=None):
......@@ -313,7 +314,7 @@ class SparseSolver(metaclass=abc.ABCMeta):
Excitation energy at which to solve the scattering problem.
args : tuple, defaults to empty
Positional arguments to pass to the ``hamiltonian`` method.
Mutually exclusive with 'params'.
Deprecated in favor of 'params' (and mutually exclusive with it).
out_leads : sequence of integers or ``None``
Numbers of leads where current or wave function is extracted. None
is interpreted as all leads. Default is ``None`` and means "all
......@@ -390,6 +391,7 @@ class SparseSolver(metaclass=abc.ABCMeta):
return SMatrix(data, lead_info, out_leads, in_leads, check_hermiticity)
@deprecate_args
def greens_function(self, sys, energy=0, args=(),
out_leads=None, in_leads=None, check_hermiticity=True,
*, params=None):
......@@ -407,7 +409,7 @@ class SparseSolver(metaclass=abc.ABCMeta):
Excitation energy at which to solve the scattering problem.
args : tuple, defaults to empty
Positional arguments to pass to the ``hamiltonian`` method.
Mutually exclusive with 'params'.
Deprecated in favor of 'params' (and mutually exclusive with it).
out_leads : sequence of integers or ``None``
Numbers of leads where current or wave function is extracted. None
is interpreted as all leads. Default is ``None`` and means "all
......@@ -488,6 +490,7 @@ class SparseSolver(metaclass=abc.ABCMeta):
return GreensFunction(data, lead_info, out_leads, in_leads,
check_hermiticity)
@deprecate_args
def ldos(self, sys, energy=0, args=(), check_hermiticity=True,
*, params=None):
"""
......@@ -504,8 +507,8 @@ class SparseSolver(metaclass=abc.ABCMeta):
Excitation energy at which to solve the scattering problem.
args : tuple of arguments, or empty tuple
Positional arguments to pass to the function(s) which
evaluate the hamiltonian matrix elements. Mutually exclusive
with 'params'.
evaluate the hamiltonian matrix elements.
Deprecated in favor of 'params' (and mutually exclusive with it).
check_hermiticity : ``bool``
Check if the Hamiltonian matrices are Hermitian.
params : dict, optional
......@@ -553,6 +556,7 @@ class SparseSolver(metaclass=abc.ABCMeta):
return ldos * (0.5 / np.pi)
@deprecate_args
def wave_function(self, sys, energy=0, args=(), check_hermiticity=True,
*, params=None):
r"""
......@@ -568,8 +572,8 @@ class SparseSolver(metaclass=abc.ABCMeta):
calculated.
args : tuple of arguments, or empty tuple
Positional arguments to pass to the function(s) which
evaluate the hamiltonian matrix elements. Mutually exclusive
with 'params'.
evaluate the hamiltonian matrix elements.
Deprecated in favor of 'params' (and mutually exclusive with it).
check_hermiticity : ``bool``
Check if the Hamiltonian matrices are Hermitian.
params : dict, optional
......
......@@ -114,5 +114,8 @@ def test_wavefunc_ldos_consistency():
_test_sparse.test_wavefunc_ldos_consistency(wave_function, ldos)
# We need to keep testing 'args', but we don't want to see
# all the deprecation warnings in the test logs
@pytest.mark.filterwarnings("ignore:.*'args' parameter")
def test_arg_passing():
_test_sparse.test_arg_passing(wave_function, ldos, smatrix)
......@@ -6,6 +6,8 @@
# the file AUTHORS.rst at the top-level directory of this distribution and at
# http://kwant-project.org/authors.
import pytest
from kwant.solvers.sparse import smatrix, greens_function, ldos, wave_function
from . import _test_sparse
......@@ -60,5 +62,9 @@ def test_ldos():
def test_wavefunc_ldos_consistency():
_test_sparse.test_wavefunc_ldos_consistency(wave_function, ldos)
# We need to keep testing 'args', but we don't want to see
# all the deprecation warnings in the test logs
@pytest.mark.filterwarnings("ignore:.*'args' parameter")
def test_arg_passing():
_test_sparse.test_arg_passing(wave_function, ldos, smatrix)
......@@ -14,6 +14,7 @@ import abc
import warnings
from copy import copy
from . import _system
from ._common import deprecate_args
class System(metaclass=abc.ABCMeta):
......@@ -59,12 +60,20 @@ class System(metaclass=abc.ABCMeta):
if ``i != j``, return the hopping between site ``i`` and ``j``.
Hamiltonians may depend (optionally) on positional and
keyword arguments
keyword arguments.
Providing positional arguments via 'args' is deprecated,
instead, provide named parameters as a dictionary via 'params'.
"""
pass
@deprecate_args
def discrete_symmetry(self, args, *, params=None):
"""Return the discrete symmetry of the system."""
"""Return the discrete symmetry of the system.
Providing positional arguments via 'args' is deprecated,
instead, provide named parameters as a dictionary via 'params'.
"""
# Avoid the circular import.
from .physics import DiscreteSymmetry
return DiscreteSymmetry()
......@@ -131,6 +140,7 @@ class FiniteSystem(System, metaclass=abc.ABCMeta):
"""
@deprecate_args
def precalculate(self, energy=0, args=(), leads=None,
what='modes', *, params=None):
"""
......@@ -147,7 +157,7 @@ class FiniteSystem(System, metaclass=abc.ABCMeta):
evaluated.
args : sequence
Additional parameters required for calculating the Hamiltionians.
Mutually exclusive with 'params'.
Deprecated in favor of 'params' (and mutually exclusive with it).
leads : sequence of integers or None
Numbers of the leads to be precalculated. If ``None``, all are
precalculated.
......@@ -194,12 +204,16 @@ class FiniteSystem(System, metaclass=abc.ABCMeta):
result.leads = new_leads
return result
@deprecate_args
def validate_symmetries(self, args=(), *, params=None):
"""Check that the Hamiltonian satisfies discrete symmetries.
Applies `~kwant.physics.DiscreteSymmetry.validate` to the
Hamiltonian, see its documentation for details on the return
format.
Providing positional arguments via 'args' is deprecated,
instead, provide named parameters as a dictionary via 'params'.
"""
symmetries = self.discrete_symmetry(args=args, params=params)
ham = self.hamiltonian_submatrix(args, sparse=True, params=params)
......@@ -248,19 +262,30 @@ class InfiniteSystem(System, metaclass=abc.ABCMeta):
exchanged, as well as of site 3 and 4.
"""
@deprecate_args
def cell_hamiltonian(self, args=(), sparse=False, *, params=None):
"""Hamiltonian of a single cell of the infinite system."""
"""Hamiltonian of a single cell of the infinite system.
Providing positional arguments via 'args' is deprecated,
instead, provide named parameters as a dictionary via 'params'.
"""
cell_sites = range(self.cell_size)
return self.hamiltonian_submatrix(args, cell_sites, cell_sites,
sparse=sparse, params=params)
@deprecate_args
def inter_cell_hopping(self, args=(), sparse=False, *, params=None):
"""Hopping Hamiltonian between two cells of the infinite system."""
"""Hopping Hamiltonian between two cells of the infinite system.
Providing positional arguments via 'args' is deprecated,
instead, provide named parameters as a dictionary via 'params'.
"""
cell_sites = range(self.cell_size)
interface_sites = range(self.cell_size, self.graph.num_nodes)
return self.hamiltonian_submatrix(args, cell_sites, interface_sites,
sparse=sparse, params=params)
@deprecate_args
def modes(self, energy=0, args=(), *, params=None):
"""Return mode decomposition of the lead
......@@ -272,6 +297,9 @@ class InfiniteSystem(System, metaclass=abc.ABCMeta):
freedom on the first ``cell_sites`` sites of the system
(recall that infinite systems store first the sites in the unit
cell, then connected sites in the neighboring unit cell).
Providing positional arguments via 'args' is deprecated,
instead, provide named parameters as a dictionary via 'params'.
"""
from . import physics # Putting this here avoids a circular import.
ham = self.cell_hamiltonian(args, params=params)
......@@ -301,12 +329,16 @@ class InfiniteSystem(System, metaclass=abc.ABCMeta):
symmetries.particle_hole = symmetries.chiral = None
return physics.modes(ham, hop, discrete_symmetry=symmetries)
@deprecate_args
def selfenergy(self, energy=0, args=(), *, params=None):
"""Return self-energy of a lead.
The returned matrix has the shape (s, s), where s is
``sum(len(self.hamiltonian(i, i)) for i in range(self.graph.num_nodes -
self.cell_size))``.
Providing positional arguments via 'args' is deprecated,
instead, provide named parameters as a dictionary via 'params'.
"""
from . import physics # Putting this here avoids a circular import.
ham = self.cell_hamiltonian(args, params=params)
......@@ -318,12 +350,16 @@ class InfiniteSystem(System, metaclass=abc.ABCMeta):
return physics.selfenergy(ham,
self.inter_cell_hopping(args, params=params))
@deprecate_args
def validate_symmetries(self, args=(), *, params=None):
"""Check that the Hamiltonian satisfies discrete symmetries.
Returns `~kwant.physics.DiscreteSymmetry.validate` applied
to the onsite matrix and the hopping. See its documentation for
details on the return format.
Providing positional arguments via 'args' is deprecated,
instead, provide named parameters as a dictionary via 'params'.
"""
symmetries = self.discrete_symmetry(args=args, params=params)
ham = self.cell_hamiltonian(args=args, sparse=True, params=params)
......@@ -355,6 +391,7 @@ class PrecalculatedLead:
# is no parametric dependence anymore
self.parameters = frozenset()
@deprecate_args
def modes(self, energy=0, args=(), *, params=None):
if self._modes is not None:
return self._modes
......@@ -363,6 +400,7 @@ class PrecalculatedLead:
"Consider using precalculate() with "
"what='modes' or what='all'")
@deprecate_args
def selfenergy(self, energy=0, args=(), *, params=None):
if self._selfenergy is not None:
return self._selfenergy
......
......@@ -14,6 +14,7 @@ from random import Random
import numpy as np
import tinyarray as ta
import pytest
from pytest import raises, warns
from numpy.testing import assert_almost_equal
......@@ -1145,12 +1146,14 @@ def test_discrete_symmetries():
syst[lat(1)] = np.identity(2)
syst[lat2(1)] = 1
sym = syst.finalized().discrete_symmetry(args=[0])
params=dict(p=0)
sym = syst.finalized().discrete_symmetry(params=params)
for proj, should_be in zip(sym.projectors, np.identity(3)):
assert np.allclose(proj.toarray(), should_be.reshape((3, 1)))
assert np.allclose(sym.time_reversal.toarray(), np.identity(3))
syst.conservation_law = lambda site, p: cons_law[site.family]
sym = syst.finalized().discrete_symmetry(args=[0])
sym = syst.finalized().discrete_symmetry(params=params)
for proj, should_be in zip(sym.projectors, np.identity(3)):
assert np.allclose(proj.toarray(), should_be.reshape((-1, 1)))
......@@ -1186,8 +1189,10 @@ def test_discrete_symmetries():
assert np.allclose(proj.toarray(), [[1]])
# We need to keep testing 'args', but we don't want to see
# all the deprecation warnings in the test logs
@pytest.mark.filterwarnings("ignore:.*'args' parameter")
def test_argument_passing():
chain = kwant.lattice.chain()
# Test for passing parameters to hamiltonian matrix elements
......
......@@ -48,8 +48,8 @@ def test_qhe(W=16, L=8):
((3.2, 3.7), 2, 1e-3),
((5.2, 5.5), 3, 1e-1)]:
for r_phi in r_phis:
args = (1.0 / r_phi, "")
pc = syst.precalculate(1.0, args, what='all')
for result in [kwant.smatrix(pc, 1, args),
kwant.solvers.default.greens_function(pc, 1, args)]:
params = dict(phi=1.0 / r_phi, salt="")
pc = syst.precalculate(1.0, params=params, what='all')
for result in [kwant.smatrix(pc, 1, params=params),
kwant.solvers.default.greens_function(pc, 1, params=params)]:
assert abs(T_nominal - result.transmission(1, 0)) < max_err
......@@ -156,22 +156,22 @@ def test_operator_construction():
A(fsyst, sum=True).sum == True
def _test(A, bra, ket=None, per_el_val=None, reduced_val=None, args=()):
def _test(A, bra, ket=None, per_el_val=None, reduced_val=None, params=None):
if per_el_val is not None:
val = A(bra, ket, args=args)
val = A(bra, ket, params=params)
assert np.allclose(val, per_el_val)
# with bound args
val = A.bind(args)(bra, ket)
val = A.bind(params=params)(bra, ket)
assert np.allclose(val, per_el_val)
# test that inner products give the same thing
ket = bra if ket is None else ket
act_val = np.dot(bra.conj(), A.act(ket, args=args))
inner_val = np.sum(A(bra, ket, args=args))
act_val = np.dot(bra.conj(), A.act(ket, params=params))
inner_val = np.sum(A(bra, ket, params=params))
# check also when sum is done internally by operator
try:
sum_reset = A.sum
A.sum = True
sum_inner_val = A(bra, ket, args=args)