QCircuitNet
收藏资源简介:
QCircuitNet是第一个用于评估AI从编程语言角度设计和实现量子算法能力的基准数据集。它包含了一个全面的框架,涵盖了量子算法设计任务的关键特征,包括问题描述、量子电路代码、经典后处理和验证函数。数据集涵盖了从基本原语到高级应用的广泛量子算法,并具有自动验证和验证功能,允许迭代评估和交互推理而无需人工检查。此外,它还展示了作为训练数据集的潜力。
QCircuitNet is the first benchmark dataset for evaluating the capability of AI to design and implement quantum algorithms from the perspective of programming languages. It includes a comprehensive framework covering the key characteristics of quantum algorithm design tasks, including problem descriptions, quantum circuit code, classical post-processing, and verification functions. The dataset covers a wide range of quantum algorithms spanning from basic primitives to advanced applications, and is equipped with automatic verification and validation functions, enabling iterative evaluation and interactive inference without manual inspection. Additionally, it demonstrates its potential as a training dataset.
QCircuitNet Dataset
QCircuitNet 是一个用于评估人工智能在设计和实现量子算法方面能力的基准数据集,从编程语言的角度出发。
关键特性
- 综合框架:提供了一个通用框架,用于表述量子算法设计任务的关键特征,包括问题描述、量子电路代码、经典后处理和验证函数。
- 广泛的量子算法:涵盖了从基本原语到高级应用的广泛量子算法,并易于扩展到更多量子算法。
- 验证和验证函数:自动验证和验证函数,允许迭代评估和交互推理,无需人工检查。
- 训练潜力:通过原语微调结果,显示出作为训练数据集的潜力。
目录结构
📂 QCircuitNet/ │ ├──📂 Oracle Construction/ │ ├──📂 Quantum Logic Synthesis/ │ │ └── 包含教科书级别和高级的Oracle。 │ ├──📂 Problem Encoding/ │ └── Oracle编码应用场景。 │ ├──📂 Algorithm Design/ │ ├──📂 Quantum Computing/ │ │ └── 包含通用量子计算算法。 │ ├──📂 Quantum Information/ │ └── 包含与量子信息协议相关的任务。 │ ├──📂 Random Circuits/ ├──📂 Clifford/ │ └── 包含Clifford门集的随机电路。 ├──📂 Universal/ └── 包含通用门集的随机电路。
示例
问题描述
问题描述以文本文件形式提供,例如 {algorithm_name}_description.txt。以下是Simons Problem的示例模板:
text 给定一个黑盒函数 f: {0,1}^n → {0,1}^n。该函数保证根据一个秘密字符串 s ∈ {0, 1}^n, s ≠ 0^n 进行双射映射,其中给定 x_1 ≠ x_2, f(x_1) = f(x_2) 当且仅当 x_1 ⊕ x_2 = s。
设计一个量子算法来找到秘密字符串 s。该函数作为黑盒Oracle门名为 "Oracle" 提供在 oracle.inc 文件中,操作如下:
O_f |x⟩|y⟩ = |x⟩ |y ⊕ f(x)⟩
输入量子比特 |x⟩ 从 0 到 n-1 索引,输出量子比特 |f(x)⟩ 从 n 到 2n-1 索引。对于算法,提供:
- 使用QASM或Qiskit实现的量子电路。
- 后处理代码 run_and_analyze(circuit, aer_sim) 在Python中运行,模拟电路并返回基于模拟结果的秘密字符串 s。
生成代码
以下是生成Simons Problem量子电路的Qiskit代码,文件名为 {algorithm_name}_generation.py。
python from Qiskit import QuantumCircuit
def simon_algorithm(n, oracle): """生成Simon算法电路。
参数:
- n (int): 量子比特数
- oracle: Oracle函数
返回:
- QuantumCircuit: Simon算法电路
"""
# 创建一个2n量子比特的量子电路
simon_circuit = QuantumCircuit(2 * n, n)
# 初始化第一个寄存器到 |+> 状态
simon_circuit.h(range(n))
# 附加Simon的Oracle
simon_circuit.append(oracle, range(2 * n))
# 对第一个寄存器应用H门
simon_circuit.h(range(n))
# 测量第一个寄存器
simon_circuit.measure(range(n), range(n))
return simon_circuit
算法电路
OpenQASM 3.0文件存储特定设置的量子电路。以下是Simons算法在 n = 3 的示例,文件名为 {algorithm_name}_n{qubit_number}.qasm。
qasm OPENQASM 3.0; include "stdgates.inc"; include "oracle.inc"; bit[3] c; qubit[6] q; h q[0]; h q[1]; h q[2]; Oracle q[0], q[1], q[2], q[3], q[4], q[5]; h q[0]; h q[1]; h q[2]; c[0] = measure q[0]; c[1] = measure q[1]; c[2] = measure q[2];
后处理函数
此Python函数模拟电路并推导Simons Problem的最终答案,文件名为 {algorithm_name}_post_processing.py。
python from sympy import Matrix import numpy as np from Qiskit import transpile
def mod2(x): return x.as_numer_denom()[0] % 2
def solve_equation(string_list): """解 A^T * X = 0 mod 2 的零空间。""" M = Matrix(string_list).T M_I = Matrix(np.hstack([M, np.eye(M.shape[0], dtype=int)])) M_I_rref = M_I.rref(iszerofunc=lambda x: x % 2 == 0) M_I_final = M_I_rref[0].applyfunc(mod2)
if all(value == 0 for value in M_I_final[-1, :M.shape[1]]):
result_s = "".join(str(c) for c in M_I_final[-1, M.shape[1]:])
else:
result_s = "0" * M.shape[0]
return result_s
def run_and_analyze(circuit, aer_sim): n = circuit.num_qubits // 2 circ = transpile(circuit, aer_sim) results = aer_sim.run(circ, shots=n).result() counts = results.get_counts() equations = [list(map(int, result)) for result in counts if result != "0" * n] prediction = solve_equation(equations) if len(equations) > 0 else "0" * n return prediction
Oracle / 门定义
Oracle定义在文件 oracle.inc 中,用于算法设计任务。
qasm gate Oracle q[0], q[1], q[2], q[3], q[4], q[5] { cx q[0], q[3]; cx q[1], q[4]; cx q[2], q[5]; cx q[2], q[5]; x q[3]; }
Oracle信息
此信息存储在 oracle_info.txt 文件中,提供Oracle的附加元数据,例如秘密和密钥字符串。以下是Simons Problem在 n = 3 和测试用例2的示例:
text Secret string: 100 Key string: 001
验证函数
以下函数检查生成的模型的正确性,文件名为 {algorithm_name}_verification.py。
python from simon_utils import *
def check_model(qasm_string, code_string, n): """检查Simon模型。""" with open(f"test_oracle/n{n}/trial1/oracle.inc", "r") as file: oracle_def = file.read() full_qasm = plug_in_oracle(qasm_string, oracle_def) circuit = verify_qasm_syntax(full_qasm)
if circuit is None:
return -1
try:
exec(code_string, globals())
aer_sim = AerSimulator()
total_success, total_fail = 0, 0
shots = 10
for t in range(1, 11):
with open(f"test_oracle/n{n}/trial{t}/oracle_info.txt", "r") as file:
secret_string = re.search(r"Secret string: ([01]+)", file.read()).group(1)
circuit = transpile(circuit, aer_sim)
prediction = run_and_analyze(circuit.copy(), aer_sim)
if prediction == secret_string:
total_success += 1
else:
total_fail += 1
return total_success / (total_success + total_fail)
except Exception as e:
print(f"Error: {e}")
return -1
数据集创建脚本
此脚本从头开始创建数据集,生成电路,提取门定义,并确保数据集的有效性,文件名为 {algorithm_name}_dataset.py。
python import argparse
def main(): parser = argparse.ArgumentParser() parser.add_argument( "-f", "--func", choices=["qasm", "json", "gate", "check"], help="Function to call: generate qasm, json, extract gate, or check dataset." ) args = parser.parse_args()
if args.func == "qasm":
generate_circuit_qasm()
elif args.func == "json":
generate_dataset_json()
elif args.func == "gate":
extract_gate_definition()
elif args.func == "check":
check_dataset()
数据比例
以下图表提供了QCircuitNet数据集中不同数据类型的比例概览。
算法设计

Oracle构建

随机电路

分层采样
为了提供用户对数据集内容的直观视图,便于在GitHub页面上显示,并遵守GitHub关于文件数量和大小的政策,我们对数据集进行了分层采样。随机采样用于最小化手动选择中的固有偏差,从而保持数据集的多样性和代表性。
采样过程通过名为 hierarchical_sample 的函数实现,该函数处理数据集的根目录并根据其特定结构处理子目录。每个主要目录的采样方法如下:
-
Algorithm_Design
- Quantum_Computing:包含
bernstein_vazirani、deutsch_jozsa、generalized_simon_multi、generalized_simon_ternary、grover、phase_estimation、quantum_fourier_transformation、simon子目录。从每个子目录中随机采样 10 个文件。 - Quantum_Information:包含
ghz_state、quantum_key_distribution、quantum_teleportation、random_number_generator、superdense_coding、swap_test、w_state子目录。从每个子目录中随机采样 10 个文件。
- Quantum_Computing:包含
-
Oracle_Construction
- Problem_Encoding:未进行采样,保留所有内容。
- Quantum_Logic_Synthesis:包含
bernstein_vazirani、deutsch_jozsa、diffusion_operator、generalized_simon_multi、generalized_simon_ternary、grover、simon子目录。从每个子目录中随机采样 5 个子文件夹。
-
Random_Circuits
- clifford:包含
clifford_n{}子目录。从每个子目录中随机采样 10 个子文件夹。 - universal:包含
universal_n{}子目录。从每个子目录中随机采样 10 个子文件夹。
- clifford:包含
下载
我们在GitHub仓库中托管了数据集的演示版本,以展示其结构并提供示例数据。完整数据集请从 Google Drive 下载。




