用 150 行 Python 代码写的量子计算模拟器
简评:让你更轻松地明白,量子计算机如何遵循线性代数计算的。
这是个 GItHub 项目,可以简单了解一下。
qusim.py 是一个多量子位的量子计算机模拟器(玩具?),用 150 行的 python 所编写。
这段代码可以让你轻松了解量子计算机如何遵循线性代数来计算的!
from QuSim import QuantumRegister
Introduction
Here Will Be A Few Example of Different
Quantum States / Algorithms, So You Can
Get A Feel For How The Module Works, and
Some Algorithmic Ideas
Quantum Measurement
This experiment will prepare 2 states, of a
Single qubit, and of 5 qubits, and will just
Measure them
OneQubit = QuantumRegister(1) # New Quantum Register of 1 Qubit
print('One Qubit: ' + OneQubit.measure()) # Should Print 'One Qubit: 0'
FiveQubits = QuantumRegister(5) # New Quantum Register of 5 Qubits
Should Print 'Five Qubits: 00000'
print('Five Qubits: ' + FiveQubits.measure())
Swap 2 Qubits
Here, We Will Apply a Pauli-X Gate / NOT Gate
To the first qubit, and then after the algorithm,
it will be swapped to the second qubit.
Swap = QuantumRegister(2) # New Quantum Register of 2 qubits
Swap.applyGate('X', 1) # Apply The NOT Gate. If Measured Now, it should be 10
Start the swap algorithm
Swap.applyGate('CNOT', 1, 2)
Swap.applyGate('H', 1)
Swap.applyGate('H', 2)
Swap.applyGate('CNOT', 1, 2)
Swap.applyGate('H', 1)
Swap.applyGate('H', 2)
Swap.applyGate('CNOT', 1, 2)
End the swap algorithm
print('SWAP: |' + Swap.measure() + '>') # Measure the State, Should be 01
Fair Coin Flip
Shown in this 'Experiment', is a so called 'Fair Coin Flip',
Where a state will be prepared, that has an equal chance of
Flipping to Each Possible State. to do this, the Hadamard
Gate will be used.
New Quantum Register of 1 Qubit (As a coin has only 2 states)
FairCoinFlip = QuantumRegister(1)
If measured at this point, it should be |0>
Apply the hadamard gate, now theres an even chance of measuring 0 or 1
FairCoinFlip.applyGate('H', 1)
Now, the state will be measured, flipping the state to
either 0 or 1. If its 0, we will say "Heads", or if its
1, we will say "Tails"
FairCoinFlipAnswer = FairCoinFlip.measure() # Now its flipped, so we can test
if FairCoinFlipAnswer == '0':
print('FairCoinFlip: Heads')
elif FairCoinFlipAnswer == '1':
print('FairCoinFlip: Tails')
CNOT Gate
In this experiment, 4 states will be prepared, {00, 01, 10, 11}
And then the same CNOT Gate will be run on them,
To Show The Effects of the CNOT. The Target Qubit will be 2, and the control 1
New Quantum Register of 2 Qubits, done 4 times.
If any are measured at this time, the result will be 00
ZeroZero = QuantumRegister(2)
ZeroOne = QuantumRegister(2)
OneZero = QuantumRegister(2)
OneOne = QuantumRegister(2)
Now prepare Each Into The State Based On Their Name
ZeroZero Will be left, as thats the first state anyway
ZeroOne.applyGate('X', 2)
OneZero.applyGate('X', 1)
OneOne.applyGate('X', 1)
OneOne.applyGate('X', 2)
Now, a CNOT Will Be Applied To Each.
ZeroZero.applyGate('CNOT', 1, 2)
ZeroOne.applyGate('CNOT', 1, 2)
OneZero.applyGate('CNOT', 1, 2)
OneOne.applyGate('CNOT', 1, 2)
Print the results.
print('CNOT on 00: |' + ZeroZero.measure() + '>')
print('CNOT on 01: |' + ZeroOne.measure() + '>')
print('CNOT on 10: |' + OneZero.measure() + '>')
print('CNOT on 11: |' + OneOne.measure() + '>')
主要代码来自:corbett/QuantumComputing.
如果你对用 RUST 所写的高效、高性能的硬件量子计算模拟器有兴趣,可以点击 QCGPU 来查看更多内容。
GITHUB 地址:adamisntdead/QuSimPy
用 150 行 Python 代码写的量子计算模拟器的更多相关文章
- 150+行Python代码实现带界面的数独游戏
150行代码实现图形化数独游戏 Github地址,欢迎各位大佬们fork.star啥的,感谢: 今天闲着没事干,以前做过html+js版的数独,这次做个python版本的,界面由pygame完成,数独 ...
- python 之路,200行Python代码写了个打飞机游戏!
早就知道pygame模块,就是没怎么深入研究过,恰逢这周未没约到妹子,只能自己在家玩自己啦,一时兴起,花了几个小时写了个打飞机程序. 很有意思,跟大家分享下. 先看一下项目结构 "" ...
- 10 行 Python 代码写的模糊查询
导语: 模糊匹配可以算是现代编辑器(在选择要打开的文件时)的一个必备特性了,它所做的就是根据用户输入的部分内容,猜测用户想要的文件名,并提供一个推荐列表供用户选择. 样例如下: Vim (Ctrl-P ...
- (转)python 之路,200行Python代码写了个打飞机游戏!
原文:https://www.cnblogs.com/alex3714/p/7966656.html
- 一个 11 行 Python 代码实现的神经网络
一个 11 行 Python 代码实现的神经网络 2015/12/02 · 实践项目 · 15 评论· 神经网络 分享到:18 本文由 伯乐在线 - 耶鲁怕冷 翻译,Namco 校稿.未经许可,禁止转 ...
- 200行Python代码实现2048
200行Python代码实现2048 一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境,实验中会用到桌面 ...
- 10 行 Python 代码实现模糊查询/智能提示
10 行 Python 代码实现模糊查询/智能提示 1.导语: 模糊匹配可以算是现代编辑器(如 Eclipse 等各种 IDE)的一个必备特性了,它所做的就是根据用户输入的部分内容,猜测用户想要的 ...
- 20行Python代码爬取王者荣耀全英雄皮肤
引言王者荣耀大家都玩过吧,没玩过的也应该听说过,作为时下最火的手机MOBA游戏,咳咳,好像跑题了.我们今天的重点是爬取王者荣耀所有英雄的所有皮肤,而且仅仅使用20行Python代码即可完成. 准备工作 ...
- 200行PYTHON代码实现贪吃蛇
200行Python代码实现贪吃蛇 话不多说,最后会给出全部的代码,也可以从这里Fork,正文开始: 目前实现的功能列表: 贪吃蛇的控制,通过上下左右方向键: 触碰到边缘.墙壁.自身则游戏结束: 接触 ...
随机推荐
- 为什么我们要使用ssh框架技术,及感想
前言: 在公司从C++转向Java Web方向大概有3个月(11月初-1月底)了.三个月前对Java和Web还几乎是零基础.然后从安装Eclipse,MySQL,tomcat开始,到学习HTML/CS ...
- 二叉查找树之 Java的实现
参考:http://www.cnblogs.com/skywang12345/p/3576452.html 二叉查找树简介 二叉查找树(Binary Search Tree),又被称为二叉搜索树.它是 ...
- Memcache架构新思考
2011年初Marc Kwiatkowski通过Memecache@Facebook介绍了Facebook的Memcache架构,现在重新审视这个架构,仍有很多方面在业界保持先进性.作为weibo内部 ...
- layerX参数构建
var defaults = { name: 'layerX', url: [ { name: 'sub', url: '', focus: true } ], parent: { name: '', ...
- Commandline OpenVPN client on Mac OSX with macports
http://www.tuicool.com/articles/FjuyQj 注:文中有些内容做了修改,特别是那个配置文件,不能直接抄着用. Most people use TunnelBrick ...
- 分布式文件系统MFS、Ceph、GlusterFS、Lustre的比较
原文:http://blog.csdn.net/metaxen/article/details/7108958 MooseFS(MFS) Ceph GlusterFS Lustre Metadata ...
- vfd电子时钟制作
17年也没干个啥,年后就去折腾着玩意儿了,也不知道我折腾它还是它折腾我.反正总之现在勉强可以交作业了,呵呵 硬件: 1.罗耶振荡电路输出一路4v交流,一路25v交流 其中4v直接驱动灯丝,另一路经电桥 ...
- 建站记录:设置apache .htaccess文件给网站添加404错误处理页面
有些空间服务商会在后台设置中,提供这个选项,可以直观地设置404错误指向的页面,这一点很方便,比如我之前用的阿里云虚拟主机就可以在控制台直接设置. 新租用的香港主机后台没有找到选取文件的地方,只是可以 ...
- Flask类的属性和方法大全
Flask Property__class____dict____doc____module__app_ctx_globals_classconfig_classdebugdefault_config ...
- c#学习笔记 day_one
C#学习笔记 day one Chapter 1 c#概述 1.1 c#概述 C#是微软设计的,简洁的,类型安全的,面向对象的语言.它以c/c++作为基础.它的开发环境是visual studio,最 ...