Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_tensor_product():
H = LocalSpace(hs_name(), dimension=5)
a = Create(hs=H).adjoint()
H2 = LocalSpace(hs_name(), basis=("e", "g", "h"))
sigma = LocalSigma('g', 'e', hs=H2)
assert convert_to_qutip(sigma * a) == \
qutip.tensor(convert_to_qutip(a),
convert_to_qutip(sigma))
def test_gate_objectives_shape_error():
"""Test that trying to construct gate objectives with a gate whose shape
mismatches the basis throws an exception"""
basis = [qutip.ket([0]), qutip.ket([1])]
gate = qutip.tensor(qutip.operators.sigmay(), qutip.identity(2))
H = [
qutip.operators.sigmaz(),
[qutip.operators.sigmax(), lambda t, args: 1.0],
]
with pytest.raises(ValueError) as exc_info:
krotov.objectives.gate_objectives(basis, gate, H)
assert "same dimension as the number of basis" in str(exc_info.value)
def test_liouvillian():
"""Test conversion of Hamiltonian/Lindblad operators into a Liouvillian"""
H = [
tensor(sigmaz(), identity(2)) + tensor(identity(2), sigmaz()),
[tensor(sigmax(), identity(2)), lambda t, args: 1.0],
[tensor(identity(2), sigmax()), lambda t, args: 1.0],
]
c_ops = [tensor(sigmam(), identity(2)), tensor(identity(2), sigmam())]
assert (
krotov.objectives.liouvillian(H[0], c_ops)
- qutip.liouvillian(H[0], c_ops)
).norm('max') < 1e-15
L = krotov.objectives.liouvillian(H, c_ops)
assert isinstance(L, list)
assert len(L) == 3
assert (L[0] - qutip.liouvillian(H[0], c_ops)).norm('max') < 1e-15
assert (L[1][0] - qutip.liouvillian(H[1][0])).norm('max') < 1e-15
assert (L[2][0] - qutip.liouvillian(H[2][0])).norm('max') < 1e-15
assert L[1][1] is H[1][1]
assert L[2][1] is H[2][1]
with pytest.raises(ValueError):
def __init__(self, capacity=3):
self.capacity = capacity
self.available_qubits = [ # <5>
SimulatedQubit(self, idx)
for idx in range(capacity)
]
self.register_state = qt.tensor( # <6>
*[
qt.basis(2, 0)
for _ in range(capacity)
]
def _localop(op, l, k):
"""
Create a local operator on the l'th system by tensoring
with identity operators on all the other k-1 systems
"""
if l < 1 or l > k:
raise IndexError('index l out of range')
h = op
I = qt.qeye(op.shape[0])
I.dims = op.dims
for i in range(1, l):
h = qt.tensor(I, h)
for i in range(l+1, k+1):
h = qt.tensor(h, I)
return h
def _convert_local_operator_to_qutip(expr, full_space, mapping):
"""Convert a LocalOperator instance to qutip"""
n = full_space.dimension
if full_space != expr.space:
all_spaces = full_space.local_factors
own_space_index = all_spaces.index(expr.space)
return qutip.tensor(
*([qutip.qeye(s.dimension)
for s in all_spaces[:own_space_index]] +
[convert_to_qutip(expr, expr.space, mapping=mapping), ] +
[qutip.qeye(s.dimension)
for s in all_spaces[own_space_index + 1:]])
)
if isinstance(expr, Create):
return qutip.create(n)
elif isinstance(expr, Jz):
return qutip.jmat((expr.space.dimension-1)/2., "z")
elif isinstance(expr, Jplus):
return qutip.jmat((expr.space.dimension-1)/2., "+")
elif isinstance(expr, Jminus):
return qutip.jmat((expr.space.dimension-1)/2., "-")
elif isinstance(expr, Destroy):
return qutip.destroy(n)
N = int(N)
sx = [0 for i in range(N)]
sy = [0 for i in range(N)]
sz = [0 for i in range(N)]
sp = [0 for i in range(N)]
sm = [0 for i in range(N)]
sx[0] = 0.5 * sigmax()
sy[0] = 0.5 * sigmay()
sz[0] = 0.5 * sigmaz()
sp[0] = sigmap()
sm[0] = sigmam()
# 2. Place operators in total Hilbert space
for k in range(N - 1):
sx[0] = tensor(sx[0], identity(2))
sy[0] = tensor(sy[0], identity(2))
sz[0] = tensor(sz[0], identity(2))
sp[0] = tensor(sp[0], identity(2))
sm[0] = tensor(sm[0], identity(2))
# 3. Cyclic sequence to create all N operators
a = [i for i in range(N)]
b = [[a[i - i2] for i in range(N)] for i2 in range(N)]
# 4. Create N operators
for i in range(1, N):
sx[i] = sx[0].permute(b[i])
sy[i] = sy[0].permute(b[i])
sz[i] = sz[0].permute(b[i])
sp[i] = sp[0].permute(b[i])
sm[i] = sm[0].permute(b[i])
Returns
----------
Qobj(Hamil) : qutip.Qobj
oper type Quantum object representing the lattice Hamiltonian.
"""
D = qeye(self.num_cell)
T = np.diag(np.zeros(self.num_cell-1)+1, 1)
Tdag = np.diag(np.zeros(self.num_cell-1)+1, -1)
if self.period_bnd_cond_x == 1 and self.num_cell > 2:
Tdag[0][self.num_cell-1] = 1
T[self.num_cell-1][0] = 1
T = Qobj(T)
Tdag = Qobj(Tdag)
Hamil = tensor(D, self._H_intra) + tensor(
T, self._H_inter) + tensor(Tdag, self._H_inter.dag())
dim_H = [self.lattice_tensor_config, self.lattice_tensor_config]
return Qobj(Hamil, dims=dim_H)
def _pseudo_inverse_dense(L, rhoss, w=None, **pseudo_args):
"""
Internal function for computing the pseudo inverse of an Liouvillian using
dense matrix methods. See pseudo_inverse for details.
"""
rho_vec = np.transpose(mat2vec(rhoss.full()))
tr_mat = tensor([identity(n) for n in L.dims[0][0]])
tr_vec = np.transpose(mat2vec(tr_mat.full()))
N = np.prod(L.dims[0][0])
I = np.identity(N * N)
P = np.kron(np.transpose(rho_vec), tr_vec)
Q = I - P
if w is None:
L = L
else:
L = 1.0j*w*spre(tr_mat)+L
if pseudo_args['method'] == 'direct':
try:
LIQ = np.linalg.solve(L.full(), Q)
except:
LIQ = np.linalg.lstsq(L.full(), Q)[0]