用 150 行 Python 代码写的量子计算模拟器

抽象潮涌
• 阅读 2251
简评:让你更轻松地明白,量子计算机如何遵循线性代数计算的。

这是个 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
点赞
收藏
评论区
推荐文章
浅梦一笑 浅梦一笑
4年前
学Python后到底能干什么?
Python是一种什么语言?Python是一种计算机程序设计语言。你可能已经听说过很多种流行的编程语言,比如非常难学的C语言,非常流行的Java语言,适合初学者的Basic语言,适合网页编程的Java语言等,Python是他们其中的一种。首先,我们普及一下编程语言的基础知识。用任何编程语言来开发程序,都是为了让计算机干活,比如下载一个MP3,编写一个文档等,
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
半臻 半臻
4年前
Python基础1——变量、判断、循环、字符串、列表
1认识python1.1认识python计算机识别机器语言,机器语言由二进制0和1组成计算机要执行高级语言,转换方式1.编译:C语言、C、java。一次性编译成可执行文件2.解释:一行一行地解释python是解释型语言python解释器、pycharm编辑器举个例子:要给工地煮饭编译:把饭菜都做好,做成盒饭(.exe,.class
Karen110 Karen110
4年前
求求你调试Python代码,不要再用Print了!
相信大部分人学习Python,肯定会用print()这个内置函数,来调试代码的。那么在一个大型的项目中,如果你也是使用print来调试你的Python代码,你就会发现你的终端有多个输出。那么你便不得不去分辨,每一行的输出是哪些代码的运行结果。举个例子,运行下面这个程序。num1  30num2  40 print(num1
亚马逊Braket带你探索量子计算,领略量子计算的强大
AmazonBraket是一项完全,旨在帮助加快量子计算的科学研究和软件开发。量子计算机离普通人很远。有多少头牛,牛在哪里?还不清楚。既然是世界领先,也是我们中国人的荣幸,我们想多了解一下“为什么”。在众多报道中,我发现新华每日电讯5月4日发表的一篇文章做了最通俗易懂的解读,正好满足了我们的需求。我特此转发如下:要判断量子计算好不好,学术界达成共识的指标
Stella981 Stella981
3年前
React Hooks实现异步请求实例—useReducer、useContext和useEffect代替Redux方案
<blockquote本文是学习了2018年新鲜出炉的ReactHooks提案之后,针对<strong异步请求数据</strong写的一个案例。注意,本文假设了:<br1.你已经初步了解<codehooks</code的含义了,如果不了解还请移步<ahref"https://reactjs.org/docs/hooksintro.html
Stella981 Stella981
3年前
Python小白学习笔记(成长之路)
Python语言可能是第一种即简单又功能强大的编程语言。它不仅适合于初学者,也适合于专业人员使用,更加重要的是,用Python编程是一种愉快的事。本身将帮助你学习这个奇妙的语言,并且向你展示如何即快捷又方便地完成任务——真正意义上“为编程问题提供的完美解决方案!”即便你对计算机的了解只是如何在计算机上保存文本文件,你都可以通过本书学习Python
Stella981 Stella981
3年前
Python之time模块的时间戳、时间字符串格式化与转换
Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。关于时间戳的几个概念时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。时间元组(struct_time),包含9个元素。 time.struct_time(tm_y
燕青 燕青
1年前
Macos专业的ssh客户端:Termius for Mac中文版
是一款针对Mac系统开发的SSH客户端应用程序。它提供了简单易用的界面和全面的功能,可以让用户轻松地管理和连接远程计算机。TermiusforMac主要特点包括:支持SSH协议:TermiusforMac支持SSH协议,可以轻松地连接到远程计算机、服务器或
流浪剑客 流浪剑客
1年前
Macos多视图文件管理工具:QSpace Pro for Mac中文版
是一款高度专业化的软件,它主要用于进行量子化学和材料科学计算。以下是它的主要特点:高效能计算:QSpacePro使用了最先进的算法和计算技术,可以快速准确地执行大规模的量子化学和材料科学计算。广泛的用途:QSpacePro可以用于多种类型的计算,包括从头算
带你走进量子云平台(二)
量子叠加和量子纠缠这两个基本特性,使得量子计算机在解决某些类型的问题时比经典计算机快得多。这两个属性从本质上决定了一个基本事实——量子计算天然地由概率主导。换句话说,这意味着量子程序本质上是概率性的、随机的。因此,要在量子计算机上实现业务逻辑或算法需要独特的编程模型。