String

class zixy.qubit.state.String(qubits: int | Qubits | None = None, source: SpecT | None = None)[source]

Bases: String[QubitStateArray, None | Sequence[bool] | set[int] | str, bool]

A state string.

A single qubit-based state string that may be an owning instance referencing a single element in a Rust-bound data object, or a view on an element in another collection.

__eq__(other: object) bool

Return whether self and other are equal.

__getitem__(i: int) bool[source]

Return the bit value of the string at index i.

__init__(qubits: int | Qubits | None = None, source: SpecT | None = None)[source]

Initialize the string.

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

  • source – The string specifier to use for default qubits and initial value.

__mul__(rhs: CoeffT | Cmpnt[ImplT, SpecT]) Term[ImplT, SpecT, CoeffT] | Term[ImplT, SpecT, OtherCoeffT]
Overloads:
  • self, rhs (Cmpnt[ImplT, SpecT]) → Term[ImplT, SpecT, OtherCoeffT]

  • self, rhs (CoeffT) → Term[ImplT, SpecT, CoeffT]

Return the product of self and rhs.

__repr__() str[source]

Return a string representation of self.

__rmul__(lhs: CoeffT) Term[ImplT, SpecT, CoeffT]

Return the product of lhs and self.

__setitem__(i: int, bit: bool | int) None[source]

Set the bit value of the string at index i.

aliases(other: Cmpnt[ImplT, SpecT]) bool

Determine whether self is a view of the same component as other.

clear() None

Set the value of the component to zero.

Note

This method operates in-place.

clone() Self

Return a deep copy of self.

count(elem: ElemT) int[source]

Return the number of occurrences of elem in the string.

Parameters:

elem – The element to count.

Returns:

The number of occurrences of elem in the string.

get_list() list[ElemT][source]

Get the string as a list of its elements.

get_set() set[int][source]

Get the string as a set of the indices of bits with value 1.

get_tuple() tuple[ElemT, ...][source]

Get the string as a tuple of its elements.

hamming_weight() int[source]

Get the Hamming weight of this bit string.

The Hamming weight is defined as

\[\sum_i s_i\]

where \(s_i\) is the value of the bit at index \(i\) in the string.

impl_type

alias of QubitStateArray

imul_get_phase(op: String) ComplexSign[source]

In-place multiplication of self by op, returning the phase.

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).

is_vacuum() bool[source]

Check whether the string is the vacuum state (\(\left[0, 0, \ldots, 0\right]\)).

property qubits: Qubits

Get the qubits corresponding to self.

set(source: None | Sequence[bool] | set[int] | str | String[QubitStateArray, None | Sequence[bool] | set[int] | str, bool]) None[source]

Set the value of the string.

Parameters:

source – Specification for the new value.

Note

This method operates in-place.

to_sparse_matrix(big_endian: bool = False) Any[source]

Return self as a sparse matrix.

Parameters:

big_endian – Whether to use big-endian basis ordering.

Returns:

The sparse matrix representation of self.

vdot(other: String) int[source]

Compute the inner product of this string with another.