/
rzz.py
155 lines (120 loc) · 4.92 KB
/
rzz.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
# 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.
"""Two-qubit ZZ-rotation gate."""
from cmath import exp
from typing import Optional
from qiskit.circuit.gate import Gate
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit.parameterexpression import ParameterValueType
class RZZGate(Gate):
r"""A parametric 2-qubit :math:`Z \otimes Z` interaction (rotation about ZZ).
This gate is symmetric, and is maximally entangling at :math:`\theta = \pi/2`.
Can be applied to a :class:`~qiskit.circuit.QuantumCircuit`
with the :meth:`~qiskit.circuit.QuantumCircuit.rzz` method.
**Circuit Symbol:**
.. parsed-literal::
q_0: ───■────
│zz(θ)
q_1: ───■────
**Matrix Representation:**
.. math::
\newcommand{\rotationangle}{\frac{\theta}{2}}
R_{ZZ}(\theta) = \exp\left(-i \rotationangle Z{\otimes}Z\right) =
\begin{pmatrix}
e^{-i \rotationangle} & 0 & 0 & 0 \\
0 & e^{i \rotationangle} & 0 & 0 \\
0 & 0 & e^{i \rotationangle} & 0 \\
0 & 0 & 0 & e^{-i \rotationangle}
\end{pmatrix}
This is a direct sum of RZ rotations, so this gate is equivalent to a
uniformly controlled (multiplexed) RZ gate:
.. math::
R_{ZZ}(\theta) =
\begin{pmatrix}
RZ(\theta) & 0 \\
0 & RZ(-\theta)
\end{pmatrix}
**Examples:**
.. math::
R_{ZZ}(\theta = 0) = I
.. math::
R_{ZZ}(\theta = 2\pi) = -I
.. math::
R_{ZZ}(\theta = \pi) = - Z \otimes Z
.. math::
R_{ZZ}\left(\theta = \frac{\pi}{2}\right) = \frac{1}{\sqrt{2}}
\begin{pmatrix}
1-i & 0 & 0 & 0 \\
0 & 1+i & 0 & 0 \\
0 & 0 & 1+i & 0 \\
0 & 0 & 0 & 1-i
\end{pmatrix}
"""
def __init__(
self, theta: ParameterValueType, label: Optional[str] = None, *, duration=None, unit="dt"
):
"""Create new RZZ gate."""
super().__init__("rzz", 2, [theta], label=label, duration=duration, unit=unit)
def _define(self):
"""
gate rzz(theta) a, b { cx a, b; u1(theta) b; cx a, b; }
"""
# pylint: disable=cyclic-import
from qiskit.circuit.quantumcircuit import QuantumCircuit
from .x import CXGate
from .rz import RZGate
# q_0: ──■─────────────■──
# ┌─┴─┐┌───────┐┌─┴─┐
# q_1: ┤ X ├┤ Rz(0) ├┤ X ├
# └───┘└───────┘└───┘
q = QuantumRegister(2, "q")
theta = self.params[0]
qc = QuantumCircuit(q, name=self.name)
rules = [
(CXGate(), [q[0], q[1]], []),
(RZGate(theta), [q[1]], []),
(CXGate(), [q[0], q[1]], []),
]
for instr, qargs, cargs in rules:
qc._append(instr, qargs, cargs)
self.definition = qc
def inverse(self, annotated: bool = False):
"""Return inverse RZZ gate (i.e. with the negative rotation angle).
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:`.RZZGate` with an inverted parameter value.
Returns:
RZZGate: inverse gate.
"""
return RZZGate(-self.params[0])
def __array__(self, dtype=None):
"""Return a numpy.array for the RZZ gate."""
import numpy
itheta2 = 1j * float(self.params[0]) / 2
return numpy.array(
[
[exp(-itheta2), 0, 0, 0],
[0, exp(itheta2), 0, 0],
[0, 0, exp(itheta2), 0],
[0, 0, 0, exp(-itheta2)],
],
dtype=dtype,
)
def power(self, exponent: float, annotated: bool = False):
(theta,) = self.params
return RZZGate(exponent * theta)
def __eq__(self, other):
if isinstance(other, RZZGate):
return self._compare_parameters(other)
return False