squlearn.observables.SummedProbabilities

class squlearn.observables.SummedProbabilities(num_qubits: int, one_state=False, full_sum: bool = True, include_identity: bool = True)

Observable for summing single Qubit probabilities of binary states.

Equation for a sum of 0-states:

\[\hat{H} = a\hat{I} + \sum_i b_i (\ket{0}\bra{0})_i\]

States are implemented by \(\ket{0}\bra{0} = 0.5(\hat{I}+\hat{Z})\) and \(\ket{1}\bra{1} = 0.5(\hat{I}-\hat{Z})\).

The parameter can be optionally equal for all states.

Parameters:
  • num_qubits (int) – Number of qubits.

  • one_state (bool) – If false the \(\ket{0}\bra{0}\) state is measured, if true the \(\ket{1}\bra{1}\) state is measured (default: False).

  • full_sum (bool) – If False, the parameter is the same for all states, i.e. the sum is \(b\sum_i (\ket{0}\bra{0})_i\) instead of \(\sum_i b_i (\ket{0}\bra{0})_i\) (default: True).

  • include_identity (bool) – If True, the identity operator is included in the sum. (default: True)

Attributes:

num_qubits

Number of qubits.

Type:

int

num_parameters

Number of trainable parameters in the summed probabilities operator.

Type:

int

one_state

If false the \(\ket{0}\bra{0}\) state is measured, if true the \(\ket{1}\bra{1}\) state is measured.

Type:

bool

full_sum

If False, the parameter is the same for all states.

Type:

bool

include_identity

If True, the identity operator is included in the sum.

Type:

bool

Methods:

generate_initial_parameters(ones: bool = True, seed: int | None = None) ndarray

Generates random parameters for the observable

Parameters:
  • ones (bool) – If True, returns an array of ones (default: True)

  • seed (Union[int,None]) – Seed for the random number generator

Returns:

The randomly generated parameters

get_operator(parameters: ParameterVector | ndarray) SparsePauliOp

Returns Operator as a SparsePauliOp expression.

Parameters:

parameters (Union[ParameterVector, np.ndarray]) – Vector of parameters used in the operator

Returns:

StateFn expression of the observable.

get_params(deep: bool = True) dict

Returns hyper-parameters and their values of the single summed probabilities operator.

Parameters:

deep (bool) – If True, also the parameters for contained objects are returned (default=True).

Returns:

Dictionary with hyper-parameters and values.

get_pauli(parameters: ParameterVector | ndarray = None) SparsePauliOp

Function for generating the PauliOp expression of the summed probabilities operator.

Parameters:

parameters (Union[ParameterVector, np.ndarray]) – Parameters of the summed probabilities operator.

Returns:

PauliOp expression of the specified summed probabilities operator.

get_pauli_mapped(parameters: ParameterVector | ndarray) SparsePauliOp

Changes the operator to the physical qubits, set by set_map().

Parameters:

parameters (Union[ParameterVector, np.ndarray]) – Vector of parameters used in the operator

Returns:

Expectation operator in Qiskit’s SprasePauliOp class with qubits mapped to physical ones

set_map(qubit_map: list | dict, num_all_qubits: int)

Function for setting a qubit mapping from physical qubits to the ones of the operator.

This function is necessary whenever the number of physical qubits are different from the operator definition, as for example when running on a real backend. The number of qubits in the system has to be larger than the number of qubits in the observable.

Parameters:
  • qubit_map (Union[list, dict]) – A list or dictionary specifying which of the input qubits are mapped to the output qubits.

  • num_all_qubits (int) – The total number of qubits in the system.

set_params(**params) None

Sets value of the operator hyper-parameters.

Parameters:

params – Hyper-parameters and their values, e.g. num_qubits=2.