Skip to content

Commit

Permalink
Revert "Use varname to resolve variable names (#54)"
Browse files Browse the repository at this point in the history
This reverts commit 5ec8d6b.
  • Loading branch information
kotarot authored Jan 28, 2021
1 parent 7a2e096 commit 00641c6
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 73 deletions.
11 changes: 3 additions & 8 deletions sawatabi/base_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import varname

import sawatabi.constants as constants


Expand All @@ -31,8 29,7 @@ def _get_article(name):
return article

@staticmethod
def _check_argument_type(value, atype):
name = varname.nameof(value)
def _check_argument_type(name, value, atype):
if not isinstance(value, atype):
if isinstance(atype, tuple):
typestr = [t.__name__ for t in atype]
Expand All @@ -43,8 40,7 @@ def _check_argument_type(value, atype):
raise TypeError(f"'{name}' must be {article} {typestr}.")

@staticmethod
def _check_argument_type_in_tuple(values, atype):
name = varname.nameof(values)
def _check_argument_type_in_tuple(name, values, atype):
if len(values) == 0:
raise TypeError(f"'{name}' must not be an empty tuple.")
if not isinstance(atype, tuple):
Expand All @@ -59,8 55,7 @@ def _check_argument_type_in_tuple(values, atype):
raise TypeError(f"All elements in tuple '{name}' must be one of {atypestr}.")

@staticmethod
def _check_argument_type_in_list(values, atype):
name = varname.nameof(values)
def _check_argument_type_in_list(name, values, atype):
if len(values) == 0:
raise TypeError(f"'{name}' must not be an empty list.")
if not isinstance(atype, tuple):
Expand Down
8 changes: 4 additions & 4 deletions sawatabi/model/constraint/abstract_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 24,18 @@ class AbstractConstraint(BaseMixin):
def __init__(self, label="", strength=1.0):
self._constraint_class = None

self._check_argument_type(label, str)
self._check_argument_type("label", label, str)
if label == "":
raise ValueError("'label' must not be empty.")
self._check_argument_type(strength, numbers.Number)
self._check_argument_type("strength", strength, numbers.Number)

self._label = label
self._strength = strength

def _check_variables_and_to_set(self, variables):
self._check_argument_type(variables, (pyqubo.Array, pyqubo.Spin, pyqubo.Binary, list, set))
self._check_argument_type("variables", variables, (pyqubo.Array, pyqubo.Spin, pyqubo.Binary, list, set))
if isinstance(variables, list) or isinstance(variables, set):
self._check_argument_type_in_list(variables, (pyqubo.Spin, pyqubo.Binary))
self._check_argument_type_in_list("variables", variables, (pyqubo.Spin, pyqubo.Binary))
if isinstance(variables, set):
return variables
else:
Expand Down
2 changes: 1 addition & 1 deletion sawatabi/model/constraint/n_hot_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 28,7 @@ def __init__(self, variables=None, n=1, label=constants.DEFAULT_LABEL_N_HOT, str
else:
self._variables = self._check_variables_and_to_set(variables)

self._check_argument_type(n, int)
self._check_argument_type("n", n, int)
if n <= 0:
raise ValueError("'n' must be a positive integer.")
self._n = n
Expand Down
50 changes: 25 additions & 25 deletions sawatabi/model/logical_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 66,18 @@ def variables(self, name, shape=()):
self._variables[this_name] = name
return self._variables[this_name]

self._check_argument_type(name, str)
self._check_argument_type(shape, tuple)
self._check_argument_type_in_tuple(shape, int)
self._check_argument_type("name", name, str)
self._check_argument_type("shape", shape, tuple)
self._check_argument_type_in_tuple("shape", shape, int)

vartype = self._modeltype_to_vartype(self._mtype)
self._variables[name] = pyqubo.Array.create(name, shape=shape, vartype=vartype)
return self._variables[name]

def append(self, name, shape=()):
self._check_argument_type(name, str)
self._check_argument_type(shape, tuple)
self._check_argument_type_in_tuple(shape, (int, np.int64, np.int32))
self._check_argument_type("name", name, str)
self._check_argument_type("shape", shape, tuple)
self._check_argument_type_in_tuple("shape", shape, (int, np.int64, np.int32))

if name not in self._variables:
# raise KeyError(f"Variables name '{name}' is not defined in the model.")
Expand Down Expand Up @@ -111,7 111,7 @@ def select_interactions_by_variable(self, target):
self._update_interactions_dataframe_from_arrays()

# Find interactions which interacts with the given variable.
self._check_argument_type(target, (pyqubo.Spin, pyqubo.Binary))
self._check_argument_type("target", target, (pyqubo.Spin, pyqubo.Binary))
return self._interactions[(self._interactions["key_0"] == target.label) | (self._interactions["key_1"] == target.label)]["name"].values

################################
Expand All @@ -130,17 130,17 @@ def add_interaction(
if not target:
raise ValueError("'target' must be specified.")

self._check_argument_type(coefficient, (numbers.Number, pyqubo.core.Express, pyqubo.core.Coefficient))
self._check_argument_type(scale, (numbers.Number, pyqubo.core.Express))
self._check_argument_type(attributes, dict)
self._check_argument_type(timestamp, (int, float))
self._check_argument_type("coefficient", coefficient, (numbers.Number, pyqubo.core.Express, pyqubo.core.Coefficient))
self._check_argument_type("scale", scale, (numbers.Number, pyqubo.core.Express))
self._check_argument_type("attributes", attributes, dict)
self._check_argument_type("timestamp", timestamp, (int, float))

interaction_info = self._get_interaction_info_from_target(target)

body = interaction_info["body"]
if name:
# Use the given specific name
self._check_argument_type(name, str)
self._check_argument_type("name", name, str)
internal_name = name
else:
# Automatically named by the default name
Expand Down Expand Up @@ -206,13 206,13 @@ def update_interaction(
raise ValueError("Both 'target' and 'name' cannot be specified simultaneously.")

if coefficient is not None:
self._check_argument_type(coefficient, numbers.Number)
self._check_argument_type("coefficient", coefficient, numbers.Number)
if scale is not None:
self._check_argument_type(scale, numbers.Number)
self._check_argument_type("scale", scale, numbers.Number)
if attributes is not None:
self._check_argument_type(attributes, dict)
self._check_argument_type("attributes", attributes, dict)
if timestamp is not None:
self._check_argument_type(timestamp, (int, float))
self._check_argument_type("timestamp", timestamp, (int, float))

if target is not None:
interaction_info = self._get_interaction_info_from_target(target)
Expand All @@ -222,7 222,7 @@ def update_interaction(
if not self._has_name(internal_name):
raise KeyError(f"An interaction named '{internal_name}' does not exist yet in the model. Need to be added before updating.")
# Already given the specific name
self._check_argument_type(name, (str, tuple))
self._check_argument_type("name", name, (str, tuple))
else:
# Will be automatically named by the default name
internal_name = interaction_info["name"]
Expand Down Expand Up @@ -315,7 315,7 @@ def _get_internal_name_from_target_and_name(self, target, name):

if name:
# Already given the specific name
self._check_argument_type(name, (str, tuple))
self._check_argument_type("name", name, (str, tuple))
internal_name = name
else:
# Will be automatically named by the default name
Expand Down Expand Up @@ -346,7 346,7 @@ def _update_interactions_dataframe_from_arrays(self):
def delete_variable(self, target):
if not target:
raise ValueError("'target' must be specified.")
self._check_argument_type(target, (pyqubo.Spin, pyqubo.Binary))
self._check_argument_type("target", target, (pyqubo.Spin, pyqubo.Binary))

# TODO: Delete variable physically
self._deleted[target.label] = True
Expand All @@ -367,7 367,7 @@ def delete_variable(self, target):
def fix_variable(self, target, value):
if not target:
raise ValueError("'target' must be specified.")
self._check_argument_type(target, (pyqubo.Spin, pyqubo.Binary))
self._check_argument_type("target", target, (pyqubo.Spin, pyqubo.Binary))

# Value check
if self.get_mtype() == constants.MODEL_ISING:
Expand Down Expand Up @@ -416,12 416,12 @@ def fix_variable(self, target, value):
################################

def add_constraint(self, constraint):
self._check_argument_type(constraint, AbstractConstraint)
self._check_argument_type("constraint", constraint, AbstractConstraint)
label = constraint.get_label()
self._constraints[label] = constraint

def remove_constraint(self, label):
self._check_argument_type(label, str)
self._check_argument_type("label", label, str)
self._constraints.pop(label)

################################
Expand All @@ -445,7 445,7 @@ def get_offset(self):
################################

def from_pyqubo(self, expression):
self._check_argument_type(expression, (pyqubo.Express, pyqubo.Model))
self._check_argument_type("expression", expression, (pyqubo.Express, pyqubo.Model))

if isinstance(expression, pyqubo.Express):
pyqubo_model = expression.compile()
Expand Down Expand Up @@ -591,7 591,7 @@ def to_physical(self, placeholder={}):
return physical

def merge(self, other):
self._check_argument_type(other, LogicalModel)
self._check_argument_type("other", other, LogicalModel)

# Check type
if self._mtype != other._mtype:
Expand Down Expand Up @@ -827,7 827,7 @@ def get_attribute(self, target=None, name="", key=""):
"""
Returns the value of the key for the given variable or interaction.
"""
self._check_argument_type(key, str)
self._check_argument_type("key", key, str)
attributes = self.get_attributes(target, name)
return attributes[key]

Expand Down
2 changes: 1 addition & 1 deletion sawatabi/solver/dwave_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 28,7 @@ def __init__(self, endpoint=None, token=None, solver="Advantage_system1.1"):
self._solver = solver

def solve(self, model, embedding_parameters=None, **kwargs):
self._check_argument_type(model, PhysicalModel)
self._check_argument_type("model", model, PhysicalModel)

if len(model._raw_interactions[constants.INTERACTION_LINEAR]) == 0 and len(model._raw_interactions[constants.INTERACTION_QUADRATIC]) == 0:
raise ValueError("Model cannot be empty.")
Expand Down
4 changes: 2 additions & 2 deletions sawatabi/solver/local_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 28,7 @@ def __init__(self, exact=False):
super().__init__()

def solve(self, model, **kwargs):
self._check_argument_type(model, PhysicalModel)
self._check_argument_type("model", model, PhysicalModel)

if len(model._raw_interactions[constants.INTERACTION_LINEAR]) == 0 and len(model._raw_interactions[constants.INTERACTION_QUADRATIC]) == 0:
raise ValueError("Model cannot be empty.")
Expand Down Expand Up @@ -58,7 58,7 @@ def solve(self, model, **kwargs):
return sampleset

def default_beta_range(self, model):
self._check_argument_type(model, PhysicalModel)
self._check_argument_type("model", model, PhysicalModel)

if len(model._raw_interactions[constants.INTERACTION_LINEAR]) == 0 and len(model._raw_interactions[constants.INTERACTION_QUADRATIC]) == 0:
raise ValueError("Model cannot be empty.")
Expand Down
2 changes: 1 addition & 1 deletion sawatabi/solver/optigan_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 45,7 @@ def get_config(self):
return config

def solve(self, model, num_unit_steps=10, timeout=10000, duplicate=False, gzip_request=True, gzip_response=True):
self._check_argument_type(model, PhysicalModel)
self._check_argument_type("model", model, PhysicalModel)

if len(model._raw_interactions[constants.INTERACTION_LINEAR]) == 0 and len(model._raw_interactions[constants.INTERACTION_QUADRATIC]) == 0:
raise ValueError("Model cannot be empty.")
Expand Down
2 changes: 1 addition & 1 deletion sawatabi/solver/sawatabi_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 46,7 @@ def solve(
pickup_mode=constants.PICKUP_MODE_RANDOM,
seed=None,
):
self._check_argument_type(model, PhysicalModel)
self._check_argument_type("model", model, PhysicalModel)

if len(model._raw_interactions[constants.INTERACTION_LINEAR]) == 0 and len(model._raw_interactions[constants.INTERACTION_QUADRATIC]) == 0:
raise ValueError("Model cannot be empty.")
Expand Down
13 changes: 6 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 44,15 @@
version=version,
license="Apache 2.0",
install_requires=[
"apache-beam>=2.26.0,<3.0.0",
"apache-beam[interactive]>=2.26.0,<3.0.0",
"apache-beam[gcp]>=2.26.0,<3.0.0",
"pandas>=1.1.4,<2.0.0",
"PyYAML>=5.3.1,<6.0.0",
"pyqubo>=0.4.0,<1.0.0",
"dwave-system>=1.2.1,<2.0.0",
"dwave-cloud-client>=0.8.1,<1.0.0",
"dwave-neal>=0.5.6,<1.0.0",
"pandas>=1.1.4,<2.0.0",
"pyqubo>=0.4.0,<1.0.0",
"PyYAML>=5.3.1,<6.0.0",
"varname>=0.5.6,<1.0.0",
"apache-beam>=2.26.0,<3.0.0",
"apache-beam[interactive]>=2.26.0,<3.0.0",
"apache-beam[gcp]>=2.26.0,<3.0.0",
],
extras_require={
"dev": [
Expand Down
35 changes: 12 additions & 23 deletions tests/test_base_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 61,22 @@ def test_vartype_to_modeltype():
def test_check_argument():
base = BaseMixin()

testvar_str = "test variable"
testvar_int = 12345

base._check_argument_type(value=testvar_str, atype=str)
base._check_argument_type(name="name", value="string", atype=str)
with pytest.raises(TypeError):
base._check_argument_type(testvar_str, int)
base._check_argument_type(testvar_str, (str, int))
base._check_argument_type(testvar_int, (str, int))

testvar_tuple_str = ("test variable",)
testvar_tuple_empty = ()
testvar_tuple_str_int = ("test variable", 12345)
base._check_argument_type("name", "string", int)
base._check_argument_type("name", "string", (str, int))
base._check_argument_type("name", 12345, (str, int))

base._check_argument_type_in_tuple(values=testvar_tuple_str, atype=str)
base._check_argument_type_in_tuple(name="name", values=("string"), atype=str)
with pytest.raises(TypeError):
base._check_argument_type_in_tuple(testvar_tuple_empty, str)
base._check_argument_type_in_tuple("name", (), str)
with pytest.raises(TypeError):
base._check_argument_type_in_tuple(testvar_tuple_str_int, str)
base._check_argument_type_in_tuple(testvar_tuple_str_int, (str, int))

testvar_list_str = ["test variable"]
testvar_list_empty = []
testvar_list_str_int = ["test variable", 12345]
base._check_argument_type_in_tuple("name", ("string", 12345), str)
base._check_argument_type_in_tuple("name", ("string", 12345), (str, int))

base._check_argument_type_in_list(values=testvar_list_str, atype=str)
base._check_argument_type_in_list(name="name", values=["string"], atype=str)
with pytest.raises(TypeError):
base._check_argument_type_in_list(testvar_list_empty, str)
base._check_argument_type_in_list("name", [], str)
with pytest.raises(TypeError):
base._check_argument_type_in_list(testvar_list_str_int, str)
base._check_argument_type_in_list(testvar_list_str_int, (str, int))
base._check_argument_type_in_list("name", ["string", 12345], str)
base._check_argument_type_in_list("name", ["string", 12345], (str, int))

0 comments on commit 00641c6

Please sign in to comment.