Skip to content

loqs.backends.reps.conversion

Attributes

Name Type Value Doc
T TypeVar TypeVar('T', bound='OperationRep') TypeVar for the convert() function's generic return type.
STANDARD_GATE_UNITARIES dict[str, NDArray] {'I': _I, 'X': _X, 'Y': _Y, 'Z': _Z, 'H': _H, 'S': _S, 'S_DAG': _S_DAG, 'CX': _CX, 'CZ': _CZ} Well-known single/two-qubit gate unitaries, keyed by generic name.

Reused internally to build the N-qubit Pauli basis (X/Y/Z/I) that this module's own Unitary<->PTM/Kraus<->PTM/Choi-decomposition math is built on, and exposed publicly so callers needing a small, fixed set of standard gates (e.g. example codepacks constructing an ideal noise model) have one source of truth instead of hand-copying these matrices again.

Functions

convert

convert(source: object, target: type[T] | Sequence[type[T]], qubits: str | int | Sequence[str | int] | None = None, **kwargs) -> T

Convert source (a raw payload or an OperationRep) to target.

  1. If source already is (or is an instance of) target, return it.
  2. Else try constructing each entry of target directly from source, in order; return the first that succeeds.
  3. Else resolve source's unique concrete starting class and hop through _CONVERTERS to the closest target. A raw payload matching more than one concrete class is never guessed at here -- construct the intended representation explicitly instead.

Parameters:

Name Type Description Default
source object

A raw, unwrapped payload, or an already-constructed OperationRep instance.

required
target type[T] | Sequence[type[T]]

The desired class, or a priority-ordered sequence of candidates.

required
qubits str | int | Sequence[str | int] | None

Qubit label(s) this operation acts upon, if known, else None.

None
**kwargs

Forwarded to whichever candidate class's constructor accepts them (e.g. KrausGateRep's tp_check_abstol), and to each pairwise converter used along a hop path that accepts them (e.g. _ptm_to_unitary/_kraus_to_unitary's unitarity_check_abstol).

{}

Returns:

Type Description
T

The converted (or passed-through) representation.

Raises:

Type Description
RepConstructionError

If source cannot be resolved to a starting class, or no conversion path exists from that starting class to any entry in target.


reachable_reps cached

reachable_reps(sources: tuple[type[OperationRep], ...]) -> tuple[type[OperationRep], ...]

sources, then every other concrete rep class convert can reach from one of them, in _ALL_CONCRETE_REPS order.