Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update options to be a mapping instead of a simple namespace #6355

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion qiskit/providers/basicaer/qasm_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def __init__(self, configuration=None, provider=None, **fields):
self._shots = 0
self._memory = False
self._initial_statevector = self.options.get("initial_statevector")
self._chop_threshold = self.options.get("chop_threashold")
self._chop_threshold = self.options.get("chop_threshold")
self._qobj_config = None
# TEMP
self._sample_measure = False
Expand Down
63 changes: 58 additions & 5 deletions qiskit/providers/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

"""Container class for backend options."""

import types
from collections.abc import MutableMapping


class Options(types.SimpleNamespace):
class Options(MutableMapping):
"""Base options object

This class is the abstract class that all backend options are based
Expand All @@ -26,10 +26,63 @@ class Options(types.SimpleNamespace):
options.
"""

_dict = {}

def __init__(self, **kwargs):
self._dict = {}
super().__init__()
self._dict = kwargs

def __getattr__(self, field):
if field == "_dict":
super().__getattribute__(field)
elif field not in self._dict:
raise AttributeError("%s not a valid option" % field)
return self._dict[field]

def __getitem__(self, field):
if field not in self._dict:
raise KeyError("%s not a valid option" % field)
self._dict.get(field)

def __setitem__(self, field, value):
if field in self._dict:
self._dict[field] = value
else:
raise KeyError("%s not a valid option" % field)

def __delitem__(self, field):
del self._dict[field]

def __len__(self):
return len(self._dict)

def __iter__(self):
return iter(self._dict)

def __setattr__(self, field, value):
if field == "_dict":
super().__setattr__(field, value)
elif field in self._dict:
self._dict[field] = value
else:
raise AttributeError("%s not a valid option" % field)

def __repr__(self):
return "Options(%s)" % ", ".join("k=v" for k, v in self._dict.items())

def update_options(self, **fields):
"""Update options with kwargs"""
self.__dict__.update(fields)
self._dict.update(fields)

def get(self, field, default=None):
def get(self, field, default=None): # pylint: disable=arguments-differ
"""Get an option value for a given key."""
return getattr(self, field, default)
return self._dict.get(field, default)

def __dict__(self):
return self._dict

def __eq__(self, other):
if isinstance(other, Options):
return self._dict == other._dict
return NotImplementedError