Skip to content

Commit

Permalink
Convert if-elif clauses to if-else.
Browse files Browse the repository at this point in the history
  • Loading branch information
thangleiter committed Jun 19, 2020
1 parent ca21477 commit 9ae961b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
47 changes: 30 additions & 17 deletions filter_functions/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@ def calculate_decay_amplitudes(
else:
R = pulse.get_control_matrix(omega, show_progressbar)
F = None
elif which == 'correlations':
else:
# which == 'correlations'
if pulse.is_cached('omega'):
if not np.array_equal(pulse.omega, omega):
raise ValueError('Pulse correlation decay amplitudes ' +
Expand All @@ -598,7 +599,7 @@ def calculate_decay_amplitudes(
integrand = _get_integrand(S, omega, idx, which, 'generalized',
R=[R[..., 0:1, :], R], F=F)
n_kl = R.shape[-2]
elif F is not None:
else:
integrand = _get_integrand(S, omega, idx, which, 'generalized',
R=R, F=F[..., 0:1, :, :])
n_kl = F.shape[-2]
Expand All @@ -612,7 +613,7 @@ def calculate_decay_amplitudes(
if R is not None:
integrand = _get_integrand(S, omega, idx, which, 'generalized',
R=[R[..., k:k+1, :], R], F=F)
elif F is not None:
else:
integrand = _get_integrand(S, omega, idx, which, 'generalized',
R=R, F=F[..., k:k+1, :, :])

Expand Down Expand Up @@ -669,7 +670,8 @@ def calculate_filter_function(R: ndarray, which: str = 'fidelity') -> ndarray:
"""
if which == 'fidelity':
return np.einsum('ako,bko->abo', R.conj(), R)
elif which == 'generalized':
else:
# which == 'generalized'
return np.einsum('ako,blo->abklo', R.conj(), R)


Expand Down Expand Up @@ -727,7 +729,8 @@ def calculate_pulse_correlation_filter_function(

if which == 'fidelity':
return np.einsum('gako,hbko->ghabo', R.conj(), R)
elif which == 'generalized':
else:
# which == 'generalized'
return np.einsum('gako,hblo->ghabklo', R.conj(), R)


Expand Down Expand Up @@ -1100,7 +1103,8 @@ def infidelity(pulse: 'PulseSequence',
F = np.einsum('ako,blo,kl->abo', R.conj(), R, Tp)/pulse.d
else:
F = pulse.get_filter_function(omega)
elif which == 'correlations':
else:
# which == 'correlations'
if not pulse.basis.istraceless:
warn('Calculating pulse correlation fidelities with non-' +
'traceless basis. The results will be off.')
Expand Down Expand Up @@ -1227,7 +1231,8 @@ def _get_integrand(S: ndarray, omega: ndarray, idx: ndarray, which_pulse: str,
R_left, R_right = [f(r) for f, r in zip(funs, R)]
else:
R_left, R_right = [f(r) for f, r in zip(funs, [R]*2)]
elif F is not None:
else:
# F is not None
if which_FF == 'generalized':
# Everything simpler if noise operators always on 2nd-to-last axes
F = np.moveaxis(F, source=[-5, -4], destination=[-3, -2])
Expand All @@ -1242,8 +1247,8 @@ def _get_integrand(S: ndarray, omega: ndarray, idx: ndarray, which_pulse: str,
raise ValueError(S_err_str.format(shape, S.shape))

S = np.expand_dims(S, 0)
elif S.ndim == 2:
# S is diagonal (no correlation between noise sources)
else:
# S.ndim == 2, S is diagonal (no correlation between noise sources)
shape = (len(idx), len(omega))
if S.shape != shape:
raise ValueError(S_err_str.format(shape, S.shape))
Expand All @@ -1255,16 +1260,20 @@ def _get_integrand(S: ndarray, omega: ndarray, idx: ndarray, which_pulse: str,
# move axes back to expected position, ie (pulses, noise opers,
# basis elements, frequencies)
integrand = np.moveaxis(integrand, source=-2, destination=-4)
elif R is not None:
else:
# R is not None
if which_pulse == 'correlations':
if which_FF == 'fidelity':
einsum_str = 'gako,ao,hako->ghao'
elif which_FF == 'generalized':
else:
# which_FF == 'generalized'
einsum_str = 'gako,ao,halo->ghaklo'
elif which_pulse == 'total':
else:
# which_pulse == 'total'
if which_FF == 'fidelity':
einsum_str = 'ako,ao,ako->ao'
elif which_FF == 'generalized':
else:
# which_FF == 'generalized'
einsum_str = 'ako,ao,alo->aklo'

integrand = np.einsum(einsum_str,
Expand All @@ -1281,16 +1290,20 @@ def _get_integrand(S: ndarray, omega: ndarray, idx: ndarray, which_pulse: str,
if which_FF == 'generalized':
integrand = np.moveaxis(integrand, source=[-3, -2],
destination=[-5, -4])
elif R is not None:
else:
# R is not None
if which_pulse == 'correlations':
if which_FF == 'fidelity':
einsum_str = 'gako,abo,hbko->ghabo'
elif which_FF == 'generalized':
else:
# which_FF == 'generalized'
einsum_str = 'gako,abo,hblo->ghabklo'
elif which_pulse == 'total':
else:
# which_pulse == 'total'
if which_FF == 'fidelity':
einsum_str = 'ako,abo,bko->abo'
elif which_FF == 'generalized':
else:
# which_FF == 'generalized'
einsum_str = 'ako,abo,blo->abklo'

integrand = np.einsum(einsum_str,
Expand Down
8 changes: 5 additions & 3 deletions filter_functions/pulse_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,8 @@ def _parse_Hamiltonian(H: Hamiltonian, n_dt: int,
('A_{}'.format(i) for i in range(len(opers))),
dtype='<U4'
)
elif H_str == 'H_n':
else:
# H_str == 'H_n'
identifiers = np.fromiter(
('B_{}'.format(i) for i in range(len(opers))),
dtype='<U4'
Expand All @@ -1055,7 +1056,8 @@ def _parse_Hamiltonian(H: Hamiltonian, n_dt: int,
if identifier is None:
if H_str == 'H_c':
identifiers[i] = 'A_{}'.format(i)
elif H_str == 'H_n':
else:
# H_str == 'H_n'
identifiers[i] = 'B_{}'.format(i)
if len(set(identifiers)) != len(identifiers):
raise ValueError('{} identifiers should be unique'.format(H_str))
Expand Down Expand Up @@ -2208,7 +2210,7 @@ def extend(pulse_to_qubit_mapping: PulseMapping,
basis = Basis.ggm(d_per_qubit**N)
elif btype == 'Pauli':
basis = Basis.pauli(N)
elif btype == 'Custom':
else:
warn('Original pulses had custom basis which I cannot extend.')
basis = Basis.ggm(d_per_qubit**N)

Expand Down

0 comments on commit 9ae961b

Please sign in to comment.