Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
np.asarray(self.indices, dtype=idx_dtype),
self.data,
np.asarray(other.indptr, dtype=idx_dtype),
np.asarray(other.indices, dtype=idx_dtype),
other.data,
indptr, indices, data)
actual_nnz = indptr[-1]
indices = indices[:actual_nnz]
data = data[:actual_nnz]
if actual_nnz < maxnnz // 2:
# too much waste, trim arrays
indices = indices.copy()
data = data.copy()
if isinstance(other, fast_csr_matrix) and (not op in bool_ops):
A = fast_csr_matrix((data, indices, indptr), dtype=data.dtype, shape=self.shape)
else:
A = csr_matrix((data, indices, indptr), dtype=data.dtype, shape=self.shape)
return A
np.asarray(self.indptr, dtype=idx_dtype),
np.asarray(self.indices, dtype=idx_dtype),
self.data,
np.asarray(other.indptr, dtype=idx_dtype),
np.asarray(other.indices, dtype=idx_dtype),
other.data,
indptr, indices, data)
actual_nnz = indptr[-1]
indices = indices[:actual_nnz]
data = data[:actual_nnz]
if actual_nnz < maxnnz // 2:
# too much waste, trim arrays
indices = indices.copy()
data = data.copy()
if isinstance(other, fast_csr_matrix) and (not op in bool_ops):
A = fast_csr_matrix((data, indices, indptr), dtype=data.dtype, shape=self.shape)
else:
A = csr_matrix((data, indices, indptr), dtype=data.dtype, shape=self.shape)
return A
def test_complex_control_rejection():
"""Test that complex controls are rejected"""
H0 = qutip.Qobj(0.5 * np.diag([-1, 1]))
H1 = qutip.Qobj(np.mat([[1, 2], [3, 4]]))
psi0 = qutip.Qobj(np.array([1, 0]))
psi1 = qutip.Qobj(np.array([0, 1]))
def eps0(t, args):
return 0.2 * np.exp(1j * t)
def S(t):
"""Shape function for the field update"""
return krotov.shapes.flattop(
t, t_start=0, t_stop=5, t_rise=0.3, t_fall=0.3, func='sinsq'
)
H = [H0, [H1, eps0]]
def cphase_lv_full_objectives(canonical_basis):
L = qutip.Qobj() # dummy Liouvillian (won't be used)
return krotov.objectives.gate_objectives(
canonical_basis,
gate=qutip_gates.cphase(np.pi),
H=L,
liouville_states_set='full',
)
def test_extract_controls():
"""Check that we can extract a list of controls from a list of
objectives"""
# dummy objects
X = qutip.Qobj()
Y = qutip.Qobj()
f = lambda t: 0
g = lambda t: 0
h = lambda t: 0
d = lambda t: 0
# all of these dummy objects should be distinguishable
assert f is not g
assert g is not h
assert h is not d
assert X is not Y
H1 = [X, [X, f], [X, g]]
H2 = [X, [X, f], [X, h]]
H3 = [X, [X, d], X]
# check same Hamiltonian occuring in multiple objectives
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 prep_rot_qutip(n, a):
q = qutip.basis(2)
nNorm = np.linalg.norm(n)
R = (-1j * a / (2 * nNorm) * (n[0] * qutip.sigmax() + n[1] * qutip.sigmay() + n[2] * qutip.sigmaz())).expm()
return R * q
def prep_Z_qutip():
q = qutip.basis(2)
Z = qutip.sigmaz()
return Z * q