qilisdk 0.1.4__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 +152 -159
  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.4.dist-info → qilisdk-0.1.5.dist-info}/METADATA +180 -134
  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 -596
  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.4.dist-info/RECORD +0 -51
  82. {qilisdk-0.1.4.dist-info → qilisdk-0.1.5.dist-info}/WHEEL +0 -0
  83. {qilisdk-0.1.4.dist-info → qilisdk-0.1.5.dist-info}/licenses/LICENCE +0 -0
@@ -0,0 +1,666 @@
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
+ from collections import defaultdict
17
+ from typing import Iterable, Literal
18
+
19
+ import numpy as np
20
+ from scipy.sparse import coo_matrix, csr_matrix, issparse, kron, sparray, spmatrix
21
+ from scipy.sparse.linalg import ArpackNoConvergence, eigsh, 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
+ def _is_pow2(n: int) -> bool:
30
+ return n > 0 and (n & (n - 1)) == 0
31
+
32
+
33
+ def _prod(xs: Iterable[int]) -> int:
34
+ p = 1
35
+ for x in xs:
36
+ p *= int(x)
37
+ return p
38
+
39
+
40
+ ###############################################################################
41
+ # Main Class Definition
42
+ ###############################################################################
43
+
44
+
45
+ @yaml.register_class
46
+ class QTensor:
47
+ """
48
+ Lightweight wrapper around sparse matrices representing quantum states or operators.
49
+
50
+ The QTensor class is a wrapper around sparse matrices (or NumPy arrays,
51
+ which are converted to sparse matrices) that represent quantum states (kets, bras, or density matrices)
52
+ or operators. It provides utility methods for common quantum operations such as
53
+ taking the adjoint (dagger), computing tensor products, partial traces, and norms.
54
+
55
+ The internal data is stored as a SciPy CSR (Compressed Sparse Row) matrix for
56
+ efficient arithmetic and manipulation. The expected shapes for the data are:
57
+ - (2**N, 2**N) for operators or density matrices (or scalars),
58
+ - (2**N, 1) for ket states,
59
+ - (1, 2**N) for bra states.
60
+
61
+ Converts a NumPy array to a CSR matrix if needed and validates the shape of the input.
62
+ The input must represent a valid quantum state or operator with appropriate dimensions.
63
+
64
+
65
+ Example:
66
+ .. code-block:: python
67
+
68
+ import numpy as np
69
+ from qilisdk.common import QTensor
70
+
71
+ ket = QTensor(np.array([[1.0], [0.0]]))
72
+ density = ket * ket.adjoint()
73
+ """
74
+
75
+ def __init__(self, data: np.ndarray | sparray | spmatrix) -> None:
76
+ """
77
+ Args:
78
+ data (np.ndarray | sparray | spmatrix): Dense or sparse matrix defining the quantum object. Expected
79
+ shapes are ``(2**N, 2**N)`` for operators, ``(2**N, 1)`` for kets, ``(1, 2**N)`` for bras, or ``(1, 1)`` for scalars.
80
+
81
+ Raises:
82
+ ValueError: If ``data`` is not 2-D or does not correspond to valid qubit dimensions.
83
+ """
84
+ if isinstance(data, np.ndarray):
85
+ if data.ndim != 2: # noqa: PLR2004
86
+ raise ValueError("Input ndarray must be 2D")
87
+ self._data = csr_matrix(data)
88
+ elif issparse(data):
89
+ self._data = data.tocsr()
90
+ else:
91
+ raise ValueError("Input must be a NumPy array or a SciPy sparse matrix")
92
+
93
+ r, c = self._data.shape
94
+
95
+ # Validate "qubit-like" shapes
96
+ valid = (
97
+ (r == c and _is_pow2(r)) # operator/density
98
+ or (c == 1 and _is_pow2(r)) # ket
99
+ or (r == 1 and _is_pow2(c)) # bra
100
+ or (r == c == 1) # scalar
101
+ )
102
+ if not valid:
103
+ raise ValueError(
104
+ f"Data must have shape (2**N, 2**N), (2**N, 1), (1, 2**N), or (1,1). Got {self._data.shape}."
105
+ )
106
+ # Cache nqubits (immutable once constructed since we never resize in-place)
107
+ if r == c:
108
+ self._nqubits = int(np.log2(r))
109
+ elif r == 1:
110
+ self._nqubits = int(np.log2(c))
111
+ else:
112
+ self._nqubits = int(np.log2(r))
113
+
114
+ # ------------- Properties --------------
115
+
116
+ @property
117
+ def data(self) -> csr_matrix:
118
+ """
119
+ Get the internal sparse matrix representation of the QTensor.
120
+
121
+ Returns:
122
+ csc_matrix: The internal representation as a CSR matrix.
123
+ """
124
+ return self._data
125
+
126
+ @property
127
+ def nqubits(self) -> int:
128
+ """
129
+ Compute the number of qubits represented by the QTensor.
130
+
131
+ Returns:
132
+ int: The number of qubits if determinable; otherwise, -1.
133
+ """
134
+ return self._nqubits
135
+
136
+ @property
137
+ def shape(self) -> tuple[int, int]:
138
+ """
139
+ Get the shape of the QTensor's internal matrix.
140
+
141
+ Returns:
142
+ tuple[int, ...]: The shape of the internal matrix.
143
+ """
144
+ return self._data.shape
145
+
146
+ @property
147
+ def dense(self) -> np.ndarray:
148
+ """
149
+ Get the dense (NumPy array) representation of the QTensor.
150
+
151
+ Returns:
152
+ np.ndarray: The dense array representation.
153
+ """
154
+ return self._data.toarray()
155
+
156
+ # ------------- Basic structural tests -------------
157
+
158
+ def is_ket(self) -> bool:
159
+ r, c = self.shape
160
+ return c == 1 and _is_pow2(r)
161
+
162
+ def is_bra(self) -> bool:
163
+ r, c = self.shape
164
+ return r == 1 and _is_pow2(c)
165
+
166
+ def is_scalar(self) -> bool:
167
+ return self.shape == (1, 1)
168
+
169
+ def is_operator(self) -> bool:
170
+ r, c = self.shape
171
+ return r == c and _is_pow2(r)
172
+
173
+ # ----------- Matrix Operations ------------
174
+
175
+ def adjoint(self) -> QTensor:
176
+ """
177
+ Compute the adjoint (conjugate transpose) of the QTensor.
178
+
179
+ Returns:
180
+ QTensor: A new QTensor that is the adjoint of this object.
181
+ """
182
+ return QTensor(self._data.getH())
183
+
184
+ def trace(self) -> complex:
185
+ # diagonal() returns dense 1D array; summing it is cheap
186
+ return complex(self._data.diagonal().sum())
187
+
188
+ def ptrace(self, keep: list[int], dims: list[int] | None = None) -> QTensor:
189
+ """
190
+ Compute the partial trace over subsystems not in 'keep'.
191
+
192
+ This method calculates the reduced density matrix by tracing out
193
+ the subsystems that are not specified in the 'keep' parameter.
194
+ The input 'dims' represents the dimensions of each subsystem (optional),
195
+ and 'keep' indicates the indices of those subsystems to be retained.
196
+
197
+ If the QTensor is a ket or bra, it will first be converted to a density matrix.
198
+
199
+ Args:
200
+ keep (list[int]): A list of indices corresponding to the subsystems to retain.
201
+ The order of the indices in 'keep' is not important, since dimensions will
202
+ be returned in the tensor original order, but the indices must be unique.
203
+ dims (list[int], optional): A list specifying the dimensions of each subsystem.
204
+ If not specified, a density matrix of qubit states is assumed, and the
205
+ dimensions are inferred accordingly (i.e. we split the state in dim 2 states).
206
+
207
+ Raises:
208
+ ValueError: If the product of the dimensions in dims does not match the
209
+ shape of the QTensor's dense representation or if any dimension is non-positive.
210
+ ValueError: If the indices in 'keep' are not unique or are out of range.
211
+ ValueError: If the QTensor is not a valid density matrix or state vector.
212
+ ValueError: If the number of subsystems exceeds the available ASCII letters.
213
+
214
+ Returns:
215
+ QTensor: A new QTensor representing the reduced density matrix
216
+ for the subsystems specified in 'keep'.
217
+ """
218
+ if dims is None:
219
+ dims = [2] * self.nqubits
220
+ if any(d <= 0 for d in dims):
221
+ raise ValueError("All subsystem dimensions must be positive")
222
+
223
+ dim_total = self.shape[0] if self.is_operator() else (self.shape[0] if self.is_ket() else self.shape[1])
224
+ if _prod(dims) != dim_total:
225
+ raise ValueError(
226
+ f"Product of dims {dims} = {_prod(dims)} does not match Hilbert space dimension {dim_total}."
227
+ )
228
+
229
+ nsub = len(dims)
230
+ keep_set = set(keep)
231
+ if len(keep_set) != len(keep):
232
+ raise ValueError("Duplicate indices in keep")
233
+ if any(i < 0 or i >= nsub for i in keep_set):
234
+ raise ValueError("keep indices out of range")
235
+
236
+ keep_idx = sorted(keep_set) # return order is “original” ordering
237
+ drop_idx = [i for i in range(nsub) if i not in keep_set]
238
+ dims_keep = [dims[i] for i in keep_idx]
239
+ dims_drop = [dims[i] for i in drop_idx]
240
+ Kdim = _prod(dims_keep) if dims_keep else 1
241
+
242
+ # Pure-state path ψ ⇒ p_keep = M @ M† (no NxN density matrix created)
243
+ if self.is_ket() or self.is_bra():
244
+ psi = self._data
245
+ if self.is_bra():
246
+ psi = psi.T.conj() # make it a column vector
247
+ # Decide whether to process as dense vector or sparse-vector path
248
+ N = _prod(dims)
249
+ density = psi.nnz / N
250
+ # Dense vector path is faster once the vector is reasonably filled
251
+ if density >= 0.05 or N <= (1 << 20): # noqa: PLR2004
252
+ psi1d = np.asarray(psi.toarray().reshape(-1)) # only vector, not matrix
253
+ psi_nd = psi1d.reshape(dims)
254
+ perm = keep_idx + drop_idx # bring keep first
255
+ psi_perm = np.transpose(psi_nd, perm)
256
+ M = psi_perm.reshape(Kdim, -1)
257
+ rho_keep = M @ M.conj().T
258
+ return QTensor(csr_matrix(rho_keep))
259
+ # Truly sparse ψ: build M implicitly by grouping by the traced index
260
+ coo = psi.tocoo()
261
+ nz_idx = coo.row
262
+ nz_val = coo.data
263
+ # unravel all non-zero positions once
264
+ digits = np.vstack(np.unravel_index(nz_idx, dims)) # (nsub, nnz)
265
+ k_digits = digits[keep_idx, :] if keep_idx else np.zeros((0, nz_val.size), dtype=int)
266
+ t_digits = digits[drop_idx, :] if drop_idx else np.zeros((0, nz_val.size), dtype=int)
267
+ k_lin = np.ravel_multi_index(k_digits, dims_keep) if keep_idx else np.zeros(nz_val.size, dtype=int) # type: ignore[call-overload]
268
+ t_lin = np.ravel_multi_index(t_digits, dims_drop) if drop_idx else np.zeros(nz_val.size, dtype=int) # type: ignore[call-overload]
269
+
270
+ # For each traced index t, accumulate outer products of the K-dimensional slice
271
+ buckets: dict[int, list[tuple[int, complex]]] = defaultdict(list)
272
+ for kl, tl, v in zip(k_lin, t_lin, nz_val):
273
+ buckets[int(tl)].append((int(kl), v))
274
+
275
+ data, row, col = [], [], []
276
+ for _, items in buckets.items():
277
+ # x is (Kdim,) sparse vector represented by indices & values
278
+ ks = np.fromiter((i for i, _ in items), dtype=int)
279
+ vs = np.fromiter((v for _, v in items), dtype=complex)
280
+ # Outer product of this slice: accumulate into COO lists
281
+ # Note: number of pairs is len(items)^2 which is fine for very sparse ψ.
282
+ r = np.repeat(ks, ks.size)
283
+ c = np.tile(ks, ks.size)
284
+ d = (vs[:, None] * np.conj(vs[None, :])).ravel()
285
+ row.append(r)
286
+ col.append(c)
287
+ data.append(d)
288
+
289
+ if data:
290
+ row = np.concatenate(row)
291
+ col = np.concatenate(col)
292
+ data = np.concatenate(data)
293
+ out = coo_matrix((data, (row, col)), shape=(Kdim, Kdim))
294
+ out.sum_duplicates()
295
+ return QTensor(out.tocsr())
296
+ return QTensor(csr_matrix((Kdim, Kdim)))
297
+
298
+ # Operator/density-matrix path: COO remapping with traced-equal mask
299
+ if self.is_operator():
300
+ rho = self._data.tocoo()
301
+ # unravel rows/cols to multi-indices
302
+ r_multi = np.vstack(np.unravel_index(rho.row, dims)) # (nsub, nnz)
303
+ c_multi = np.vstack(np.unravel_index(rho.col, dims))
304
+ if drop_idx:
305
+ mask = np.all(r_multi[drop_idx, :] == c_multi[drop_idx, :], axis=0)
306
+ else:
307
+ mask = np.ones(rho.nnz, dtype=bool)
308
+
309
+ if keep_idx:
310
+ rK = r_multi[keep_idx, :][:, mask]
311
+ cK = c_multi[keep_idx, :][:, mask]
312
+ new_r = np.ravel_multi_index(rK, dims_keep) # type: ignore[call-overload]
313
+ new_c = np.ravel_multi_index(cK, dims_keep) # type: ignore[call-overload]
314
+ data = rho.data[mask]
315
+ else:
316
+ # keep nothing → scalar
317
+ new_r = np.zeros(mask.sum(), dtype=int)
318
+ new_c = np.zeros(mask.sum(), dtype=int)
319
+ data = rho.data[mask]
320
+
321
+ out = coo_matrix((data, (new_r, new_c)), shape=(Kdim, Kdim))
322
+ out.sum_duplicates()
323
+ return QTensor(out.tocsr())
324
+
325
+ raise ValueError("The QTensor is not a valid state or operator for ptrace().")
326
+
327
+ def norm(self, order: int | Literal["fro", "tr"] = 1) -> float:
328
+ """
329
+ Compute the norm of the QTensor.
330
+
331
+ For density matrices, the norm order can be specified. For state vectors, the norm is computed accordingly.
332
+
333
+ Args:
334
+ order (int or {"fro", "tr"}, optional): The order of the norm.
335
+ Only applies if the QTensor represents a density matrix. Other than all the
336
+ orders accepted by scipy, it also accepts 'tr' for the trace norm. Defaults to 1.
337
+
338
+ Raises:
339
+ ValueError: If the QTensor is not a valid density matrix or state vector,
340
+
341
+ Returns:
342
+ float: The computed norm of the QTensor.
343
+ """
344
+ if self.is_scalar():
345
+ return float(abs(self._data.toarray()[0, 0]))
346
+
347
+ if self.is_operator():
348
+ if order == "tr":
349
+ if self.is_density_matrix():
350
+ return 1.0
351
+ # Only correct for Hermitian; otherwise, nuclear norm requires SVD
352
+ if self.is_hermitian():
353
+ r, _ = self.shape
354
+ if r <= 1024: # noqa: PLR2004
355
+ w = np.linalg.eigvalsh(self._data.toarray())
356
+ return float(np.sum(np.abs(w)))
357
+ raise ValueError("Trace norm for large Hermitian operators is not implemented without densifying.")
358
+ r, _ = self.shape
359
+ if r <= 1024: # noqa: PLR2004
360
+ s = np.linalg.svd(self._data.toarray(), compute_uv=False)
361
+ return float(np.sum(s))
362
+ raise ValueError(
363
+ "Trace (nuclear) norm for large non-Hermitian operators requires SVD; not implemented."
364
+ )
365
+ # Delegate other norms to SciPy; supported: 'fro', 1, np.inf, etc.
366
+ return float(scipy_norm(self._data, ord=order))
367
+
368
+ # kets/bras: 2-norm of vector
369
+ v = self._data
370
+ if self.is_bra():
371
+ v = v.T.conj()
372
+ return float(np.sqrt(np.real(v.conj().multiply(v).sum())))
373
+
374
+ def unit(self, order: int | Literal["fro", "tr"] = "tr") -> QTensor:
375
+ """
376
+ Normalize the QTensor.
377
+
378
+ Scales the QTensor so that its norm becomes 1, according to the specified norm order.
379
+
380
+ Args:
381
+ order (int or {"fro", "tr"}, optional): The order of the norm to use for normalization.
382
+ Only applies if the QTensor represents a density matrix. Other than all the
383
+ orders accepted by scipy, it also accepts 'tr' for the trace norm. Defaults to "tr".
384
+
385
+ Raises:
386
+ ValueError: If the norm of the QTensor is 0, making normalization impossible.
387
+
388
+ Returns:
389
+ QTensor: A new QTensor that is the normalized version of this object.
390
+ """
391
+ norm = self.norm(order=order)
392
+ if norm == 0:
393
+ raise ValueError("Cannot normalize a zero-norm Quantum Object")
394
+
395
+ return QTensor(self._data / norm)
396
+
397
+ def expm(self) -> QTensor:
398
+ """
399
+ Compute the matrix exponential of the QTensor.
400
+
401
+ Returns:
402
+ QTensor: A new QTensor representing the matrix exponential.
403
+ """
404
+ return QTensor(expm(self._data.tocsc()))
405
+
406
+ def to_density_matrix(self) -> QTensor:
407
+ """
408
+ Convert the QTensor to a density matrix.
409
+
410
+ If the QTensor represents a state vector (ket or bra), this method
411
+ calculates the corresponding density matrix by taking the outer product.
412
+ If the QTensor is already a density matrix, it is returned unchanged.
413
+ The resulting density matrix is normalized.
414
+
415
+ Raises:
416
+ ValueError: If the QTensor is a scalar, as a density matrix cannot be derived.
417
+ ValueError: If the QTensor is an operator that is not a density matrix.
418
+
419
+ Returns:
420
+ QTensor: A new QTensor representing the density matrix.
421
+ """
422
+ if self.is_scalar():
423
+ raise ValueError("Cannot make a density matrix from a scalar.")
424
+ if self.is_density_matrix():
425
+ return self
426
+ if self.is_ket():
427
+ rho = self @ self.adjoint()
428
+ elif self.is_bra():
429
+ rho = self.adjoint() @ self
430
+ elif self.is_operator():
431
+ raise ValueError("Operator is not a density matrix (trace≠1 or not Hermitian).")
432
+ else:
433
+ raise ValueError("Invalid object for density matrix conversion.")
434
+
435
+ tr = float(np.real(rho.trace()))
436
+ if tr == 0.0:
437
+ raise ValueError("Cannot normalize density matrix with zero trace.")
438
+ return QTensor(rho.data / tr) # keep it sparse
439
+
440
+ def is_density_matrix(self, tol: float = 1e-8) -> bool:
441
+ """
442
+ Determine if the QTensor is a valid density matrix.
443
+
444
+ A valid density matrix must be square, Hermitian, positive semi-definite, and have a trace equal to 1.
445
+
446
+ Args:
447
+ tol (float, optional): The numerical tolerance for verifying Hermiticity,
448
+ eigenvalue non-negativity, and trace. Defaults to 1e-8.
449
+
450
+ Returns:
451
+ bool: True if the QTensor is a valid density matrix, False otherwise.
452
+ """
453
+ # Check if rho is a square matrix
454
+ if not self.is_operator():
455
+ return False
456
+ if abs(self.trace() - 1.0) > tol:
457
+ return False
458
+ if not self.is_hermitian(tol=tol):
459
+ return False
460
+ # PSD check via smallest eigenvalue of Hermitian matrix
461
+ try:
462
+ vals = eigsh(self._data, k=1, which="SA", return_eigenvectors=False, tol=1e-6)
463
+ lam_min = float(np.real(vals[0]))
464
+ except ArpackNoConvergence:
465
+ # If ARPACK fails, fall back to dense only if small
466
+ r, _ = self.shape
467
+ if r <= 2048: # noqa: PLR2004
468
+ lam_min = float(np.linalg.eigvalsh(self._data.toarray()).min())
469
+ else:
470
+ # Conservative fallback: don't claim it's a DM if we can't certify PSD
471
+ return False
472
+ return lam_min >= -tol
473
+
474
+ def is_hermitian(self, tol: float = 1e-8) -> bool:
475
+ """
476
+ Check if the QTensor is Hermitian.
477
+
478
+ Args:
479
+ tol (float, optional): The numerical tolerance for verifying Hermiticity.
480
+ Defaults to 1e-8.
481
+
482
+ Returns:
483
+ bool: True if the QTensor is Hermitian, False otherwise.
484
+ """
485
+ if not self.is_operator():
486
+ return False
487
+ diff = self._data - self._data.getH()
488
+ if diff.nnz == 0:
489
+ return True
490
+ return float(scipy_norm(diff, ord="fro")) <= tol
491
+
492
+ # ----------- Basic Arithmetic Operators ------------
493
+
494
+ def __add__(self, other: QTensor | Complex) -> QTensor:
495
+ if isinstance(other, QTensor):
496
+ return QTensor(self._data + other._data)
497
+ if other == 0:
498
+ return self
499
+ return NotImplemented
500
+
501
+ def __radd__(self, other: QTensor | Complex) -> QTensor:
502
+ return self.__add__(other)
503
+
504
+ def __sub__(self, other: QTensor) -> QTensor:
505
+ if isinstance(other, QTensor):
506
+ return QTensor(self._data - other._data)
507
+ return NotImplemented
508
+
509
+ def __mul__(self, other: QTensor | Complex) -> QTensor:
510
+ if isinstance(other, (int, float, complex)):
511
+ return QTensor(self._data * other)
512
+ if isinstance(other, QTensor):
513
+ return QTensor(self._data * other._data)
514
+ return NotImplemented
515
+
516
+ def __matmul__(self, other: QTensor) -> QTensor:
517
+ if isinstance(other, QTensor):
518
+ return QTensor(self._data @ other._data)
519
+ return NotImplemented
520
+
521
+ def __rmul__(self, other: QTensor | Complex) -> QTensor:
522
+ return self.__mul__(other)
523
+
524
+ def __repr__(self) -> str:
525
+ r, c = self.shape
526
+ nnz = self._data.nnz
527
+ s = f"QTensor(shape={r}x{c}, nnz={nnz}, format='csr')"
528
+ if r * c <= 64: # noqa: PLR2004
529
+ s += f"\n{self._data.toarray()}"
530
+ return s
531
+
532
+
533
+ ###############################################################################
534
+ # Outside class Function Definitions
535
+ ###############################################################################
536
+
537
+
538
+ def basis_state(n: int, N: int) -> QTensor:
539
+ r"""
540
+ Generate the n'th basis vector representation, on a N-size Hilbert space (N=2**num_qubits).
541
+
542
+ This function creates a column vector (ket) representing the Fock state \|n⟩ in a Hilbert space of dimension N.
543
+
544
+ Args:
545
+ n (int): The desired number state (from 0 to N-1).
546
+ N (int): The dimension of the Hilbert space, has a value 2**num_qubits.
547
+
548
+ Raises:
549
+ ValueError: If n >= N.
550
+
551
+ Returns:
552
+ QTensor: A QTensor representing the \|n⟩'th basis state on a N-size Hilbert space (N=2**num_qubits).
553
+ """
554
+ if not (0 <= n < N):
555
+ raise ValueError(f"n must be in [0, {N - 1}]")
556
+ # one nonzero at (row=n, col=0), value=1.0
557
+ mat = csr_matrix(([1.0], ([n], [0])), shape=(N, 1))
558
+ return QTensor(mat)
559
+
560
+
561
+ def ket(*state: int) -> QTensor:
562
+ r"""
563
+ Generate a ket state for a multi-qubit system.
564
+
565
+ This function creates a tensor product of individual qubit states (kets) based on the input values.
566
+ Each input must be either 0 or 1. For example, ket(0, 1) creates a two-qubit ket state \|0⟩ ⊗ \|1⟩.
567
+
568
+ Args:
569
+ *state (int): A sequence of integers representing the state of each qubit (0 or 1).
570
+
571
+ Raises:
572
+ ValueError: If any of the provided qubit states is not 0 or 1.
573
+
574
+ Returns:
575
+ QTensor: A QTensor representing the multi-qubit ket state.
576
+ """
577
+ if not state:
578
+ raise ValueError("ket() requires at least one qubit (0/1).")
579
+ if any(s not in {0, 1} for s in state):
580
+ raise ValueError(f"state must contain only 0s/1s, got {state}")
581
+
582
+ # Number of qubits
583
+ n = len(state)
584
+ N = 1 << n # 2**n
585
+
586
+ # Big-endian linear index: kron(|s0>, |s1>, ..., |s_{n-1}>) -> index int(s0...s_{n-1}, base=2)
587
+ idx = 0
588
+ for s in state:
589
+ idx = (idx << 1) | s
590
+
591
+ # Reuse existing basis_state creator (sparse, single 1 at (idx, 0))
592
+ return basis_state(idx, N)
593
+
594
+
595
+ def bra(*state: int) -> QTensor:
596
+ r"""
597
+ Generate a bra state for a multi-qubit system.
598
+
599
+ This function creates a tensor product of individual qubit states (bras) based on the input values.
600
+ Each input must be either 0 or 1. For example, bra(0, 1) creates a two-qubit bra state ⟨0\| ⊗ ⟨1\|.
601
+
602
+ Args:
603
+ *state (int): A sequence of integers representing the state of each qubit (0 or 1).
604
+
605
+ Returns:
606
+ QTensor: A QTensor representing the multi-qubit bra state.
607
+ """
608
+ return ket(*state).adjoint()
609
+
610
+
611
+ def tensor_prod(operators: list[QTensor]) -> QTensor:
612
+ """
613
+ Calculate the tensor product of a list of QTensors.
614
+
615
+ This function computes the tensor (Kronecker) product of all input QTensors,
616
+ resulting in a composite QTensor that represents the combined state or operator.
617
+
618
+ Args:
619
+ operators (list[QTensor]): A list of QTensors to be combined via tensor product.
620
+
621
+ Returns:
622
+ QTensor: A new QTensor representing the tensor product of the inputs.
623
+
624
+ Raises:
625
+ ValueError: If operators list is empty.
626
+ """
627
+ if not operators:
628
+ raise ValueError("tensor_prod requires at least one operator/state")
629
+ out = operators[0].data
630
+ for op in operators[1:]:
631
+ # Sparse kron returns same sparse type; keep CSR at the end
632
+ out = kron(out, op.data).tocsr()
633
+ return QTensor(out)
634
+
635
+
636
+ def expect_val(operator: QTensor, state: QTensor) -> Complex:
637
+ r"""
638
+ Calculate the expectation value of an operator with respect to a quantum state.
639
+
640
+ Computes the expectation value ⟨state\| operator \|state⟩. The function handles both
641
+ pure state vectors and density matrices appropriately.
642
+
643
+ Args:
644
+ operator (QTensor): The quantum operator represented as a QTensor.
645
+ state (QTensor): The quantum state or density matrix represented as a QTensor.
646
+
647
+ Raises:
648
+ ValueError: If the operator is not a square matrix.
649
+ ValueError: If the state provided is not a valid quantum state.
650
+
651
+ Returns:
652
+ Complex: The expectation value. The result is guaranteed to be real if the operator is Hermitian, and may be complex otherwise.
653
+ """
654
+ if not operator.is_operator():
655
+ raise ValueError("operator must be square")
656
+ # p case: tr(O p) = sum((O.T) ⊙ p)
657
+ if state.is_density_matrix():
658
+ return complex((operator.data.T.multiply(state.data)).sum())
659
+ # |ψ⟩ case: ⟨ψ| O |ψ⟩ = (ψ† (O ψ))
660
+ if state.is_ket():
661
+ v = operator.data @ state.data # (N,1)
662
+ return complex((state.data.getH() @ v).toarray()[0, 0])
663
+ if state.is_bra():
664
+ v = state.data @ operator.data # (1,N)
665
+ return complex((v @ state.data.getH()).toarray()[0, 0])
666
+ raise ValueError("state is invalid for expect_val")
qilisdk/common/result.py CHANGED
@@ -14,4 +14,5 @@
14
14
  from abc import ABC
15
15
 
16
16
 
17
- class Result(ABC): ...
17
+ class Result(ABC):
18
+ """Marker base class for results produced by QiliSDK workflows."""