This is a Python version of PauliStrings.jl. The Julia version is more mature, we recommend using it if possible.
Install with pip:
pip install paulistrings
Install the dev version from github:
pip install git+https://github.com/nicolasloizeau/paulistrings.py.git
https://nicolasloizeau.github.io/PauliStrings.py/
Import the library and initialize an operator of 4 qubits:
import paulistrings as ps
H = ps.Operator(4)
Add Pauli strings to the operator:
H += "XYZ1"
H += "1YZY"
print(H)
(1.0 + 0.0j) XYZ1
(1.0 + 0.0j) 1YZY
Add a Pauli string with a coefficient:
# Coefficients can be complex
H = ps.Operator(4)
H += -1.2, "XXXZ"
(1.0 + 0.0j) XYZ1
(1.0 + 0.0j) 1YZY
(-1.2 + 0.0j) XXXZ
Add a 2-qubit string coupling qubits i and j with X and Y:
i, j = 0, 2 # example qubit indices
H += 2.0, "X", i, "Y", j # with coefficient=2
H += "X", i, "Y", j # with coefficient=1
Add a 1-qubit string:
i = 1 # example qubit index
H += 2.0, "Z", i # with coefficient=2
H += "Z", i # with coefficient=1
Operators support standard arithmetic operations with other operators and numbers:
# Multiplication
H3 = H1 * H2
# Addition and subtraction
H3 = H1 + H2
H3 = H1 - H2
# Scalar operations
H3 = H1 + 2 # Adding a scalar (equivalent to adding identity times scalar)
H = 5 * H # Multiply operator by a scalar
Common operations on operators:
# Trace of an operator
trace = ps.trace(H)
# Frobenius norm
norm = ps.opnorm(H)
# Number of terms in the operator
num_terms = len(H) # or len(H.coeffs)
# Commutator [H1, H2] = H1*H2 - H2*H1
# This is more efficient than computing H1*H2 - H2*H1 directly
comm = ps.commutator(H1, H2)
Contributions are welcome! Feel free to open a pull request if you'd like to contribute code or documentation. For bugs and feature requests, please [open an issue](https://github.com/nicolasloizeau/PauliStrings.py/issues). For questions, you can either contact nicolas.loizeau@nbi.ku.dk or start a new [discussion](https://github.com/nicolasloizeau/PauliStrings.py/discussions) in the repository.
@Article{Loizeau2025,
title={{Quantum many-body simulations with PauliStrings.jl}},
author={Nicolas Loizeau and J. Clayton Peacock and Dries Sels},
journal={SciPost Phys. Codebases},
pages={54},
year={2025},
publisher={SciPost},
doi={10.21468/SciPostPhysCodeb.54},
url={https://scipost.org/10.21468/SciPostPhysCodeb.54},
}
@Article{Loizeau2025,
title={{Codebase release 1.5 for PauliStrings.jl}},
author={Nicolas Loizeau and J. Clayton Peacock and Dries Sels},
journal={SciPost Phys. Codebases},
pages={54-r1.5},
year={2025},
publisher={SciPost},
doi={10.21468/SciPostPhysCodeb.54-r1.5},
url={https://scipost.org/10.21468/SciPostPhysCodeb.54-r1.5},
}