qilisdk 0.1.3__py3-none-any.whl → 0.1.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. qilisdk/__init__.py +11 -2
  2. qilisdk/__init__.pyi +2 -3
  3. qilisdk/_logging.py +135 -0
  4. qilisdk/_optionals.py +5 -7
  5. qilisdk/analog/__init__.py +3 -18
  6. qilisdk/analog/exceptions.py +2 -4
  7. qilisdk/analog/hamiltonian.py +455 -110
  8. qilisdk/analog/linear_schedule.py +118 -0
  9. qilisdk/analog/schedule.py +272 -79
  10. qilisdk/backends/__init__.py +45 -0
  11. qilisdk/{digital/digital_algorithm.py → backends/__init__.pyi} +3 -5
  12. qilisdk/backends/backend.py +117 -0
  13. qilisdk/{extras/cuda → backends}/cuda_backend.py +153 -161
  14. qilisdk/backends/qutip_backend.py +492 -0
  15. qilisdk/common/__init__.py +48 -2
  16. qilisdk/common/algorithm.py +2 -1
  17. qilisdk/{extras/qaas/qaas_settings.py → common/exceptions.py} +12 -6
  18. qilisdk/common/model.py +1019 -1
  19. qilisdk/common/parameterizable.py +75 -0
  20. qilisdk/common/qtensor.py +666 -0
  21. qilisdk/common/result.py +2 -1
  22. qilisdk/common/variables.py +1931 -0
  23. qilisdk/{extras/cuda/cuda_analog_result.py → cost_functions/__init__.py} +3 -4
  24. qilisdk/cost_functions/cost_function.py +77 -0
  25. qilisdk/cost_functions/model_cost_function.py +145 -0
  26. qilisdk/cost_functions/observable_cost_function.py +109 -0
  27. qilisdk/digital/__init__.py +3 -22
  28. qilisdk/digital/ansatz.py +203 -160
  29. qilisdk/digital/circuit.py +81 -9
  30. qilisdk/digital/exceptions.py +12 -6
  31. qilisdk/digital/gates.py +228 -85
  32. qilisdk/{extras/qaas/qaas_analog_result.py → functionals/__init__.py} +14 -5
  33. qilisdk/functionals/functional.py +39 -0
  34. qilisdk/{extras/cuda/cuda_digital_result.py → functionals/functional_result.py} +3 -4
  35. qilisdk/functionals/sampling.py +81 -0
  36. qilisdk/functionals/sampling_result.py +92 -0
  37. qilisdk/functionals/time_evolution.py +98 -0
  38. qilisdk/functionals/time_evolution_result.py +84 -0
  39. qilisdk/functionals/variational_program.py +80 -0
  40. qilisdk/functionals/variational_program_result.py +69 -0
  41. qilisdk/logging_config.yaml +16 -0
  42. qilisdk/{common/backend.py → optimizers/__init__.py} +2 -1
  43. qilisdk/optimizers/optimizer.py +39 -0
  44. qilisdk/{common → optimizers}/optimizer_result.py +3 -12
  45. qilisdk/{common/optimizer.py → optimizers/scipy_optimizer.py} +10 -28
  46. qilisdk/settings.py +78 -0
  47. qilisdk/{extras → speqtrum}/__init__.py +7 -8
  48. qilisdk/{extras → speqtrum}/__init__.pyi +3 -3
  49. qilisdk/speqtrum/experiments/__init__.py +25 -0
  50. qilisdk/speqtrum/experiments/experiment_functional.py +124 -0
  51. qilisdk/speqtrum/experiments/experiment_result.py +231 -0
  52. qilisdk/{extras/qaas → speqtrum}/keyring.py +8 -4
  53. qilisdk/speqtrum/speqtrum.py +432 -0
  54. qilisdk/speqtrum/speqtrum_models.py +300 -0
  55. qilisdk/utils/__init__.py +0 -14
  56. qilisdk/utils/openqasm2.py +1 -1
  57. qilisdk/utils/serialization.py +1 -1
  58. qilisdk/utils/visualization/PlusJakartaSans-SemiBold.ttf +0 -0
  59. qilisdk/utils/visualization/__init__.py +24 -0
  60. qilisdk/utils/visualization/circuit_renderers.py +781 -0
  61. qilisdk/utils/visualization/schedule_renderers.py +161 -0
  62. qilisdk/utils/visualization/style.py +154 -0
  63. qilisdk/utils/visualization/themes.py +76 -0
  64. qilisdk/yaml.py +126 -0
  65. {qilisdk-0.1.3.dist-info → qilisdk-0.1.5.dist-info}/METADATA +180 -135
  66. qilisdk-0.1.5.dist-info/RECORD +69 -0
  67. qilisdk/analog/algorithms.py +0 -111
  68. qilisdk/analog/analog_backend.py +0 -43
  69. qilisdk/analog/analog_result.py +0 -114
  70. qilisdk/analog/quantum_objects.py +0 -533
  71. qilisdk/digital/digital_backend.py +0 -90
  72. qilisdk/digital/digital_result.py +0 -145
  73. qilisdk/digital/vqe.py +0 -166
  74. qilisdk/extras/cuda/__init__.py +0 -13
  75. qilisdk/extras/qaas/__init__.py +0 -13
  76. qilisdk/extras/qaas/models.py +0 -132
  77. qilisdk/extras/qaas/qaas_backend.py +0 -255
  78. qilisdk/extras/qaas/qaas_digital_result.py +0 -20
  79. qilisdk/extras/qaas/qaas_time_evolution_result.py +0 -20
  80. qilisdk/extras/qaas/qaas_vqe_result.py +0 -20
  81. qilisdk-0.1.3.dist-info/RECORD +0 -51
  82. {qilisdk-0.1.3.dist-info → qilisdk-0.1.5.dist-info}/WHEEL +0 -0
  83. {qilisdk-0.1.3.dist-info → qilisdk-0.1.5.dist-info}/licenses/LICENCE +0 -0
@@ -1,533 +0,0 @@
1
- # Copyright 2025 Qilimanjaro Quantum Tech
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- from __future__ import annotations
15
-
16
- import string
17
- from typing import Literal
18
-
19
- import numpy as np
20
- from scipy.sparse import csc_array, csr_matrix, issparse, kron, sparray, spmatrix
21
- from scipy.sparse.linalg import expm
22
- from scipy.sparse.linalg import norm as scipy_norm
23
-
24
- from qilisdk.yaml import yaml
25
-
26
- Complex = int | float | complex
27
-
28
-
29
- ###############################################################################
30
- # Main Class Definition
31
- ###############################################################################
32
-
33
-
34
- @yaml.register_class
35
- class QuantumObject:
36
- """
37
- Represents a quantum state or operator using a sparse matrix representation.
38
-
39
- The QuantumObject class is a wrapper around sparse matrices (or NumPy arrays,
40
- which are converted to sparse matrices) that represent quantum states (kets, bras)
41
- or operators. It provides utility methods for common quantum operations such as
42
- taking the adjoint (dagger), computing tensor products, partial traces, and norms.
43
-
44
- The internal data is stored as a SciPy CSR (Compressed Sparse Row) matrix for
45
- efficient arithmetic and manipulation. The expected shapes for the data are:
46
- - (2**N, 2**N) for operators or density matrices (or scalars),
47
- - (2**N, 1) for ket states,
48
- - (1, 2**N) or (2**N,) for bra states.
49
- """
50
-
51
- def __init__(self, data: np.ndarray | sparray | spmatrix) -> None:
52
- """
53
- Initialize a QuantumObject with the given data.
54
-
55
- Converts a NumPy array to a CSR matrix if needed and validates the shape of the input.
56
- The input must represent a valid quantum state or operator with appropriate dimensions.
57
- Notice that 1D arrays of shape (2N,) are considered/transformed to bras with shape (1, 2N).
58
-
59
- Args:
60
- data (np.ndarray | sparray | spmatrix): A dense NumPy array or a SciPy sparse matrix
61
- representing a quantum state or operator. Should be of shape: (2**N, 2**N) for operators
62
- (1, 2**N) for ket states, (2**N, 1) or (2**N,) for bra states, or (1, 1) for scalars.
63
-
64
- Raises:
65
- ValueError: If the input data is not a NumPy array or a SciPy sparse matrix,
66
- or if the data's shape does not correspond to a valid quantum state/operator.
67
- """
68
- if isinstance(data, np.ndarray):
69
- self._data = csr_matrix(data)
70
- elif issparse(data):
71
- self._data = data.tocsr()
72
- else:
73
- raise ValueError("Input must be a NumPy array or a SciPy sparse matrix")
74
-
75
- # Valid shapes are operators = (2**N, 2**N) (scalars included), bra's = (1, 2**N) / (2**N,), or ket's =(2**N, 1):
76
- valid_shape = self.is_operator() or self.is_ket() or self.is_bra()
77
-
78
- if len(self._data.shape) != 2 or not valid_shape: # noqa: PLR2004
79
- raise ValueError(
80
- "Dimension of data is wrong. expected data to have shape similar to (2**N, 2**N), (1, 2**N), (2**N, 1)",
81
- f"but received {self._data.shape}",
82
- )
83
-
84
- # ------------- Properties --------------
85
-
86
- @property
87
- def data(self) -> csr_matrix:
88
- """
89
- Get the internal sparse matrix representation of the QuantumObject.
90
-
91
- Returns:
92
- csr_matrix: The internal representation as a CSR matrix.
93
- """
94
- return self._data
95
-
96
- @property
97
- def dense(self) -> np.ndarray:
98
- """
99
- Get the dense (NumPy array) representation of the QuantumObject.
100
-
101
- Returns:
102
- np.ndarray: The dense array representation.
103
- """
104
- return self._data.toarray()
105
-
106
- @property
107
- def nqubits(self) -> int:
108
- """
109
- Compute the number of qubits represented by the QuantumObject.
110
-
111
- Returns:
112
- int: The number of qubits if determinable; otherwise, -1.
113
- """
114
- if self._data.shape[0] == self._data.shape[1]:
115
- return int(np.log2(self._data.shape[0]))
116
- if self._data.shape[0] == 1:
117
- return int(np.log2(self._data.shape[1]))
118
- if self._data.shape[1] == 1:
119
- return int(np.log2(self._data.shape[0]))
120
- return -1
121
-
122
- @property
123
- def shape(self) -> tuple[int, ...]:
124
- """
125
- Get the shape of the QuantumObject's internal matrix.
126
-
127
- Returns:
128
- tuple[int, ...]: The shape of the internal matrix.
129
- """
130
- return self._data.shape
131
-
132
- # ----------- Matrix Logic Operations ------------
133
-
134
- def adjoint(self) -> QuantumObject:
135
- """
136
- Compute the adjoint (conjugate transpose) of the QuantumObject.
137
-
138
- Returns:
139
- QuantumObject: A new QuantumObject that is the adjoint of this object.
140
- """
141
- out = QuantumObject(self._data.conj().T)
142
- return out
143
-
144
- def ptrace(self, dims: list[int], keep: list[int]) -> "QuantumObject":
145
- """
146
- Compute the partial trace over subsystems not in 'keep'.
147
-
148
- This method calculates the reduced density matrix by tracing out
149
- the subsystems that are not specified in the 'keep' parameter.
150
- The input 'dims' represents the dimensions of each subsystem,
151
- and 'keep' indicates the indices of the subsystems to be retained.
152
-
153
- Args:
154
- dims (list[int]): A list specifying the dimensions of each subsystem.
155
- keep (list[int]): A list of indices corresponding to the subsystems to retain.
156
-
157
- Raises:
158
- ValueError: If the product of the dimensions in dims does not match the
159
- shape of the QuantumObject's dense representation.
160
-
161
- Returns:
162
- QuantumObject: A new QuantumObject representing the reduced density matrix
163
- for the subsystems specified in 'keep'.
164
- """
165
- rho = self.dense
166
- total_dim = np.prod(dims)
167
- if rho.shape != (total_dim, total_dim):
168
- raise ValueError("Dimension mismatch between provided dims and QuantumObject shape")
169
-
170
- # Use letters from the ASCII alphabet (both cases) for einsum indices.
171
- # For each subsystem, assign two letters: one for the row index and one for the column index.
172
- row_letters, col_letters = [], []
173
- out_row, out_col = [], [] # Letters that will remain in the output for the row part and for the column part.
174
- letters = iter(string.ascii_letters)
175
-
176
- for i in range(len(dims)):
177
- if i in keep:
178
- # For a subsystem we want to keep, use two different letters (r, c)
179
- r, c = next(letters), next(letters)
180
- row_letters.append(r)
181
- col_letters.append(c)
182
- out_row.append(r)
183
- out_col.append(c)
184
- else:
185
- # For subsystems to be traced out, assign the same letter (r, r) so that those indices are summed.
186
- r = next(letters)
187
- row_letters.append(r)
188
- col_letters.append(r)
189
-
190
- # Create the einsum subscript strings.
191
- # The input tensor has 2*n indices (first n for rows, next n for columns).
192
- input_subscript = "".join(row_letters + col_letters)
193
- # The output will only contain the indices corresponding to the subsystems we keep.
194
- output_subscript = "".join(out_row + out_col)
195
-
196
- # Reshape rho into a tensor with shape dims + dims.
197
- reshaped = rho.reshape(dims + dims)
198
- # Use einsum to sum over the indices that appear twice (i.e. those being traced out).
199
- reduced_tensor = np.einsum(f"{input_subscript}->{output_subscript}", reshaped)
200
-
201
- # The resulting tensor has separate indices for each subsystem kept.
202
- # Reshape it into a matrix (i.e. combine the row indices and column indices).
203
- dims_keep = [dims[i] for i in keep]
204
- new_dim = np.prod(dims_keep)
205
- reduced_matrix = reduced_tensor.reshape(new_dim, new_dim)
206
-
207
- return QuantumObject(reduced_matrix)
208
-
209
- def norm(self, order: int | Literal["fro", "tr"] = 1) -> float:
210
- """
211
- Compute the norm of the QuantumObject.
212
-
213
- For density matrices, the norm order can be specified. For state vectors, the norm is computed accordingly.
214
-
215
- Args:
216
- order (int or {"fro", "tr"}, optional): The order of the norm.
217
- Only applies if the QuantumObject represents a density matrix. Other than all the
218
- orders accepted by scipy, it also accepts 'tr' for the trace norm. Defaults to 1.
219
-
220
- Raises:
221
- ValueError: If the QuantumObject is not a valid density matrix or state vector,
222
-
223
- Returns:
224
- float: The computed norm of the QuantumObject.
225
- """
226
- if self.is_scalar():
227
- return self.dense[0][0]
228
-
229
- if self.is_density_matrix() or self.shape[0] == self.shape[1]:
230
- if order == "tr":
231
- return np.sum(np.abs(np.linalg.eigvalsh(self.dense)))
232
- return scipy_norm(self._data, ord=order)
233
-
234
- if self.is_bra():
235
- return np.sqrt(self._data @ self._data.conj().T).toarray()[0, 0]
236
-
237
- if self.is_ket():
238
- return np.sqrt(self._data.conj().T @ self._data).toarray()[0, 0]
239
-
240
- raise ValueError("The QuantumObject is not a valid density matrix or state vector. Cannot compute the norm.")
241
-
242
- def unit(self, order: int | Literal["fro", "tr"] = "tr") -> QuantumObject:
243
- """
244
- Normalize the QuantumObject.
245
-
246
- Scales the QuantumObject so that its norm becomes 1, according to the specified norm order.
247
-
248
- Args:
249
- order (int or {"fro", "tr"}, optional): The order of the norm to use for normalization.
250
- Only applies if the QuantumObject represents a density matrix. Other than all the
251
- orders accepted by scipy, it also accepts 'tr' for the trace norm. Defaults to "tr".
252
-
253
- Raises:
254
- ValueError: If the norm of the QuantumObject is 0, making normalization impossible.
255
-
256
- Returns:
257
- QuantumObject: A new QuantumObject that is the normalized version of this object.
258
- """
259
- norm = self.norm(order=order)
260
- if norm == 0:
261
- raise ValueError("Cannot normalize a zero-norm Quantum Object")
262
-
263
- return QuantumObject(self._data / norm)
264
-
265
- def expm(self) -> QuantumObject:
266
- """
267
- Compute the matrix exponential of the QuantumObject.
268
-
269
- Returns:
270
- QuantumObject: A new QuantumObject representing the matrix exponential.
271
- """
272
- return QuantumObject(expm(self._data))
273
-
274
- def to_density_matrix(self) -> QuantumObject:
275
- """
276
- Convert the QuantumObject to a density matrix.
277
-
278
- If the QuantumObject represents a state vector (ket or bra), this method
279
- calculates the corresponding density matrix by taking the outer product.
280
- If the QuantumObject is already a density matrix, it is returned unchanged.
281
- The resulting density matrix is normalized.
282
-
283
- Raises:
284
- ValueError: If the QuantumObject is a scalar, as a density matrix cannot be derived.
285
-
286
- Returns:
287
- QuantumObject: A new QuantumObject representing the density matrix.
288
- """
289
- if self.is_scalar():
290
- raise ValueError("Cannot make a density matrix from scalar.")
291
-
292
- if self.is_density_matrix():
293
- return self
294
-
295
- if self.is_bra():
296
- return (self.adjoint() @ self).unit()
297
-
298
- if self.is_ket():
299
- return (self @ self.adjoint()).unit()
300
-
301
- raise ValueError(
302
- "Cannot make a density matrix from this QuantumObject. "
303
- "It must be either a ket, a bra or already a density matrix."
304
- )
305
-
306
- # ----------- Checks for Matrices ------------
307
-
308
- def is_ket(self) -> bool:
309
- """
310
- Check if the QuantumObject represents a ket (column vector) state.
311
-
312
- Returns:
313
- bool: True if the QuantumObject is a ket state, False otherwise.
314
- """
315
- return self.shape[1] == 1 and self.shape[0].bit_count() == 1
316
-
317
- def is_bra(self) -> bool:
318
- """
319
- Check if the QuantumObject represents a bra (row vector) state.
320
-
321
- Returns:
322
- bool: True if the QuantumObject is a bra state, False otherwise.
323
- """
324
- return self.shape[0] == 1 and self.shape[1].bit_count() == 1
325
-
326
- def is_scalar(self) -> bool:
327
- """
328
- Check if the QuantumObject is a scalar (1x1 matrix).
329
-
330
- Returns:
331
- bool: True if the QuantumObject is a scalar, False otherwise.
332
- """
333
- return self.shape == (1, 1)
334
-
335
- def is_operator(self) -> bool:
336
- """
337
- Check if the QuantumObject is an operator (square matrix).
338
-
339
- Returns:
340
- bool: True if the QuantumObject is an operator, False otherwise.
341
- """
342
- return self._data.shape[1] == self._data.shape[0] and self._data.shape[0].bit_count() == 1
343
-
344
- def is_density_matrix(self, tol: float = 1e-8) -> bool:
345
- """
346
- Determine if the QuantumObject is a valid density matrix.
347
-
348
- A valid density matrix must be square, Hermitian, positive semi-definite, and have a trace equal to 1.
349
-
350
- Args:
351
- tol (float, optional): The numerical tolerance for verifying Hermiticity,
352
- eigenvalue non-negativity, and trace. Defaults to 1e-8.
353
-
354
- Returns:
355
- bool: True if the QuantumObject is a valid density matrix, False otherwise.
356
- """
357
- # Check if rho is a square matrix
358
- if not self.is_operator():
359
- return False
360
-
361
- # Check Hermitian condition: rho should be equal to its conjugate transpose
362
- if not self.is_hermitian(tol=tol):
363
- return False
364
-
365
- # Check if eigenvalues are non-negative (positive semi-definite)
366
- eigenvalues = np.linalg.eigvalsh(self.dense) # More stable for Hermitian matrices
367
- if np.any(eigenvalues < -tol): # Allow small numerical errors
368
- return False
369
-
370
- # Check if the trace is 1
371
- return np.isclose(self._data.trace(), 1, atol=tol)
372
-
373
- def is_hermitian(self, tol: float = 1e-8) -> bool:
374
- """
375
- Check if the QuantumObject is Hermitian.
376
-
377
- Args:
378
- tol (float, optional): The numerical tolerance for verifying Hermiticity.
379
- Defaults to 1e-8.
380
-
381
- Returns:
382
- bool: True if the QuantumObject is Hermitian, False otherwise.
383
- """
384
- return np.allclose(self.dense, self._data.conj().T.toarray(), atol=tol)
385
-
386
- # ----------- Basic Arithmetic Operators ------------
387
-
388
- def __add__(self, other: QuantumObject | Complex) -> QuantumObject:
389
- if isinstance(other, QuantumObject):
390
- return QuantumObject(self._data + other._data)
391
- if isinstance(other, Complex) and other == 0:
392
- return self
393
-
394
- raise TypeError("Addition is only supported between QuantumState instances")
395
-
396
- def __sub__(self, other: QuantumObject) -> QuantumObject:
397
- if isinstance(other, QuantumObject):
398
- return QuantumObject(self._data - other._data)
399
-
400
- raise TypeError("Subtraction is only supported between QuantumState instances")
401
-
402
- def __mul__(self, other: QuantumObject | Complex) -> QuantumObject:
403
- if isinstance(other, (int, float, complex)):
404
- return QuantumObject(self._data * other)
405
- if isinstance(other, QuantumObject):
406
- return QuantumObject(self._data * other._data)
407
-
408
- raise TypeError("Unsupported multiplication type")
409
-
410
- def __matmul__(self, other: QuantumObject) -> QuantumObject:
411
- if isinstance(other, QuantumObject):
412
- return QuantumObject(self._data @ other._data)
413
-
414
- raise TypeError("Dot product is only supported between QuantumState instances")
415
-
416
- def __rmul__(self, other: QuantumObject | Complex) -> QuantumObject:
417
- return self.__mul__(other)
418
-
419
- def __repr__(self) -> str:
420
- return f"{self.dense}"
421
-
422
-
423
- ###############################################################################
424
- # Outside class Function Definitions
425
- ###############################################################################
426
-
427
-
428
- def basis_state(n: int, N: int) -> QuantumObject:
429
- """
430
- Generate the n'th basis vector representation, on a N-size Hilbert space (N=2**num_qubits).
431
-
432
- This function creates a column vector (ket) representing the Fock state |n⟩ in a Hilbert space of dimension N.
433
-
434
- Args:
435
- n (int): The desired number state (from 0 to N-1).
436
- N (int): The dimension of the Hilbert space, has a value 2**num_qubits.
437
-
438
- Returns:
439
- QuantumObject: A QuantumObject representing the |n⟩'th basis state on a N-size Hilbert space (N=2**num_qubits).
440
- """
441
- return QuantumObject(csc_array(([1], ([n], [0])), shape=(N, 1)))
442
-
443
-
444
- def ket(*state: int) -> QuantumObject:
445
- """
446
- Generate a ket state for a multi-qubit system.
447
-
448
- This function creates a tensor product of individual qubit states (kets) based on the input values.
449
- Each input must be either 0 or 1. For example, ket(0, 1) creates a two-qubit ket state |0⟩ ⊗ |1⟩.
450
-
451
- Args:
452
- *state (int): A sequence of integers representing the state of each qubit (0 or 1).
453
-
454
- Raises:
455
- ValueError: If any of the provided qubit states is not 0 or 1.
456
-
457
- Returns:
458
- QuantumObject: A QuantumObject representing the multi-qubit ket state.
459
- """
460
- if any(s not in {0, 1} for s in state):
461
- raise ValueError(f"the state can only contain 1s or 0s. But received: {state}")
462
-
463
- return tensor_prod([QuantumObject(csc_array(([1], ([s], [0])), shape=(2, 1))) for s in state])
464
-
465
-
466
- def bra(*state: int) -> QuantumObject:
467
- """
468
- Generate a bra state for a multi-qubit system.
469
-
470
- This function creates a tensor product of individual qubit states (bras) based on the input values.
471
- Each input must be either 0 or 1. For example, bra(0, 1) creates a two-qubit bra state ⟨0| ⊗ ⟨1|.
472
-
473
- Args:
474
- *state (int): A sequence of integers representing the state of each qubit (0 or 1).
475
-
476
- Raises:
477
- ValueError: If any of the provided qubit states is not 0 or 1.
478
-
479
- Returns:
480
- QuantumObject: A QuantumObject representing the multi-qubit bra state.
481
- """
482
- if any(s not in {0, 1} for s in state):
483
- raise ValueError(f"the state can only contain 1s or 0s. But received:: {state}")
484
-
485
- return tensor_prod([QuantumObject(csc_array(([1], ([0], [s])), shape=(1, 2))) for s in state])
486
-
487
-
488
- def tensor_prod(operators: list[QuantumObject]) -> QuantumObject:
489
- """
490
- Calculate the tensor product of a list of QuantumObjects.
491
-
492
- This function computes the tensor (Kronecker) product of all input QuantumObjects,
493
- resulting in a composite QuantumObject that represents the combined state or operator.
494
-
495
- Args:
496
- operators (list[QuantumObject]): A list of QuantumObjects to be combined via tensor product.
497
-
498
- Returns:
499
- QuantumObject: A new QuantumObject representing the tensor product of the inputs.
500
- """
501
- out = operators[0].data
502
- if len(operators) > 1:
503
- for i in range(1, len(operators)):
504
- out = kron(out, operators[i].data)
505
-
506
- return QuantumObject(out)
507
-
508
-
509
- def expect_val(operator: QuantumObject, state: QuantumObject) -> Complex:
510
- """
511
- Calculate the expectation value of an operator with respect to a quantum state.
512
-
513
- Computes the expectation value ⟨state| operator |state⟩. The function handles both
514
- pure state vectors and density matrices appropriately.
515
-
516
- Args:
517
- operator (QuantumObject): The quantum operator represented as a QuantumObject.
518
- state (QuantumObject): The quantum state or density matrix represented as a QuantumObject.
519
-
520
- Raises:
521
- ValueError: If the operator is not a square matrix.
522
-
523
- Returns:
524
- Complex: The expectation value. The result is guaranteed to be real if the operator
525
- is Hermitian, and may be complex otherwise.
526
- """
527
- if not operator.is_operator():
528
- raise ValueError("The operator must be a square matrix.")
529
-
530
- if state.data.shape[1] == state.data.shape[0]:
531
- return (operator @ state).dense.trace()
532
-
533
- return (state.adjoint() @ operator @ state).dense[0, 0]
@@ -1,90 +0,0 @@
1
- # Copyright 2025 Qilimanjaro Quantum Tech
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- from abc import ABC, abstractmethod
15
- from enum import Enum
16
-
17
- from qilisdk.digital.circuit import Circuit
18
- from qilisdk.digital.digital_result import DigitalResult
19
-
20
-
21
- class DigitalSimulationMethod(str, Enum):
22
- """
23
- Enumeration of available simulation methods for the CUDA backend.
24
- """
25
-
26
- STATE_VECTOR = "state_vector"
27
- TENSOR_NETWORK = "tensor_network"
28
- MATRIX_PRODUCT_STATE = "matrix_product_state"
29
-
30
-
31
- class DigitalBackend(ABC):
32
- """
33
- Abstract base class for digital quantum circuit backends.
34
-
35
- This abstract class defines the interface for a digital backend capable of executing a
36
- quantum circuit. Subclasses must implement the execute method to run the circuit with a
37
- specified number of measurement shots and return a DigitalResult encapsulating the measurement
38
- outcomes.
39
- """
40
-
41
- def __init__(
42
- self, digital_simulation_method: DigitalSimulationMethod = DigitalSimulationMethod.STATE_VECTOR
43
- ) -> None:
44
- """
45
- Initialize the DigitalBackend.
46
-
47
- Args:
48
- simulation_method (DigitalSimulationMethod, optional): The simulation method to use.
49
- Options include STATE_VECTOR, TENSOR_NETWORK, or MATRIX_PRODUCT_STATE.
50
- Defaults to STATE_VECTOR.
51
- """
52
- self._digital_simulation_method = digital_simulation_method
53
-
54
- @property
55
- def digital_simulation_method(self) -> DigitalSimulationMethod:
56
- """
57
- Get the simulation method currently configured for the backend.
58
-
59
- Returns:
60
- SimulationMethod: The simulation method to be used for circuit execution.
61
- """
62
- return self._digital_simulation_method
63
-
64
- @digital_simulation_method.setter
65
- def digital_simulation_method(self, value: DigitalSimulationMethod) -> None:
66
- """
67
- Set the simulation method for the backend.
68
-
69
- Args:
70
- value (SimulationMethod): The simulation method to set. Options include
71
- STATE_VECTOR, TENSOR_NETWORK, or MATRIX_PRODUCT_STATE.
72
- """
73
- self._digital_simulation_method = value
74
-
75
- @abstractmethod
76
- def execute(self, circuit: Circuit, nshots: int = 1000) -> DigitalResult:
77
- """
78
- Execute the provided quantum circuit and return the measurement results.
79
-
80
- This method should run the given circuit for the specified number of measurement shots and
81
- produce a DigitalResult instance containing the raw measurement samples and any computed
82
- probabilities.
83
-
84
- Args:
85
- circuit (Circuit): The quantum circuit to be executed.
86
- nshots (int, optional): The number of measurement shots to perform. Defaults to 1000.
87
-
88
- Returns:
89
- DigitalResult: The result of executing the circuit, including measurement samples and probabilities.
90
- """