Skip to content

Commit

Permalink
rename methods
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Nov 12, 2024
1 parent a8e7edd commit f2e7c6d
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 46 deletions.
9 changes: 5 additions & 4 deletions src/qibo/backends/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ def sample_frequencies(self, probabilities, nshots): # pragma: no cover
raise_error(NotImplementedError)

@abc.abstractmethod
def calculate_norm(self, state, order=2): # pragma: no cover
"""Calculate norm of a state vector.
def calculate_vector_norm(self, state, order=2): # pragma: no cover
"""Calculate norm of an :math:`1`-dimensional array.
For specifications on possible values of the parameter ``order``
for the ``tensorflow`` backend, please refer to
Expand All @@ -315,9 +315,10 @@ def calculate_norm(self, state, order=2): # pragma: no cover
raise_error(NotImplementedError)

@abc.abstractmethod
def calculate_norm_density_matrix(self, state, order="nuc"): # pragma: no cover
"""Calculate norm of a density matrix. Default is the ``nuclear`` norm.
def calculate_matrix_norm(self, state, order="nuc"): # pragma: no cover
"""Calculate norm of a :math:`2`-dimensional array.
Default is the ``nuclear`` norm.
If ``order="nuc"``, it returns the nuclear norm of ``state``,
assuming ``state`` is Hermitian (also known as trace norm).
For specifications on the other possible values of the
Expand Down
4 changes: 2 additions & 2 deletions src/qibo/backends/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,11 +719,11 @@ def apply_bitflips(self, noiseless_samples, bitflip_probabilities):
noisy_samples = noisy_samples - noiseless_samples * flip_1
return noisy_samples

def calculate_norm(self, state, order=2):
def calculate_vector_norm(self, state, order=2):
state = self.cast(state)
return self.np.linalg.norm(state, order)

def calculate_norm_density_matrix(self, state, order="nuc"):
def calculate_matrix_norm(self, state, order="nuc"):
state = self.cast(state)
return self.np.linalg.norm(state, ord=order)

Expand Down
4 changes: 2 additions & 2 deletions src/qibo/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ class Norm(Callback):
"""

def apply(self, backend, state):
norm = backend.calculate_norm(state)
norm = backend.calculate_vector_norm(state)
self.append(norm)
return norm

def apply_density_matrix(self, backend, state):
norm = backend.calculate_norm_density_matrix(state)
norm = backend.calculate_matrix_norm_density_matrix(state)
self.append(norm)
return norm

Expand Down
2 changes: 1 addition & 1 deletion src/qibo/models/evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(self, hamiltonian, dt, solver="exp", callbacks=[], accelerators=Non
def _create_normalize_state(self, solver):
if "rk" in solver:
log.info("Normalizing state during RK solution.")
return lambda s: s / self.backend.calculate_norm(s)
return lambda s: s / self.backend.calculate_vector_norm(s)
else:
return lambda s: s

Expand Down
4 changes: 2 additions & 2 deletions src/qibo/quantum_info/entropies.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ def renyi_entropy(state, alpha: Union[float, int], base: float = 2, backend=None
if alpha == np.inf:
return (
-1
* backend.np.log2(backend.calculate_norm_density_matrix(state, order=2))
* backend.np.log2(backend.calculate_matrix_norm_density_matrix(state, order=2))
/ np.log2(base)
)

Expand Down Expand Up @@ -890,7 +890,7 @@ def relative_renyi_entropy(
new_target = matrix_power(target, 0.5, backend=backend)

log = backend.np.log2(
backend.calculate_norm_density_matrix(new_state @ new_target, order=1)
backend.calculate_matrix_norm_density_matrix(new_state @ new_target, order=1)
)

return -2 * log / np.log2(base)
Expand Down
12 changes: 6 additions & 6 deletions src/qibo/quantum_info/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def purity(state, backend=None):
)

if len(state.shape) == 1:
pur = backend.np.real(backend.calculate_norm(state)) ** 2
pur = backend.np.real(backend.calculate_vector_norm(state)) ** 2
else:
pur = backend.np.real(backend.np.trace(backend.np.matmul(state, state)))
return float(pur)
Expand Down Expand Up @@ -109,7 +109,7 @@ def trace_distance(state, target, check_hermitian: bool = False, backend=None):
if check_hermitian is True:
hermitian = bool(
float(
backend.calculate_norm_density_matrix(
backend.calculate_matrix_norm_density_matrix(
backend.np.transpose(backend.np.conj(difference), (1, 0))
- difference,
order=2,
Expand Down Expand Up @@ -440,7 +440,7 @@ def process_fidelity(channel, target=None, check_unitary: bool = False, backend=

if check_unitary is True:
norm_channel = float(
backend.calculate_norm_density_matrix(
backend.calculate_matrix_norm_density_matrix(
backend.np.matmul(
backend.np.conj(backend.np.transpose(channel, (1, 0))), channel
)
Expand All @@ -451,7 +451,7 @@ def process_fidelity(channel, target=None, check_unitary: bool = False, backend=
raise_error(TypeError, "Channel is not unitary and Target is None.")
if target is not None:
norm_target = float(
backend.calculate_norm(
backend.calculate_vector_norm(
backend.np.matmul(
backend.np.conj(backend.np.transpose(target, (1, 0))), target
)
Expand Down Expand Up @@ -762,7 +762,7 @@ def expressibility(
circuit.nqubits, power_t, samples=None, backend=backend
) - pqc_integral(circuit, power_t, samples, backend=backend)

fid = float(backend.calculate_norm(deviation, order=order))
fid = float(backend.calculate_vector_norm(deviation, order=order))

return fid

Expand Down Expand Up @@ -929,7 +929,7 @@ def _check_hermitian(matrix, backend=None):
"""
backend = _check_backend(backend)

norm = backend.calculate_norm_density_matrix(
norm = backend.calculate_matrix_norm_density_matrix(
backend.np.conj(matrix).T - matrix, order=2
)

Expand Down
6 changes: 3 additions & 3 deletions src/qibo/quantum_info/quantum_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def is_hermitian(
adjoint = self._backend.np.transpose(reshaped)

mat_diff = self._backend.np.conj(adjoint) - reshaped
norm = self._backend.calculate_norm_density_matrix(mat_diff, order=order)
norm = self._backend.calculate_matrix_norm_density_matrix(mat_diff, order=order)

return float(norm) <= precision_tol

Expand Down Expand Up @@ -787,7 +787,7 @@ def is_causal(
sub_comb = self._tensordot(reduced, trace_in, axes=(-1, 0))
expected = self._tensordot(sub_comb, trace_in / dim_in, axes=0)

norm = self._backend.calculate_norm(reduced - expected, order=order)
norm = self._backend.calculate_vector_norm(reduced - expected, order=order)

if float(norm) > precision_tol:
return False
Expand Down Expand Up @@ -926,7 +926,7 @@ def is_unital(
sub_comb = self._tensordot(reduced, trace_out, axes=(0, 0))
expected = self._tensordot(trace_out / dim_out, sub_comb, axes=0)

norm = self._backend.calculate_norm((reduced - expected), order=order)
norm = self._backend.calculate_vector_norm((reduced - expected), order=order)
if float(norm) > precision_tol:
return False

Expand Down
6 changes: 4 additions & 2 deletions src/qibo/quantum_info/superoperator_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def choi_to_kraus(

if validate_cp:
norm = float(
backend.calculate_norm_density_matrix(
backend.calculate_matrix_norm_density_matrix(
choi_super_op - backend.np.conj(choi_super_op).T, order=2
)
)
Expand Down Expand Up @@ -2132,7 +2132,9 @@ def function(x0, operators):
for prob, oper in zip(x0, operators):
operator = operator + prob * oper

return float(backend.calculate_norm_density_matrix(target - operator, order=2))
return float(
backend.calculate_matrix_norm_density_matrix(target - operator, order=2)
)

# initial parameters as flat distribution
x0 = [1.0 / (len(kraus_ops) + 1)] * len(kraus_ops)
Expand Down
4 changes: 2 additions & 2 deletions src/qibo/quantum_info/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def hellinger_distance(prob_dist_p, prob_dist_q, validate: bool = False, backend
raise_error(ValueError, "Second probability array must sum to 1.")

distance = float(
backend.calculate_norm(
backend.calculate_vector_norm(
backend.np.sqrt(prob_dist_p) - backend.np.sqrt(prob_dist_q)
)
/ np.sqrt(2)
Expand Down Expand Up @@ -386,7 +386,7 @@ def total_variation_distance(
if backend.np.abs(backend.np.sum(prob_dist_q) - 1.0) > PRECISION_TOL:
raise_error(ValueError, "Second probability array must sum to 1.")

tvd = backend.calculate_norm(prob_dist_p - prob_dist_q, order=1)
tvd = backend.calculate_vector_norm(prob_dist_p - prob_dist_q, order=1)

return tvd / 2

Expand Down
2 changes: 1 addition & 1 deletion src/qibo/transpiler/unitary_decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def calculate_single_qubit_unitaries(psi, backend=None):
psi_magic = backend.np.matmul(backend.np.conj(backend.cast(magic_basis)).T, psi)
if (
backend.np.real(
backend.calculate_norm_density_matrix(backend.np.imag(psi_magic))
backend.calculate_matrix_norm_density_matrix(backend.np.imag(psi_magic))
)
> 1e-6
): # pragma: no cover
Expand Down
10 changes: 5 additions & 5 deletions tests/test_gates_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def test_kraus_channel(backend, pauli_order):

backend.assert_allclose(
float(
backend.calculate_norm_density_matrix(
backend.calculate_matrix_norm_density_matrix(
channel.to_liouville(backend=backend) - test_superop, order=2
)
)
Expand All @@ -117,7 +117,7 @@ def test_kraus_channel(backend, pauli_order):
)
backend.assert_allclose(
float(
backend.calculate_norm_density_matrix(
backend.calculate_matrix_norm_density_matrix(
channel.to_choi(backend=backend) - test_choi, order=2
)
)
Expand All @@ -126,7 +126,7 @@ def test_kraus_channel(backend, pauli_order):
)
backend.assert_allclose(
float(
backend.calculate_norm(
backend.calculate_vector_norm(
channel.to_pauli_liouville(pauli_order=pauli_order, backend=backend)
- test_pauli
)
Expand Down Expand Up @@ -240,7 +240,7 @@ def test_pauli_noise_channel(backend, pauli_order):
normalize=True, pauli_order=pauli_order, backend=backend
)
norm = float(
backend.calculate_norm_density_matrix(
backend.calculate_matrix_norm_density_matrix(
backend.to_numpy(liouville) - test_representation, order=2
)
)
Expand Down Expand Up @@ -384,7 +384,7 @@ def test_thermal_relaxation_channel(backend, t_1, t_2, time, excpop):

backend.assert_allclose(
float(
backend.calculate_norm_density_matrix(final_state - target_state, order=2)
backend.calculate_matrix_norm_density_matrix(final_state - target_state, order=2)
)
< PRECISION_TOL,
True,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_models_encodings.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_unary_encoder(backend, nqubits, architecture, kind):

backend.assert_allclose(
state,
backend.cast(data, dtype=np.float64) / backend.calculate_norm(data, 2),
backend.cast(data, dtype=np.float64) / backend.calculate_vector_norm(data, 2),
rtol=1e-5,
)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_quantum_info_entropies.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def test_renyi_entropy(backend, alpha, base):
elif alpha == 1.0:
target = von_neumann_entropy(state, base=base, backend=backend)
elif alpha == np.inf:
target = backend.calculate_norm_density_matrix(state, order=2)
target = backend.calculate_matrix_norm_density_matrix(state, order=2)
target = -1 * backend.np.log2(target) / np.log2(base)
else:
target = np.log2(
Expand Down Expand Up @@ -687,7 +687,7 @@ def test_relative_renyi_entropy(backend, alpha, base, state_flag, target_flag):
new_target = matrix_power(target_outer, 0.5, backend=backend)

log = backend.np.log2(
backend.calculate_norm_density_matrix(
backend.calculate_matrix_norm_density_matrix(
new_state @ new_target, order=1
)
)
Expand Down
22 changes: 11 additions & 11 deletions tests/test_quantum_info_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ def test_random_hermitian(backend):
dims = 4
matrix = random_hermitian(dims, backend=backend)
matrix_dagger = backend.np.conj(matrix).T
norm = float(backend.calculate_norm_density_matrix(matrix - matrix_dagger, order=2))
norm = float(backend.calculate_matrix_norm_density_matrix(matrix - matrix_dagger, order=2))
backend.assert_allclose(norm < PRECISION_TOL, True)

# test if function returns semidefinite Hermitian operator
dims = 4
matrix = random_hermitian(dims, semidefinite=True, backend=backend)
matrix_dagger = backend.np.conj(matrix).T
norm = float(backend.calculate_norm_density_matrix(matrix - matrix_dagger, order=2))
norm = float(backend.calculate_matrix_norm_density_matrix(matrix - matrix_dagger, order=2))
backend.assert_allclose(norm < PRECISION_TOL, True)

eigenvalues = np.linalg.eigvalsh(backend.to_numpy(matrix))
Expand All @@ -130,7 +130,7 @@ def test_random_hermitian(backend):
dims = 4
matrix = random_hermitian(dims, normalize=True, backend=backend)
matrix_dagger = backend.np.conj(matrix).T
norm = float(backend.calculate_norm_density_matrix(matrix - matrix_dagger, order=2))
norm = float(backend.calculate_matrix_norm_density_matrix(matrix - matrix_dagger, order=2))
backend.assert_allclose(norm < PRECISION_TOL, True)

eigenvalues = np.linalg.eigvalsh(backend.to_numpy(matrix))
Expand All @@ -141,7 +141,7 @@ def test_random_hermitian(backend):
dims = 4
matrix = random_hermitian(dims, semidefinite=True, normalize=True, backend=backend)
matrix_dagger = backend.np.conj(matrix).T
norm = float(backend.calculate_norm(matrix - matrix_dagger, order=2))
norm = float(backend.calculate_vector_norm(matrix - matrix_dagger, order=2))
backend.assert_allclose(norm < PRECISION_TOL, True)

eigenvalues = np.linalg.eigvalsh(backend.to_numpy(matrix))
Expand Down Expand Up @@ -178,7 +178,7 @@ def test_random_unitary(backend, measure):
else np.linalg.inv(matrix)
)
norm = float(
backend.calculate_norm_density_matrix(matrix_inv - matrix_dagger, order=2)
backend.calculate_matrix_norm_density_matrix(matrix_inv - matrix_dagger, order=2)
)
backend.assert_allclose(norm < PRECISION_TOL, True)

Expand Down Expand Up @@ -275,7 +275,7 @@ def test_random_density_matrix(backend, dims, pure, metric, basis, normalize):
test = random_density_matrix(dims=dims, normalize=True)
else:
norm_function = (
backend.calculate_norm_density_matrix
backend.calculate_matrix_norm_density_matrix
if basis is None
else backend.calculate_norm
)
Expand Down Expand Up @@ -416,7 +416,7 @@ def test_pauli_single(backend):
backend.assert_allclose(
np.abs(
backend.to_numpy(
backend.calculate_norm_density_matrix(matrix - result, order=2)
backend.calculate_matrix_norm_density_matrix(matrix - result, order=2)
)
)
< PRECISION_TOL,
Expand Down Expand Up @@ -457,7 +457,7 @@ def test_random_pauli(
if subset is None:
backend.assert_allclose(
float(
backend.calculate_norm_density_matrix(
backend.calculate_matrix_norm_density_matrix(
matrix - result_complete_set, order=2
)
)
Expand All @@ -467,7 +467,7 @@ def test_random_pauli(
else:
backend.assert_allclose(
float(
backend.calculate_norm_density_matrix(
backend.calculate_matrix_norm_density_matrix(
matrix - result_subset, order=2
)
)
Expand All @@ -482,7 +482,7 @@ def test_random_pauli(
if subset is None:
backend.assert_allclose(
float(
backend.calculate_norm_density_matrix(
backend.calculate_matrix_norm_density_matrix(
matrix - result_complete_set, order=2
)
)
Expand All @@ -492,7 +492,7 @@ def test_random_pauli(
else:
backend.assert_allclose(
float(
backend.calculate_norm_density_matrix(
backend.calculate_matrix_norm_density_matrix(
matrix - result_subset, order=2
)
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_quantum_info_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def test_hellinger(backend, validate, kind):
prob_q = backend.cast(prob_q, dtype=prob_q.dtype)

target = float(
backend.calculate_norm(backend.np.sqrt(prob_p) - backend.np.sqrt(prob_q))
backend.calculate_vector_norm(backend.np.sqrt(prob_p) - backend.np.sqrt(prob_q))
/ np.sqrt(2)
)

Expand Down Expand Up @@ -242,7 +242,7 @@ def test_total_variation_distance(backend, validate, kind):
prob_p = backend.cast(prob_p, dtype=prob_p.dtype)
prob_q = backend.cast(prob_q, dtype=prob_q.dtype)

target = float(backend.calculate_norm(prob_p - prob_q, order=1) / 2)
target = float(backend.calculate_vector_norm(prob_p - prob_q, order=1) / 2)

prob_p = (
kind(prob_p) if kind is not None else backend.cast(prob_p, dtype=prob_p.dtype)
Expand Down

0 comments on commit f2e7c6d

Please sign in to comment.