-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
t.py
179 lines (128 loc) · 5.29 KB
/
t.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
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017.
#
# 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.
"""T and Tdg gate."""
import math
from math import pi
from typing import Optional
import numpy
from qiskit.circuit.singleton import SingletonGate, stdlib_singleton_key
from qiskit.circuit.library.standard_gates.p import PhaseGate
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit._utils import with_gate_array
from qiskit._accelerate.circuit import StandardGate
@with_gate_array([[1, 0], [0, (1 + 1j) / math.sqrt(2)]])
class TGate(SingletonGate):
r"""Single qubit T gate (Z**0.25).
It induces a :math:`\pi/4` phase, and is sometimes called the pi/8 gate
(because of how the RZ(\pi/4) matrix looks like).
This is a non-Clifford gate and a fourth-root of Pauli-Z.
Can be applied to a :class:`~qiskit.circuit.QuantumCircuit`
with the :meth:`~qiskit.circuit.QuantumCircuit.t` method.
**Matrix Representation:**
.. math::
T = \begin{pmatrix}
1 & 0 \\
0 & e^{i\pi/4}
\end{pmatrix}
**Circuit symbol:**
.. parsed-literal::
┌───┐
q_0: ┤ T ├
└───┘
Equivalent to a :math:`\pi/4` radian rotation about the Z axis.
"""
_standard_gate = StandardGate.TGate
def __init__(self, label: Optional[str] = None, *, duration=None, unit="dt"):
"""Create new T gate."""
super().__init__("t", 1, [], label=label, duration=duration, unit=unit)
_singleton_lookup_key = stdlib_singleton_key()
def _define(self):
"""
gate t a { u1(pi/4) a; }
"""
# pylint: disable=cyclic-import
from qiskit.circuit.quantumcircuit import QuantumCircuit
from .u1 import U1Gate
q = QuantumRegister(1, "q")
qc = QuantumCircuit(q, name=self.name)
rules = [(U1Gate(pi / 4), [q[0]], [])]
for instr, qargs, cargs in rules:
qc._append(instr, qargs, cargs)
self.definition = qc
def inverse(self, annotated: bool = False):
"""Return inverse T gate (i.e. Tdg).
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
of this gate is always a :class:`.TdgGate`.
Returns:
TdgGate: inverse of :class:`.TGate`
"""
return TdgGate()
def power(self, exponent: float, annotated: bool = False):
return PhaseGate(0.25 * numpy.pi * exponent)
def __eq__(self, other):
return isinstance(other, TGate)
@with_gate_array([[1, 0], [0, (1 - 1j) / math.sqrt(2)]])
class TdgGate(SingletonGate):
r"""Single qubit T-adjoint gate (~Z**0.25).
It induces a :math:`-\pi/4` phase.
This is a non-Clifford gate and a fourth-root of Pauli-Z.
Can be applied to a :class:`~qiskit.circuit.QuantumCircuit`
with the :meth:`~qiskit.circuit.QuantumCircuit.tdg` method.
**Matrix Representation:**
.. math::
Tdg = \begin{pmatrix}
1 & 0 \\
0 & e^{-i\pi/4}
\end{pmatrix}
**Circuit symbol:**
.. parsed-literal::
┌─────┐
q_0: ┤ Tdg ├
└─────┘
Equivalent to a :math:`-\pi/4` radian rotation about the Z axis.
"""
_standard_gate = StandardGate.TdgGate
def __init__(self, label: Optional[str] = None, *, duration=None, unit="dt"):
"""Create new Tdg gate."""
super().__init__("tdg", 1, [], label=label, duration=duration, unit=unit)
_singleton_lookup_key = stdlib_singleton_key()
def _define(self):
"""
gate tdg a { u1(pi/4) a; }
"""
# pylint: disable=cyclic-import
from qiskit.circuit.quantumcircuit import QuantumCircuit
from .u1 import U1Gate
q = QuantumRegister(1, "q")
qc = QuantumCircuit(q, name=self.name)
rules = [(U1Gate(-pi / 4), [q[0]], [])]
for instr, qargs, cargs in rules:
qc._append(instr, qargs, cargs)
self.definition = qc
def inverse(self, annotated: bool = False):
"""Return inverse Tdg gate (i.e. T).
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
of this gate is always a :class:`.TGate`.
Returns:
TGate: inverse of :class:`.TdgGate`
"""
return TGate()
def power(self, exponent: float, annotated: bool = False):
return PhaseGate(-0.25 * numpy.pi * exponent)
def __eq__(self, other):
return isinstance(other, TdgGate)