量子对象的基本操作
使用python加载qutip模块
from qutip import *
或者是使用如下的代码建立一个qutip的对象
import qutip as qtp
常用的搭配使用的两个模块,一个是numpy用于计算主要是那些qutip没有定义的运算,需要说明的是qutip是封装了numpy的,但是需要说明的是之间混用两者进行编程是不明智的,建议最后都转化为qutip,转化的途径就是采用Qobj()函数,一个是matplotlib用于画图。如果是迫不得已采用numpy也要在尽量靠前的时间,用Qobj()进行转换。
print(Qobj())
Quantum object: dims = [[1], [1]], shape = (1, 1), type = bra
Qobj data =
[[0.]]
可以看出Qobj()负责建立了一个量子对象,这里是空的一维的对象,标记维度的方式也是不同的,这在编成的过程中让人产生迷惑
x=Qobj([[2],[3],[4]])
print(x)
Quantum object: dims = [[3], [1]], shape = (3, 1), type = ket
Qobj data =
[[2.]
[3.]
[4.]]
x[[1],[0]]
matrix([[3.+0.j]])
x[1,0]
(3+0j)
但毕竟很多地方要实现量子对象的排序,交换。下面的方法给出了交换的例子。
从上面可以看出,下标取出的都不是量子对象。但毕竟很多地方要实现量子对象的排序,交换。下面的方法给出了交换的例子
#产生一个张量积,实现从Qobj所形成的直积态中第i个矩阵和第j个矩阵的位置互换。
def swappedOp(obj, i, j):
if i==j: return obj
numberOfQubits = len(obj.dims[0])#读取输入矩阵的长度
permute = list(range(numberOfQubits))#生成一个长度list
permute[i], permute[j] = permute[j], permute[i]#实现第i和第j个直积态的互换。
return obj.permute(permute)#从而实现obj交换。
#ket states
qubit0 = qtp.basis(2,0)
qubit1 = qtp.basis(2,1)
#density matrices
qubit0mat = qubit0 * qubit0.dag()
qubit1mat = qubit1 * qubit1.dag()
#print(qubit0)
#print(qubit0mat)
print(qtp.tensor(qubit0mat,qubit1mat))
sw=swappedOp(qtp.tensor(qubit0mat,qubit1mat),1,0)
#从而实现两矩阵直积位置的互换
print(sw)
Quantum object: dims = [[2, 2], [2, 2]], shape = (4, 4), type = oper, isherm = True
Qobj data =
[[0. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
Quantum object: dims = [[2, 2], [2, 2]], shape = (4, 4), type = oper, isherm = True
Qobj data =
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 0.]]
核心代码就是Qobj中的permute函数。
xyy = qtp.tensor(qubit0mat,qubit1mat)
xyy
Quantum object: dims = [[2, 2], [2, 2]], shape = (4, 4), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}0.0 & 0.0 & 0.0 & 0.0\0.0 & 1.0 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0\\end{array}\right)\end{equation}
xyy.permute((1,0))
Quantum object: dims = [[2, 2], [2, 2]], shape = (4, 4), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}0.0 & 0.0 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0\0.0 & 0.0 & 1.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0\\end{array}\right)\end{equation}
由此,我们可以看到qutip中的操作肯定不如numpy,scipy中的操作那么自由灵活,能够进行的操作必须是符合量子力学的。需要说明的是
量子态和算子States and operators
1、福克态、福克密度矩阵、相干态、相干态密度矩阵、热库密度矩阵
2、电荷算子、对易算子、对角化算子、移位算子、上升自旋算子、恒等算子、下降算子、动量算子、量子数算子、相位算子、位置算子、产生算子、压缩算子(单模的、一般的都有)、Pauli算子,
#福克态 Fock state
basis(5,2)
Quantum object: dims = [[5], [1]], shape = (5, 1), type = ket\begin{equation*}\left(\begin{array}{{11}c}0.0\0.0\1.0\0.0\0.0\\end{array}\right)\end{equation}
#相干态 Coherent()
coherent(5,0.5-0.5j)
Quantum object: dims = [[5], [1]], shape = (5, 1), type = ket\begin{equation*}\left(\begin{array}{{11}c}0.779\(0.389-0.389j)\-0.275j\(-0.079-0.079j)\-0.043\\end{array}\right)\end{equation}
相干态就是最接近于经典态的量子态,又称为猫态。可以通过位移算子作用于真空Fock态产生。相干态是湮灭算子的本正态。所以,给定的两个参数的意义是,单模的维度为5的,对于玻色子来说,本征值0.5-0.5j是c-number,对于费米子(反对易)的就是Grassmann number。关于相干态有个教程是非常不错的。
https://zhuanlan.zhihu.com/p/183878494
https://wenku.baidu.com/view/5015e0671ed9ad51f01df220.html ppt所讲的内容。都非常清晰
根据湮灭算子和产生算子构造相干态相关的算子
a = destroy(5)
n = a.dag()*a#光子数算子
print(n)
nn = num(5)#也可以直接用num()函数产生
print(nn)
#电磁场正交分量算符
X_1 = (1/2)*(a+a.dag())
X_2 = 1/(2j)*(a-a.dag())
print(X_1)
#旋转正交分量
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True
Qobj data =
[[0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 2. 0. 0.]
[0. 0. 0. 3. 0.]
[0. 0. 0. 0. 4.]]
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True
Qobj data =
[[0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 2. 0. 0.]
[0. 0. 0. 3. 0.]
[0. 0. 0. 0. 4.]]
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True
Qobj data =
[[0. 0.5 0. 0. 0. ]
[0.5 0. 0.70710678 0. 0. ]
[0. 0.70710678 0. 0.8660254 0. ]
[0. 0. 0.8660254 0. 1. ]
[0. 0. 0. 1. 0. ]]
这里适合写函数,下面写个例子以后补充全部。
#定义一个平移算子的函数
def dispOfCoherent(N, alpha):
#N是维度,alpha是复数,相干态的c数,(代数),也是某单模态(行波)系数(物理意义)。
a = destroy(N)
x = alpha*a.dag()+np.conj(alpha)*a
return x.expm()
dispOfCoherent(5,0.5*np.pi)
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}3.428 & 5.355 & 5.815 & 4.842 & 2.749\5.355 & 11.652 & 15.960 & 15.570 & 9.684\5.815 & 15.960 & 26.609 & 29.818 & 20.075\4.842 & 15.570 & 29.818 & 37.077 & 26.524\2.749 & 9.684 & 20.075 & 26.524 & 19.692\\end{array}\right)\end{equation}
x = destroy(5)
x.expm()
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = False\begin{equation*}\left(\begin{array}{{11}c}1.0 & 1.0 & 0.707 & 0.408 & 0.204\0.0 & 1.0 & 1.414 & 1.225 & 0.816\0.0 & 0.0 & 1.0 & 1.732 & 1.732\0.0 & 0.0 & 0.0 & 1.0 & 2.0\0.0 & 0.0 & 0.0 & 0.0 & 1.0\\end{array}\right)\end{equation}
np.exp(destroy(3))#直接运算不不行。
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-156-c8d1a7097be2> in <module>
----> 1 np.exp(destroy(3))#直接运算不不行。
TypeError: operand 'Qobj' does not support ufuncs (__array_ufunc__=None)
这里引出了Qutip的一个编程规则,就是要尽量使用Qobj自带的运算,而且调用的方式跟numpy不同,都是Qobj.function();但显然这些运算仅仅是对Qobj的,其余的操作就根一般的函数没有区别了。
方法列表:
copy() 拷贝
conj() 共轭
cosm()
dag()
dnorm() #Diamond norm of quantum operator. 钻石模
dual_chan() #Dual channel of quantum object representing a CP map.
eigenenergies(sparse=False, sort=’low’, eigvals=0, tol=0, maxiter=100000) 本征能量
Returns eigenenergies (eigenvalues) of a quantum object.
eigenstates(sparse=False, sort=’low’, eigvals=0, tol=0, maxiter=100000) 本正态
Returns eigenenergies and eigenstates of quantum object.
expm() 指数运算
Matrix exponential of quantum object.
full(order=’C’) 全部现实数据
Returns dense array of quantum object data attribute.
groundstate(sparse=False, tol=0, maxiter=100000) 量子对象的基态
Returns eigenvalue and eigenket for the groundstate of a quantum object.
inv() Qobj的逆运算
Return a Qobj corresponding to the matrix inverse of the operator.
matrix_element(bra, ket) 矩阵的元
Returns the matrix element of operator between bra and ket vectors.
norm(norm=’tr’, sparse=False, tol=0, maxiter=100000) #模运算
Returns norm of a ket or an operator.
permute(order)#置换
Returns composite qobj with indices reordered.
proj() #投影
Computes the projector for a ket or bra vector.
ptrace(sel)#取偏迹
Returns quantum object for selected dimensions after performing partial trace.
sinm()
Sine of quantum object.
sqrtm()
Matrix square root of quantum object.
tidyup(atol=1e-12)
Removes small elements from quantum object.
tr()
Trace of quantum object.
trans()
Transpose of quantum object.
transform(inpt, inverse=False)
Performs a basis transformation defined by inpt matrix.
trunc_neg(method=’clip’)
Removes negative eigenvalues and returns a new Qobj that is a valid density operator.
unit(norm=’tr’, sparse=False, tol=0, maxiter=100000)
Returns normalized quantum object.
qq=destroy(5)#湮灭算子
print(qq)
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = False
Qobj data =
[[0. 1. 0. 0. 0. ]
[0. 0. 1.41421356 0. 0. ]
[0. 0. 0. 1.73205081 0. ]
[0. 0. 0. 0. 2. ]
[0. 0. 0. 0. 0. ]]
qq.dims
[[5], [5]]
qq.norm()
6.146264369941973
yy = basis(5,2)
yy
Quantum object: dims = [[5], [1]], shape = (5, 1), type = ket\begin{equation*}\left(\begin{array}{{11}c}0.0\0.0\1.0\0.0\0.0\\end{array}\right)\end{equation}
yy.norm()
1.0
qq.shape
(5, 5)
qq.full()
array([[0. +0.j, 1. +0.j, 0. +0.j, 0. +0.j,
0. +0.j],
[0. +0.j, 0. +0.j, 1.41421356+0.j, 0. +0.j,
0. +0.j],
[0. +0.j, 0. +0.j, 0. +0.j, 1.73205081+0.j,
0. +0.j],
[0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j,
2. +0.j],
[0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j,
0. +0.j]])
tt = create(5)
print(tt)
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = False
Qobj data =
[[0. 0. 0. 0. 0. ]
[1. 0. 0. 0. 0. ]
[0. 1.41421356 0. 0. 0. ]
[0. 0. 1.73205081 0. 0. ]
[0. 0. 0. 2. 0. ]]
tt*qq
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}0.0 & 0.0 & 0.0 & 0.0 & 0.0\0.0 & 1.0 & 0.0 & 0.0 & 0.0\0.0 & 0.0 & 2.0 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 3.000 & 0.0\0.0 & 0.0 & 0.0 & 0.0 & 4.0\\end{array}\right)\end{equation}
qq.isherm
False
bra = basis(5)
ket = basis(5).trans()
print(ket)
qq.matrix_element
#bra.matrix_element
#ket.matrix_element
Quantum object: dims = [[1], [5]], shape = (1, 5), type = bra
Qobj data =
[[1. 0. 0. 0. 0.]]
<bound method Qobj.matrix_element of Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = False
Qobj data =
[[0. 1. 0. 0. 0. ]
[0. 0. 1.41421356 0. 0. ]
[0. 0. 0. 1.73205081 0. ]
[0. 0. 0. 0. 2. ]
[0. 0. 0. 0. 0. ]]>
张量积和取偏迹运算
psi = tensor((basis(2, 0) + basis(2, 1)).unit(), basis(2, 0))
psi
Quantum object: dims = [[2, 2], [1, 1]], shape = (4, 1), type = ket\begin{equation*}\left(\begin{array}{{11}c}0.707\0.0\0.707\0.0\\end{array}\right)\end{equation}
(basis(2, 0) + basis(2, 1)).unit()
Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket\begin{equation*}\left(\begin{array}{{11}c}0.707\0.707\\end{array}\right)\end{equation}
psi.ptrace(0)
Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}0.500 & 0.500\0.500 & 0.500\\end{array}\right)\end{equation}
rho = tensor(ket2dm((basis(2, 0) + basis(2, 1)).unit()), fock_dm(2, 0))
ket2dm((basis(2, 0) + basis(2, 1)).unit())
Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}0.500 & 0.500\0.500 & 0.500\\end{array}\right)\end{equation}
fock_dm(2, 0)
Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}1.0 & 0.0\0.0 & 0.0\\end{array}\right)\end{equation}
rho
Quantum object: dims = [[2, 2], [2, 2]], shape = (4, 4), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}0.500 & 0.0 & 0.500 & 0.0\0.0 & 0.0 & 0.0 & 0.0\0.500 & 0.0 & 0.500 & 0.0\0.0 & 0.0 & 0.0 & 0.0\\end{array}\right)\end{equation}
rho.ptrace(0)
Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}0.500 & 0.500\0.500 & 0.500\\end{array}\right)\end{equation}
rho.ptrace(1)
Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}1.000 & 0.0\0.0 & 0.0\\end{array}\right)\end{equation}
#定义取偏迹时约化掉的部分和保留的部分
def partialTraceRem(obj, rem):
#prepare keep list
rem.sort(reverse=True) #rem 应该是一个list对象。
keep = list(range(len(obj.dims[0])))
for x in rem:
keep.pop(x)
res = obj;
#return partial trace:
if len(keep) != len(obj.dims[0]):
res = obj.ptrace(keep);
return res;
def partialTraceKeep(obj, keep):
#return partial trace:
res = obj;
if len(keep) != len(obj.dims[0]):
res = obj.ptrace(keep);
return res;
partialTraceRem(rho,[0])#通过实验后看到这个偏迹函数是删除掉选取的部分
Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}1.000 & 0.0\0.0 & 0.0\\end{array}\right)\end{equation}
partialTraceKeep(rho,[0])# 这个才是封装以后的取偏迹函数。
Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}0.500 & 0.500\0.500 & 0.500\\end{array}\right)\end{equation}
rhorho = tensor(rho,rho)
rhorho
Quantum object: dims = [[2, 2, 2, 2], [2, 2, 2, 2]], shape = (16, 16), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}0.250 & 0.0 & 0.250 & 0.0 & 0.0 & \cdots & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & \cdots & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\0.250 & 0.0 & 0.250 & 0.0 & 0.0 & \cdots & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & \cdots & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & \cdots & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\vdots & \vdots & \vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \vdots & \vdots & \vdots\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & \cdots & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & \cdots & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & \cdots & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & \cdots & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & \cdots & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\end{array}\right)\end{equation}
partialTraceKeep(rhorho,[0,1,2])
Quantum object: dims = [[2, 2, 2], [2, 2, 2]], shape = (8, 8), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}0.250 & 0.250 & 0.0 & 0.0 & 0.250 & 0.250 & 0.0 & 0.0\0.250 & 0.250 & 0.0 & 0.0 & 0.250 & 0.250 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\0.250 & 0.250 & 0.0 & 0.0 & 0.250 & 0.250 & 0.0 & 0.0\0.250 & 0.250 & 0.0 & 0.0 & 0.250 & 0.250 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\end{array}\right)\end{equation}
partialTraceRem(rhorho,[0,1,2])
Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}1.000 & 0.0\0.0 & 0.0\\end{array}\right)\end{equation}
查看量子对象的属性:Q.data,Q.dims,Q.shape,Q.isherm,Q.type
小结:可以看出,Qutip给出了最基本的算子,但是并没有给出基本的门的表示方式。
第二章 量子态的基本操作
vac = basis(5,0)#产生一个5维度的基态。
a = destroy(5)# 定义下降算子
a*vac
Quantum object: dims = [[5], [1]], shape = (5, 1), type = ket\begin{equation*}\left(\begin{array}{{11}c}0.0\0.0\0.0\0.0\0.0\\end{array}\right)\end{equation}
#产生基态
vac = basis(5,0)
#第一个数字是维度,第二个数字是1的位置
a = destroy(5)
#表示平方
a**2* basis(5,2)
Quantum object: dims = [[5], [1]], shape = (5, 1), type = ket\begin{equation*}\left(\begin{array}{{11}c}1.414\0.0\0.0\0.0\0.0\\end{array}\right)\end{equation}
c = create(5)
c*a
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}0.0 & 0.0 & 0.0 & 0.0 & 0.0\0.0 & 1.0 & 0.0 & 0.0 & 0.0\0.0 & 0.0 & 2.0 & 0.0 & 0.0\0.0 & 0.0 & 0.0 & 3.000 & 0.0\0.0 & 0.0 & 0.0 & 0.0 & 4.0\\end{array}\right)\end{equation}
c*a*vac
Quantum object: dims = [[5], [1]], shape = (5, 1), type = ket\begin{equation*}\left(\begin{array}{{11}c}0.0\0.0\0.0\0.0\0.0\\end{array}\right)\end{equation}
a*c*vac
Quantum object: dims = [[5], [1]], shape = (5, 1), type = ket\begin{equation*}\left(\begin{array}{{11}c}1.0\0.0\0.0\0.0\0.0\\end{array}\right)\end{equation}
x = coherent_dm(5, 1.25)
y = coherent_dm(5, np.complex(0, 1.25)) # <-- note the 'j'
z = thermal_dm(5, 0.125)
np.testing.assert_almost_equal(fidelity(x, x), 1)
np.testing.assert_almost_equal(hellinger_dist(x, y), 1.3819080728932833)
y = coherent_dm(5, np.complex(0, 1.25))
y
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}0.210 & -0.261j & -0.235 & 0.156j & 0.134\0.261j & 0.326 & -0.293j & -0.194 & 0.167j\-0.235 & 0.293j & 0.263 & -0.174j & -0.150\-0.156j & -0.194 & 0.174j & 0.116 & -0.099j\0.134 & -0.167j & -0.150 & 0.099j & 0.085\\end{array}\right)\end{equation}
yy = coherent_dm(5, 1.25j)
yy
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}0.210 & -0.261j & -0.235 & 0.156j & 0.134\0.261j & 0.326 & -0.293j & -0.194 & 0.167j\-0.235 & 0.293j & 0.263 & -0.174j & -0.150\-0.156j & -0.194 & 0.174j & 0.116 & -0.099j\0.134 & -0.167j & -0.150 & 0.099j & 0.085\\end{array}\right)\end{equation}
np.testing.assert_almost_equal(tracedist(y,x),np.sqrt(1-fidelity(y,x)**2))
这里提供了一个检验两者相等的工具,是numpy提供的。由于计算的原因,可能两者有些许的差别。
np.testing.assert_almost_equal(3.141512556,3.141512557)
#默认的小数点后6位数据开始判断
np.testing.assert_almost_equal(3.1415126,3.1415127)
np.testing.assert_almost_equal(3.141516,3.141517)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-222-185a9168fa5a> in <module>
2 #默认的小数点后7位数据开始判断
3 np.testing.assert_almost_equal(3.1415126,3.1415127)
----> 4 np.testing.assert_almost_equal(3.141516,3.141517)
[... skipping hidden 1 frame]
AssertionError:
Arrays are not almost equal to 7 decimals
ACTUAL: 3.141516
DESIRED: 3.141517
np.testing.assert_almost_equal(3.141512556,3.141512,0) #表示不比较小数,一切都是猜测
np.testing.assert_almost_equal(3.141512556,3.241512,1) #表示比较整数
np.testing.assert_almost_equal(3.141512556,3.241512,2) #表示比较小数第一位。
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-219-d4ca1476850d> in <module>
1 np.testing.assert_almost_equal(3.141512556,3.141512,0)
2 np.testing.assert_almost_equal(3.141512556,3.241512,1)
----> 3 np.testing.assert_almost_equal(3.141512556,3.241512,2)
[... skipping hidden 1 frame]
AssertionError:
Arrays are not almost equal to 2 decimals
ACTUAL: 3.141512556
DESIRED: 3.241512
assert 1 - fidelity(x, z) ** 2 < tracedist(x, z)
#assert 语句的用法
assert 1 - fidelity(x, z) ** 2 > tracedist(x, z)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-199-2b3defc86d8e> in <module>
----> 1 assert 1 - fidelity(x, z) ** 2 > tracedist(x, z)
AssertionError:
产生一个错误,这个错误可以被try cache语句捕获
期望值
states = [(c**k * vac).unit() for k in range(5)] # must normalize
N = num(5)
print(expect(N, states))
[0. 1. 2. 3. 4.]
#求期望值必须归一化
#期望值可以处理列表化的多个量子态下的期望值
#输出仍然为一个列表
二能级系统的一些表示
spin1 = basis(2, 0)
spin2 = basis(2, 1)
two_spins = tensor(spin1, spin2)
sz1 = tensor(sigmaz(), qeye(2))
sz2 = tensor(qeye(2), sigmaz())
np.testing.assert_almost_equal(expect(sz1, two_spins), 1)
np.testing.assert_almost_equal(expect(sz2, two_spins), -1)
超算符和矢量化
#矢量转化为算子(也就是矢量转化为矩阵的qutip方案)
psi = basis(2,0)
rho = ket2dm(psi)
print(psi)
print(rho)
Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket
Qobj data =
[[1.]
[0.]]
Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True
Qobj data =
[[1. 0.]
[0. 0.]]
#把rho转化为向量
vec_rho = operator_to_vector(rho)
vec_rho
Quantum object: dims = [[[2], [2]], [1]], shape = (4, 1), type = operator-ket\begin{equation*}\left(\begin{array}{{11}c}1.0\0.0\0.0\0.0\\end{array}\right)\end{equation}
rhoRecretor = vector_to_operator(vec_rho)
rhoRecretor
Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True\begin{equation*}\left(\begin{array}{{11}c}1.0 & 0.0\0.0 & 0.0\\end{array}\right)\end{equation}
np.testing.assert_almost_equal((rho - rhoRecretor).norm(), 0)#猜测应该是截至到整数位相同