squlearn.observables.CustomObservable
- class squlearn.observables.CustomObservable(num_qubits: int, operator_string: str | list[str] | tuple[str], parameterized: bool = False)
Class for defining a custom observable.
The operator is supplied as a string of Pauli operators, e.g.
operator_string='ZI'for a two qubit operator with a Z operator on the first qubit. Qubitqis characterqof the string, i.e. the first qubit is the first (leftmost) character.Multiple operators that are summed can be specified by a list of strings, e.g.
operator_string=['ZZ', 'XX'].- Parameters:
num_qubits (int) – Number of qubits.
operator_string (Union[str, list[str], tuple[str]]) – String of operator to measure. Also list or tuples of strings are allowed for multiple operators.
parameterized (bool) – If True, the operator is parameterized.
Attributes:
- num_qubits
Number of qubits.
- Type:
int
- num_parameters
Number of trainable parameters in the custom operator.
- Type:
int
- operator_string
String of operator to measure.
- Type:
Union[str, list[str], tuple[str]]
- parameterized
If True, the operator is parameterized.
- 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: Parameters | ndarray) QuantumOperator
Returns Operator as a QuantumOperator expression.
- Parameters:
parameters (Union[Parameters, 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 custom 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: Parameters | ndarray = None) QuantumOperator
Function for generating the QuantumOperator expression of the custom operator.
- Parameters:
parameters (Union[Parameters, np.ndarray]) – Parameters of the custom operator.
- Returns:
QuantumOperator expression of the specified custom operator.
- get_pauli_mapped(parameters: Parameters | ndarray) QuantumOperator
Changes the operator to the physical qubits, set by
set_map().- Parameters:
parameters (Union[Parameters, np.ndarray]) – Vector of parameters used in the operator
- Returns:
Expectation operator in qc-executor’s QuantumOperator 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.