_ibm_http_client.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:ProjectQ 作者: ProjectQ-Framework 项目源码 文件源码
def send(qasm, device='sim_trivial_2', user=None, password=None,
         shots=1, verbose=False):
    """
    Sends QASM through the IBM API and runs the quantum circuit.

    Args:
        qasm: QASM representation of the circuit to run.
        device (str): 'sim_trivial_2' or 'real' to run on simulator or on the
            real chip, respectively.
        user (str): IBM quantum experience user.
        password (str): IBM quantum experience user password.
        shots (int): Number of runs of the same circuit to collect statistics.
        verbose (bool): If True, additional information is printed, such as
            measurement statistics. Otherwise, the backend simply registers
            one measurement result (same behavior as the projectq Simulator).
    """
    try:
        # check if the device is online
        if device in ['ibmqx2', 'ibmqx4']:
            url = 'Backends/{}/queue/status'.format(device)
            r = requests.get(urljoin(_api_url_status, url))
            online = r.json()['state']

            if not online:
                print("The device is offline (for maintenance?). Use the "
                      "simulator instead or try again later.")
                raise DeviceOfflineError("Device is offline.")
            if device == 'ibmqx2':
                device = 'real'

        if verbose:
            print("Authenticating...")
        user_id, access_token = _authenticate(user, password)
        if verbose:
            print("Running code...")
        execution_id = _run(qasm, device, user_id, access_token, shots)
        if verbose:
            print("Waiting for results...")
        res = _get_result(execution_id, access_token)
        if verbose:
            print("Done.")
        return res
    except requests.exceptions.HTTPError as err:
        print("There was an error running your code:")
        print(err)
    except requests.exceptions.RequestException as err:
        print("Looks like something is wrong with server:")
        print(err)
    except KeyError as err:
        print("Failed to parse response:")
        print(err)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号