Utilities#
Utility functions.
- ferrmion.utils.find_pauli_weight(symplectic_hamiltonian: ndarray[tuple[int, ...], dtype[bool]]) floating[source]#
Find the average Pauli weight of a symplectic hamiltonian.
- Parameters:
symplectic_hamiltonian (NDArray) – The symplectic Hamiltonian.
- Returns:
The average Pauli weight.
- Return type:
float
Example
>>> import numpy as np >>> from ferrmion.utils import find_pauli_weight >>> arr = np.eye((2, 4), dtype=bool) >>> find_pauli_weight(arr) 1.0
- ferrmion.utils.icount_to_sign(icount: int) complex64[source]#
Convert a power of i to a complex value.
- Parameters:
icount (int) – The power of i.
- Returns:
The complex value.
- Return type:
np.complex64
Example
>>> from ferrmion.utils import icount_to_sign >>> icount_to_sign(1) 1j >>> icount_to_sign(2) -1
- ferrmion.utils.pauli_to_symplectic(pauli: str, ipower: int = 0) tuple[ndarray[tuple[int, ...], dtype[bool]], int][source]#
Convert a Pauli operator to symplectic form.
- Parameters:
pauli (str) – The Pauli operator string.
ipower (NDArray[np.uint]) – power of i coefficient
- Returns:
The imaginary cofactor and symplectic matrix.
- Return type:
tuple[int, NDArray[np.uint8, np.uint8]]
Example
>>> from ferrmion.utils import pauli_to_symplectic >>> symp, ipower = pauli_to_symplectic('XIZY')
- ferrmion.utils.symplectic_hash(symp: ndarray[tuple[int, ...], dtype[bool]]) bytes[source]#
Convert a symplectic vector into a hashable form.
- Parameters:
symp (NDArray[bool]) – The symplectic vector.
- Returns:
The hashed form of the symplectic vector.
- Return type:
bytes
Example
>>> import numpy as np >>> from ferrmion.utils import symplectic_hash >>> symp = np.array([True, False, True, False], dtype=bool) >>> h = symplectic_hash(symp) >>> isinstance(h, bytes) True
- ferrmion.utils.symplectic_to_pauli(symplectic: ndarray[tuple[int, ...], dtype[bool]], ipower: int = 0) tuple[str, int][source]#
Convert a symplectic vector into a Pauli String.
- Parameters:
symplectic (NDArray[np.uint8]) – symplectic vector [X terms, Y terms]
ipower (NDArray[np.uint]) – power of i coefficient
- Returns:
The Pauli string and imaginary cofactor.
- Return type:
tuple[str, int]
- NOTE: symplectic XZ does represent XZ and not Y
So Y=-iXZ needs an imaginary cofactor
Example
>>> import numpy as np >>> from ferrmion.utils import symplectic_to_pauli >>> arr = np.array([1, 0, 0, 1], dtype=bool) >>> pauli, ipower = symplectic_to_pauli(arr) >>> isinstance(pauli, str) True
- ferrmion.utils.symplectic_to_sparse(symplectic: ndarray[tuple[int, ...], dtype[bool]], ipower: int = 0) tuple[str, ndarray[tuple[int, ...], dtype[int]], complex64][source]#
Convert a symplectic vector into a Pauli String (sparse form).
- Parameters:
symplectic (NDArray[np.uint8]) – symplectic vector [X terms, Y terms]
ipower (NDArray[np.uint]) – power of i coefficient
- Returns:
The Pauli string, indices of non-identity terms and imaginary coefficient.
- Return type:
tuple[str, NDArray[int]]
- NOTE: symplectic XZ does represent XZ and not Y
So Y=-iXZ needs an imaginary cofactor
Example
>>> import numpy as np >>> from ferrmion.utils import symplectic_to_sparse >>> arr = np.array([1, 0, 0, 1], dtype=bool) >>> pauli, idx, coeff = symplectic_to_sparse(arr) >>> isinstance(pauli, str) True >>> isinstance(idx, np.ndarray) True
- ferrmion.utils.symplectic_unhash(symp: bytes, length: int) ndarray[tuple[int, ...], dtype[bool]][source]#
Convert a hashed symplectic vector back to its original form.
- Parameters:
symp (bytes) – The hashed form of the symplectic vector.
length (int) – The length of the original symplectic vector.
- Returns:
The original symplectic vector.
- Return type:
NDArray[bool]
Example
>>> import numpy as np >>> from ferrmion.utils import symplectic_hash, symplectic_unhash >>> arr = np.array([True, False, True, False], dtype=bool) >>> h = symplectic_hash(arr) >>> arr2 = symplectic_unhash(h, 4) >>> np.all(arr == arr2) True
- ferrmion.utils.two_operator_product(creation: tuple[bool, bool], left, right) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]#
Calculate the product of two operators in symplectic form.
- Parameters:
creation (tuple[bool, bool]) – A tuple of two booleans indicating if the operators are creation operators.
left (NDArray) – The left operator in symplectic form.
right (NDArray) – The right operator in symplectic form.
- Returns:
The product of the two operators in symplectic form.
- Return type:
NDArray
Example
>>> left = np.array([[1, 0], [0, 1]]) >>> right = np.array([[0, 1], [1, 0]]) >>> creation = (True, False) >>> two_operator_product(creation, left, right) array([ [0, 0], [1, 0] [0, 1], [0, 0]])