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

这是个 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. linux下64位汇编的系统调用(4)

    经过上一篇的铺垫貌似可以很轻松的用汇编写出mmap的代码来,可仔细一看,还是有不少问题需要解决: 1.系统调用mmap如果出错并不直接返回MAP_FAILED(-1),而是一个"类似&quo ...

  2. Emmet之html语法

    一.简写语法 Emmet 用和 CSS 选择器相似的语法来描述元素的嵌套层级关系和属性,实现 HTML/XML/CSS 等代码的智能自动补全. 其通过文件名后缀识别文件类型,从而使用对应的自动补全语法 ...

  3. SpringBoot yml 配置

    1. 在 spring boot 中,有两种配置文件,一种是application.properties,另一种是application.yml,两种都可以配置spring boot 项目中的一些变量 ...

  4. Oracle——多表查询

    本次预计讲解的知识点 1. 多表查询的操作.限制.笛卡尔积的问题: 2. 统计函数及分组统计的操作: 3. 子查询的操作,并且结合限定查询.数据排序.多表查询.统计查询一起完成各个复杂查询的操作: 一 ...

  5. Docker 生态概览

    Docker 和容器技术的发展可谓是日新月异,本文试图以全局的视角来梳理一下 docker 目前的生态圈.既然是概览,所以不会涉及具体的技术细节. Docker 自从发布以来发生了很多的变化,并且有些 ...

  6. 【备忘】Windows的命令行下设置网络代理

    在公司访问网络的时候,需要设置代理,设置浏览器的代理是相当的方便了.但有的时候要使用命令行,就需要自己设置了(貌似只要浏览器设置好了,可以直接使用命令行,但我们公司的借,需要有用户名和密码,如果没有使 ...

  7. AUTOSAR分层-MCAL辨析

    8. AUTOSAR中MCAL虽然包含各种drvier,但毕竟是AL即抽象层,不应包含architecture和device特定的信息.应该只包含模型定义,不包含实现细节.   AUTOSAR文档中的 ...

  8. mybatis源码解读(五)——sql语句的执行流程

    还是以第一篇博客中给出的例子,根据代码实例来入手分析. static { InputStream inputStream = MybatisTest.class.getClassLoader().ge ...

  9. Scrapy爬虫框架补充内容一(Linux环境)

    Scrapy爬虫框架结构及工作原理详解 scrapy框架的框架结构如下: 组件分析: ENGINE:(核心):处理整个框架的数据流,各个组件在其控制下协同工作 SCHEDULER(调度器):负责接收引 ...

  10. mysql学习 第二章 数据库的基本操作

    3.1   创建数据库 MySQL安装好之后,首先需要创建数据库,这是使用MySQL各种功能的前提.本章将详细介绍数据的基本操作,主要内容包括:创建数据库.删除数据库.不同类型的数据存储引擎和存储引擎 ...