pauli_state¶
Implementation of the State interface for states in Pauli bases (XBasis, YBasis, ZBasis).
Here we define several classes for different use cases:
PauliBasisState: Single product state in a given Pauli basis.
PauliQuantumState: General quantum state given as a weighted superposition of basis states(
PauliBasisStateinstances).
PauliBasisStateCounts: Statistical ensemble (integer counts) of basis states(
PauliBasisStateinstances) as obtained e.g. from multiple evaluation shots on a QPU.
Examples
>>> # Define |00> and |11> product states in the Z pauli basis
>>> basis_state_1 = PauliBasisState("00")
>>> basis_state_2 = PauliBasisState("11")
>>> # Create bell states (`PauliQuantumState`s) from arithmetics
>>> bell_state_p = 1. / sqrt(2) * (basis_state_1 + basis_state_2)
>>> bell_state_m = (basis_state_1 - basis_state_2).to_normalized()
>>> # Print the resulting states
>>> print(bell_state_p)
>>> # 0.7071067811865475*|00⟩ + 0.7071067811865475*|11⟩
>>> print(bell_state_m)
>>> # 0.7071067811865475*|00⟩ - 0.7071067811865475*|11⟩
- class parityos.states.pauli_state.HasBitstrings(*args, **kwargs)¶
Bases:
ProtocolA protocol for objects that provide a in iterator of bitstrings.
- class parityos.states.pauli_state.OrderedPauliState¶
Bases:
HasFrozenOrderedQubits[QubitT_co],HasPauliBasis[BasisT_co],ABCBase class for states in a Pauli basis with a definite qubit order.
Implements
reorderto create a copy with changed qubit order.- abstractmethod reorder(target_ordered_qubits: Sequence[QubitT_co] | None = None) Self¶
Reorder this state’s qubits to the order defined by
target_ordered_qubits.- Parameters:
target_ordered_qubits – Target qubit order for bitstring. If
None,sorted(self.ordered_qubits)is used. Defaults toNone.- Returns:
A copy of
selfwhere qubit ordering has been changed totarget_ordered_qubits.- Raises:
ParityOSException – If this state’s qubits and qubits in
target_ordered_qubitsare different up to ordering.
- class parityos.states.pauli_state.PauliBasisState(bitstring: str, ordered_qubits: T | Iterable[T] = NOTHING, basis: type[BasisT_co] = parityos.operators.elementary_operator.Z[typing.Never])¶
Bases:
OrderedPauliState[QubitT_co,BasisT_co],BasisState[QubitT_co]Basis state (product state) according to a Pauli basis (either
XBasis,YBasis, orZBasis).The state is stored as a bitstring and a sequence of qubits, where the i-th entry in
bitstringdetermines the state of the i-th qubit as eigenstate of the chosen Pauli basis.The state of each qubit is represented as a character “0” or “1”, where
The +1 eigenstate of the chosen Pauli basis is mapped to “0”
The -1 eigenstate of the chosen Pauli basis is mapped to “1”
See
bit_to_spin_valuefor more details on this conversion.Caution
The bitstring and the sequence of qubits must be of same length and the bitstring must not be empty.
- Raises:
ParityOSException – If the bitstring is empty.
ParityOSException – If the bitstring contains other characters besides “0” or “1”.
ParityOSException – If
bitstringandordered_qubitsare not of equal length.ParityOSUniquenessError – If there are any duplicate qubits.
- ordered_qubits: tuple[QubitT_co, ...]¶
Ordered
Qubitsequence on which the state is defined. Defaults to an integerQubitsequence. with increasing integer ids for each entry inbitstring.
- basis: type[BasisT_co]¶
The Pauli basis of this state represented by one of
XBasis,YBasisorZBasis. Defaults toZBasis.
- classmethod from_qubit_to_value(qubit_to_value: Mapping[QubitT_co, bool], basis: type[BasisT_co] = parityos.operators.elementary_operator.Z[typing.Never]) Self¶
Create a
PauliBasisStatefrom a qubit to boolean bit map and aBasis.- Parameters:
Returns: A
PauliBasisStatecorresponding toqubit_to_valuemap.
- qubit_to_value: MappingProxyType¶
Qubit to boolean bit map representation of the Pauli basis state.
Here “1” is mapped to
Trueand “0” is mapped toFalsecompared to the state’s bitstring.- Example:
A PauliBasisState defined by the bitstring
"001"on qubits namedq_0,q_1,q_2gives a dict like{"q_0": False, "q_1": True, "q_2": True}.
- get_spin_value(qubit: QubitT_co) int¶
Get integer spin value (+/-1) for
qubit.The bitstrings “0” or “1”, are mapped to spin values according to:
“0” is mapped to the +1 eigenstate of the chosen Pauli basis
“1” is mapped to the -1 eigenstate of the chosen Pauli basis.
See
bit_to_spin_valuefor more details on this conversion.- Parameters:
qubit – Qubit for which to get the integer spin value.
- Returns:
Integer spin value (+/-1) of
qubitin this state- Raises:
ParityOSException – If
qubitis not contained in this state
- flip_qubits(qubits: QubitT_co | Iterable[QubitT_co] | None = None) Self¶
Flip the state on every qubit in
qubits.For each qubit in
qubitsthe state on this qubit is flipped (0 <-> 1). Qubits inqubitsthat are not in this state are ignored.Note
If
qubitsisNone(default), all qubits are flipped.- Parameters:
qubits – Qubits to flip. If
None, all qubits are flipped. Defaults toNone.- Returns:
Copy of
selfwith state on all qubits inqubitsflipped
- reorder(target_ordered_qubits: Sequence[Qubit] | None = None) Self¶
Reorder this state’s qubits to the order defined by
target_ordered_qubits.- Parameters:
target_ordered_qubits – Target qubit order for bitstring. If
None,sorted(self.ordered_qubits)is used. Defaults toNone.- Returns:
A copy of
selfwhere qubit ordering has been changed totarget_ordered_qubits.- Raises:
ParityOSException – If this state’s qubits and qubits in
target_ordered_qubitsare different up to ordering.
- __neg__() PauliQuantumState[QubitT_co, BasisT_co]¶
The negation operator
-.- Returns:
A single-term
PauliQuantumStatewithselfas single term with amplitude -1.
- __mul__(other: Symbolic | complex) PauliQuantumState[QubitT_co, BasisT_co]¶
- __mul__(other: PauliBasisState[OtherQubitT_co, BasisT_co]) PauliBasisState[QubitT_co | OtherQubitT_co, BasisT_co]
The multiplication operator
*.Multiplication between states is to be interpreted as a tensor product.
Caution
If
otheris a state, it must be in the same basis and not share any qubits withself.- Parameters:
other – A
Coefficientor anotherPauliBasisState.- Returns:
If
otheris aCoefficient, returns a one-term PauliQuantumState with this amplitude. Ifotheris aPauliBasisState, returns a tensor product of this and the other basis state. The resulting basis state’s qubits are the concatenation of the qubits ofselfandother, no reordering is applied.
- __add__(other: PauliBasisState[QubitT_co, BasisT_co]) PauliQuantumState[QubitT_co, BasisT_co]¶
The addition operator
+.Caution
selfandothermust be defined on the same set of ordered qubits in the same basis.- Parameters:
other – Another
PauliBasisState.- Returns:
A
PauliQuantumStateas a superposition ofselfandother, each with amplitude 1. (not normalized).- Raises:
ParityOSException – If
selfandotherare defined on different qubit sequences or in a different basis.
- __sub__(other: PauliBasisState[QubitT_co, BasisT_co]) PauliQuantumState[QubitT_co, BasisT_co]¶
The subtraction operator
+.Caution
selfandothermust be defined on the same set of ordered qubits in the same basis.- Parameters:
other – Another
PauliBasisState.- Returns:
PauliQuantumState consisting of the two PauliBasisStates,
selfwith amplitude 1,otherwith amplitude -1 (not normalized).
- class parityos.states.pauli_state.PauliQuantumState(bitstring_amplitude_pairs: Iterable[tuple[KeyT, Coefficient]] | Mapping[KeyT, Coefficient], ordered_qubits: T | Iterable[T] = NOTHING, basis: type[BasisT_co] = parityos.operators.elementary_operator.Z[typing.Never])¶
Bases:
OrderedPauliState[QubitT_co,BasisT_co],HasBitstrings,Parameterized,QuantumState[QubitT_co,PauliBasisState[QubitT_co,BasisT_co]]Quantum state as amplitude weighted superposition of basis states in the same Pauli basis.
The state is stored as a set of (bitstring, amplitude) pairs and an ordered sequence of qubits. As such it constitutes a sparse representation, as only basis states with non-zero amplitudes are stored.
For each
bitstring, the i-th entry determines the state of the i-th qubit inordered_qubitsas eigenstate of the chosen Pauli basis with the following mapping:The +1 eigenstate of the chosen Pauli basis is mapped to “0” in the bitstring
The -1 eigenstate of the chosen Pauli basis is mapped to “1” in the bitstring
See
PauliBasisStatefor more details.Caution
The bitstrings and sequence of qubits must be of same length.
- Raises:
ParityOSException – If not all basis state bitstrings are the same length as
ordered_qubits.ParityOSException – If any basis state bitstrings contain other characters besides “0” or “1”.
ParityOSException – If not all basis states are defined in the same Pauli basis
ParityOSUniquenessError – If there are duplicate qubits.
- bitstring_amplitude_pairs: frozenset[tuple[str, Symbolic | complex]]¶
Collection of pairs of basis state bitstrings and their amplitude.
- basis: type[BasisT_co]¶
The Pauli basis of this state represented by one of
XBasis,YBasisorZBasis. Defaults toZBasis.
- classmethod from_state_to_amplitude(state_to_amplitude: Mapping[PauliBasisState[QubitT_co, BasisT_co], Symbolic | complex])¶
Create a
PauliQuantumStatefrom mapping ofPauliBasisStateinstances to their amplitudes.Note
The state is not normalized upon creation.
- Parameters:
state_to_amplitude – mapping of
PauliBasisStateinstances to their amplitude.- Returns:
- a
PauliQuantumStateas weighted superposition ofPauliBasisStateinstances with given amplitudes.
- a
- Raises:
ParityOSException – If not all basis state bitstrings are the same length as
ordered_qubits.ParityOSException – If any basis state bitstrings contain other characters besides “0” or “1”.
ParityOSException – If not all basis states are defined in the same Pauli basis.
ParityOSUniquenessError – If there are duplicate qubits.
- state_to_amplitude: MappingProxyType¶
A mapping of each
PauliBasisStateto its amplitude, representing the quantum state.
- to_normalized() Self¶
Get a normalized copy of this state.
The squared absolute values of the returned state’s amplitudes sum to 1, constituting a normalized state.
- Returns:
A normalized copy of this state.
- property bitstrings: Iterator[str]¶
Iterator of bitstrings.
An iterator that yields strings consisting of binary digits (e.g., “0101”, “1111”).
- substitute_parameters(old_to_new: Mapping[Parameter, Symbolic | complex]) Self¶
Substitute symbolic parameters with new symbolic expressions or numeric values.
- Parameters:
old_to_new – Mapping from existing parameter symbols to new values (numeric or symbolic).
- Returns:
Copy of
selfwith replaced parameters.
- flip_qubits(qubits: QubitT_co | Iterable[QubitT_co] | None = None) Self¶
Flip the state on every qubit in
qubits.For each qubit in
qubitsthe state on this qubit is flipped (0 <-> 1). Qubits inqubitsthat are not in this state are ignored.Note
If
qubitsisNone(default), all qubits are flipped.- Parameters:
qubits – Qubits to flip. If
None, all qubits are flipped. Defaults toNone.- Returns:
Copy of
selfwith state on all qubits inqubitsflipped
- bitstrings_to_amplitude: MappingProxyType¶
A mapping of bitstrings to their amplitude, representing the quantum state.
- reorder(target_ordered_qubits: Sequence[QubitT_co] | None = None) Self¶
Reorder this state’s qubits to the order defined by
target_ordered_qubits.- Parameters:
target_ordered_qubits – Target qubit order for bitstring. If
None,sorted(self.ordered_qubits)is used. Defaults toNone.- Returns:
A copy of
selfwhere qubit ordering has been changed totarget_ordered_qubits.- Raises:
ParityOSException – If this state’s qubits and qubits in
target_ordered_qubitsare different up to ordering.
- __neg__() Self¶
The negation operator
-.- Returns:
A copy of
selfwith the sign of all amplitudes flipped.
- __mul__(other: Symbolic | complex) Self¶
- __mul__(other: PauliBasisState[OtherQubitT_co, BasisT_co] | PauliQuantumState[OtherQubitT_co, BasisT_co]) PauliQuantumState[QubitT_co | OtherQubitT_co, BasisT_co]
The multiplication operator
*.Multiplication between states is to be interpreted as a tensor product.
Caution
If
otheris a state, it must be in the same basis and not share any qubits withself.- Parameters:
other – A
Coefficient, aPauliBasisStateor anotherPauliQuantumState.- Returns:
If
otheris aCoefficient, returns a copy ofselfwith all amplitudes multiplied byother. Ifotheris aPauliBasisState, returns a newPauliQuantumStatewith new basis states being the tensor products between the old basis states andother. Ifotheris anotherPauliQuantumState, returns a full state-state multiplication with all possible pairs of terms fromselfandother.- Raises:
ParityOSException – For state multiplication, if
selfandotherare defined in a different basis.
- __truediv__(other: Symbolic | complex) Self¶
The division operator
/.- Parameters:
other – The
Coefficientby which to divide.- Returns:
Copy of the state with all amplitudes divided by
other.
- __add__(other: PauliQuantumState[QubitT_co, BasisT_co] | PauliBasisState[QubitT_co, BasisT_co]) Self¶
The addition operator
+.Caution
othermust be in the same basis and be defined on the same qubits asself.- Parameters:
other – A
PauliBasisStateor anotherPauliQuantumState.- Returns:
If
otheris aPauliBasisState, returns a new quantum state withotheradded toselfwith amplitude 1. Ifotheris anotherPauliQuantumState, returns a new state with the union of the basis states ofselfandotherand their amplitudes added.- Raises:
ParityOSException – if
selfandotherare not defined on the same sequence of qubits or are in a different basis.
- __sub__(other: PauliQuantumState[QubitT_co, BasisT_co] | PauliBasisState[QubitT_co, BasisT_co]) Self¶
The subtraction operator
-.- Parameters:
other – A
PauliBasisStateor anotherPauliQuantumState.- Returns:
- class parityos.states.pauli_state.PauliBasisStateCounts(bitstring_count_pairs: Iterable[tuple[KeyT, int]] | Mapping[KeyT, int], ordered_qubits: T | Iterable[T] = NOTHING, basis: type[BasisT_co] = parityos.operators.elementary_operator.Z[typing.Never])¶
Bases:
OrderedPauliState[QubitT_co,BasisT_co],HasBitstrings,BasisStateCounts[QubitT_co,PauliBasisState[QubitT_co,BasisT_co]]A statistical ensemble of basis states (
PauliBasisState) in a given Pauli basis as a mapping of bitstrings to measurement counts (integer) as obtained e.g. from multiple evaluation shots on a QPU.The ensemble is stored as a set of (bitstring, count) pairs and an ordered sequence of qubits.
See
PauliBasisStatefor more information on the mapping of bitstrings to qubits and eigenstates of the Pauli operator.Caution
The bitstrings and sequence of qubits must be of same length.
- Raises:
ParityOSException – If counts are not positive.
ParityOSException – If the (bitstring, count) pairs are empty.
ParityOSException – If not all basis state bitstrings are the same length as
ordered_qubitsParityOSException – If any basis state bitstrings contain other characters besides “0” or “1”.
ParityOSUniquenessError – If there are duplicate qubits.
- bitstring_count_pairs: frozenset[tuple[str, int]]¶
Collection of pairs of basis state bitstrings and their counts.
- basis: type[BasisT_co]¶
The Pauli basis of this state represented by one of
XBasis,YBasisorZBasis. Defaults toZBasis.
- classmethod from_state_to_count(state_to_count: Mapping[PauliBasisState[QubitT_co, BasisT_co], int])¶
Create a
PauliBasisStateCountsinstance from a mapping of basis states to their counts.- Raises:
ParityOSException – If counts are not positive.
ParityOSException – If the (bitstring, count) pairs are empty.
ParityOSException – If not all basis state bitstrings are the same length as
ordered_qubitsParityOSException – If any basis state bitstrings contain other characters besides “0” or “1”.
ParityOSUniquenessError – If there are duplicate qubits.
- property bitstrings: Iterator[str]¶
Iterator of bitstrings.
An iterator that yields strings consisting of binary digits (e.g., “0101”, “1111”).
- state_to_count: MappingProxyType¶
A mapping of basis states (
PauliBasisState) to their measurement counts.
- bitstring_to_count: dict[str, int]¶
A mapping of the basis state bitstrings to their measurement counts.
This is the mapping version of
bitstring_count_pairs.
- flip_qubits(qubits: QubitT_co | Iterable[QubitT_co] | None = None) Self¶
Flip the state on every qubit in
qubits.For each qubit in
qubitsthe state on this qubit is flipped (0 <-> 1). Qubits inqubitsthat are not in this state are ignored.Note
If
qubitsisNone(default), all qubits are flipped.- Parameters:
qubits – Qubits to flip. If
None, all qubits are flipped. Defaults toNone.- Returns:
Copy of
selfwith state on all qubits inqubitsflipped
- reorder(target_ordered_qubits: Sequence[QubitT_co] | None = None) Self¶
Reorder this state’s qubits to the order defined by
target_ordered_qubits.- Parameters:
target_ordered_qubits – Target qubit order for bitstring. If
None,sorted(self.ordered_qubits)is used. Defaults toNone.- Returns:
A copy of
selfwhere qubit ordering has been changed totarget_ordered_qubits.- Raises:
ParityOSException – If this state’s qubits and qubits in
target_ordered_qubitsare different up to ordering.
- parityos.states.pauli_state.flip_qubits(qubit_bit_pairs: Iterable[tuple[QubitT_co, str]], qubits_to_flip: QubitT_co | Iterable[QubitT_co] | None) str¶
Flip subset of bits in an iterable of (qubit, bit) pairs.
If
qubits_to_flipisNone, all bits are flipped. Otherwise, only bits for qubits that appear inqubits_to_flipare flipped.Note
Only “0” and “1” are flipped, any other input is left unchanged.
Note
Any qubits in
qubits_to_flip, that are not also inqubit_bit_pairsare ignored.- Parameters:
qubit_bit_pairs – Pairs of qubits and their corresponding single character bit value.
qubits_to_flip – Subset of qubits for which bits in
qubit_bit_pairsshould be flipped.
- Returns:
- The bitstring with bits flipped if the original bit was one of (“0”, “1”) and its
corresponding qubit is in
qubits_to_flip.
- parityos.states.pauli_state.to_bitstring(bits: Iterable[bool]) str¶
Convert boolean bits to bitstring.
Trueis converted to “1”,Falseis converted to “0”.Inverse of
to_bits.- Parameters:
bits – Bits to be converted to a bitstring.
Returns: String of “0” and “1” representing the given
bits.
- parityos.states.pauli_state.to_bits(bitstring: str) tuple[bool, ...]¶
Convert bitstring to tuple of boolean bits.
“1” is converted to
True, “0” is converted toFalse.Inverse of
to_bitstring.- Parameters:
bitstring – Bitstring to be converted to a tuple of bits.
Returns: Tuple of boolean bits representation the given
bitstring.- Raises:
ParityOSException – If the given
bitstringcontains characters other than “0” or “1”.
- parityos.states.pauli_state.evaluate_parity(state: BasisState[QubitT_co], qubits: Iterable[QubitT_co]) Parity¶
Compute parity of given qubits in a state.
Caution
All passed qubits must be present in the state.
Note
Any qubits in
qubitsthat are not also instateare ignored.- Returns:
Parity of given qubits in this state.
- Raises:
ParityOSException – If not all of the given qubits are present in this state.
ParityOSException – If the passed qubits set is empty.