Пример #1
0
    def test_bool(self):
        """Test boolean value of the Configuration object."""
        self.logTestName()

        # test false if no config is loaded
        config = Configuration('noconfig')
        self.assertFalse(config)

        # test true if config is loaded
        config = Configuration(filename)
        self.assertTrue(config)
Пример #2
0
    def test_save(self, tmp_path):
        """Test saving a configuration file."""
        config = Configuration(name=config_filename)

        # make a change
        config['strawberryfields.global']['shots'] = 10

        temp_config_path = tmp_path / 'test_config.toml'
        config.save(temp_config_path)

        result = toml.load(temp_config_path)
        config._config == result
Пример #3
0
    def test_save(self):
        """Test saving a configuration file."""
        self.logTestName()

        config = Configuration(name=filename)

        # make a change
        config['strawberryfields.global']['shots'] = 10
        config.save('test_config.toml')

        result = toml.load('test_config.toml')
        os.remove('test_config.toml')
        self.assertEqual(config._config, result)
Пример #4
0
    def test_save(self, tmp_path):
        """Test saving a configuration file."""
        config = Configuration(name=config_path)

        # make a change
        config['strawberryfields.global']['shots'] = 10

        # Need to convert to string for Python 3.5 compatibility
        temp_config_path = str(tmp_path / 'test_config.toml')
        config.save(temp_config_path)

        result = toml.load(temp_config_path)
        config._config == result
Пример #5
0
def default_config(tmpdir):
    config_path = os.path.join(tmpdir, config_filename)

    with open(config_path, "w") as f:
        f.write(test_config)

    return Configuration(name=config_path)
Пример #6
0
    def test_set_item(self):
        """Test setting items."""
        self.logTestName()

        config = Configuration(name=filename)

        # set existing options
        config['main.shots'] = 10
        self.assertEqual(config['main.shots'], 10)
        self.assertEqual(config['main']['shots'], 10)

        config['strawberryfields.global']['hbar'] = 5
        self.assertEqual(config['strawberryfields.global.hbar'], 5)
        self.assertEqual(config['strawberryfields.global']['hbar'], 5)

        # set new options
        config['projectq.ibm']['device'] = 'ibmqx4'
        self.assertEqual(config['projectq.ibm.device'], 'ibmqx4')

        # set nested dictionaries
        config['strawberryfields.tf'] = {'batched': True, 'cutoff_dim': 6}
        self.assertEqual(config['strawberryfields.tf'], {
            'batched': True,
            'cutoff_dim': 6
        })

        # set nested keys that don't exist dictionaries
        config['strawberryfields.another.hello.world'] = 5
        self.assertEqual(config['strawberryfields.another'],
                         {'hello': {
                             'world': 5
                         }})
Пример #7
0
    def test_bool(self, default_config):
        """Test boolean value of the Configuration object."""

        # test false if no config is loaded
        config = Configuration('noconfig')

        assert not config
        assert default_config
Пример #8
0
    def test_device_load(self):
        """Test loading a device with a configuration."""
        self.logTestName()

        config = Configuration(name=filename)
        dev = qml.device('default.gaussian', wires=2, config=config)

        self.assertTrue(dev.hbar, 1)
Пример #9
0
    def test_not_found_warning(self):
        """Test that a warning is raised if no configuration file found."""
        self.logTestName()

        with self.assertLogs(level='INFO') as l:
            Configuration('noconfig')
            self.assertEqual(len(l.output), 1)
            self.assertEqual(len(l.records), 1)
            self.assertIn('No PennyLane configuration file found.', l.output[0])
Пример #10
0
    def test_not_found_warning(self, caplog):
        """Test that a warning is raised if no configuration file found."""

        caplog.clear()
        caplog.set_level(log.INFO)

        Configuration("noconfig")

        assert len(caplog.records) == 1
        assert caplog.records[0].message == "No PennyLane configuration file found."
Пример #11
0
    def test_loading_current_directory(self, monkeypatch, default_config_toml):
        """Test that the default configuration file can be loaded
        from the current directory."""

        monkeypatch.chdir(".")
        monkeypatch.setenv("PENNYLANE_CONF", "")
        config = Configuration(name=config_path)

        assert config.path == os.path.join(os.curdir, config_path)
        assert config._config == default_config_toml
Пример #12
0
    def test_loading_absolute_path(self, monkeypatch, default_config_toml):
        """Test that the default configuration file can be loaded
        from an absolute path."""

        os.curdir = "None"
        monkeypatch.setenv("PENNYLANE_CONF", "")

        config = Configuration(name=os.path.join(os.getcwd(), config_path))

        assert config._config == default_config_toml
        assert config.path == os.path.join(os.getcwd(), config_path)
Пример #13
0
    def test_loading_current_directory(self):
        """Test that the default configuration file can be loaded
        from the current directory."""
        self.logTestName()

        os.curdir = "."
        os.environ["PENNYLANE_CONF"] = ""
        config = Configuration(name=filename)

        self.assertEqual(config._config, expected_config)
        self.assertEqual(config.path, os.path.join(os.curdir, filename))
Пример #14
0
    def test_loading_absolute_path(self):
        """Test that the default configuration file can be loaded
        from an absolute path."""
        self.logTestName()

        os.curdir = "None"
        os.environ["PENNYLANE_CONF"] = ""
        config = Configuration(name=os.path.join(os.getcwd(), filename))

        self.assertEqual(config._config, expected_config)
        self.assertEqual(config.path, os.path.join(os.getcwd(), filename))
Пример #15
0
    def test_loading_environment_variable(self):
        """Test that the default configuration file can be loaded
        from an environment variable."""
        self.logTestName()

        os.curdir = "None"
        os.environ["PENNYLANE_CONF"] = os.getcwd()
        config = Configuration(name=filename)

        self.assertEqual(config._config, expected_config)
        self.assertEqual(config._env_config_dir, os.environ["PENNYLANE_CONF"])
        self.assertEqual(config.path, os.path.join(os.environ["PENNYLANE_CONF"], filename))
Пример #16
0
    def test_loading_environment_variable(self, monkeypatch, default_config_toml):
        """Test that the default configuration file can be loaded
        from an environment variable."""

        os.curdir = "None"
        monkeypatch.setenv("PENNYLANE_CONF", os.getcwd())

        config = Configuration(name=config_path)

        assert config._config == default_config_toml
        assert config._env_config_dir == os.environ["PENNYLANE_CONF"]
        assert config.path == os.path.join(os.environ["PENNYLANE_CONF"], config_path)
Пример #17
0
    def test_get_item(self):
        """Test getting items."""
        self.logTestName()

        config = Configuration(name=filename)

        # get existing options
        self.assertEqual(config['main.shots'], 0)
        self.assertEqual(config['main']['shots'], 0)
        self.assertEqual(config['strawberryfields.global.hbar'], 1)
        self.assertEqual(config['strawberryfields.global']['hbar'], 1)

        # get nested dictionaries
        self.assertEqual(config['strawberryfields.fock'], {'cutoff_dim': 10})

        # get key that doesn't exist
        self.assertEqual(config['projectq.ibm.idonotexist'], {})
Пример #18
0
def default_config():
    return Configuration(name=config_path)