简评:让你更轻松地明白,量子计算机如何遵循线性代数计算的。

这是个 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 代码写的量子计算模拟器的更多相关文章

  1. 150+行Python代码实现带界面的数独游戏

    150行代码实现图形化数独游戏 Github地址,欢迎各位大佬们fork.star啥的,感谢: 今天闲着没事干,以前做过html+js版的数独,这次做个python版本的,界面由pygame完成,数独 ...

  2. python 之路,200行Python代码写了个打飞机游戏!

    早就知道pygame模块,就是没怎么深入研究过,恰逢这周未没约到妹子,只能自己在家玩自己啦,一时兴起,花了几个小时写了个打飞机程序. 很有意思,跟大家分享下. 先看一下项目结构 "" ...

  3. 10 行 Python 代码写的模糊查询

    导语: 模糊匹配可以算是现代编辑器(在选择要打开的文件时)的一个必备特性了,它所做的就是根据用户输入的部分内容,猜测用户想要的文件名,并提供一个推荐列表供用户选择. 样例如下: Vim (Ctrl-P ...

  4. (转)python 之路,200行Python代码写了个打飞机游戏!

    原文:https://www.cnblogs.com/alex3714/p/7966656.html

  5. 一个 11 行 Python 代码实现的神经网络

    一个 11 行 Python 代码实现的神经网络 2015/12/02 · 实践项目 · 15 评论· 神经网络 分享到:18 本文由 伯乐在线 - 耶鲁怕冷 翻译,Namco 校稿.未经许可,禁止转 ...

  6. 200行Python代码实现2048

    200行Python代码实现2048 一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境,实验中会用到桌面 ...

  7. 10 行 Python 代码实现模糊查询/智能提示

    10 行 Python 代码实现模糊查询/智能提示   1.导语: 模糊匹配可以算是现代编辑器(如 Eclipse 等各种 IDE)的一个必备特性了,它所做的就是根据用户输入的部分内容,猜测用户想要的 ...

  8. 20行Python代码爬取王者荣耀全英雄皮肤

    引言王者荣耀大家都玩过吧,没玩过的也应该听说过,作为时下最火的手机MOBA游戏,咳咳,好像跑题了.我们今天的重点是爬取王者荣耀所有英雄的所有皮肤,而且仅仅使用20行Python代码即可完成. 准备工作 ...

  9. 200行PYTHON代码实现贪吃蛇

    200行Python代码实现贪吃蛇 话不多说,最后会给出全部的代码,也可以从这里Fork,正文开始: 目前实现的功能列表: 贪吃蛇的控制,通过上下左右方向键: 触碰到边缘.墙壁.自身则游戏结束: 接触 ...

随机推荐

  1. 【Qt编程】基于Qt的词典开发系列<四>--无边框窗口的缩放与拖动

    在现在,绝大多数软件都向着简洁,时尚发展.就拿有道的单词本和我做的单词本来说,绝大多数用户肯定喜欢我所做的单词本(就单单界面,关于颜色搭配和布局问题,大家就不要在意了). 有道的单词本: 我所做的单词 ...

  2. HDFS APPEND性能测试

    hbase在写入数据之前会先写hlog,hlog目前是sequencefile格式,采用append的方式往里追加数据.之前团队的同学测试关闭hlog会一定程序上提升写hbase的稳定性.而在我之前的 ...

  3. Linux内核中断和异常分析(上)

    中断,通常被定义为一个事件.打个比方,你烧热水,水沸腾了,这时候你要去关掉烧热水的电磁炉,然后再去办之前手中停不下来的事情.那么热水沸腾就是打断你正常工作的一个信号机制.当然,还有其它的情况,我们以后 ...

  4. db2 查新索引 主键 sql

    1.查询主键 select * from sysibm.syskeycoluse where tbname='...' 2.查询索引 select * from sysibm.indexs where ...

  5. profile bashrc bash_profile之间的区别和联系

    profile bashrc bash_profile之间的区别和联系 博客分类: Linux   执行顺序为:/etc/profile -> (~/.bash_profile | ~/.bas ...

  6. 畅通工程-HZNU寒假集训

    畅通工程 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只 ...

  7. pc端页面打包成安卓apk

    一.phoneGap PhoneGap是一个采用HTML,CSS和JavaScript的技术,创建移动跨平台移动应用程序的快速开发平台.它使开发者能够在网页中调用IOS,Android,Palm,Sy ...

  8. mysql高级之编程优化

    ★编程优化一.字符编码(mysql控制台乱码输出解决:character_set_results='gbk')表/列编码设置  列:alter table 表名 change 列名 列名 数据类型 c ...

  9. Java 包装类笔记

    @(JDK)[包装类] Java 包装类笔记 当需要使用到集合的时候,如果是基础数据类型,需要转换为包装类,再使用,在JDK1.5之前,每次使用的时候,都需要手动转换为包装类(称为装箱),然后在使用的 ...

  10. QT窗体的小技巧

    1.界面透明 setWindowOpacity(0.8);//构造函数中加此句,1为不透明,0为完全透明,0.8为80%不透明. 2.设置背景图片 QPixmap pixmap = QPixmap(& ...