Modules¶
Interpret¶
- pauli.interpreter.interpret(text)¶
Simplifies a Pauli expression from a string. Uses a custom Lexer, Parser, and Interpreter. Backend built using pauli.PauliAlgebra
Example
>>> from pauli import interpret # or from pauli.interpreter import interpret >>> interpret("2 * (XIX + IXI)") >>> 2XIX + 2IXI
- Parameters:
text (str) – Pauli expression to simplify.
- Returns:
The reduced Pauli expression.
- Return type:
str
PauliAlgebra¶
- class pauli.paulialgebra.PauliAlgebra(term: str | PauliAlgebraElement | Sequence[PauliAlgebraElement] | Sequence[str])¶
Supports a Pauli Algebra. A single term in the algebra, or a sum of terms in the algebra. Can be initialized using PauliAlgebraElements or strings which initilize PauliAlgebraElements.
- Parameters:
term (Union[str, PauliAlgebraElement, Sequence[PauliAlgebraElement], Sequence[str]]) –
Example
>>> p = PauliAlgebra(["XIX", "XIX", "IXI", "IXI"]) >>> print(p) >>> 2XIX + 2IXI
- __add__(other: PauliAlgebraElement | PauliAlgebra | int | float | complex) PauliAlgebra ¶
Adds two PauliAlgebras together. Automatically simplifies the result. Supports scalar multiplication by adding a sI*self.qubits term.
Example
>>> p = PauliAlgebra(["XIX", "IXI"]) >>> print(p + p) >>> 2XIX + 2IXI
- __mul__(other: PauliAlgebraElement | PauliAlgebra | int | float | complex) PauliAlgebra ¶
If other is a scalar, multiplies each term in the PauliAlgebra by the scalar. Otherwise, multiplies two PauliAlgebras together via elementwise multiplication of PauliAlgebraElements. Automatically simplifies the result.
Example
>>> p = PauliAlgebra(["XIX", "IXI"]) >>> print(p * p) >>> 2XXX + 2III
- simplify()¶
Simplifies the PauliAlgebra by combining terms with the same Pauli string and adding their phase. Returns a new PauliAlgebra
Example
>>> p = PauliAlgebra(["XIX", "XIX", "IXI", "IXI"]) >>> p.simplify() >>> 2XIX + 2IXI
- pauli.paulialgebra.add_pauli_algebra(pr1, pr2)¶
Adds two PauliAlgebras together.
- pauli.paulialgebra.multiply_pauli_algebra(pr1, pr2)¶
Multiplies two PauliAlgebras together.
- pauli.paulialgebra.multiply_pauli_algebra_by_scalar(z, pr)¶
Multiplies a PauliAlgebra by a scalar.
- pauli.paulialgebra.pauli_algebra_to_string(pr)¶
Converts a PauliAlgebra to a string.
- pauli.paulialgebra.simplify(pr)¶
Simplifies a PauliAlgebra.
Decompose¶
- pauli.decompose.decompToPauli(x: List[List]) List[float] ¶
Decomposes a (2,2) matrix into a composition of Pauli matrices.
x = \(r_0I + r_1X + r_2Y + r_3Z\)
\(\mathbf{r} = [(x_{0,0} + x_{1,1})/2, (x_{0,1} + x_{1,0})/2, -1j * (x_{1,0} - x_{0,1})/2, (x_{0,0} - x_{1,1})/2]\)
- Parameters:
List[List] – x
- Returns:
\(\mathbf{r} = [r_0, r_1, r_2, r_3]\) coefficients of the decomposition
- Return type:
List(float)
Generator¶
- pauli.generator.generateFromPauliDecomp(r: List[float]) ndarray ¶
Generate a matrix by composing Pauli matrices with coefficients r.
- Parameters:
r (List[Numeric]) – Pauli decomposition coefficients.
- Returns:
x = \(r_0 I + r_1 X + r_2 Y + r_3 Z\)
- pauli.generator.generateHermitian(n: int, b: float) ndarray ¶
Generates a random (n,n) Hermitian matrix with bounded elements \(|Re(x_{ij})| < b\) and \(|Im(x_{ij})| < b\).
- Parameters:
n (int) – dimension of the matrix.
b (int) – bound real and imaginary components of elements.
- Returns:
An (n,n) Hermitian matrix with bounded elements.
- Return type:
np.ndarry
Pauli Group Elements¶
- class pauli.pauli.PauliAlgebraElement(pauli: Sequence[int] | Sequence[PauliElement] | str, phase: float | int | complex | None = 1.0)¶
Represents an n-qubit symbolic Pauli group element with a phase. Supports multiplication and addition with other PauliAlgebraElements.
Example
>>> X = PauliElement(PauliSymbol.X) >>> Y = PauliElement(PauliSymbol.Y) >>> Z = PauliElement(PauliSymbol.Z) >>> pauli1 = PauliAlgebraElement([X, Y, Z], phase=1.0) >>> print(pauli1) >>> XYZ >>> pauli2 = PauliAlgebraElement("XYZ", phase=1.0) >>> print(pauli2) >>> XYZ >>> pauli3 = pauli1 * pauli2 >>> print(pauli3) >>> III
- Parameters:
pauli – Union[Sequence[int], Sequence[PauliElement], str] Pauli elements for each qubit or a string of Pauli symbols to be converted to Pauli elements.
phase – Optional[float, int, complex] Phase of the Pauli group element.
- eq_group(other)¶
- class pauli.pauli.PauliElement(symbol: PauliSymbol, phase=1.0)¶
PauliElement represents a single qubit Pauli group element with a phase.
- Parameters:
symbol – PauliSymbol
phase – Optional[float, int, complex]
- class pauli.pauli.PauliSymbol(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)¶
- I = 3¶
- X = 0¶
- Y = 1¶
- Z = 2¶
- pauli.pauli.multiply_pauli(pauli1: PauliElement | PauliAlgebraElement, pauli2: PauliElement | PauliAlgebraElement) PauliElement | PauliAlgebraElement ¶
Wrapper around element multiplication.
- pauli.pauli.multiply_phase(phi: int | complex | float, pauli: PauliElement | PauliAlgebraElement) PauliElement | PauliAlgebraElement ¶
Wrapper around phase-element multiplication.
- pauli.pauli.pauli_to_string(pauli: PauliElement | PauliAlgebraElement) str ¶
Convert PauliElement to string.
- pauli.pauli.phase_to_string(phase: int | float | complex) str ¶
Convert phase value to string for printing
- Parameters:
phase (Union[int, float, complex]) – Pauli phase
- Returns:
valid string for printing phase
- Return type:
str
- pauli.pauli.simplify_val(val: int | float | complex) str ¶
Convert float for string representation