用 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,正文开始: 目前实现的功能列表: 贪吃蛇的控制,通过上下左右方向键: 触碰到边缘.墙壁.自身则游戏结束: 接触 ...
随机推荐
- SharePoint 2007 列表页定制--4个默认页定制
以"简单的领导简介"为例,欢迎大家指正 背景:项目中需要有领导简介的模块,就开始制作领导简介,本来很简单,有一个列表就可以,然后在前台展示出来,但是客户看到我们的效果,尤其输入领导 ...
- Android的启动过程分析(从进程和Framework的角度)-android学习之旅(98)
Android的启动过程包含从Linux加载到home程序运行的过程,如下图所示: 1.linux内核: Android是基于Linux内核的系统平台.启动时,首先通过bootloader加载LInu ...
- Django之ModelForm
简介 Model + Form ==> ModelForm.model和form的结合体,所以有以下功能: 验证 数据库操作 Form回顾 models.py class UserType(mo ...
- Demo3
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- java中文拼音字母排序
package com.yputil.util; import java.text.CollationKey;import java.text.Collator;import java.util.Ar ...
- java 如何使的float保留2位或者多位小数 (转载)
转载自 http://blog.csdn.net/com_stu_zhang/article/details/7214565 方法1: float f = 34.232323; Big ...
- 一个SQL存储过程面试题(比较简单)
三个月前刚毕业的时候,听到存储过程就头疼. 写一个SQL存储过程,建立一个表USER 字段是姓名,年龄,职位,权限,然后向里面插入6条数据,然后查询出年龄大于18的所有信息. 下面是答案: 复制代码 ...
- 简单验证码的识别:Bitmap类的使用
验证码的智能识别是一项比较复杂的工作,甚至需要掌握点图像学的知识. 当然对于写程序的来说不用那么深入,只需要掌握几个常规步骤就行了. 验证码图像识别步骤:1.获取图像 2.清除边框 3.灰度处理 4. ...
- 【Web页面测试】测试点和测试用例
1. 需求符合度测试 1. 各级菜单名称显示是否按照需求说明书规定的设计,并且没有遗漏和多余 2. 各级菜单所完成的功能是否按照需求说明书规定的设计,并且没有遗漏和多余 3. 各级菜单的操作顺序和操作 ...
- Day7 类的继承和继承实现的原理
继承可以分为但继承,多继承. 继承的基本形式 class ParentClass1(object): #定义父类 pass class ParentClass2: #定义父类 pass class S ...