def test_type_validator(self): options = Options(meas_level=MeasLevel.KERNELED) options.set_validator("meas_level", MeasLevel) with self.assertRaises(TypeError): options.update_options(meas_level=2) options.update_options(meas_level=MeasLevel.CLASSIFIED) self.assertEqual(2, options.meas_level.value)
def test_list_choice(self): options = Options(method="auto") options.set_validator("method", ["auto", "statevector", "mps"]) with self.assertRaises(ValueError): options.update_options(method="stabilizer") options.update_options(method="mps") self.assertEqual(options.method, "mps")
def test_no_validators(self): options = Options(shots=1024, method="auto", meas_level=MeasLevel.KERNELED) self.assertEqual(options.shots, 1024) options.update_options(method="statevector") self.assertEqual(options.method, "statevector")
def test_range_bound_validator_multiple_fields(self): options = Options(shots=1024, method="auto", meas_level=MeasLevel.KERNELED) options.set_validator("shots", (1, 1024)) options.set_validator("method", ["auto", "statevector", "mps"]) options.set_validator("meas_level", MeasLevel) with self.assertRaises(ValueError): options.update_options(shots=2048, method="statevector") options.update_options(shots=512, method="statevector") self.assertEqual(options.shots, 512) self.assertEqual(options.method, "statevector")
def test_overriding_instance_attributes(self): """Test that setting instance attributes and methods does not interfere with previously defined attributes and methods. This produces an inconsistency where >>> options = Options() >>> options.validators = "hello" >>> options.validators {} >>> options.get("validators") "hello" """ options = Options(get="a string") options.validator = "another string" setattr(options, "update_options", "not a method") options.update_options(_fields="not a dict") options.__dict__ = "also not a dict" self.assertEqual( options.__dict__, { "get": "a string", "validator": "another string", "update_options": "not a method", "_fields": "not a dict", "__dict__": "also not a dict", }, ) self.assertEqual( options._fields, { "get": "a string", "validator": "another string", "update_options": "not a method", "_fields": "not a dict", "__dict__": "also not a dict", }, ) self.assertEqual(options.validator, {}) self.assertEqual(options.get("_fields"), "not a dict")
def test_range_bound_validator(self): options = Options(shots=1024) options.set_validator("shots", (1, 4096)) with self.assertRaises(ValueError): options.update_options(shots=8192)