Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add order parameter to backend.calculate_norm #1043

Merged
merged 17 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/api-reference/qibo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ Parametric ZX interaction (RZX)
:member-order: bysource

Parametric XX-YY interaction (RXXYY)
""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""

.. autoclass:: qibo.gates.RXXYY
:members:
Expand Down
24 changes: 20 additions & 4 deletions src/qibo/backends/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,29 @@ def entanglement_entropy(self, rho): # pragma: no cover
raise_error(NotImplementedError)

@abc.abstractmethod
def calculate_norm(self, state): # pragma: no cover
"""Calculate norm of a state vector."""
def calculate_norm(self, state, order=2): # pragma: no cover
"""Calculate norm of a state vector.
renatomello marked this conversation as resolved.
Show resolved Hide resolved

For specifications on possible values of the parameter ``order``
for the ``tensorflow`` backend, please refer to
`tensorflow.norm <https://www.tensorflow.org/api_docs/python/tf/norm>`_.
For all other backends, please refer to
`numpy.linalg.norm <https://numpy.org/doc/stable/reference/generated/numpy.linalg.norm.html>`_.
"""
raise_error(NotImplementedError)

@abc.abstractmethod
def calculate_norm_density_matrix(self, state): # pragma: no cover
"""Calculate norm (trace) of a density matrix."""
def calculate_norm_density_matrix(self, state, order="nuc"): # pragma: no cover
"""Calculate norm (trace) of a density matrix.

If ``order="nuc"``, it returns the nuclear norm os ``state``,
renatomello marked this conversation as resolved.
Show resolved Hide resolved
assuming ``state`` is Hermitian. For specifications on the other
possible values of the parameter ``order`` for the ``tensorflow`` backend,
please refer to
`tensorflow.norm <https://www.tensorflow.org/api_docs/python/tf/norm>`_.
For all other backends, please refer to
`numpy.linalg.norm <https://numpy.org/doc/stable/reference/generated/numpy.linalg.norm.html>`_.
"""
raise_error(NotImplementedError)

@abc.abstractmethod
Expand Down
8 changes: 4 additions & 4 deletions src/qibo/backends/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,13 +673,13 @@ def entanglement_entropy(self, rho):
entropy = self.np.sum(masked_eigvals * spectrum) / self.np.log(2.0)
return entropy, spectrum

def calculate_norm(self, state):
def calculate_norm(self, state, order=2):
state = self.cast(state)
return self.np.sqrt(self.np.sum(self.np.abs(state) ** 2))
return self.np.linalg.norm(state, ord=order)

def calculate_norm_density_matrix(self, state):
def calculate_norm_density_matrix(self, state, order="nuc"):
state = self.cast(state)
return self.np.trace(state)
renatomello marked this conversation as resolved.
Show resolved Hide resolved
return self.np.linalg.norm(state, ord=order)

def calculate_overlap(self, state1, state2):
state1 = self.cast(state1)
Expand Down
10 changes: 10 additions & 0 deletions src/qibo/backends/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,16 @@ def entanglement_entropy(self, rho):
entropy = self.np.sum(masked_eigvals * spectrum) / self.np.log(2.0)
return entropy, spectrum

def calculate_norm(self, state, order=2):
state = self.cast(state)
renatomello marked this conversation as resolved.
Show resolved Hide resolved
return self.tf.norm(state, ord=order)

def calculate_norm_density_matrix(self, state, order="nuc"):
state = self.cast(state)
if order == "nuc":
return self.np.trace(state)
renatomello marked this conversation as resolved.
Show resolved Hide resolved
return self.tf.norm(state, ord=order)

def calculate_eigenvalues(self, matrix, k=6):
return self.tf.linalg.eigvalsh(matrix)

Expand Down
8 changes: 5 additions & 3 deletions tests/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Absolute testing tolerance for the cases of zero entanglement entropy
from qibo.config import PRECISION_TOL
from qibo.models import AdiabaticEvolution, Circuit
from qibo.quantum_info.random_ensembles import random_density_matrix, random_statevector


def test_abstract_callback_properties():
Expand Down Expand Up @@ -324,17 +325,18 @@ def test_state_callback(backend, density_matrix, copy):
backend.assert_allclose(statec[1], target_state1)


@pytest.mark.parametrize("seed", list(range(1, 5 + 1)))
@pytest.mark.parametrize("density_matrix", [False, True])
def test_norm(backend, density_matrix):
def test_norm(backend, density_matrix, seed):
norm = callbacks.Norm()
if density_matrix:
norm.nqubits = 1
state = np.random.random((2, 2)) + 1j * np.random.random((2, 2))
state = random_density_matrix(2**norm.nqubits, seed=seed, backend=backend)
target_norm = np.trace(state)
final_norm = norm.apply_density_matrix(backend, state)
else:
norm.nqubits = 2
state = np.random.random(4) + 1j * np.random.random(4)
state = random_statevector(2**norm.nqubits, seed=seed, backend=backend)
target_norm = np.sqrt((np.abs(state) ** 2).sum())
final_norm = norm.apply(backend, state)

Expand Down
Loading