def test_type_validator_str(self): options = Options(meas_level=MeasLevel.KERNELED) options.set_validator("meas_level", MeasLevel) expected = """Options(meas_level=<MeasLevel.KERNELED: 1>) Where: \tmeas_level is of type <enum 'MeasLevel'>\n""" self.assertEqual(str(options), expected)
def test_list_choice_string(self): options = Options(method="auto") options.set_validator("method", ["auto", "statevector", "mps"]) expected = """Options(method='auto') Where: \tmethod is one of ['auto', 'statevector', 'mps']\n""" self.assertEqual(str(options), expected)
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_range_bound_string(self): options = Options(shots=1024) options.set_validator("shots", (1, 1024)) expected = """Options(shots=1024) Where: \tshots is >= 1 and <= 1024\n""" self.assertEqual(str(options), expected)
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_range_bound_validator_multiple_fields_string(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) expected = """Options(shots=1024, method='auto', meas_level=<MeasLevel.KERNELED: 1>) Where: \tshots is >= 1 and <= 1024 \tmethod is one of ['auto', 'statevector', 'mps'] \tmeas_level is of type <enum 'MeasLevel'>\n""" self.assertEqual(str(options), expected)
def test_pickle(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) expected = """Options(shots=1024, method='auto', meas_level=<MeasLevel.KERNELED: 1>) Where: \tshots is >= 1 and <= 1024 \tmethod is one of ['auto', 'statevector', 'mps'] \tmeas_level is of type <enum 'MeasLevel'>\n""" self.assertEqual(str(options), expected) self.assertEqual(str(pickle.loads(pickle.dumps(options))), expected)
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)