-
Notifications
You must be signed in to change notification settings - Fork 558
/
observables.py
714 lines (533 loc) · 25.8 KB
/
observables.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# Copyright 2018-2021 Xanadu Quantum Technologies Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This submodule contains the discrete-variable quantum observables,
excepting the Pauli gates and Hadamard gate in ``non_parametric_ops.py``.
"""
from copy import copy
from collections.abc import Sequence
import numpy as np
from scipy.sparse import csr_matrix
import pennylane as qml
from pennylane.operation import AnyWires, Observable
from pennylane.wires import Wires
from .matrix_ops import QubitUnitary
class Hermitian(Observable):
r"""
An arbitrary Hermitian observable.
For a Hermitian matrix :math:`A`, the expectation command returns the value
.. math::
\braket{A} = \braketT{\psi}{\cdots \otimes I\otimes A\otimes I\cdots}{\psi}
where :math:`A` acts on the requested wires.
If acting on :math:`N` wires, then the matrix :math:`A` must be of size
:math:`2^N\times 2^N`.
**Details:**
* Number of wires: Any
* Number of parameters: 1
* Gradient recipe: None
Args:
A (array or Sequence): square hermitian matrix
wires (Sequence[int] or int): the wire(s) the operation acts on
id (str or None): String representing the operation (optional)
"""
num_wires = AnyWires
num_params = 1
"""int: Number of trainable parameters that the operator depends on."""
grad_method = "F"
# Qubit case
_num_basis_states = 2
_eigs = {}
def __init__(self, A, wires, id=None):
A = np.array(A) if isinstance(A, list) else A
if not qml.math.is_abstract(A):
if isinstance(wires, Sequence) and not isinstance(wires, str):
if len(wires) == 0:
raise ValueError(
"Hermitian: wrong number of wires. At least one wire has to be given."
)
expected_mx_shape = self._num_basis_states ** len(wires)
else:
# Assumably wires is an int; further validation checks are performed by calling super().__init__
expected_mx_shape = self._num_basis_states
Hermitian._validate_input(A, expected_mx_shape)
super().__init__(A, wires=wires, id=id)
@staticmethod
def _validate_input(A, expected_mx_shape=None):
"""Validate the input matrix."""
if len(A.shape) != 2 or A.shape[0] != A.shape[1]:
raise ValueError("Observable must be a square matrix.")
if expected_mx_shape is not None and A.shape[0] != expected_mx_shape:
raise ValueError(
f"Expected input matrix to have shape {expected_mx_shape}x{expected_mx_shape}, but "
f"a matrix with shape {A.shape[0]}x{A.shape[0]} was passed."
)
if not qml.math.allclose(A, qml.math.T(qml.math.conj(A))):
raise ValueError("Observable must be Hermitian.")
def label(self, decimals=None, base_label=None, cache=None):
return super().label(decimals=decimals, base_label=base_label or "𝓗", cache=cache)
@staticmethod
def compute_matrix(A): # pylint: disable=arguments-differ
r"""Representation of the operator as a canonical matrix in the computational basis (static method).
The canonical matrix is the textbook matrix representation that does not consider wires.
Implicitly, this assumes that the wires of the operator correspond to the global wire order.
.. seealso:: :meth:`~.Hermitian.matrix`
Args:
A (tensor_like): hermitian matrix
Returns:
tensor_like: canonical matrix
**Example**
>>> A = np.array([[6+0j, 1-2j],[1+2j, -1]])
>>> qml.Hermitian.compute_matrix(A)
[[ 6.+0.j 1.-2.j]
[ 1.+2.j -1.+0.j]]
"""
A = qml.math.asarray(A)
Hermitian._validate_input(A)
return A
@property
def eigendecomposition(self):
"""Return the eigendecomposition of the matrix specified by the Hermitian observable.
This method uses pre-stored eigenvalues for standard observables where
possible and stores the corresponding eigenvectors from the eigendecomposition.
It transforms the input operator according to the wires specified.
Returns:
dict[str, array]: dictionary containing the eigenvalues and the eigenvectors of the Hermitian observable
"""
Hmat = self.matrix()
Hmat = qml.math.to_numpy(Hmat)
Hkey = tuple(Hmat.flatten().tolist())
if Hkey not in Hermitian._eigs:
w, U = np.linalg.eigh(Hmat)
Hermitian._eigs[Hkey] = {"eigvec": U, "eigval": w}
return Hermitian._eigs[Hkey]
def eigvals(self):
"""Return the eigenvalues of the specified Hermitian observable.
This method uses pre-stored eigenvalues for standard observables where
possible and stores the corresponding eigenvectors from the eigendecomposition.
Returns:
array: array containing the eigenvalues of the Hermitian observable
"""
return self.eigendecomposition["eigval"]
@staticmethod
def compute_diagonalizing_gates(eigenvectors, wires): # pylint: disable=arguments-differ
r"""Sequence of gates that diagonalize the operator in the computational basis (static method).
Given the eigendecomposition :math:`O = U \Sigma U^{\dagger}` where
:math:`\Sigma` is a diagonal matrix containing the eigenvalues,
the sequence of diagonalizing gates implements the unitary :math:`U^{\dagger}`.
The diagonalizing gates rotate the state into the eigenbasis
of the operator.
.. seealso:: :meth:`~.Hermitian.diagonalizing_gates`.
Args:
eigenvectors (array): eigenvectors of the operator, as extracted from op.eigendecomposition["eigvec"].
wires (Iterable[Any], Wires): wires that the operator acts on
Returns:
list[.Operator]: list of diagonalizing gates
**Example**
>>> A = np.array([[-6, 2 + 1j], [2 - 1j, 0]])
>>> _, evecs = np.linalg.eigh(A)
>>> qml.Hermitian.compute_diagonalizing_gates(evecs, wires=[0])
[QubitUnitary(tensor([[-0.94915323-0.j, 0.2815786 +0.1407893j ],
[ 0.31481445-0.j, 0.84894846+0.42447423j]], requires_grad=True), wires=[0])]
"""
return [QubitUnitary(eigenvectors.conj().T, wires=wires)]
def diagonalizing_gates(self):
"""Return the gate set that diagonalizes a circuit according to the
specified Hermitian observable.
Returns:
list: list containing the gates diagonalizing the Hermitian observable
"""
# note: compute_diagonalizing_gates has a custom signature, which is why we overwrite this method
return self.compute_diagonalizing_gates(self.eigendecomposition["eigvec"], self.wires)
class SparseHamiltonian(Observable):
r"""
A Hamiltonian represented directly as a sparse matrix in Compressed Sparse Row (CSR) format.
.. warning::
``SparseHamiltonian`` observables can only be used to return expectation values.
Variances and samples are not supported.
**Details:**
* Number of wires: Any
* Number of parameters: 1
* Gradient recipe: None
Args:
H (csr_matrix): a sparse matrix in SciPy Compressed Sparse Row (CSR) format with
dimension :math:`(2^n, 2^n)`, where :math:`n` is the number of wires.
wires (Sequence[int]): the wire(s) the operation acts on
id (str or None): String representing the operation (optional)
**Example**
Sparse Hamiltonians can be constructed directly with a SciPy-compatible sparse matrix.
Alternatively, you can construct your Hamiltonian as usual using :class:`~.Hamiltonian`, and then use
:meth:`~.Hamiltonian.sparse_matrix` to construct the sparse matrix that serves as the input
to ``SparseHamiltonian``:
>>> wires = range(20)
>>> coeffs = [1 for _ in wires]
>>> observables = [qml.PauliZ(i) for i in wires]
>>> H = qml.Hamiltonian(coeffs, observables)
>>> Hmat = H.sparse_matrix()
>>> H_sparse = qml.SparseHamiltonian(Hmat, wires)
"""
num_wires = AnyWires
num_params = 1
"""int: Number of trainable parameters that the operator depends on."""
grad_method = None
def __init__(self, H, wires=None, id=None):
if not isinstance(H, csr_matrix):
raise TypeError("Observable must be a scipy sparse csr_matrix.")
super().__init__(H, wires=wires, id=id)
mat_len = 2 ** len(self.wires)
if H.shape != (mat_len, mat_len):
raise ValueError(
f"Sparse Matrix must be of shape ({mat_len}, {mat_len}). Got {H.shape}."
)
def label(self, decimals=None, base_label=None, cache=None):
return super().label(decimals=decimals, base_label=base_label or "𝓗", cache=cache)
@staticmethod
def compute_matrix(H): # pylint: disable=arguments-differ
r"""Representation of the operator as a canonical matrix in the computational basis (static method).
The canonical matrix is the textbook matrix representation that does not consider wires.
Implicitly, this assumes that the wires of the operator correspond to the global wire order.
.. seealso:: :meth:`~.SparseHamiltonian.matrix`
This method returns a dense matrix. For a sparse matrix representation, see
:meth:`~.SparseHamiltonian.compute_sparse_matrix`.
Args:
H (scipy.sparse.csr_matrix): sparse matrix used to create the operator
Returns:
array: dense matrix
**Example**
>>> from scipy.sparse import csr_matrix
>>> H = np.array([[6+0j, 1-2j],[1+2j, -1]])
>>> H = csr_matrix(H)
>>> res = qml.SparseHamiltonian.compute_matrix(H)
>>> res
[[ 6.+0.j 1.-2.j]
[ 1.+2.j -1.+0.j]]
>>> type(res)
<class 'numpy.ndarray'>
"""
return H.toarray()
@staticmethod
def compute_sparse_matrix(H): # pylint: disable=arguments-differ
r"""Representation of the operator as a sparse canonical matrix in the computational basis (static method).
The canonical matrix is the textbook matrix representation that does not consider wires.
Implicitly, this assumes that the wires of the operator correspond to the global wire order.
.. seealso:: :meth:`~.SparseHamiltonian.sparse_matrix`
This method returns a sparse matrix. For a dense matrix representation, see
:meth:`~.SparseHamiltonian.compute_matrix`.
Args:
H (scipy.sparse.csr_matrix): sparse matrix used to create the operator
Returns:
scipy.sparse.csr_matrix: sparse matrix
**Example**
>>> from scipy.sparse import csr_matrix
>>> H = np.array([[6+0j, 1-2j],[1+2j, -1]])
>>> H = csr_matrix(H)
>>> res = qml.SparseHamiltonian.compute_sparse_matrix(H)
>>> res
(0, 0) (6+0j)
(0, 1) (1-2j)
(1, 0) (1+2j)
(1, 1) (-1+0j)
>>> type(res)
<class 'scipy.sparse.csr_matrix'>
"""
return H
class Projector(Observable):
r"""Projector(state, wires, id=None)
Observable corresponding to the state projector :math:`P=\ket{\phi}\bra{\phi}`.
The expectation of this observable returns the value
.. math::
|\langle \psi | \phi \rangle |^2
corresponding to the probability that :math:`|\psi\rangle` is projected onto :math:`|\phi\rangle` during measurement.
**Details:**
* Number of wires: Any
* Number of parameters: 1
* Gradient recipe: None
Args:
state (tensor-like): Input state of shape ``(n,)`` for a basis-state projector, or ``(2**n,)``
for a state-vector projector.
wires (Iterable): wires that the projector acts on.
id (str or None): String representing the operation (optional).
**Example**
In the following example we consider projectors over two states: the :math:`|00\rangle` and the
:math:`|++\rangle`. Since the first one is in the computational basis, we create its projector
directly from its basis state representation, which is, ``zero_state=[0, 0]``. For the latter,
we need to use its state vector form ``plusplus_state=np.array([1, 1, 1, 1])/2``.
.. code-block::
>>> dev = qml.device("default.qubit", wires=2)
>>> @qml.qnode(dev)
... def circuit(state):
... return qml.expval(qml.Projector(state, wires=[0, 1]))
>>> zero_state = [0, 0]
>>> circuit(zero_state)
1.
>>> plusplus_state = np.array([1, 1, 1, 1]) / 2
>>> circuit(plusplus_state)
0.25
"""
name = "Projector"
num_wires = AnyWires
num_params = 1
"""int: Number of trainable parameters that the operator depends on."""
def __new__(cls, state, wires, **_):
"""Changes parents based on the state representation.
Though all the types will be named "Projector", their *identity* and location in memory
will be different based on whether the input state is a basis state or a state vector.
We cache the different types in private class variables so that:
>>> Projector(state, wires).__class__ is Projector(state, wires).__class__
True
>>> type(Projector(state, wries)) == type(Projector(state, wires))
True
>>> isinstance(Projector(state, wires), type(Projector(state, wires)))
True
>>> Projector(basis_state, wires).__class__ is Projector._basis_state_type
True
>>> Projector(state_vector, wires).__class__ is Projector._state_vector_type
True
"""
wires = Wires(wires)
shape = qml.math.shape(state)
if len(shape) != 1:
raise ValueError(f"Input state must be one-dimensional; got shape {shape}.")
if len(state) == len(wires):
return object.__new__(BasisStateProjector)
if len(state) == 2 ** len(wires):
return object.__new__(StateVectorProjector)
raise ValueError(
"Input state should have the same length as the wires in the case "
"of a basis state, or 2**len(wires) in case of a state vector. "
f"The lengths for the input state and wires are {len(state)} and "
f"{len(wires)}, respectively."
)
def pow(self, z):
"""Raise this projector to the power ``z``."""
return [copy(self)] if (isinstance(z, int) and z > 0) else super().pow(z)
def __copy__(self):
copied_op = self.__new__(Projector, self.data[0], self.wires)
copied_op.data = copy(self.data)
for attr, value in vars(self).items():
if attr != "data":
setattr(copied_op, attr, value)
return copied_op
class BasisStateProjector(Projector, Observable):
r"""Observable corresponding to the state projector :math:`P=\ket{\phi}\bra{\phi}`, where
:math:`\phi` denotes a basis state."""
# The call signature should be the same as Projector.__new__ for the positional
# arguments, but with free key word arguments.
def __init__(self, state, wires, id=None):
wires = Wires(wires)
state = list(qml.math.toarray(state).astype(int))
if not set(state).issubset({0, 1}):
raise ValueError(f"Basis state must only consist of 0s and 1s; got {state}")
super().__init__(state, wires=wires, id=id)
def __new__(cls): # pylint: disable=arguments-differ
return object.__new__(cls)
def label(self, decimals=None, base_label=None, cache=None):
r"""A customizable string representation of the operator.
Args:
decimals=None (int): If ``None``, no parameters are included. Else,
specifies how to round the parameters.
base_label=None (str): overwrite the non-parameter component of the label.
cache=None (dict): dictionary that caries information between label calls
in the same drawing.
Returns:
str: label to use in drawings.
**Example:**
>>> BasisStateProjector([0, 1, 0], wires=(0, 1, 2)).label()
'|010⟩⟨010|'
"""
if base_label is not None:
return base_label
basis_string = "".join(str(int(i)) for i in self.parameters[0])
return f"|{basis_string}⟩⟨{basis_string}|"
@staticmethod
def compute_matrix(basis_state): # pylint: disable=arguments-differ
r"""Representation of the operator as a canonical matrix in the computational basis (static method).
The canonical matrix is the textbook matrix representation that does not consider wires.
Implicitly, this assumes that the wires of the operator correspond to the global wire order.
.. seealso:: :meth:`~.BasisStateProjector.matrix`
Args:
basis_state (Iterable): basis state to project on
Returns:
ndarray: matrix
**Example**
>>> BasisStateProjector.compute_matrix([0, 1])
[[0. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
"""
m = np.zeros((2 ** len(basis_state), 2 ** len(basis_state)))
idx = int("".join(str(i) for i in basis_state), 2)
m[idx, idx] = 1
return m
@staticmethod
def compute_eigvals(basis_state): # pylint: disable=arguments-differ
r"""Eigenvalues of the operator in the computational basis (static method).
If :attr:`diagonalizing_gates` are specified and implement a unitary :math:`U^{\dagger}`,
the operator can be reconstructed as
.. math:: O = U \Sigma U^{\dagger},
where :math:`\Sigma` is the diagonal matrix containing the eigenvalues.
Otherwise, no particular order for the eigenvalues is guaranteed.
.. seealso:: :meth:`~.BasisStateProjector.eigvals`
Args:
basis_state (Iterable): basis state to project on
Returns:
array: eigenvalues
**Example**
>>> BasisStateProjector.compute_eigvals([0, 1])
[0. 1. 0. 0.]
"""
w = np.zeros(2 ** len(basis_state))
idx = int("".join(str(i) for i in basis_state), 2)
w[idx] = 1
return w
@staticmethod
def compute_diagonalizing_gates(
basis_state, wires
): # pylint: disable=arguments-differ,unused-argument
r"""Sequence of gates that diagonalize the operator in the computational basis (static method).
Given the eigendecomposition :math:`O = U \Sigma U^{\dagger}` where
:math:`\Sigma` is a diagonal matrix containing the eigenvalues,
the sequence of diagonalizing gates implements the unitary :math:`U^{\dagger}`.
The diagonalizing gates rotate the state into the eigenbasis
of the operator.
.. seealso:: :meth:`~.BasisStateProjector.diagonalizing_gates`.
Args:
basis_state (Iterable): basis state that the operator projects on
wires (Iterable[Any], Wires): wires that the operator acts on
Returns:
list[.Operator]: list of diagonalizing gates
**Example**
>>> BasisStateProjector.compute_diagonalizing_gates([0, 1, 0, 0], wires=[0, 1])
[]
"""
return []
class StateVectorProjector(Projector, Observable):
r"""Observable corresponding to the state projector :math:`P=\ket{\phi}\bra{\phi}`, where
:math:`\phi` denotes a state."""
# The call signature should be the same as Projector.__new__ for the positional
# arguments, but with free key word arguments.
def __init__(self, state, wires, id=None):
wires = Wires(wires)
state = list(qml.math.toarray(state))
super().__init__(state, wires=wires, id=id)
def __new__(cls): # pylint: disable=arguments-differ
return object.__new__(cls)
def label(self, decimals=None, base_label=None, cache=None):
r"""A customizable string representation of the operator.
Args:
decimals=None (int): If ``None``, no parameters are included. Else,
specifies how to round the parameters.
base_label=None (str): overwrite the non-parameter component of the label.
cache=None (dict): dictionary that caries information between label calls
in the same drawing.
Returns:
str: label to use in drawings.
**Example:**
>>> state_vector = np.array([0, 1, 1, 0])/np.sqrt(2)
>>> StateVectorProjector(state_vector, wires=(0, 1)).label()
'P'
>>> StateVectorProjector(state_vector, wires=(0, 1)).label(base_label="hi!")
'hi!'
>>> dev = qml.device("default.qubit", wires=1)
>>> @qml.qnode(dev)
>>> def circuit(state):
... return qml.expval(StateVectorProjector(state, [0]))
>>> print(qml.draw(circuit)([1, 0]))
0: ───┤ <|0⟩⟨0|>
>>> print(qml.draw(circuit)(np.array([1, 1]) / np.sqrt(2)))
0: ───┤ <P(M0)>
M0 =
[0.70710678 0.70710678]
"""
if base_label is not None:
return base_label
state_vector = self.parameters[0]
n_wires = int(qml.math.log2(len(state_vector)))
basis_state_idx = qml.math.nonzero(state_vector)[0]
if len(basis_state_idx) == 1:
basis_string = f"{basis_state_idx[0]:0{n_wires}b}"
return f"|{basis_string}⟩⟨{basis_string}|"
if cache is None or not isinstance(cache.get("matrices", None), list):
return "P"
mat_num = len(cache["matrices"])
cache["matrices"].append(self.parameters[0])
return f"P(M{mat_num})"
@staticmethod
def compute_matrix(state_vector): # pylint: disable=arguments-differ,arguments-renamed
r"""Representation of the operator as a canonical matrix in the computational basis (static method).
The canonical matrix is the textbook matrix representation that does not consider wires.
Implicitly, this assumes that the wires of the operator correspond to the global wire order.
.. seealso:: :meth:`~.Projector.matrix`
Args:
state_vector (Iterable): state vector to project on
Returns:
ndarray: matrix
**Example**
The projector of the state :math:`\frac{1}{\sqrt{2}}(\ket{01}+\ket{10})`
>>> StateVectorProjector.compute_matrix([0, 1/np.sqrt(2), 1/np.sqrt(2), 0])
[[0. 0. 0. 0.]
[0. 0.5 0.5 0.]
[0. 0.5 0.5 0.]
[0. 0. 0. 0.]]
"""
return qml.math.outer(state_vector, qml.math.conj(state_vector))
@staticmethod
def compute_eigvals(state_vector): # pylint: disable=arguments-differ,arguments-renamed
r"""Eigenvalues of the operator in the computational basis (static method).
If :attr:`diagonalizing_gates` are specified and implement a unitary :math:`U^{\dagger}`,
the operator can be reconstructed as
.. math:: O = U \Sigma U^{\dagger},
where :math:`\Sigma` is the diagonal matrix containing the eigenvalues.
Otherwise, no particular order for the eigenvalues is guaranteed.
.. seealso:: :meth:`~.StateVectorProjector.eigvals`
Args:
state_vector (Iterable): state vector to project on
Returns:
array: eigenvalues
**Example**
>>> StateVectorProjector.compute_eigvals([0, 0, 1, 0])
array([1, 0, 0, 0])
"""
w = qml.math.zeros_like(state_vector)
w[0] = 1
return w
@staticmethod
def compute_diagonalizing_gates(
state_vector, wires
): # pylint: disable=arguments-differ,unused-argument,arguments-renamed
r"""Sequence of gates that diagonalize the operator in the computational basis (static method).
Given the eigendecomposition :math:`O = U \Sigma U^{\dagger}` where
:math:`\Sigma` is a diagonal matrix containing the eigenvalues,
the sequence of diagonalizing gates implements the unitary :math:`U^{\dagger}`.
The diagonalizing gates rotate the state into the eigenbasis
of the operator.
.. seealso:: :meth:`~.StateVectorProjector.diagonalizing_gates`.
Args:
state_vector (Iterable): state vector that the operator projects on.
wires (Iterable[Any], Wires): wires that the operator acts on.
Returns:
list[.Operator]: list of diagonalizing gates.
**Example**
>>> state_vector = np.array([1., 1j])/np.sqrt(2)
>>> StateVectorProjector.compute_diagonalizing_gates(state_vector, wires=[0])
[QubitUnitary(array([[ 0.70710678+0.j , 0. -0.70710678j],
[ 0. +0.70710678j, -0.70710678+0.j ]]), wires=[0])]
"""
# Adapting the approach discussed in the link below to work with arbitrary complex-valued state vectors.
# Alternatively, we could take the adjoint of the Mottonen decomposition for the state vector.
# https://quantumcomputing.stackexchange.com/questions/10239/how-can-i-fill-a-unitary-knowing-only-its-first-column
phase = qml.math.exp(-1j * qml.math.angle(state_vector[0]))
psi = phase * state_vector
denominator = qml.math.sqrt(2 + 2 * psi[0])
psi = qml.math.set_index(psi, 0, psi[0] + 1) # psi[0] += 1, but JAX-JIT compatible
psi /= denominator
u = 2 * qml.math.outer(psi, qml.math.conj(psi)) - qml.math.eye(len(psi))
return [QubitUnitary(u, wires=wires)]