由于量子测量会导致量子态的坍缩,也就是说,每一次测量之后,都需要重新制备量子态 。而制备一个产生随机数的量子态,我们可以用如下所示的H量子门操作来实现 。其具体的矩阵表述可以参考这篇博客,量子计算本质上就是用特殊的硬件方法,快速地实现大规模的矩阵运算,这也是其展现量子优势的来源 。
这里我们直接使用开源量子计算框架ProjectQ来实现这个门操作,就省去了自己去模拟实习的繁琐操作:
# random_number.pyfrom projectq import MainEnginefrom projectq.ops import H, Measuredef random_number_generator():eng = MainEngine()qubit = eng.allocate_qubit()H | qubiteng.flush()Measure | qubitrandom_number = int(qubit)eng.flush(deallocate_qubits=True)return random_numberif __name__ == '__main__':random_number = random_number_generator()print ('Random number generated by quantum simulator is: {}'.format(random_number))这里顺带的介绍下ProjectQ的一些操作实现的方法,在ProjectQ中将众多的量子比特作为寄存器来进行调用,这些寄存器都在给定的engine下赋予了各种操作,如量子门操作、打印线路、编译优化等 。编程框架与实际的量子比特并不在架构上的同一个层次,每次传送指令都是通过eng.flush()打包进行编译优化之后,再传送到后端去逐一执行 。因此如果要测量量子比特(在ProjectQ模拟器中必须只能全部执行测量操作,不能只测量其中的某一个部分),或者是读取某一个量子态的概率时,都需要先执行eng.flush()才行 。接下来简单看下上述代码的执行效果:
[dechin@dechin-manjaro pytest]$ python3 random_number.py Random number generated by quantum simulator is: 1[dechin@dechin-manjaro pytest]$ python3 random_number.py Random number generated by quantum simulator is: 1[dechin@dechin-manjaro pytest]$ python3 random_number.py Random number generated by quantum simulator is: 0[dechin@dechin-manjaro pytest]$ python3 random_number.py Random number generated by quantum simulator is: 0[dechin@dechin-manjaro pytest]$ python3 random_number.py Random number generated by quantum simulator is: 1在这个案例中,每一次都会产生一个新的随机数0或者1,并且在量子比特使用结束之后,通过eng.flush(deallocate_qubits=True)可以初始化该寄存器,所有的振幅和测量结果都会被丢弃 。
注意!!!需要注意的是,由于这里我们是使用了模拟器来模拟量子计算机的行为,因此得到的随机数结果还是伪随机数 。只有当这一串代码执行在量子处理器上时,我们才能得到真随机数 。
pytest测试安装pytest库首先我们可以通过pip来更新安装pytest:
[dechin@dechin-manjaro pytest]$ python3 -m pip install -U pytestCollecting pytestDownloading pytest-6.2.2-py3-none-any.whl (280 kB)|████████████████████████████████| 280 kB 464 kB/s Requirement already satisfied, skipping upgrade: iniconfig in /home/dechin/anaconda3/lib/python3.8/site-packages (from pytest) (1.1.1)Requirement already satisfied, skipping upgrade: pluggy<1.0.0a1,>=0.12 in /home/dechin/anaconda3/lib/python3.8/site-packages (from pytest) (0.13.1)Requirement already satisfied, skipping upgrade: attrs>=19.2.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from pytest) (20.3.0)Requirement already satisfied, skipping upgrade: toml in /home/dechin/anaconda3/lib/python3.8/site-packages (from pytest) (0.10.1)Requirement already satisfied, skipping upgrade: packaging in /home/dechin/anaconda3/lib/python3.8/site-packages (from pytest) (20.4)Requirement already satisfied, skipping upgrade: py>=1.8.2 in /home/dechin/anaconda3/lib/python3.8/site-packages (from pytest) (1.9.0)Requirement already satisfied, skipping upgrade: pyparsing>=2.0.2 in /home/dechin/anaconda3/lib/python3.8/site-packages (from packaging->pytest) (2.4.7)Requirement already satisfied, skipping upgrade: six in /home/dechin/anaconda3/lib/python3.8/site-packages (from packaging->pytest) (1.15.0)Installing collected packages: pytestAttempting uninstall: pytestFound existing installation: pytest 6.2.1Uninstalling pytest-6.2.1:Successfully uninstalled pytest-6.2.1Successfully installed pytest-6.2.2安装完成后,可以通过如下指令来查看安装的pytest版本:
以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!
「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助:- js实现文件下载功能 html图片按钮代码
- 自己制作一个网站的方法 html5制作网页教程
- 浪漫的html表白源代码 网页表白代码大全
- html页面获取url参数 jquery获取url中的参数有哪些
- 讲解html中title标签的作用 html标题代码
- 网页设计菜单栏导航实例 html5导航栏代码
- 分享文件转换工具类 html转换成word文档
- html左图右文布局方法 html左右布局模板
- c语言二维数组定义和赋值 随机数c语言代码
- html简易计算器代码 javascript计算器代码