Skip to content

base

Base device.

Device

Bases: ABC

Source code in jaxquantum/devices/base/base.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
@struct.dataclass
class Device(ABC):
    DEFAULT_BASIS = BasisTypes.fock
    DEFAULT_HAMILTONIAN = HamiltonianTypes.full

    N: int = struct.field(pytree_node=False)
    N_pre_diag: int = struct.field(pytree_node=False)
    params: Dict[str, Any]
    _label: int = struct.field(pytree_node=False)
    _basis: BasisTypes = struct.field(pytree_node=False)
    _hamiltonian: HamiltonianTypes = struct.field(pytree_node=False)

    @classmethod
    def param_validation(cls, N, N_pre_diag, params, hamiltonian, basis):
        """This can be overridden by subclasses."""
        pass

    @classmethod
    def create(
        cls,
        N,
        params,
        label=0,
        N_pre_diag=None,
        use_linear=False,
        hamiltonian: HamiltonianTypes = None,
        basis: BasisTypes = None,
    ):
        """Create a device.

        Args:
            N (int): dimension of Hilbert space.
            params (dict): parameters of the device.
            label (int, optional): label for the device. Defaults to 0. This is useful when you have multiple of the same device type in the same system.
            N_pre_diag (int, optional): dimension of Hilbert space before diagonalization. Defaults to None, in which case it is set to N. This must be greater than or rqual to N.
            use_linear (bool): whether to use the linearized device. Defaults to False. This will override the hamiltonian keyword argument. This is a bit redundant with hamiltonian, but it is kept for backwards compatibility.
            hamiltonian (HamiltonianTypes, optional): type of Hamiltonian. Defaults to None, in which case the full hamiltonian is used.
            basis (BasisTypes, optional): type of basis. Defaults to None, in which case the fock basis is used.
        """

        if N_pre_diag is None:
            N_pre_diag = N

        assert N_pre_diag >= N, "N_pre_diag must be greater than or equal to N."

        _basis = basis if basis is not None else cls.DEFAULT_BASIS
        _hamiltonian = (
            hamiltonian if hamiltonian is not None else cls.DEFAULT_HAMILTONIAN
        )

        if use_linear:
            _hamiltonian = HamiltonianTypes.linear

        cls.param_validation(N, N_pre_diag, params, _hamiltonian, _basis)

        return cls(N, N_pre_diag, params, label, _basis, _hamiltonian)

    @property
    def basis(self):
        return self._basis

    @property
    def hamiltonian(self):
        return self._hamiltonian

    @property
    def label(self):
        return self.__class__.__name__ + str(self._label)

    @property
    def linear_ops(self):
        return self.common_ops()

    @property
    def original_ops(self):
        return self.common_ops()

    @property
    def ops(self):
        return self.full_ops()

    @abstractmethod
    def common_ops(self) -> Dict[str, Qarray]:
        """Set up common ops in the specified basis."""

    @abstractmethod
    def get_linear_ω(self):
        """Get frequency of linear terms."""

    @abstractmethod
    def get_H_linear(self):
        """Return linear terms in H."""

    @abstractmethod
    def get_H_full(self):
        """Return full H."""

    def get_H(self):
        """
        Return diagonalized H. Explicitly keep only diagonal elements of matrix.
        """
        return self.get_op_in_H_eigenbasis(
            self._get_H_in_original_basis()
        ).keep_only_diag_elements()

    def _get_H_in_original_basis(self):
        """This returns the Hamiltonian in the original specified basis. This can be overridden by subclasses."""

        if self.hamiltonian == HamiltonianTypes.linear:
            return self.get_H_linear()
        elif self.hamiltonian == HamiltonianTypes.full:
            return self.get_H_full()

    def _calculate_eig_systems(self):
        evs, evecs = jnp.linalg.eigh(self._get_H_in_original_basis().data)  # Hermitian
        idxs_sorted = jnp.argsort(evs)
        return evs[idxs_sorted], evecs[:, idxs_sorted]

    @property
    def eig_systems(self):
        eig_systems = {}
        eig_systems["vals"], eig_systems["vecs"] = self._calculate_eig_systems()

        eig_systems["vecs"] = eig_systems["vecs"]
        eig_systems["vals"] = eig_systems["vals"]
        return eig_systems

    def get_op_in_H_eigenbasis(self, op: Qarray):
        evecs = self.eig_systems["vecs"][:, : self.N]
        dims = [[self.N], [self.N]]
        return get_op_in_new_basis(op, evecs, dims)

    def get_op_data_in_H_eigenbasis(self, op: Array):
        evecs = self.eig_systems["vecs"][:, : self.N]
        return get_op_data_in_new_basis(op, evecs)

    def get_vec_in_H_eigenbasis(self, vec: Qarray):
        evecs = self.eig_systems["vecs"][:, : self.N]
        if vec.qtype == Qtypes.ket:
            dims = [[self.N], [1]]
        else:
            dims = [[1], [self.N]]
        return get_vec_in_new_basis(vec, evecs, dims)

    def get_vec_data_in_H_eigenbasis(self, vec: Array):
        evecs = self.eig_systems["vecs"][:, : self.N]
        return get_vec_data_in_new_basis(vec, evecs)

    def full_ops(self):
        # TODO: use JAX vmap here

        linear_ops = self.linear_ops
        ops = {}
        for name, op in linear_ops.items():
            ops[name] = self.get_op_in_H_eigenbasis(op)

        return ops

common_ops() abstractmethod

Set up common ops in the specified basis.

Source code in jaxquantum/devices/base/base.py
148
149
150
@abstractmethod
def common_ops(self) -> Dict[str, Qarray]:
    """Set up common ops in the specified basis."""

create(N, params, label=0, N_pre_diag=None, use_linear=False, hamiltonian=None, basis=None) classmethod

Create a device.

Parameters:

Name Type Description Default
N int

dimension of Hilbert space.

required
params dict

parameters of the device.

required
label int

label for the device. Defaults to 0. This is useful when you have multiple of the same device type in the same system.

0
N_pre_diag int

dimension of Hilbert space before diagonalization. Defaults to None, in which case it is set to N. This must be greater than or rqual to N.

None
use_linear bool

whether to use the linearized device. Defaults to False. This will override the hamiltonian keyword argument. This is a bit redundant with hamiltonian, but it is kept for backwards compatibility.

False
hamiltonian HamiltonianTypes

type of Hamiltonian. Defaults to None, in which case the full hamiltonian is used.

None
basis BasisTypes

type of basis. Defaults to None, in which case the fock basis is used.

None
Source code in jaxquantum/devices/base/base.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
@classmethod
def create(
    cls,
    N,
    params,
    label=0,
    N_pre_diag=None,
    use_linear=False,
    hamiltonian: HamiltonianTypes = None,
    basis: BasisTypes = None,
):
    """Create a device.

    Args:
        N (int): dimension of Hilbert space.
        params (dict): parameters of the device.
        label (int, optional): label for the device. Defaults to 0. This is useful when you have multiple of the same device type in the same system.
        N_pre_diag (int, optional): dimension of Hilbert space before diagonalization. Defaults to None, in which case it is set to N. This must be greater than or rqual to N.
        use_linear (bool): whether to use the linearized device. Defaults to False. This will override the hamiltonian keyword argument. This is a bit redundant with hamiltonian, but it is kept for backwards compatibility.
        hamiltonian (HamiltonianTypes, optional): type of Hamiltonian. Defaults to None, in which case the full hamiltonian is used.
        basis (BasisTypes, optional): type of basis. Defaults to None, in which case the fock basis is used.
    """

    if N_pre_diag is None:
        N_pre_diag = N

    assert N_pre_diag >= N, "N_pre_diag must be greater than or equal to N."

    _basis = basis if basis is not None else cls.DEFAULT_BASIS
    _hamiltonian = (
        hamiltonian if hamiltonian is not None else cls.DEFAULT_HAMILTONIAN
    )

    if use_linear:
        _hamiltonian = HamiltonianTypes.linear

    cls.param_validation(N, N_pre_diag, params, _hamiltonian, _basis)

    return cls(N, N_pre_diag, params, label, _basis, _hamiltonian)

get_H()

Return diagonalized H. Explicitly keep only diagonal elements of matrix.

Source code in jaxquantum/devices/base/base.py
164
165
166
167
168
169
170
def get_H(self):
    """
    Return diagonalized H. Explicitly keep only diagonal elements of matrix.
    """
    return self.get_op_in_H_eigenbasis(
        self._get_H_in_original_basis()
    ).keep_only_diag_elements()

get_H_full() abstractmethod

Return full H.

Source code in jaxquantum/devices/base/base.py
160
161
162
@abstractmethod
def get_H_full(self):
    """Return full H."""

get_H_linear() abstractmethod

Return linear terms in H.

Source code in jaxquantum/devices/base/base.py
156
157
158
@abstractmethod
def get_H_linear(self):
    """Return linear terms in H."""

get_linear_ω() abstractmethod

Get frequency of linear terms.

Source code in jaxquantum/devices/base/base.py
152
153
154
@abstractmethod
def get_linear_ω(self):
    """Get frequency of linear terms."""

param_validation(N, N_pre_diag, params, hamiltonian, basis) classmethod

This can be overridden by subclasses.

Source code in jaxquantum/devices/base/base.py
79
80
81
82
@classmethod
def param_validation(cls, N, N_pre_diag, params, hamiltonian, basis):
    """This can be overridden by subclasses."""
    pass