Skip to content

Commit

Permalink
function
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Feb 9, 2024
1 parent 45e4e22 commit 93ef112
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/qibo/quantum_info/clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def generators(self, return_array: bool = False):
self.symplectic_matrix, return_array
)

def stabilizers(self, symplectic: bool = False, return_array: bool = False):
def stabilizers(
self, symplectic: bool = False, return_array: bool = False, cores: int = 1
):
"""Extracts the stabilizers of the state.
Args:
Expand All @@ -150,6 +152,8 @@ def stabilizers(self, symplectic: bool = False, return_array: bool = False):
return_array (bool, optional): To be used when ``symplectic = False``.
If ``True`` returns the stabilizers as ``ndarray``.
If ``False``, returns stabilizers as strings. Defaults to ``False``.
cores (int, optional): number of processes to be used to calculate all stabilizers.
To be used ``symplectic=False`` and ``return_array=False``. Defaults to :math:`1`.
Returns:
(ndarray or list): Stabilizers of the state.
Expand All @@ -160,11 +164,14 @@ def stabilizers(self, symplectic: bool = False, return_array: bool = False):
return self._construct_operators(
generators[self.nqubits :],
phases[self.nqubits :],
cores=cores,
)

return self.symplectic_matrix[self.nqubits : -1, :]

def destabilizers(self, symplectic: bool = False, return_array: bool = False):
def destabilizers(
self, symplectic: bool = False, return_array: bool = False, cores: int = 1
):
"""Extracts the destabilizers of the state.
Args:
Expand All @@ -175,15 +182,16 @@ def destabilizers(self, symplectic: bool = False, return_array: bool = False):
If ``True`` returns the destabilizers as ``ndarray``.
If ``False``, their representation as strings is returned.
Defaults to ``False``.
cores (int, optional): number of processes to be used to calculate all destabilizers.
To be used ``symplectic=False`` and ``return_array=False``. Defaults to :math:`1`.
Returns:
(ndarray or list): Destabilizers of the state.
"""
if not symplectic:
generators, phases = self.generators(return_array)

return self._construct_operators(
generators[: self.nqubits], phases[: self.nqubits]
generators[: self.nqubits], phases[: self.nqubits], cores=cores
)

return self.symplectic_matrix[: self.nqubits, :]
Expand Down Expand Up @@ -392,13 +400,14 @@ def copy(self, deep: bool = False):
symplectic_matrix, self.nqubits, self.measurements, self.nshots, self.engine
)

def _construct_operators(self, generators: list, phases: list):
def _construct_operators(self, generators: list, phases: list, cores: int):
"""Helper function to construct all the operators from their generators.
Args:
generators (list or ndarray): generators.
phases (list or ndarray): phases of the generators.
cores (int, optional): number of processes to be used to construct the operators.
To be used when operators are strings. Defaults to :math:`1`.
Returns:
(list): All operators generated by the generators of the stabilizer group.
"""
Expand Down Expand Up @@ -426,4 +435,14 @@ def _construct_operators(self, generators: list, phases: list):

operators = [(g, identity) for g in operators]

if cores > 1:
from multiprocessing import Pool # pylint: disable=E0401

with Pool(cores) as pool:
results = pool.map(_string_product, product(*operators))
pool.close()
pool.join()

return results

return [_string_product(ops) for ops in product(*operators)]

0 comments on commit 93ef112

Please sign in to comment.