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

这是个 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. 8 个实用的 Bootstrap 3 案例教程

    Bootstrap 3发布各大设计论坛议论纷纷.这次Bootstrap 3最大的特点就是--扁平化.下面就是一些早期的Bootstrap 3例子,不过亲们注意咯,因为大部分最早期的测试案例,可能用到一 ...

  2. Log4j.properties 配置详解

    一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使 ...

  3. 图片验证码demo示例

    1.首先我们需要一个生成图片验证码图片的一个工具类(下方会有代码示例) 代码如下: package com.util; import java.awt.BasicStroke; import java ...

  4. JavaScript中两个对象数组 属性undefined

    var BaiduUsers = []; var UserArray = function(name, phone, id, id2) { this.name = name; this.phone = ...

  5. dp,px,pt,sp 的区别 以及dp 和 px 互转

    dp = dip : device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不 ...

  6. ES6浅谈之Promise

    首先来回想一下Promise对象的写法: // 方法1 let promise = new Promise ( (resolve, reject) => { if ( success ) { . ...

  7. 发布 Rafy .NET Standard 版本 Nuget 包

    去年年中,Rafy 框架的源码就已经支持了 Net Standard 2.0 版本.其开源代码也已经上传到 Github 中:https://github.com/zgynhqf/rafy/tree/ ...

  8. kaggle入门项目:Titanic存亡预测(二)数据处理

    原kaggle比赛地址:https://www.kaggle.com/c/titanic 原kernel地址:A Data Science Framework: To Achieve 99% Accu ...

  9. 转载《分布式任务调度平台XXL-JOB》

    <分布式任务调度平台XXL-JOB>       博文转自 https://www.cnblogs.com/xuxueli/p/5021979.html 一.简介 1.1 概述 XXL-J ...

  10. (汇总)os模块以及shutil模块对文件的操作

    ''' # os 模块 os.sep 可以取代操作系统特定的路径分隔符.windows下为 '\\' os.name 字符串指示你正在使用的平台.比如对于Windows,它是'nt',而对于Linux ...