qarp.factories

Block factories. Public depth: flat. Submodules are private.

class qarp.factories.BlockFactory[source]

Bases: ABC

Abstract base class for block factories.

abstractmethod create_blocks(*args, **kwargs) List[Block][source]

Create blocks from input data.

Returns:

List of block objects

class qarp.factories.PauliBlockFactory[source]

Bases: BlockFactory

A factory class for creating PauliBlock objects from various Hamiltonian representations.

This factory supports creation from QubitOperator and FermionOperator objects, with configurable options for coefficients, basis changes, and measurements.

create_blocks(hamiltonian: QubitOperator | FermionOperator, n_qubits: int | None = None, include_coefficients: bool = True, change_basis: bool = False, measure: bool = False, mapping: Mapping | None = None) List[PauliBlock][source]

Create blocks from a Hamiltonian operator.

Automatically dispatches to the appropriate method based on input type.

Parameters:
  • hamiltonian – OpenFermion QubitOperator or FermionOperator representing the Hamiltonian

  • n_qubits – Total number of qubits/spin orbitals. If None, inferred from the operator

  • include_coefficients – Whether to include the coefficients in the blocks

  • change_basis – Whether to change basis for the Pauli blocks

  • measure – Whether to measure the Pauli blocks

  • mapping – Fermion-to-qubit mapping (only used for FermionOperator)

Returns:

List of PauliBlock objects

Raises:

TypeError – If hamiltonian is not a QubitOperator or FermionOperator

static from_fermion_operator(operator: FermionOperator, spin_orbitals: int | None = None, mapping: Mapping | None = None, include_coefficients: bool = True, change_basis: bool = False, measure: bool = False) List[PauliBlock][source]

Create PauliBlock objects from a FermionOperator.

Requires a fermion-to-qubit mapping (e.g., Jordan-Wigner transform).

Parameters:
  • operator – OpenFermion FermionOperator

  • spin_orbitals – Number of spin orbitals. If None, inferred from the operator

  • mapping – Fermion-to-qubit mapping. Defaults to Jordan-Wigner if None

  • include_coefficients – Whether to include the coefficients in the blocks

  • change_basis – Whether to change basis for the Pauli blocks

  • measure – Whether to measure the Pauli blocks

Returns:

List of PauliBlock objects

static from_qubit_operator(operator: QubitOperator, n_qubits: int | None = None, include_coefficients: bool = True, change_basis: bool = False, measure: bool = False) List[PauliBlock][source]

Create PauliBlock objects from a QubitOperator.

Parameters:
  • operator – OpenFermion QubitOperator representing the Hamiltonian

  • n_qubits – Total number of qubits. If None, inferred from the operator

  • include_coefficients – Whether to include the coefficients in the blocks

  • change_basis – Whether to change basis for the Pauli blocks

  • measure – Whether to measure the Pauli blocks

Returns:

List of PauliBlock objects, one for each term in the Hamiltonian

static get_coefficients(blocks: List[PauliBlock]) List[complex][source]

Extract coefficients from a list of PauliBlocks.

Parameters:

blocks – List of PauliBlock objects

Returns:

List of complex coefficients

static get_pauli_strings(blocks: List[PauliBlock]) List[Dict[int, str]][source]

Extract Pauli strings from a list of PauliBlocks.

Parameters:

blocks – List of PauliBlock objects

Returns:

List of dictionaries mapping qubit indices to Pauli operators