Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
    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)
Пример #4
0
 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")
Пример #5
0
 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")
Пример #6
0
 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)
Пример #7
0
 def test_no_validators_str(self):
     options = Options(shots=1024,
                       method="auto",
                       meas_level=MeasLevel.KERNELED)
     self.assertEqual(
         str(options),
         "Options(shots=1024, method='auto', meas_level=<MeasLevel.KERNELED: 1>)"
     )
Пример #8
0
 def test_copy(self):
     options = Options(opt1=1, opt2=2)
     cpy = copy.copy(options)
     cpy.update_options(opt1=10, opt3=20)
     self.assertEqual(options.opt1, 1)
     self.assertEqual(options.opt2, 2)
     self.assertNotIn("opt3", options)
     self.assertEqual(cpy.opt1, 10)
     self.assertEqual(cpy.opt2, 2)
     self.assertEqual(cpy.opt3, 20)
Пример #9
0
    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)
Пример #10
0
 def test_setting_attributes(self):
     options = Options()
     options.hello = "world"  # pylint: disable=assigning-non-slot
     options.a = "b"  # pylint: disable=assigning-non-slot
     self.assertEqual(options.get("hello"), "world")
     self.assertEqual(options.get("a"), "b")
     self.assertEqual(options.__dict__, {"hello": "world", "a": "b"})
Пример #11
0
    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)
Пример #12
0
    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 _default_options(cls):
     return Options(shots=100)
Пример #14
0
 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")
Пример #15
0
 def test_hasattr(self):
     options = Options(shots=1024)
     self.assertTrue(hasattr(options, "shots"))
     self.assertFalse(hasattr(options, "method"))
Пример #16
0
 def test_unpacking_dict(self):
     kwargs = {"hello": "world", "a": "b"}
     options = Options(**kwargs)
     self.assertEqual(options.__dict__, kwargs)
     self.assertEqual({**options.__dict__}, kwargs)
Пример #17
0
 def _default_options(cls) -> Options:
     return Options(shots=1)
Пример #18
0
 def _default_options(cls) -> Options:
     return Options(shots=1024, memory=False, initial_statevector=None)
Пример #19
0
 def _default_options(cls):
     return Options(count=500)
Пример #20
0
 def _default_options(cls) -> Options:
     """ Returns default runtime options. Only the options that are relevant to Quantum Inspire backends are added.
     """
     return Options(shots=1024, memory=True)
Пример #21
0
 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)
Пример #22
0
 def _default_options(cls):
     return Options(shots=0,
                    qobj_id=str(uuid4()),
                    qobj_header={},
                    parameter_binds={})
 def _default_options(cls):
     return Options(shots=1024, priority='normal')