{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 量子随机数生成器\n", "*Copyright (c) 2023 Institute for Quantum Computing, Baidu Inc. All Rights Reserved.*\n", "\n", "随机数生成器是通过算法、物理信号、环境噪音等来产生随机数列的方法或装置,其在如密码学、机器学习等诸多领域扮演重要角色,它直接影响了密码的可破解性、机器学习模型的泛化性能。经典计算机通过如线性同余法等伪随机算法生成伪随机数,存在一定的缺陷,比如存在周期性等。而量子计算机可以利用量子测量的不确定性生成真随机数,具有不可预测性。本模型库封装了能够调用百度量子云平台上线的百度自研量子计算机、模拟器、第三方硬件生成随机数的方法。\n", "\n", "## 生成原理\n", "利用量子计算机可对零初始态 $|0\\rangle$ 作用 $H$ 门,得到量子态 $|\\psi\\rangle$, \n", "$$\n", "|\\psi\\rangle = \\frac{\\sqrt2}{2} |0\\rangle + \\frac{\\sqrt2}{2} |1\\rangle .\n", "$$\n", "该量子态 $|\\psi\\rangle$ 在 Pauli-$Z$ 量子测量(即计算基测量,以 $|0\\rangle\\langle 0|$,$|1\\rangle \\langle 1 |$ 为测量算子)下为 $0$ 或为 $1$ 的概率均为 $\\frac12$。量子态坍缩的不确定性可以保证得到的 $0$ 或 $1$ 的结果为真随机。重复上述流程即可得到随机比特串,流程示意图如下。\n", "![random_number](./randnum_CN.png)\n", "\n", "若实际量子计算机为无噪声理想量子计算机,则测量结果为真随机。不过当前量子计算机均存在一定的噪声,因此我们同时封装了经典的隐私增强算法 [1],其可以通过利用两台独立的量子计算机(或一台量子计算机的两个独立比特)分别测量得到更长的比特串数据并对两者数据进行后处理提取较短的比特串数据,从而过滤噪声的影响。提取过程示意图如下。\n", "![extractor](./extractor_CN.png)\n", "\n", "## 如何使用\n", "### 快速使用\n", "用户可以通过在命令行下输入 `python randnum.py --config config.toml` 即可运行,其中 `config.toml` 为模型配置文件,用户可修改该文件内的参数或输入其它文件实现自定义参数。\n", "### 在线演示\n", "同时,我们这里给出一个在线演示的版本,可以在线进行调试。首先定义配置文件的内容:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "example_toml = r'''\n", "# 所需比特串长度\n", "bit_len = 10\n", "\n", "# 处理器硬件\n", "backend = 'local_baidu_sim2' # 本地模拟器\n", "# backend = 'cloud_baidu_qpu_qian' # 百度自研量子计算机 乾始 (Qian)\n", "\n", "# 用户 token,在使用云端服务时需要输入\n", "token = ''\n", "\n", "# 是否使用隐私增强后处理,toml 文件中 true 和 false 都应为小写\n", "extract = false\n", "\n", "# 信息安全参数,越小则越接近真随机(后处理所需参数,不启动后处理时无需修改)\n", "security = 1e-8\n", "\n", "# 处理器硬件 1 和 2 的最小熵(后处理所需参数,不启动后处理时无需修改,取值范围 (0,1))\n", "min_entr_1 = 0.9 \n", "min_entr_2 = 0.9\n", "\n", "# 日志文件路径\n", "log_path = './qrng.log'\n", "'''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "接下来执行模型运行过程。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Applications/anaconda3/envs/pqtest/lib/python3.7/site-packages/paddle/tensor/creation.py:125: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. \n", "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n", " if data.dtype == np.object:\n" ] }, { "data": { "text/plain": [ "[0, 0, 0, 1, 1, 1, 1, 0, 1, 0]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import warnings\n", "import toml\n", "from paddle_quantum.data_analysis.rand_num import random_number_generation\n", "warnings.filterwarnings('ignore')\n", "random_number_generation(**toml.loads(example_toml))" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "在这里,我们只需要修改 `example_toml` 字典中的参数,再运行上文全部代码,就可以快速自定义调试。如需使用百度自研量子计算机“乾始”,可将 `backend` 参数的输入修改为 ` 'cloud_baidu_qpu_qian'` (需保留引号,python 字符串数据类型),并输入自己账户 `token`。(若无 `token` 可前往[百度量易伏平台](https://quantum-hub.baidu.com/)注册。)\n", "## 注意事项\n", "- 请保持环境中 `Qcompute` 版本更新,过低版本会导致新上线后端无法识别。后端 `'cloud_baidu_qpu_qian'` 要求 `>= 3.0.0`。\n", "- 使用百度量子云平台的后端时需输入 `token`,请留意个人账户中对应云服务的剩余可用时长。\n", "- 使用云服务时请留意[百度量子云平台](https://quantum-hub.baidu.com/)的各机器运行状态和服务时间。\n", "\n", "## 引用信息\n", "```\n", "@article{hayashi2016more,\n", " title={More efficient privacy amplification with less random seeds via dual universal hash function},\n", " author={Hayashi, Masahito and Tsurumaru, Toyohiro},\n", " journal={IEEE Transactions on Information Theory},\n", " volume={62},\n", " number={4},\n", " pages={2213--2232},\n", " year={2016},\n", " publisher={IEEE}\n", "}\n", "```\n", "## 参考文献\n", "[1] Hayashi, Masahito, and Toyohiro Tsurumaru. \"More efficient privacy amplification with less random seeds via dual universal hash function.\" IEEE Transactions on Information Theory 62.4 (2016): 2213-2232." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.15" }, "vscode": { "interpreter": { "hash": "d3caffbb123012c2d0622db402df9f37d80adc57c1cef1fdb856f61446d88d0a" } } }, "nbformat": 4, "nbformat_minor": 2 }