资讯专栏INFORMATION COLUMN

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

Edison / 1828人阅读

摘要:是一个多量子位的量子计算机模拟器玩具,用行的所编写。这段代码可以让你轻松了解量子计算机如何遵循线性代数来计算的主要代码来自如果你对用所写的高效高性能的硬件量子计算模拟器有兴趣,可以点击来查看更多内容。

</>复制代码

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

这是个 GItHub 项目,可以简单了解一下。

qusim.py 是一个多量子位的量子计算机模拟器(玩具?),用 150 行的 python 所编写。

这段代码可以让你轻松了解量子计算机如何遵循线性代数来计算的!

</>复制代码

  1. from QuSim import QuantumRegister
  2. #############################################
  3. # Introduction #
  4. #############################################
  5. # Here Will Be A Few Example of Different #
  6. # Quantum States / Algorithms, So You Can #
  7. # Get A Feel For How The Module Works, and #
  8. # Some Algorithmic Ideas #
  9. #############################################
  10. #############################################
  11. # Quantum Measurement #
  12. #############################################
  13. # This experiment will prepare 2 states, of a
  14. # Single qubit, and of 5 qubits, and will just
  15. # Measure them
  16. OneQubit = QuantumRegister(1) # New Quantum Register of 1 Qubit
  17. print("One Qubit: " + OneQubit.measure()) # Should Print "One Qubit: 0"
  18. FiveQubits = QuantumRegister(5) # New Quantum Register of 5 Qubits
  19. # Should Print "Five Qubits: 00000"
  20. print("Five Qubits: " + FiveQubits.measure())
  21. #############################################
  22. # Swap 2 Qubits #
  23. #############################################
  24. # Here, We Will Apply a Pauli-X Gate / NOT Gate
  25. # To the first qubit, and then after the algorithm,
  26. # it will be swapped to the second qubit.
  27. Swap = QuantumRegister(2) # New Quantum Register of 2 qubits
  28. Swap.applyGate("X", 1) # Apply The NOT Gate. If Measured Now, it should be 10
  29. # Start the swap algorithm
  30. Swap.applyGate("CNOT", 1, 2)
  31. Swap.applyGate("H", 1)
  32. Swap.applyGate("H", 2)
  33. Swap.applyGate("CNOT", 1, 2)
  34. Swap.applyGate("H", 1)
  35. Swap.applyGate("H", 2)
  36. Swap.applyGate("CNOT", 1, 2)
  37. # End the swap algorithm
  38. print("SWAP: |" + Swap.measure() + ">") # Measure the State, Should be 01
  39. #############################################
  40. # Fair Coin Flip #
  41. #############################################
  42. # Shown in this "Experiment", is a so called "Fair Coin Flip",
  43. # Where a state will be prepared, that has an equal chance of
  44. # Flipping to Each Possible State. to do this, the Hadamard
  45. # Gate will be used.
  46. # New Quantum Register of 1 Qubit (As a coin has only 2 states)
  47. FairCoinFlip = QuantumRegister(1)
  48. # If measured at this point, it should be |0>
  49. # Apply the hadamard gate, now theres an even chance of measuring 0 or 1
  50. FairCoinFlip.applyGate("H", 1)
  51. # Now, the state will be measured, flipping the state to
  52. # either 0 or 1. If its 0, we will say "Heads", or if its
  53. # 1, we will say "Tails"
  54. FairCoinFlipAnswer = FairCoinFlip.measure() # Now its flipped, so we can test
  55. if FairCoinFlipAnswer == "0":
  56. print("FairCoinFlip: Heads")
  57. elif FairCoinFlipAnswer == "1":
  58. print("FairCoinFlip: Tails")
  59. #############################################
  60. # CNOT Gate #
  61. #############################################
  62. # In this experiment, 4 states will be prepared, {00, 01, 10, 11}
  63. # And then the same CNOT Gate will be run on them,
  64. # To Show The Effects of the CNOT. The Target Qubit will be 2, and the control 1
  65. # New Quantum Register of 2 Qubits, done 4 times.
  66. # If any are measured at this time, the result will be 00
  67. ZeroZero = QuantumRegister(2)
  68. ZeroOne = QuantumRegister(2)
  69. OneZero = QuantumRegister(2)
  70. OneOne = QuantumRegister(2)
  71. # Now prepare Each Into The State Based On Their Name
  72. # ZeroZero Will be left, as thats the first state anyway
  73. ZeroOne.applyGate("X", 2)
  74. OneZero.applyGate("X", 1)
  75. OneOne.applyGate("X", 1)
  76. OneOne.applyGate("X", 2)
  77. # Now, a CNOT Will Be Applied To Each.
  78. ZeroZero.applyGate("CNOT", 1, 2)
  79. ZeroOne.applyGate("CNOT", 1, 2)
  80. OneZero.applyGate("CNOT", 1, 2)
  81. OneOne.applyGate("CNOT", 1, 2)
  82. # Print the results.
  83. print("CNOT on 00: |" + ZeroZero.measure() + ">")
  84. print("CNOT on 01: |" + ZeroOne.measure() + ">")
  85. print("CNOT on 10: |" + OneZero.measure() + ">")
  86. print("CNOT on 11: |" + OneOne.measure() + ">")

主要代码来自:corbett/QuantumComputing.

如果你对用 RUST 所写的高效、高性能的硬件量子计算模拟器有兴趣,可以点击 QCGPU 来查看更多内容。

</>复制代码

  1. GITHUB 地址:adamisntdead/QuSimPy

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/45554.html

相关文章

  • 最大规模光量子芯片诞生,一枚“玻璃片”为量子计算提速

    摘要:一个量子计算过程完成,而其中最关键的就是这枚玻璃片。最大规模是此次金贤敏团队发布的光量子芯片的一个关键词。所谓模拟量子计算机,就是指专用量子计算机。5月15日,金贤敏教授展示制备的芯片。新华社记者 丁汀摄一个个肉眼看不见的单光子穿过透明的玻璃片,几秒之后,显示屏幕上呈现出单光子的二维量子行走演化结果。一个量子计算过程完成,而其中最关键的就是这枚玻璃片。在灯光下,从某个角度看去,这枚完全透明的...

    jk_v1 评论0 收藏0
  • 橡树岭国家实验室是如何以神经元等搭建超混合深度学习框架的?

    摘要:橡树岭国家实验室的研究人员通过使用基于的方法,将数千个网络划分开,在超过个上运行,从而进行大规模深度学习。神经元装置,特别是那些像橡树岭国家实验室开发的脉冲神经网络,,可以卸载一些包含时间序列元素神经网络。 橡树岭国家实验室图从系统的架构的复杂性上来讲,摩尔定律很难对其适用。尽管如此,过去两年来,我们一直在迎来了新一轮针对深度学习和其他专业工作的新架构热潮,并涌现出FPGA、更快的GPU,以...

    Yang_River 评论0 收藏0
  • 中国互联网发展之5G、人工智能、云计算、大数据等新兴科技发展状况

    摘要:截至年月,全国已有个省区市发布了人工智能规划,其中个制定了具体的产业规模发展目标。年我国企业相继发布人工智能芯片。五大数据发展情况在促进大数据发展行动纲要等政策的指 showImg(http://upload-images.jianshu.io/upload_images/13825820-5b1886a2a4a6c96f.jpg?imageMogr2/auto-orient/stri...

    learn_shifeng 评论0 收藏0
  • 阿里云又添最强大脑:理论计算机最高奖得主马里奥入职达摩院

    摘要:据阿里云官方消息报道,两次理论计算机最高奖哥德尔奖得主匈牙利裔美国计算机科学家马里奥塞格德入职阿里巴巴达摩院位于西雅图的阿里云量子实验室。据阿里云官方消息报道,两次理论计算机最高奖哥德尔奖得主、匈牙利裔美国计算机科学家马里奥·塞格德(Mario Szegedy)入职阿里巴巴达摩院位于西雅图的阿里云量子实验室(AQL)。马里奥·塞格德出生于盛产科学家的国度匈牙利,研究领域包括量子计算和计算复杂...

    csRyan 评论0 收藏0
  • 探访微软总部,解析巨头转型的全貌

    摘要:但问题是这一切的改变究竟是怎么发生的月中下旬,在西雅图地区难得的艳阳高照里,我受邀前往微软雷德蒙德总部,近距离探寻微软转型的秘密。微软在年公司上市前几周,搬到现在的雷德蒙德总部。谈完战略转型,再来聊聊微软转型的第二场战役文化变革。为什么是微软?3月26日,伴随着史上最软发布会的落幕,苹果市值蒸发100亿美元,总市值再次低于9000亿美元,这意味着,他们将全球市值第一的王座再次让给微软。我相信...

    SmallBoyO 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<