Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def canonical_basis():
return [ket('00'), ket('01'), ket('10'), ket('11')]
def objective_with_c_ops():
u1 = lambda t, args: 1.0
u2 = lambda t, args: 1.0
a1 = np.random.random(100) + 1j * np.random.random(100)
a2 = np.random.random(100) + 1j * np.random.random(100)
H = [
tensor(sigmaz(), identity(2)) + tensor(identity(2), sigmaz()),
[tensor(sigmax(), identity(2)), u1],
[tensor(identity(2), sigmax()), u2],
]
C1 = [[tensor(identity(2), sigmap()), a1]]
C2 = [[tensor(sigmap(), identity(2)), a2]]
ket00 = ket((0, 0))
ket11 = ket((1, 1))
obj = krotov.Objective(
initial_state=ket00, target=ket11, H=H, c_ops=[C1, C2]
)
return obj
def ketbra(a, b):
return ket(a) * ket(b).dag()
def test_chi_hs_transmon(transmon_3states_objectives):
objectives = transmon_3states_objectives
n_qubit = objectives[0].initial_state.dims[0][0]
ket00 = qutip.ket((0, 0), dim=(n_qubit, n_qubit))
ket01 = qutip.ket((0, 1), dim=(n_qubit, n_qubit))
ket10 = qutip.ket((1, 0), dim=(n_qubit, n_qubit))
ket11 = qutip.ket((1, 1), dim=(n_qubit, n_qubit))
ρ_mixed = 0.25 * (
ket00 * ket00.dag()
+ ket01 * ket01.dag()
+ ket10 * ket10.dag()
+ ket11 * ket11.dag()
)
assert (ρ_mixed * ρ_mixed).tr() == 0.25
assert (ρ_mixed - objectives[2].target).norm('max') < 1e-14
fw_states_T = [ρ_mixed, ρ_mixed, ρ_mixed]
χs = krotov.functionals.chis_hs(fw_states_T, objectives, None)
χ1 = (1 / 6.0) * (60.0 / 22.0) * (objectives[0].target - ρ_mixed)
χ2 = (1 / 6.0) * (3.0 / 22.0) * (objectives[1].target - ρ_mixed)
χ3 = 0.0 * ρ_mixed
assert (χs[0] - χ1).norm('max') < 1e-14
assert (χs[1] - χ2).norm('max') < 1e-14
def test_gate_objectives_5states(two_qubit_liouvillian):
"""Test the initialization of the "d + 1" objectives"""
L = two_qubit_liouvillian
basis = [qutip.ket(n) for n in [(0, 0), (0, 1), (1, 0), (1, 1)]]
CNOT = qutip_gates.cnot()
objectives = krotov.objectives.gate_objectives(
basis, CNOT, L, liouville_states_set='d+1'
)
assert len(objectives) == 5
rho_1 = basis[0] * basis[0].dag()
rho_2 = basis[1] * basis[1].dag()
rho_3 = basis[2] * basis[2].dag()
rho_4 = basis[3] * basis[3].dag()
rho_5 = qutip.Qobj(np.full((4, 4), 1 / 4), dims=[[2, 2], [2, 2]])
tgt_1 = CNOT * rho_1 * CNOT.dag()
tgt_2 = CNOT * rho_2 * CNOT.dag()
tgt_3 = CNOT * rho_3 * CNOT.dag()
def tls_control_system():
"""Non-trivial control system defined on a TLS"""
eps1 = lambda t, args: 0.5
eps2 = lambda t, args: 1
H1 = [0.5 * sigmaz(), [sigmap(), eps1], [sigmam(), eps1]]
H2 = [0.5 * sigmaz(), [sigmaz(), eps2]]
c_ops = [0.1 * sigmap()]
objectives = [
krotov.Objective(
initial_state=ket('0'), target=ket('1'), H=H1, c_ops=c_ops
),
krotov.Objective(
initial_state=ket('0'), target=ket('1'), H=H2, c_ops=c_ops
),
]
controls = [eps1, eps2]
controls_mapping = krotov.conversions.extract_controls_mapping(
objectives, controls
)
return objectives, controls, controls_mapping
def transmon_3states_objectives():
# see also test_objectives:test_transmon_3states_objectives
L = qutip.Qobj() # dummy Liouvillian (won't be used)
n_qubit = 3
ket00 = qutip.ket((0, 0), dim=(n_qubit, n_qubit))
ket01 = qutip.ket((0, 1), dim=(n_qubit, n_qubit))
ket10 = qutip.ket((1, 0), dim=(n_qubit, n_qubit))
ket11 = qutip.ket((1, 1), dim=(n_qubit, n_qubit))
basis = [ket00, ket01, ket10, ket11]
weights = [20, 1, 1]
objectives = krotov.gate_objectives(
basis,
qutip_gates.sqrtiswap(),
L,
liouville_states_set='3states',
weights=weights,
)
return objectives
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)
pulses,
pulses_mapping,
i_pulse=0,
time_index=0,
)
# however, we do allow the c_ops to be time-dependent with controls we're
# not taking the derivative with respect to
mu = krotov.mu.derivative_wrt_pulse(
objectives,
i_objective,
pulses,
pulses_mapping,
i_pulse=1,
time_index=0,
)
for state in (ket('0'), ket('1')):
assert mu(state).norm('max') == 0
assert (mu(state)).dims == state.dims