squlearn.observables
.IsingHamiltonian
- class squlearn.observables.IsingHamiltonian(num_qubits: int, I: str = 'S', Z: str = 'F', X: str = 'N', ZZ: str = 'F')
Implementation of Ising type Hamiltonians.
Equation for a the full Ising Hamiltonian:
\[\hat{H} = a\hat{I} + \sum_i b_i \hat{Z}_i + \sum_i c_i \hat{X}_i + \sum_{i>j} d_{ij} \hat{Z}_i \hat{Z}_j\]where \(a\), \(b_i\), \(c_i\), and \(d_{ij}\) are trainable parameters.
The options allow, to switch terms on and off and to set the parameters additionally to be equal for the same kind of term.
Example for creating an Ising Hamiltonian with no \(\hat{Z}\) term, \(\hat{X}\) term with equal parameters, and the \(\hat{Z}\hat{Z}\) term with different parameters:
from squlearn.observables import IsingHamiltonian ob = IsingHamiltonian(num_qubits=2, I='S', Z='N', X='S', ZZ='F') print(ob)
SparsePauliOp(['II', 'IX', 'XI', 'ZZ'], coeffs=[ParameterExpression(1.0*p[0]), ParameterExpression(1.0*p[1]), ParameterExpression(1.0*p[1]), ParameterExpression(1.0*p[2])])
The default Ising Hamiltonian reads:
\[\hat{H} = a\hat{I} + \sum_i b_i \hat{Z}_i + \sum_{i>j} d_{ij} \hat{Z}_i \hat{Z}_j\]- Parameters:
num_qubits (int) – number of qubits
I (str) – parameter options for identity term.
I='S'
trainable parameter,I='N'
for zeroZ (str) – parameter options for Z term.
Z='S'
same parameter in the sum (\(\forall ~i:~ b_i=b\)),Z='N'
for zero,Z='F'
all \(b_i\) values are consideredX (str) – parameter options for X term.
X='S'
same parameter in the sum (\(\forall~ i: ~c_i=c\)),X='N'
for zero,X='F'
all \(c_i\) values are consideredZZ (str) – parameter options for ZZ term.
ZZ='S'
same parameter in the sum (\(\forall~ i,j: ~d_{ij}=d\)),ZZ='N'
for zero,ZZ='F'
all \(d_{ij}\) values are considered
Attributes:
- num_qubits
number of qubits
- Type:
int
- num_parameters
number of trainable parameters in the Ising Hamiltonian
- Type:
int
- I
parameter options for identity term
- Type:
str
- Z
parameter options for Z term
- Type:
str
- X
parameter options for X term
- Type:
str
- ZZ
parameter options for ZZ term
- Type:
str
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 Ising Hamiltonian 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) SparsePauliOp
Function for generating the SparsePauliOp expression of the Ising Hamiltonian.
- Parameters:
parameters (Union[ParameterVector, np.ndarray]) – parameters of the Ising Hamiltonian.
- Returns:
SparsePauliOp expression of the specified Ising Hamiltonian.
- 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
.