From 93ef112602f2d360a24070cdd981536f134668cc Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Fri, 9 Feb 2024 12:58:55 +0400 Subject: [PATCH] function --- src/qibo/quantum_info/clifford.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/qibo/quantum_info/clifford.py b/src/qibo/quantum_info/clifford.py index 9bc52c66b1..a275924bc1 100644 --- a/src/qibo/quantum_info/clifford.py +++ b/src/qibo/quantum_info/clifford.py @@ -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: @@ -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. @@ -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: @@ -175,7 +182,8 @@ 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. """ @@ -183,7 +191,7 @@ def destabilizers(self, symplectic: bool = False, return_array: bool = False): 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, :] @@ -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. """ @@ -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)]