SymbolicTerms

class zixy.qubit.pauli.SymbolicTerms(qubits: int | Qubits = 0, n: int = 0)[source]

Bases: Terms[Expr]

A collection of terms consisting of Pauli strings and symbolic coefficients.

An array-like container of qubit-based terms consisting of Pauli strings and Expr coefficients that may be an owning instance referencing a TermData instance, or a view on a slice of the elements in another collection.

__eq__(other: object) bool

Return whether self and other are equal.

__getitem__(indexer: int | slice) Term[ImplT, SpecT, CoeffT] | Terms[ImplT, SpecT, CoeffT]
Overloads:
  • self, indexer (int) → Term[ImplT, SpecT, CoeffT]

  • self, indexer (builtins.slice) → Terms[ImplT, SpecT, CoeffT]

Get the element or elements selected by indexer.

Parameters:

indexer – Index or slice selecting the element(s) to return.

Returns:

Element or slice selected by indexer.

__imul__(scalar: CoeffT) Self

Multiply self by scalar in-place.

__init__(qubits: int | Qubits = 0, n: int = 0)

Initialize the term array.

Parameters:
  • qubits – The qubit register or qubit count.

  • n – The number of items to initialize the array with.

__iter__() Iterator[T]

Iterate over the elements of self.

__len__() int

Get the number of elements in self.

__repr__() str

Return a string representation of self.

__setitem__(indexer: int | slice, source: Cmpnt[ImplT, SpecT] | SpecT | tuple[SpecT | Cmpnt[ImplT, SpecT] | None, CoeffT | None] | None | Term[ImplT, SpecT, CoeffT] | Terms[ImplT, SpecT, CoeffT]) None
Overloads:
  • self, indexer (int), source (TermSpecT[ImplT, SpecT, CoeffT] | Term[ImplT, SpecT, CoeffT]) → None

  • self, indexer (builtins.slice), source (Terms[ImplT, SpecT, CoeffT]) → None

Set the term at indexer in self to source.

Parameters:
  • indexer – Index of the string or slice of strings within self to assign.

  • source – Value specifying the term or a view of many components to assign at indexer.

append(source: Cmpnt[ImplT, SpecT] | SpecT | tuple[SpecT | Cmpnt[ImplT, SpecT] | None, CoeffT | None] | None | Term[ImplT, SpecT, CoeffT] = None) Self

Append source to the end of self.

Parameters:

source – Value to append.

Note

This method operates in-place.

append_iterable(source: Iterable[Cmpnt[ImplT, SpecT] | SpecT | tuple[SpecT | Cmpnt[ImplT, SpecT] | None, CoeffT | None] | None | Term[ImplT, SpecT, CoeffT]] = ()) Self

Append the elements of source to the end of self.

Parameters:

source – Other iterable whose terms to append to self.

Note

This method operates in-place.

append_n(n: int, source: Cmpnt[ImplT, SpecT] | SpecT | tuple[SpecT | Cmpnt[ImplT, SpecT] | None, CoeffT | None] | None | Term[ImplT, SpecT, CoeffT] = None) Self

Append source to the end of self n times.

Parameters:
  • n – Number of times to repeatedly append source.

  • source – Value to append.

Note

This method operates in-place.

as_view() Self

Return a view of self.

Returns:

If self is owning, a new view on the same underlying data, otherwise self.

canonicalize(mode_order: Sequence[tuple[int, SymplecticPart]], to_solve: Sequence[int], additional_reduces: Sequence[int]) Sequence[tuple[int, int]]

In place tableau canonicalization.

Canonicalizes a given subset with respect to a given ordering of the binary entries in the symplectic form.

Parameters:
  • mode_order – The order of binary entries to try reducing to at most one non-zero entry.

  • to_solve – The subset of the components to canonicalize over (e.g. if some partial canonicalization has already been done, skip those components).

  • additional_reduces – Components outside of to_solve to include in the reduction step (e.g. if some partial canonicalization has already been done, reduce the components that already have leading entries).

Returns:

The sequence of imul operations as pairs (lhs_written, rhs_read).

canonicalize_all() Sequence[tuple[int, int]]

In place tableau canonicalization.

Canonicalizes the entire Terms with respect to solving X parts first (in qubit order), then Z parts.

Returns:

The sequence of imul operations as pairs (lhs_written, rhs_read).

clone() Self

Return a deep copy of self.

property cmpnt_type: type[Cmpnt[ImplT, SpecT]]

Get the component type of self.

property cmpnts: Cmpnts[ImplT, SpecT]

Get the components of self.

property cmpnts_type: type[Cmpnts[ImplT, SpecT]]

Get the component container type of self.

property coeff_type: type[CoeffT]

Get the coefficient type of self.

property coeffs: SymbolicCoeffs

Get the coefficients of self.

property coeffs_type: type[Coeffs[CoeffT]]

Get the coefficient container type of self.

conj_clifford_list(gates: GateList) None

Conjugate self by a list of Clifford gates.

Parameters:

gates – The Clifford gates to conjugate by.

Note

This method operates in-place.

See also

conj_clifford_list()

count(value) integer -- return number of occurrences of value
diff(variable: Symbol | str) SymbolicTerms[source]

Differentiate partially with respect to variable out of place.

Parameters:

variable – Symbol or name of symbol by which to differentiate the viewed symbolic expressions.

Returns:

A new contiguously stored instance with the differentiation applied.

filter_map(f: Callable[[Term[ImplT, SpecT, CoeffT]], bool]) Self

Eagerly evaluate a filter-map operation over the terms of self.

Parameters:

f – Function which may mutate copies of the terms of self, returning True if those mutated copies are to be included in the generator. The function signature should take a single Term instance as an argument, and return a boolean.

Returns:

New instance containing the occurrences of selected (and possibly mutated) components of self according to f.

property free_symbols: set[Symbol]

Get the set of free (unsubstituted) symbols in self.

Returns:

Union of the sets of free symbols across all coefficients in self.

classmethod from_iterable(source: Iterable[Cmpnt[ImplT, SpecT] | SpecT | tuple[SpecT | Cmpnt[ImplT, SpecT] | None, CoeffT | None] | None | Term[ImplT, SpecT, CoeffT]], *args: Any, **kwargs: Any) Self

Create a new instance of cls from an iterable.

Parameters:
  • source – Iterable returning specifiers of all the terms to be appended.

  • args – Positional arguments to forward to the constructor of cls.

  • kwargs – Keyword arguments to forward to the constructor of cls.

Returns:

New instance of cls containing the terms specified by source.

classmethod from_size(n: int) Self

Create a new instance of cls with the given size.

Parameters:

n – The size of the new instance.

Returns:

An instance of cls with the given size.

classmethod from_str(source: str, qubits: int | Qubits | None = None) Self

Create a new instance of cls by parsing an input string.

Parameters:
  • source – Input string to parse.

  • qubits – Space of qubits or a number of qubits. If None, infer from the max qubit index in the input string.

Returns:

A new instance containing the Pauli strings and coefficients in the source.

classmethod from_view(source: Self) Self

Create a new instance of cls from a view.

Parameters:

source – View to clone into the new instance.

Returns:

An owning clone of source.

idiff(variable: Symbol | str) None[source]

Differentiate partially with respect to variable in-place.

Parameters:

variable – Symbol or name of symbol by which to differentiate the viewed symbolic expressions.

Note

This method operates in-place.

into(t: type[OutT]) OutT

Clone self into a new related container of type t.

Parameters:

t – Type of the new container to create.

Returns:

A new instance of t containing the same data as self.

is_owning() bool

Check if self is owning (i.e. not a view).

isubs(values: dict[Symbol | str, int | float | complex | Expr]) None[source]

Apply a partial substitution of the symbols in-place.

Parameters:

values – Map from a symbol or symbol name to its new expression or numeric value.

Note

This method operates in-place.

iter_filter_map(f: Callable[[Term[ImplT, SpecT, CoeffT]], bool]) Iterator[Term[ImplT, SpecT, CoeffT]]

Lazily evaluate a filter-map operation over the components of self.

Parameters:

f – Function which may mutate copies of the terms of self, returning True if those mutated copies are to be included in the generator. The function signature should take a single Term instance as an argument, and return a boolean.

Returns:

Iterator over the selected (and possibly mutated) terms of self according to f.

lexicographic_sort(ascending: bool = False) None

Lexicographically sort the terms in-place.

Parameters:

ascending – Whether to sort in ascending order.

Note

This method operates in-place.

map_index(i: int) int

Map an index in self to an index in the underlying data.

Parameters:

i – Index in self.

Returns:

Corresponding index in the underlying data.

new_clear_term() Term[ImplT, SpecT, CoeffT]

Factory to make a new term with a zero component and a unit coefficient.

Returns:

A new owing term with the component and coefficient type of self.

property qubits: Qubits

Get the qubits corresponding to self.

relabel(qubits: Qubits) Self

Relabel the strings using a new register of qubits.

Parameters:

qubits – The qubit register or qubit count.

Returns:

self for chaining.

Note

This method operates in-place.

See also

relabel()

relabelled(qubits: Qubits) Self

Relabel the strings using a new register of qubits.

Parameters:

qubits – The qubit register or qubit count.

Returns:

The resulting value.

See also

relabelled()

reordered(inds: Sequence[int]) Self

Get a new instance with the elements of self in a new order.

Parameters:

inds – Sequence of indices defining the new order. Should be a permutation of tuple(range(len(self))).

Returns:

A new instance with the reordered elements.

resize(n: int) Self

Resize the underlying container.

Parameters:

n – The new size of the container.

Note

This method operates in-place.

Raises:

ValueError – If the container is a view.

scale(scalar: CoeffT) None

Scale all elements by a given factor.

Parameters:

scalar – Scalar factor by which to scale all elements.

Note

This method operates in-place.

standardize(n_qubit: int) Self

Standardize the string labels according to a given number of qubits.

Parameters:

n_qubit – The number of qubits.

Returns:

self for chaining.

Note

This method operates in-place.

See also

standardize()

standardized(n_qubit: int) Self

Standardize the string labels according to a given number of qubits.

Parameters:

n_qubit – The number of qubits.

Returns:

The resulting value.

See also

standardized()

property strings: Strings

Get the components of self.

subs(values: dict[Symbol | str, int | float | complex | Expr]) SymbolicTerms[source]

Apply a partial substitution of the symbols out of place.

Parameters:

values – Map from a symbol or symbol name to its new expression or numeric value.

Returns:

A new contiguously stored instance with the substitution applied.

term_type

alias of SymbolicTerm

to_dataframe() DataFrame

Convert self to a DataFrame.

try_to_complex() ComplexTerms[source]

Try to evaluate self as terms containing a vector of complex coefficients.

Returns:

An instance of ComplexTerms with the evaluated coefficients.

Raises:

TypeError – A coefficient is not representable as complex or there are free symbols.

try_to_real() RealTerms[source]

Try to evaluate self as terms containing a vector of real coefficients.

Returns:

An instance of RealTerms with the evaluated coefficients.

Raises:

TypeError – A coefficient is not representable as real or there are free symbols.