-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
global_phase.py
85 lines (66 loc) · 2.84 KB
/
global_phase.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
# This code is part of Qiskit.
#
# (C) Copyright IBM 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""Global Phase Gate"""
from typing import Optional
import numpy
from qiskit.circuit.gate import Gate
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit.quantumcircuit import QuantumCircuit
from qiskit.circuit.parameterexpression import ParameterValueType
from qiskit._accelerate.circuit import StandardGate
class GlobalPhaseGate(Gate):
r"""The global phase gate (:math:`e^{i\theta}`).
Can be applied to a :class:`~qiskit.circuit.QuantumCircuit`
**Mathematical Representation:**
.. math::
\text{GlobalPhaseGate}\ =
\begin{pmatrix}
e^{i\theta}
\end{pmatrix}
"""
_standard_gate = StandardGate.GlobalPhaseGate
def __init__(
self, phase: ParameterValueType, label: Optional[str] = None, *, duration=None, unit="dt"
):
"""
Args:
phase: The value of phase it takes.
label: An optional label for the gate.
"""
super().__init__("global_phase", 0, [phase], label=label, duration=duration, unit=unit)
def _define(self):
q = QuantumRegister(0, "q")
qc = QuantumCircuit(q, name=self.name, global_phase=self.params[0])
self.definition = qc
def inverse(self, annotated: bool = False):
r"""Return inverse GlobalPhaseGate gate.
:math:`\text{GlobalPhaseGate}(\lambda)^{\dagger} = \text{GlobalPhaseGate}(-\lambda)`
Args:
annotated: when set to ``True``, this is typically used to return an
:class:`.AnnotatedOperation` with an inverse modifier set instead of a concrete
:class:`.Gate`. However, for this class this argument is ignored as the inverse
is always another :class:`.GlobalPhaseGate` with an inverted
parameter value.
Returns:
GlobalPhaseGate: inverse gate.
"""
return GlobalPhaseGate(-self.params[0])
def __array__(self, dtype=None, copy=None):
"""Return a numpy.array for the global_phase gate."""
if copy is False:
raise ValueError("unable to avoid copy while creating an array as requested")
theta = self.params[0]
return numpy.array([[numpy.exp(1j * theta)]], dtype=dtype or complex)
def __eq__(self, other):
if isinstance(other, GlobalPhaseGate):
return self._compare_parameters(other)
return False