Ejemplo n.º 1
0
    def __init__(self, backend, backend_name, local=True):
        """FakeBackend initializer.

        Args:
            backend (IBMQBackend): IBMQBackend instance
            backend_name (str): The name to give the backend.
            local (bool): Determines if Aer of IBMQ simulator should be used.
        """
        if backend.configuration().open_pulse:
            config = PulseBackendConfiguration.from_dict(backend.configuration().to_dict())
        else:
            config = QasmBackendConfiguration.from_dict(backend.configuration().to_dict())
        super().__init__(config)
        self._credentials = _Credentials()
        self._properties = backend.properties()
        self._configuration.simulator = True
        self._configuration.local = local
        self._configuration.backend_name = backend_name
        if AER_VERSION >= 60000:
            self.noise_model = NoiseModel.from_backend(self, warnings=False)
        else:
            self.noise_model = NoiseModel.from_backend(self)
        if local:
            self.sim = Aer.get_backend('qasm_simulator')
        else:
            pro = IBMQ.get_provider(hub='ibm-q', group='open', project='main')
            self.sim = pro.backends.ibmq_qasm_simulator
Ejemplo n.º 2
0
    def _discover_remote_backends(self):
        """Return the remote backends available.

        Returns:
            dict[str:IBMQBackend]: a dict of the remote backend instances,
                keyed by backend name.
        """
        ret = OrderedDict()
        configs_list = self._api.available_backends()
        for raw_config in configs_list:
            try:
                if raw_config.get('open_pulse', False):
                    config = PulseBackendConfiguration.from_dict(raw_config)
                else:
                    config = QasmBackendConfiguration.from_dict(raw_config)
                backend_cls = IBMQSimulator if config.simulator else IBMQBackend
                ret[config.backend_name] = backend_cls(
                    configuration=config,
                    provider=self._ibm_provider,
                    credentials=self.credentials,
                    api=self._api)
            except ModelValidationError as ex:
                logger.warning(
                    'Remote backend "%s" could not be instantiated due to an '
                    'invalid config: %s',
                    raw_config.get('backend_name',
                                   raw_config.get('name', 'unknown')),
                    ex)

        return ret
Ejemplo n.º 3
0
def configuration_from_server_data(
    raw_config: Dict,
    instance: str = "",
) -> Optional[Union[QasmBackendConfiguration, PulseBackendConfiguration]]:
    """Create an IBMBackend instance from raw server data.

    Args:
        raw_config: Raw configuration.
        instance: Service instance.

    Returns:
        Backend configuration.
    """
    # Make sure the raw_config is of proper type
    if not isinstance(raw_config, dict):
        logger.warning(  # type: ignore[unreachable]
            "An error occurred when retrieving backend "
            "information. Some backends might not be available.")
        return None
    try:
        _decode_backend_configuration(raw_config)
        try:
            return PulseBackendConfiguration.from_dict(raw_config)
        except (KeyError, TypeError):
            return QasmBackendConfiguration.from_dict(raw_config)
    except Exception:  # pylint: disable=broad-except
        logger.warning(
            'Remote backend "%s" for service instance %s could not be instantiated due to an '
            "invalid config: %s",
            raw_config.get("backend_name", raw_config.get("name", "unknown")),
            repr(instance),
            traceback.format_exc(),
        )
    return None
Ejemplo n.º 4
0
 def __init__(self):
     dirname = os.path.dirname(__file__)
     filename = "conf_manhattan.json"
     with open(os.path.join(dirname, filename)) as f_conf:
         conf = json.load(f_conf)
     configuration = PulseBackendConfiguration.from_dict(conf)
     configuration.backend_name = 'fake_manhattan'
     self._defaults = None
     self._properties = None
     super().__init__(configuration)
Ejemplo n.º 5
0
    def __init__(self):
        """
         0 ↔ 1 ↔ 3 ↔ 4
             ↕
             2
        """
        dirname = os.path.dirname(__file__)
        filename = "conf_burlington.json"
        with open(os.path.join(dirname, filename), "r") as f_conf:
            conf = json.load(f_conf)

        configuration = PulseBackendConfiguration.from_dict(conf)
        configuration.backend_name = 'fake_burlington'
        super().__init__(configuration)
Ejemplo n.º 6
0
    def __init__(self):
        """
         0 ↔ 1 ↔ 2
             ↕
             3
             ↕
             4
        """
        dirname = os.path.dirname(__file__)
        filename = "conf_valencia.json"
        with open(os.path.join(dirname, filename), "r") as f_conf:
            conf = json.load(f_conf)

        configuration = PulseBackendConfiguration.from_dict(conf)
        configuration.backend_name = 'fake_valencia'
        self._defaults = None
        self._properties = None
        super().__init__(configuration)
Ejemplo n.º 7
0
    def __init__(self):
        """
          00 ↔ 01 ↔ 02 ↔ 03 ↔ 04
                ↕         ↕
          05 ↔ 06 ↔ 07 ↔ 08 ↔ 09
           ↕         ↕         ↕
          10 ↔ 11 ↔ 12 ↔ 13 ↔ 14
                ↕         ↕
          15 ↔ 16 ↔ 17 ↔ 18 ↔ 19
        """
        dirname = os.path.dirname(__file__)
        filename = "conf_almaden.json"
        with open(os.path.join(dirname, filename), "r") as f_conf:
            conf = json.load(f_conf)

        configuration = PulseBackendConfiguration.from_dict(conf)
        configuration.backend_name = 'fake_almaden'
        super().__init__(configuration)
Ejemplo n.º 8
0
    def _discover_remote_backends(self,
                                  timeout: Optional[float] = None
                                  ) -> Dict[str, IBMQBackend]:
        """Return the remote backends available for this provider.

        Args:
            timeout: Maximum number of seconds to wait for the discovery of
                remote backends.

        Returns:
            A dict of the remote backend instances, keyed by backend name.
        """
        ret = OrderedDict()  # type: ignore[var-annotated]
        configs_list = self._api_client.list_backends(timeout=timeout)
        for raw_config in configs_list:
            # Make sure the raw_config is of proper type
            if not isinstance(raw_config, dict):
                logger.warning(
                    "An error occurred when retrieving backend "
                    "information. Some backends might not be available.")
                continue

            try:
                decode_backend_configuration(raw_config)
                if raw_config.get('open_pulse', False):
                    config = PulseBackendConfiguration.from_dict(raw_config)
                else:
                    config = QasmBackendConfiguration.from_dict(raw_config)
                backend_cls = IBMQSimulator if config.simulator else IBMQBackend
                ret[config.backend_name] = backend_cls(
                    configuration=config,
                    provider=self,
                    credentials=self.credentials,
                    api_client=self._api_client)
            except Exception as ex:  # pylint: disable=broad-except
                logger.warning(
                    'Remote backend "%s" for provider %s could not be instantiated due to an '
                    'invalid config: %s: %s',
                    raw_config.get('backend_name',
                                   raw_config.get('name', 'unknown')),
                    repr(self),
                    type(ex).__name__, ex)

        return ret
Ejemplo n.º 9
0
 def __init__(self):
     """
       00 ↔ 01 ↔ 02 ↔ 03 ↔ 04
        ↕                   ↕
       05 ↔ 06 ↔ 07 ↔ 08 ↔ 09
        ↕         ↕         ↕
       10 ↔ 11 ↔ 12 ↔ 13 ↔ 14
        ↕                   ↕
       15 ↔ 16 ↔ 17 ↔ 18 ↔ 19
     """
     dirname = os.path.dirname(__file__)
     filename = "conf_johannesburg.json"
     with open(os.path.join(dirname, filename)) as f_conf:
         conf = json.load(f_conf)
     configuration = PulseBackendConfiguration.from_dict(conf)
     configuration.backend_name = 'fake_johannesburg'
     self._defaults = None
     self._properties = None
     super().__init__(configuration)
Ejemplo n.º 10
0
    def _discover_remote_backends(self,
                                  timeout: Optional[float] = None
                                  ) -> Dict[str, IBMQBackend]:
        """Return the remote backends available.

        Args:
            timeout (float or None): number of seconds to wait for the discovery.

        Returns:
            dict[str:IBMQBackend]: a dict of the remote backend instances,
                keyed by backend name.
        """
        ret = OrderedDict()
        configs_list = self._api.list_backends(timeout=timeout)
        for raw_config in configs_list:
            # Make sure the raw_config is of proper type
            if not isinstance(raw_config, dict):
                logger.warning(
                    "An error occurred when retrieving backend "
                    "information. Some backends might not be available.")
                continue

            try:
                if raw_config.get('open_pulse', False):
                    config = PulseBackendConfiguration.from_dict(raw_config)
                else:
                    config = QasmBackendConfiguration.from_dict(raw_config)
                backend_cls = IBMQSimulator if config.simulator else IBMQBackend
                ret[config.backend_name] = backend_cls(
                    configuration=config,
                    provider=self,
                    credentials=self.credentials,
                    api=self._api)
            except ModelValidationError as ex:
                logger.warning(
                    'Remote backend "%s" could not be instantiated due to an '
                    'invalid config: %s',
                    raw_config.get('backend_name',
                                   raw_config.get('name', 'unknown')), ex)

        return ret
Ejemplo n.º 11
0
    def __init__(self):
        """
                       06                  17
                       ↕                    ↕
        00 ↔ 01 ↔ 04 ↔ 07 ↔ 10 ↔ 12 ↔ 15 ↔ 18 ↔ 20 ↔ 23
             ↕                   ↕                    ↕
             02                  13                  24
             ↕                   ↕                    ↕
             03 ↔ 05 ↔ 08 ↔ 11 ↔ 14 ↔ 16 ↔ 19 ↔ 22 ↔ 25 ↔ 26
                       ↕                    ↕
                       09                  20
        """
        dirname = os.path.dirname(__file__)
        filename = "conf_paris.json"
        with open(os.path.join(dirname, filename), "r") as f_conf:
            conf = json.load(f_conf)

        configuration = PulseBackendConfiguration.from_dict(conf)
        configuration.backend_name = 'fake_paris'
        self._defaults = None
        self._properties = None
        super().__init__(configuration)
Ejemplo n.º 12
0
 def _get_config_from_dict(self, conf):
     return PulseBackendConfiguration.from_dict(conf)