Skip to content

QPilotServiceBase

QPilotServiceBase 是与 QPilot OS 量子计算平台交互的主要服务类。它提供了在各种后端(包括模拟器和真实量子芯片)上提交量子程序、查询结果和管理任务的方法。

签名

python
QPilotServiceBase(url: str, log_cout: bool = False, api_key: str = None)
QPilotServiceBase(url: str, log_cout: bool = False, username: str = None, password: str = None)

参数

参数类型默认值描述
urlstr必填QPilot OS 服务器的 URL
log_coutboolFalse是否将日志输出到控制台
api_keystrNone用于身份验证的 API 密钥
usernamestrNone用于凭据认证的用户名
passwordstrNone用于凭据认证的密码

支持两种身份验证模式:基于 API 密钥和基于用户名/密码。

方法

配置

init_config

python
init_config(url: str, log_cout: bool) -> None

初始化或重新配置服务连接。

参数类型描述
urlstrQPilot OS 服务器 URL
log_coutbool是否启用控制台日志

get_token

python
get_token(rep_json: str) -> str

从 JSON 响应字符串中提取身份验证令牌。

参数类型描述
rep_jsonstr来自身份验证端点的 JSON 响应

消息构建

build_init_msg

python
build_init_msg(api_key: str) -> str

构建用于与 QPilot OS 服务器进行身份验证的初始化消息。

参数类型描述
api_keystr用于身份验证的 API 密钥

build_task_msg

python
build_task_msg(prog: QProg, shot: int, chip_id: str, is_amend: bool,
               is_mapping: bool, is_optimization: bool,
               specified_block: list[int], task_describe: str,
               point_lable: int, priority) -> str

构建真实芯片测量任务消息体。

参数类型描述
progQProg量子程序
shotint执行次数
chip_idstr目标芯片标识符
is_amendbool是否应用修正
is_mappingbool是否应用量子比特映射
is_optimizationbool是否应用线路优化
specified_blocklist[int]指定块约束
task_describestr任务描述
point_lableint任务的点标签
priorityint任务优先级

build_task_proto_msg

python
build_task_proto_msg(shot: int, chip_id: str, is_amend: bool,
                     is_mapping: bool, is_optimization: bool,
                     specified_block: list[int], task_describe: str,
                     task_type, point_lable: int, priority) -> str

使用协议缓冲区(Protocol Buffer)格式构建真实芯片测量任务消息。

build_expectation_task_msg

python
build_expectation_task_msg(prog: QProg, hamiltonian,
                           qubits: list[int] = [], shot: int = 1000,
                           chip_id: str = "any_quantum_chip",
                           is_amend: bool = True, is_mapping: bool = True,
                           is_optimization: bool = True,
                           specified_block: list[int] = [],
                           task_describe: str = "") -> str

构建用于哈密顿量期望值计算的期望值任务消息。

参数类型默认值描述
progQProg必填量子程序
hamiltonianHamiltonian必填目标哈密顿量
qubitslist[int][]用于计算的量子比特子集
shotint1000执行次数
chip_idstr"any_quantum_chip"目标芯片标识符
is_amendboolTrue应用修正
is_mappingboolTrue应用量子比特映射
is_optimizationboolTrue应用优化
specified_blocklist[int][]块约束
task_describestr""任务描述

build_qst_task_msg

python
build_qst_task_msg(prog: QProg, shot: int = 1000,
                   chip_id: str = "any_quantum_chip",
                   is_amend: bool = True, is_mapping: bool = True,
                   is_optimization: bool = True,
                   specified_block: list[int] = [],
                   task_describe: str = "") -> str

构建量子态层析(Quantum State Tomography, QST)任务消息。

build_query_msg

python
build_query_msg(task_id: str) -> str

构建用于检查任务状态的查询消息。

参数类型描述
task_idstr要查询的任务标识符

执行 -- 同步

run

提交量子程序并等待结果。多个重载版本支持 QProgstr(OriginIR)、list[QProg]list[str] 作为输入。

python
# Single program
run(prog: QProg, shot: int = 1000, chip_id: str = "any_quantum_chip",
    is_amend: bool = True, is_mapping: bool = True, is_optimization: bool = True,
    specified_block: list[int] = [], describe: str = "",
    point_lable: int = 0) -> str

# Single OriginIR string
run(ir: str, shot: int = 1000, chip_id: str = "any_quantum_chip",
    is_amend: bool = True, is_mapping: bool = True, is_optimization: bool = True,
    specified_block: list[int] = [], describe: str = "",
    point_lable: int = 0) -> str

# Multiple programs
run(prog: list[QProg], shot: int = 1000, chip_id: str = "any_quantum_chip",
    is_amend: bool = True, is_mapping: bool = True, is_optimization: bool = True,
    specified_block: list[int] = [], describe: str = "",
    point_lable: int = 0) -> str

# Multiple OriginIR strings
run(ir: list[str], shot: int = 1000, chip_id: str = "any_quantum_chip",
    is_amend: bool = True, is_mapping: bool = True, is_optimization: bool = True,
    specified_block: list[int] = [], describe: str = "",
    point_lable: int = 0) -> str

# With configuration string
run(prog: list[QProg], config_str: str) -> str
参数类型默认值描述
progQProg or list[QProg]必填要执行的量子程序
irstr or list[str]必填要执行的 OriginIR 字符串
shotint1000执行次数
chip_idstr"any_quantum_chip"目标芯片标识符
is_amendboolTrue应用修正
is_mappingboolTrue应用量子比特映射
is_optimizationboolTrue应用线路优化
specified_blocklist[int][]块约束
describestr""任务描述
point_lableint0点标签
config_strstr必填JSON 配置字符串

返回值: 结果字符串。

run_simulator

python
run_simulator(prog: QProg, shot: int) -> dict[str, float]
run_simulator(prog: QProg, qubit_vec: list[int]) -> dict[str, float]

在本地模拟器上运行量子程序。

参数类型描述
progQProg要模拟的量子程序
shotint测量次数
qubit_veclist[int]要测量的量子比特(部分测量)

real_chip_measure_prob_count

提交真实芯片测量任务并返回概率计数。支持 QProgstrlist[QProg]list[str] 输入。

python
real_chip_measure_prob_count(prog: QProg, shot: int = 1000,
                            chip_id: str = "any_quantum_chip",
                            is_mapping: bool = True, is_optimization: bool = True,
                            specified_block: list[int] = [], describe: str = "",
                            point_lable: int = 0) -> str
参数类型默认值描述
progQProg or str or list必填量子程序或 OriginIR
shotint1000执行次数
chip_idstr"any_quantum_chip"目标芯片
is_mappingboolTrue应用量子比特映射
is_optimizationboolTrue应用线路优化
specified_blocklist[int][]块约束
describestr""任务描述
point_lableint0点标签

real_chip_expectation

python
real_chip_expectation(prog: QProg, hamiltonian, qubits: list[int] = [],
                      shot: int = 1000, chip_id: str = "any_quantum_chip",
                      is_amend: bool = True, is_mapping: bool = True,
                      is_optimization: bool = True,
                      specified_block: list[int] = [],
                      describe: str = "") -> str

在真实芯片上计算哈密顿量的期望值。此方法已弃用,请改用 runasync_run 方法。

执行 -- 异步

async_run

提交量子程序并立即返回任务 ID。支持与 run 相同的输入类型,并额外提供 is_prob_counts 参数。

python
# Single program
async_run(prog: QProg, shot: int = 1000, chip_id: str = "any_quantum_chip",
          is_amend: bool = True, is_mapping: bool = True, is_optimization: bool = True,
          specified_block: list[int] = [], is_prob_counts: bool = True,
          describe: str = "", point_lable: int = 0) -> str

# With configuration string
async_run(prog: list[QProg], config_str: str) -> str
参数类型默认值描述
progQProg or str or list必填量子程序或 OriginIR
shotint1000执行次数
chip_idstr"any_quantum_chip"目标芯片
is_amendboolTrue应用修正
is_mappingboolTrue应用量子比特映射
is_optimizationboolTrue应用优化
specified_blocklist[int][]块约束
is_prob_countsboolTrue返回概率计数
describestr""任务描述
point_lableint0点标签
config_strstr必填JSON 配置字符串

返回值: 用于后续查询的任务 ID 字符串。

async_real_chip_expectation

python
async_real_chip_expectation(prog: QProg, hamiltonian, qubits: list[int] = [],
                            shot: int = 1000, chip_id: str = "any_quantum_chip",
                            is_amend: bool = True, is_mapping: bool = True,
                            is_optimization: bool = True,
                            specified_block: list[int] = [],
                            describe: str = "") -> str

在真实芯片上进行异步哈密顿量期望值计算。此方法已弃用,请改用 async_run

量子态层析

async_real_chip_qst

python
async_real_chip_qst(prog: QProg, shot: int = 1000,
                    chip_id: str = "any_quantum_chip",
                    is_amend: bool = True, is_mapping: bool = True,
                    is_optimization: bool = True,
                    specified_block: list[int] = [],
                    describe: str = "", point_lable: int = 0) -> str

提交异步量子态层析(QST)任务。返回一个任务 ID。

async_real_chip_qst_density

python
async_real_chip_qst_density(prog: QProg, shot: int = 1000,
                            chip_id: str = "any_quantum_chip",
                            is_amend: bool = True, is_mapping: bool = True,
                            is_optimization: bool = True,
                            specified_block: list[int] = [],
                            describe: str = "", point_lable: int = 0) -> str

提交异步 QST 密度矩阵计算任务。返回一个任务 ID。

async_real_chip_qst_fidelity

python
async_real_chip_qst_fidelity(prog: QProg, shot: int = 1000,
                             chip_id: str = "any_quantum_chip",
                             is_amend: bool = True, is_mapping: bool = True,
                             is_optimization: bool = True,
                             specified_block: list[int] = [],
                             describe: str = "", point_lable: int = 0) -> str

提交异步 QST 保真度计算任务。返回一个任务 ID。

错误缓解

noise_learning

python
noise_learning(parameter_json: str) -> str

使用给定的 JSON 参数提交噪声学习任务。

em_compute

python
em_compute(parameter_json: str) -> str

使用给定的 JSON 参数同步运行错误缓解(Error Mitigation)计算。

async_em_compute

python
async_em_compute(parameter_json: str) -> str

异步提交错误缓解计算任务。

结果检索

query_result

python
query_result(task_id: str) -> list
query_result(task_id: str, is_save: bool, file_path: str = "") -> list

根据任务 ID 查询任务结果。

参数类型默认值描述
task_idstr必填任务标识符
is_savebool必填是否将结果保存到文件
file_pathstr""用于保存结果的文件路径

返回值: 列表 [state, result_vec, err_code, err_info],其中:

  • state (str):任务状态 -- "2" 表示运行中,"3" 表示已完成,"4" 表示失败
  • result_vec (list):结果数据字符串
  • err_code (ErrorCode):错误代码(如有)
  • err_info (str):错误信息字符串

query_task_state_vec

python
query_task_state_vec(task_id: str) -> list

查询任务状态和结果向量。返回格式与 query_result 相同。

query_compile_prog

python
query_compile_prog(task_id: str, without_compensate: bool = True) -> list

查询给定任务的编译后的程序。

参数类型默认值描述
task_idstr必填任务标识符
without_compensateboolTrue是否返回不带角度补偿的程序

返回值: 列表 [err_code, err_info, compile_prog]

get_measure_result

python
get_measure_result(task_id: str) -> list

检索已完成任务的测量结果。

返回值: 列表 [result, err_code, err_info],其中 result 是将态字符串映射到测量值的字典列表。

get_expectation_result

python
get_expectation_result(task_id: str) -> list

检索已完成任务的期望值结果。

返回值: 列表 [expectation, err_code, err_info],其中 expectation 是浮点数。

_get_qst_result

python
_get_qst_result(task_id: str) -> list

检索已完成任务的 QST 测量结果。

返回值: 列表 [result, err_code, err_info]

_get_qst_density_result

python
_get_qst_density_result(task_id: str) -> list

检索 QST 密度矩阵结果。

返回值: 列表 [density, err_code, err_info]

_get_qst_fidelity_result

python
_get_qst_fidelity_result(task_id: str) -> list

检索 QST 保真度结果。

返回值: 列表 [fidelity, err_code, err_info],其中 fidelity 是浮点数。

结果解析

parse_task_result

python
parse_task_result(result_str: str) -> dict[str, float]

将结果字符串解析为将态字符串映射到浮点值的字典。

parse_probability_result

python
parse_probability_result(result_str: list[str]) -> list[dict[str, float]]

将结果字符串列表解析为包含概率值的字典列表。

parse_prob_counts_result

python
parse_prob_counts_result(result_str: list[str]) -> list[dict[str, int]]

将结果字符串列表解析为包含整型计数值的字典列表。

parser_sync_result

python
parser_sync_result(json_str: str) -> list[dict[str, float]]

将同步结果 JSON 字符串解析为概率数据。

parser_expectation_result

python
parser_expectation_result(json_str: str) -> list[list[float]]

解析期望值结果 JSON 字符串。此方法已弃用,请改用 Python 的 json 库。

网络

tcp_recv

python
tcp_recv(ip: str, port: int, task_id: str) -> list

通过 TCP 连接接收任务结果。

参数类型描述
ipstrTCP 服务器 IP 地址
portintTCP 服务器端口
task_idstr要检索的任务标识符

返回值: 列表 [is_ok, response],其中 is_ok 是布尔值,response 是结果字符串。

工具

output_version

python
output_version() -> str

返回 QPilot OS 服务的版本字符串。

示例

python
from pyqpanda3.pilot_service import QPilotService
from pyqpanda3.core import QCircuit, H, CNOT, measure

# 使用 API 密钥初始化
service = QPilotService(
    url="https://qpilot.originquantum.cn",
    api_key="your_api_key"
)

# 创建量子程序
circuit = QCircuit(2)
circuit << H(0) << CNOT(0, 1)
prog = QProg()
prog << circuit << measure([0, 1], [0, 1])

# 同步执行
result = service.run(prog, shot=1000, chip_id="simulation")

# 异步执行
task_id = service.async_run(prog, shot=1000, chip_id="real_chip")

# 查询结果
result_dict = service.query_result(task_id)
print(f"State: {result_dict['taskState']}, Results: {result_dict['taskResult']}")

# 解析结果
if result_dict['taskState'] == "3":  # 已完成
    parsed = service.parse_probability_result(result_dict['taskResult'])
    print(f"Parsed results: {parsed}")

另见

Released under the MIT License.