zoukankan      html  css  js  c++  java
  • 用 150 行 Python 代码写的量子计算模拟器

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

    这是个 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

  • 相关阅读:
    在数组中的两个数字如果前面一个数字大于后面的数字, 则这两个数字组成一个逆序对。 输入一个数组,求出这个数组中的逆序对的总数
    输入一个正整数数组,把数组里所有数字拼接起来排成一个数。打印能拼接出所有数字中最小的一个
    输入一个整型数组,数组里有正数,也有负数。求所有子数组的和的最大值
    数组中有一个数字出现的次数超过数组长度的一半
    输入一个字符串,打印出该字符串中字符的所有排列
    输入一颗二叉搜索树,将该二叉搜索树转换成一个排序的双向链表
    输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径
    判断某数组是否是某二叉搜索树的后序遍历的结果
    栈的压人、弹出序列
    Valid Number
  • 原文地址:https://www.cnblogs.com/jpush88/p/9070662.html
Copyright © 2011-2022 走看看