API reference#

PauliWord#

class quantlop.PauliWord(coeff, string)#

Represent a weighted tensor product of single-qubit Pauli operators

\[P = c\,\bigotimes_i \sigma_i\]

where \(c\) is a complex coeff and each \(\sigma_i\) is a single-qubit Pauli operator applied to qubit \(i\). The leftmost character of string acts on the most-significant qubit in the computational-basis index. For example, "XII" represents \(X \otimes I \otimes I\) and maps \(|000\rangle\) to \(|100\rangle\).

Parameters:
coeffcomplex

Scalar complex coefficient of the Pauli word.

stringstr

Non-empty string of single-qubit Pauli operators. Each character should be one of "I", "X", "Y", or "Z". Its length determines the total number of qubits.

Attributes:
num_qubitsint

Number of qubits in the Pauli word.

coeffcomplex

Coefficient supplied at construction.

stringstr

Pauli string supplied at construction.


Hamiltonian#

class quantlop.Hamiltonian(pwords)#

Represent a qubit Hamiltonian expressed as a sum of PauliWord terms

\[H = \sum_k c_k P_k\]

This representation lets quantlop.evolve_higham() and quantlop.evolve_krylov() apply the unitary evolution generated by \(H\) to a state vector \(|\psi\rangle\) without materializing the Hamiltonian matrix.

Parameters:
pwordssequence of PauliWord

Non-empty collection of Pauli terms. All words must act on the same number of qubits. The evolution algorithm assumes their sum is Hermitian.

Attributes:
num_qubitsint

Number of qubits of the Hamiltonian.

num_termsint

Number of Pauli words in the sum.

Methods

from_pennylane(operator, num_qubits)

Construct a Hamiltonian from a PennyLane Pauli operator.

from_qiskit(operator)

Construct a Hamiltonian from a Qiskit SparsePauliOp.

matrix()

Return the dense matrix representation of the Hamiltonian.

sparse_matrix()

Return the Hamiltonian as a Compressed Sparse Row (CSR) matrix.

classmethod from_pennylane(operator, num_qubits)#

Construct a Hamiltonian from a PennyLane Pauli operator.

Parameters:
operatorpennylane.operation.Operator

PennyLane operator with a defined pauli_rep. Its wire labels must be integer indices in range(num_qubits).

num_qubitsint

Total number of qubits in the returned Hamiltonian. This can be larger than the number of wires used by the PennyLane operator.

Returns:
Hamiltonian

Native Hamiltonian with one term per entry in the PennyLane Pauli representation.

classmethod from_qiskit(operator)#

Construct a Hamiltonian from a Qiskit SparsePauliOp.

Parameters:
operatorqiskit.quantum_info.SparsePauliOp

Qiskit sparse Pauli operator. All labels are expected to have the same width.

Returns:
Hamiltonian

Native Hamiltonian with the input terms and coefficients.

matrix()#

Return the dense matrix representation of the Hamiltonian.

The matrix is assembled as a sum of tensor products. Characters are processed from left to right, so the first character in each Pauli word is the leftmost most-significant tensor factor.

Returns:
numpy.ndarray

Complex array representing the dense Hamiltonian matrix.

Notes

This method requires \(O(4^n)\) memory where \(n\) is the number of qubits. It is intended for inspection and validation on small systems only.

sparse_matrix()#

Return the Hamiltonian as a Compressed Sparse Row (CSR) matrix.

The matrix is assembled as a sum of sparse tensor products, preserving existing qubit-ordering and phase conventions.

Returns:
scipy.sparse.csr_matrix

Complex CSR matrix representing the Hamiltonian.


Evolution#

quantlop.evolve_higham(ham, psi, theta=1.0, rtol=1e-09, num_threads=None)#

Apply Hamiltonian evolution using the Higham exponential-action algorithm.

\[|\psi(\theta)\rangle = e^{-i \theta H}|\psi\rangle\]

The implementation uses adaptive scaling and a truncated Taylor series. It applies Pauli terms directly and never constructs the dense Hamiltonian or its exponential.

See Higham method for algorithm details.

Parameters:
hamHamiltonian

Pauli-sum quantlop Hamiltonian. The algorithm assumes that the operator is Hermitian.

psiarray_like

Nonzero one-dimensional input state vector.

thetafloat, optional

Finite real floating point parameter in the exponential. The default is 1.0.

rtolfloat, optional

Relative accuracy target used to select the approximation. Smaller values generally require more computation. The default is 1e-9.

num_threadsint, optional

OpenMP thread selection for Hamiltonian-vector products. If None the execution is serial, if “auto” it automatically selects the thread count reported by the operating system. The default is None.

Returns:
numpy.ndarray

Evolved dense state vector.

Examples

import numpy as np
import quantlop as ql

ham = ql.Hamiltonian([ql.PauliWord(1.0, "X")])
psi = np.array([1.0, 0.0])
out = ql.evolve_higham(ham, psi, theta=np.pi / 2)
quantlop.evolve_krylov(ham, psi, theta=1.0, rtol=1e-09, num_threads=None)#

Apply Hamiltonian evolution using the Lanczos-Krylov subspace algorithm.

\[|\psi(\theta)\rangle = e^{-i \theta H}|\psi\rangle\]

The implementation projects the Hamiltonian onto a Lanczos basis, evolves within that Krylov subspace, and reconstructs the dense state vector.

See Krylov method for algorithm details.

Parameters:
hamHamiltonian

Pauli-sum quantlop Hamiltonian. The algorithm assumes that the operator is Hermitian.

psiarray_like

Nonzero one-dimensional input state vector.

thetafloat, optional

Finite real floating point parameter in the exponential. The default is 1.0.

rtolfloat, optional

Relative accuracy target used to select the approximation. Smaller values generally require more computation. The default is 1e-9.

num_threadsint, optional

OpenMP thread selection for Hamiltonian-vector products. If None the execution is serial, if "auto" it automatically selects the thread count reported by the operating system. The default is None.

Returns:
numpy.ndarray

Evolved dense state vector.

Examples

import numpy as np
import quantlop as ql

ham = ql.Hamiltonian([ql.PauliWord(1.0, "X")])
psi = np.array([1.0, 0.0])
out = ql.evolve_krylov(ham, psi, theta=np.pi / 2)