本文整理汇总了Python中qutip.superoperator.operator_to_vector函数的典型用法代码示例。如果您正苦于以下问题:Python operator_to_vector函数的具体用法?Python operator_to_vector怎么用?Python operator_to_vector使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了operator_to_vector函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: case
def case(S, n_trials=50):
S = to_super(S)
left_dims, right_dims = S.dims
# Assume for the purposes of the test that S maps square operators to square operators.
in_dim = np.prod(right_dims[0])
out_dim = np.prod(left_dims[0])
S_dual = to_super(S.dual_chan())
primals = []
duals = []
for idx_trial in range(n_trials):
X = rand_dm_ginibre(out_dim)
X.dims = left_dims
X = operator_to_vector(X)
Y = rand_dm_ginibre(in_dim)
Y.dims = right_dims
Y = operator_to_vector(Y)
primals.append((X.dag() * S * Y)[0, 0])
duals.append((X.dag() * S_dual.dag() * Y)[0, 0])
np.testing.assert_array_almost_equal(primals, duals)
示例2: test_call
def test_call():
"""
Test Qobj: Call
"""
# Make test objects.
psi = rand_ket(3)
rho = rand_dm_ginibre(3)
U = rand_unitary(3)
S = rand_super_bcsz(3)
# Case 0: oper(ket).
assert U(psi) == U * psi
# Case 1: oper(oper). Should raise TypeError.
with expect_exception(TypeError):
U(rho)
# Case 2: super(ket).
assert S(psi) == vector_to_operator(S * operator_to_vector(ket2dm(psi)))
# Case 3: super(oper).
assert S(rho) == vector_to_operator(S * operator_to_vector(rho))
# Case 4: super(super). Should raise TypeError.
with expect_exception(TypeError):
S(S)
示例3: test_super_tensor_operket
def test_super_tensor_operket():
"""
Tensor: Checks that super_tensor respects states.
"""
rho1, rho2 = rand_dm(5), rand_dm(7)
operator_to_vector(rho1)
operator_to_vector(rho2)
示例4: test_CheckMulType
def test_CheckMulType():
"Qobj multiplication type"
# ket-bra and bra-ket multiplication
psi = basis(5)
dm = psi * psi.dag()
assert_(dm.isoper)
assert_(dm.isherm)
nrm = psi.dag() * psi
assert_equal(np.prod(nrm.shape), 1)
assert_((abs(nrm) == 1)[0, 0])
# operator-operator multiplication
H1 = rand_herm(3)
H2 = rand_herm(3)
out = H1 * H2
assert_(out.isoper)
out = H1 * H1
assert_(out.isoper)
assert_(out.isherm)
out = H2 * H2
assert_(out.isoper)
assert_(out.isherm)
U = rand_unitary(5)
out = U.dag() * U
assert_(out.isoper)
assert_(out.isherm)
N = num(5)
out = N * N
assert_(out.isoper)
assert_(out.isherm)
# operator-ket and bra-operator multiplication
op = sigmax()
ket1 = basis(2)
ket2 = op * ket1
assert_(ket2.isket)
bra1 = basis(2).dag()
bra2 = bra1 * op
assert_(bra2.isbra)
assert_(bra2.dag() == ket2)
# superoperator-operket and operbra-superoperator multiplication
sop = to_super(sigmax())
opket1 = operator_to_vector(fock_dm(2))
opket2 = sop * opket1
assert(opket2.isoperket)
opbra1 = operator_to_vector(fock_dm(2)).dag()
opbra2 = opbra1 * sop
assert(opbra2.isoperbra)
assert_(opbra2.dag() == opket2)
示例5: test_QobjPermute
def test_QobjPermute():
"Qobj permute"
A = basis(3, 0)
B = basis(5, 4)
C = basis(4, 2)
psi = tensor(A, B, C)
psi2 = psi.permute([2, 0, 1])
assert_(psi2 == tensor(C, A, B))
psi_bra = psi.dag()
psi2_bra = psi_bra.permute([2, 0, 1])
assert_(psi2_bra == tensor(C, A, B).dag())
A = fock_dm(3, 0)
B = fock_dm(5, 4)
C = fock_dm(4, 2)
rho = tensor(A, B, C)
rho2 = rho.permute([2, 0, 1])
assert_(rho2 == tensor(C, A, B))
for ii in range(3):
A = rand_ket(3)
B = rand_ket(4)
C = rand_ket(5)
psi = tensor(A, B, C)
psi2 = psi.permute([1, 0, 2])
assert_(psi2 == tensor(B, A, C))
psi_bra = psi.dag()
psi2_bra = psi_bra.permute([1, 0, 2])
assert_(psi2_bra == tensor(B, A, C).dag())
for ii in range(3):
A = rand_dm(3)
B = rand_dm(4)
C = rand_dm(5)
rho = tensor(A, B, C)
rho2 = rho.permute([1, 0, 2])
assert_(rho2 == tensor(B, A, C))
rho_vec = operator_to_vector(rho)
rho2_vec = rho_vec.permute([[1, 0, 2],[4,3,5]])
assert_(rho2_vec == operator_to_vector(tensor(B, A, C)))
rho_vec_bra = operator_to_vector(rho).dag()
rho2_vec_bra = rho_vec_bra.permute([[1, 0, 2],[4,3,5]])
assert_(rho2_vec_bra == operator_to_vector(tensor(B, A, C)).dag())
for ii in range(3):
super_dims = [3, 5, 4]
U = rand_unitary(np.prod(super_dims), density=0.02, dims=[super_dims, super_dims])
Unew = U.permute([2,1,0])
S_tens = to_super(U)
S_tens_new = to_super(Unew)
assert_(S_tens_new == S_tens.permute([[2,1,0],[5,4,3]]))
示例6: test_composite_vec
def test_composite_vec():
"""
Composite: Tests compositing states and density operators.
"""
k1 = rand_ket(5)
k2 = rand_ket(7)
r1 = operator_to_vector(ket2dm(k1))
r2 = operator_to_vector(ket2dm(k2))
r3 = operator_to_vector(rand_dm(3))
r4 = operator_to_vector(rand_dm(4))
assert_(composite(k1, k2) == tensor(k1, k2))
assert_(composite(r3, r4) == super_tensor(r3, r4))
assert_(composite(k1, r4) == super_tensor(r1, r4))
assert_(composite(r3, k2) == super_tensor(r3, r2))
示例7: test_QobjType
def test_QobjType():
"Qobj type"
N = int(np.ceil(10.0 * np.random.random())) + 5
ket_data = np.random.random((N, 1))
ket_qobj = Qobj(ket_data)
assert_equal(ket_qobj.type, 'ket')
assert_(ket_qobj.isket)
bra_data = np.random.random((1, N))
bra_qobj = Qobj(bra_data)
assert_equal(bra_qobj.type, 'bra')
assert_(bra_qobj.isbra)
oper_data = np.random.random((N, N))
oper_qobj = Qobj(oper_data)
assert_equal(oper_qobj.type, 'oper')
assert_(oper_qobj.isoper)
N = 9
super_data = np.random.random((N, N))
super_qobj = Qobj(super_data, dims=[[[3]], [[3]]])
assert_equal(super_qobj.type, 'super')
assert_(super_qobj.issuper)
operket_qobj = operator_to_vector(oper_qobj)
assert_(operket_qobj.isoperket)
operbra_qobj = operket_qobj.dag()
assert_(operbra_qobj.isoperbra)
示例8: composite
def composite(*args):
"""
Given two or more operators, kets or bras, returns the Qobj
corresponding to a composite system over each argument.
For ordinary operators and vectors, this is the tensor product,
while for superoperators and vectorized operators, this is
the column-reshuffled tensor product.
If a mix of Qobjs supported on Hilbert and Liouville spaces
are passed in, the former are promoted. Ordinary operators
are assumed to be unitaries, and are promoted using ``to_super``,
while kets and bras are promoted by taking their projectors and
using ``operator_to_vector(ket2dm(arg))``.
"""
# First step will be to ensure everything is a Qobj at all.
if not all(isinstance(arg, Qobj) for arg in args):
raise TypeError("All arguments must be Qobjs.")
# Next, figure out if we have something oper-like (isoper or issuper),
# or something ket-like (isket or isoperket). Bra-like we'll deal with
# by turning things into ket-likes and back.
if all(map(_isoperlike, args)):
# OK, we have oper/supers.
if any(arg.issuper for arg in args):
# Note that to_super does nothing to things
# that are already type=super, while it will
# promote unitaries to superunitaries.
return super_tensor(*map(qutip.superop_reps.to_super, args))
else:
# Everything's just an oper, so ordinary tensor products work.
return tensor(*args)
elif all(map(_isketlike, args)):
# Ket-likes.
if any(arg.isoperket for arg in args):
# We have a vectorized operator, we we may need to promote
# something.
return super_tensor(*(
arg if arg.isoperket
else operator_to_vector(qutip.states.ket2dm(arg))
for arg in args
))
else:
# Everything's ordinary, so we can use the tensor product here.
return tensor(*args)
elif all(map(_isbralike, args)):
# Turn into ket-likes and recurse.
return composite(*(arg.dag() for arg in args)).dag()
else:
raise TypeError("Unsupported Qobj types [{}].".format(
", ".join(arg.type for arg in args)
))
示例9: _pauli_basis
def _pauli_basis(nq=1):
# NOTE: This is slow as can be.
# TODO: Make this sparse. CSR format was causing problems for the [idx, :]
# slicing below.
B = zeros((4**nq, 4**nq), dtype=complex)
dims = [[[2]*nq]*2]*2
for idx, op in enumerate(starmap(tensor, product(_SINGLE_QUBIT_PAULI_BASIS, repeat=nq))):
B[:, idx] = operator_to_vector(op).dag().data.todense()
return Qobj(B, dims=dims)
示例10: case
def case(map, state):
S = to_super(map)
A, B = to_stinespring(map)
q1 = vector_to_operator(
S * operator_to_vector(state)
)
# FIXME: problem if Kraus index is implicitly
# ptraced!
q2 = (A * state * B.dag()).ptrace((0,))
assert_((q1 - q2).norm('tr') <= thresh)
示例11: propagator
#.........这里部分代码省略.........
List of qobj collapse operators.
args : list/array/dictionary
Parameters to callback functions for time-dependent Hamiltonians and
collapse operators.
options : :class:`qutip.Options`
with options for the ODE solver.
Returns
-------
a : qobj
Instance representing the propagator :math:`U(t)`.
"""
if options is None:
options = Options()
options.rhs_reuse = True
rhs_clear()
if isinstance(t, (int, float, np.integer, np.floating)):
tlist = [0, t]
else:
tlist = t
if isinstance(H, (types.FunctionType, types.BuiltinFunctionType,
functools.partial)):
H0 = H(0.0, args)
elif isinstance(H, list):
H0 = H[0][0] if isinstance(H[0], list) else H[0]
else:
H0 = H
if len(c_op_list) == 0 and H0.isoper:
# calculate propagator for the wave function
N = H0.shape[0]
dims = H0.dims
u = np.zeros([N, N, len(tlist)], dtype=complex)
for n in range(0, N):
psi0 = basis(N, n)
output = sesolve(H, psi0, tlist, [], args, options)
for k, t in enumerate(tlist):
u[:, n, k] = output.states[k].full().T
# todo: evolving a batch of wave functions:
# psi_0_list = [basis(N, n) for n in range(N)]
# psi_t_list = mesolve(H, psi_0_list, [0, t], [], [], args, options)
# for n in range(0, N):
# u[:,n] = psi_t_list[n][1].full().T
elif len(c_op_list) == 0 and H0.issuper:
# calculate the propagator for the vector representation of the
# density matrix (a superoperator propagator)
N = H0.shape[0]
dims = H0.dims
u = np.zeros([N, N, len(tlist)], dtype=complex)
for n in range(0, N):
psi0 = basis(N, n)
rho0 = Qobj(vec2mat(psi0.full()))
output = mesolve(H, rho0, tlist, [], [], args, options)
for k, t in enumerate(tlist):
u[:, n, k] = mat2vec(output.states[k].full()).T
else:
# calculate the propagator for the vector representation of the
# density matrix (a superoperator propagator)
N = H0.shape[0]
dims = [H0.dims, H0.dims]
u = np.zeros([N * N, N * N, len(tlist)], dtype=complex)
if sparse:
for n in range(N * N):
psi0 = basis(N * N, n)
psi0.dims = [dims[0], 1]
rho0 = vector_to_operator(psi0)
output = mesolve(H, rho0, tlist, c_op_list, [], args, options)
for k, t in enumerate(tlist):
u[:, n, k] = operator_to_vector(
output.states[k]).full(squeeze=True)
else:
for n in range(N * N):
psi0 = basis(N * N, n)
rho0 = Qobj(vec2mat(psi0.full()))
output = mesolve(H, rho0, tlist, c_op_list, [], args, options)
for k, t in enumerate(tlist):
u[:, n, k] = mat2vec(output.states[k].full()).T
if len(tlist) == 2:
return Qobj(u[:, :, 1], dims=dims)
else:
return [Qobj(u[:, :, k], dims=dims) for k in range(len(tlist))]
示例12: dnorm
#.........这里部分代码省略.........
Diamond norm of q_oper.
Raises
------
ImportError
If CVXPY cannot be imported.
.. _cvxpy: http://www.cvxpy.org/en/latest/
"""
if cvxpy is None: # pragma: no cover
raise ImportError("dnorm() requires CVXPY to be installed.")
# We follow the strategy of using Watrous' simpler semidefinite
# program in its primal form. This is the same strategy used,
# for instance, by both pyGSTi and SchattenNorms.jl. (By contrast,
# QETLAB uses the dual problem.)
# Check if A and B are both unitaries. If so, then we can without
# loss of generality choose B to be the identity by using the
# unitary invariance of the diamond norm,
# || A - B ||_♢ = || A B⁺ - I ||_♢.
# Then, using the technique mentioned by each of Johnston and
# da Silva,
# || A B⁺ - I ||_♢ = max_{i, j} | \lambda_i(A B⁺) - \lambda_j(A B⁺) |,
# where \lambda_i(U) is the ith eigenvalue of U.
if (
# There's a lot of conditions to check for this path.
not force_solve and B is not None and
# Only check if they aren't superoperators.
A.type == "oper" and B.type == "oper" and
# The difference of unitaries optimization is currently
# only implemented for d == 2. Much of the code below is more general,
# though, in anticipation of generalizing the optimization.
A.shape[0] == 2
):
# Make an identity the same size as A and B to
# compare against.
I = qeye(A.dims[0])
# Compare to B first, so that an error is raised
# as soon as possible.
Bd = B.dag()
if (
(B * Bd - I).norm() < 1e-6 and
(A * A.dag() - I).norm() < 1e-6
):
# Now we are on the fast path, so let's compute the
# eigenvalues, then find the diameter of the smallest circle
# containing all of them.
#
# For now, this is only implemented for dim = 2, such that
# generalizing here will allow for generalizing the optimization.
# A reasonable approach would probably be to use Welzl's algorithm
# (https://en.wikipedia.org/wiki/Smallest-circle_problem).
U = A * B.dag()
eigs = U.eigenenergies()
eig_distances = np.abs(eigs[:, None] - eigs[None, :])
return np.max(eig_distances)
# Force the input superoperator to be a Choi matrix.
J = to_choi(A)
if B is not None:
J -= to_choi(B)
# Watrous 2012 also points out that the diamond norm of Lambda
# is the same as the completely-bounded operator-norm (∞-norm)
# of the dual map of Lambda. We can evaluate that norm much more
# easily if Lambda is completely positive, since then the largest
# eigenvalue is the same as the largest singular value.
if not force_solve and J.iscp:
S_dual = to_super(J.dual_chan())
vec_eye = operator_to_vector(qeye(S_dual.dims[1][1]))
op = vector_to_operator(S_dual * vec_eye)
# The 2-norm was not implemented for sparse matrices as of the time
# of this writing. Thus, we must yet again go dense.
return la.norm(op.data.todense(), 2)
# If we're still here, we need to actually solve the problem.
# Assume square...
dim = np.prod(J.dims[0][0])
# The constraints only depend on the dimension, so
# we can cache them efficiently.
problem, Jr, Ji, X, rho0, rho1 = dnorm_problem(dim)
# Load the parameters with the Choi matrix passed in.
J_dat = J.data
Jr.value = sp.csr_matrix((J_dat.data.real, J_dat.indices, J_dat.indptr),
shape=J_dat.shape)
Ji.value = sp.csr_matrix((J_dat.data.imag, J_dat.indices, J_dat.indptr),
shape=J_dat.shape)
# Finally, set up and run the problem.
problem.solve(solver=solver, verbose=verbose)
return problem.value