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

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

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

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

from QuSim import QuantumRegister

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

print(‘Five Qubits: ‘ + FiveQubits.measure())

Swap = QuantumRegister(2) # New Quantum Register of 2 qubits
Swap.applyGate(‘X’, 1) # Apply The NOT Gate. If Measured Now, it should be 10

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)

print(‘SWAP: |’ + Swap.measure() + ‘>’) # Measure the State, Should be 01

FairCoinFlip = QuantumRegister(1)

FairCoinFlip.applyGate(‘H’, 1)

FairCoinFlipAnswer = FairCoinFlip.measure() # Now its flipped, so we can test
if FairCoinFlipAnswer == ‘0’:
print(‘FairCoinFlip: Heads’)
elif FairCoinFlipAnswer == ‘1’:
print(‘FairCoinFlip: Tails’)

ZeroZero = QuantumRegister(2)
ZeroOne = QuantumRegister(2)
OneZero = QuantumRegister(2)
OneOne = QuantumRegister(2)

ZeroOne.applyGate(‘X’, 2)
OneZero.applyGate(‘X’, 1)
OneOne.applyGate(‘X’, 1)
OneOne.applyGate(‘X’, 2)

ZeroZero.applyGate(‘CNOT’, 1, 2)
ZeroOne.applyGate(‘CNOT’, 1, 2)
OneZero.applyGate(‘CNOT’, 1, 2)
OneOne.applyGate(‘CNOT’, 1, 2)

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

版权声明:本文为jpush88原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/jpush88/p/9070662.html