英文:
Quantum: Define superposition as linear combination of orthonormal basis
问题
以下是您要翻译的代码部分:
# Standard basis |0>, |1> (Z axis)
zero_qubit = np.matrix([[1.+0.j], [0.+0.j]])
one_qubit = np.matrix([[0.+0.j], [1.+0.j]])
standard_basis: Tuple[np.matrix] = (zero_qubit, one_qubit)
def superposition(
percentage: float = 100.0,
basis: Tuple[np.matrix] = (zero_qubit, one_qubit)):
p=sqrt(percentage/100)
q=sqrt(1-p*p)
q1 = p*basis[0]+q*basis[1]
q2 = p*basis[0]-q*basis[1]
q3 = -p*basis[0]+q*basis[1]
q4 = -p*basis[0]-q*basis[1]
return q1, q2, q3, q4
# This works
q1,q2,q3,q4 = superposition(50,standard_basis)
print(q1)
print(q2)
print(q3)
print(q4)
# This generate weird values
q1,q2,q3,q4 = superposition(50j,standard_basis)
print(q1)
print(q2)
print(q3)
print(q4)
请注意,这是您提供的代码的翻译部分。如果您需要任何其他帮助,请随时告诉我。
英文:
I'm testing a code for define a superposition state using a percentage of both basis.
All seems to run well, when the linear percentage is real. But it fails when the percentage is complex parameter.
from cmath import sqrt
from typing import List, Tuple
# Standard basis |0>, |1> (Z axis)
zero_qubit = np.matrix([[1.+0.j], [0.+0.j]])
one_qubit = np.matrix([[0.+0.j], [1.+0.j]])
standard_basis: Tuple[np.matrix] = (zero_qubit, one_qubit)
def superposition(
percentage: float = 100.0,
basis: Tuple[np.matrix] = (zero_qubit, one_qubit)):
p=sqrt(percentage/100)
q=sqrt(1-p*p)
q1 = p*basis[0]+q*basis[1]
q2 = p*basis[0]-q*basis[1]
q3 = -p*basis[0]+q*basis[1]
q4 = -p*basis[0]-q*basis[1]
return q1, q2, q3, q4
# This works
q1,q2,q3,q4 = superposition(50,standard_basis)
print(q1)
print(q2)
print(q3)
print(q4)
# This generate weird values
q1,q2,q3,q4 = superposition(50j,standard_basis)
print(q1)
print(q2)
print(q3)
print(q4)
答案1
得分: 0
回答我的问题。
叠加态必须考虑系数的模(而不是系数本身)。
因此,我的当前代码如下:
from math import sqrt
from typing import List, Tuple
# https://en.wikipedia.org/wiki/Quantum_superposition
def superposition(
alfa: complex = sqrt(.5)+0.j,
basis: Tuple[np.matrix] = (zero_qubit, one_qubit)):
assert abs(alfa) <= 1.0, "断言系数的模必须小于或等于1"
if abs(alfa) >= 0.9999999:
beta: complex = 0.+0.j
elif abs(alfa) <= 0.0000001:
beta: complex = 1.+0.j
else:
alfa_module=abs(alfa)
beta_module=sqrt(1-alfa_module**2)
beta: complex=(beta_module/alfa_module)*(alfa.conjugate())
assert abs(abs(beta)-beta_module) < 0.0000001, "计算模块时出现内部错误"
q1 = alfa*basis[0]+beta*basis[1]
q2 = alfa*basis[0]+(-beta)*basis[1]
q3 = (-alfa)*basis[0]+beta*basis[1]
q4 = (-alfa)*basis[0]+(-beta)*basis[1]
print(get_bloch_coordinates(q1))
print(get_bloch_coordinates(q2))
print(get_bloch_coordinates(q3))
print(get_bloch_coordinates(q4))
return q1, q2, q3, q4
英文:
Answering my own question.
The superposition has to consider modules of coeficients (not the coeficient themselves).
So, my current code looks like
from math import sqrt
from typing import List, Tuple
# https://en.wikipedia.org/wiki/Quantum_superposition
def superposition(
alfa: complex = sqrt(.5)+0.j,
basis: Tuple[np.matrix] = (zero_qubit, one_qubit)):
assert abs(alfa)<=1.0,"Module of assertion coef has to be lower or equal 1"
if abs(alfa) >= 0.9999999:
beta: complex = 0.+0.j
elif abs(alfa) <=0.0000001:
beta: complex = 1.+0.j
else:
alfa_module=abs(alfa)
beta_module=sqrt(1-alfa_module**2)
beta: complex=(beta_module/alfa_module)*(alfa.conjugate())
assert abs(abs(beta)-beta_module)<0.0000001,"Internal error calculating modules"
q1 = alfa*basis[0]+beta*basis[1]
q2 = alfa*basis[0]+(-beta)*basis[1]
q3 = (-alfa)*basis[0]+beta*basis[1]
q4 = (-alfa)*basis[0]+(-beta)*basis[1]
print(get_bloch_coordinates(q1))
print(get_bloch_coordinates(q2))
print(get_bloch_coordinates(q3))
print(get_bloch_coordinates(q4))
return q1, q2, q3, q4
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论