Skip to content

Commit 3c8927b

Browse files
authored
Add ISWAP_INV constant (#5613)
Fixes #4615 #4624 has not been touched in a long time, so jumping in to fix this.
1 parent 8f7ced7 commit 3c8927b

9 files changed

Lines changed: 54 additions & 3 deletions

File tree

cirq-core/cirq/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@
241241
InterchangeableQubitsGate,
242242
ISWAP,
243243
ISwapPowGate,
244+
ISWAP_INV,
244245
KrausChannel,
245246
LinearCombinationOfGates,
246247
LinearCombinationOfOperations,

cirq-core/cirq/ops/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
from cirq.ops.swap_gates import (
181181
ISWAP,
182182
ISwapPowGate,
183+
ISWAP_INV,
183184
riswap,
184185
SQRT_ISWAP,
185186
SQRT_ISWAP_INV,

cirq-core/cirq/ops/pauli_string_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ def _decompose_(self, qubits):
14961496
cirq.CNOT,
14971497
cirq.CZ,
14981498
cirq.ISWAP,
1499-
cirq.ISWAP**-1,
1499+
cirq.ISWAP_INV,
15001500
cirq.SWAP,
15011501
cirq.XX**0.5,
15021502
cirq.YY**0.5,

cirq-core/cirq/ops/swap_gates.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
This module creates Gate instances for the following gates:
1717
SWAP: the swap gate.
1818
ISWAP: a swap gate with a phase on the swapped subspace.
19+
ISWAP_INV: the inverse of the ISWAP gate.
1920
SQRT_ISWAP: square root of the ISWAP gate.
2021
SQRT_ISWAP_INV: inverse square root of the ISWAP gate.
2122
@@ -284,13 +285,17 @@ def _circuit_diagram_info_(
284285
def __str__(self) -> str:
285286
if self._exponent == 1:
286287
return 'ISWAP'
288+
if self._exponent == -1:
289+
return 'ISWAP_INV'
287290
return f'ISWAP**{self._exponent}'
288291

289292
def __repr__(self) -> str:
290293
e = proper_repr(self._exponent)
291294
if self._global_shift == 0:
292295
if self._exponent == 1:
293296
return 'cirq.ISWAP'
297+
if self._exponent == -1:
298+
return 'cirq.ISWAP_INV'
294299
return f'(cirq.ISWAP**{e})'
295300
return f'cirq.ISwapPowGate(exponent={e}, global_shift={self._global_shift!r})'
296301

@@ -342,6 +347,23 @@ def riswap(rads: value.TParamVal) -> ISwapPowGate:
342347
""",
343348
)
344349

350+
ISWAP_INV = ISwapPowGate(exponent=-1)
351+
document(
352+
ISWAP_INV,
353+
r"""The inverse of the iswap gate.
354+
355+
The unitary matrix of this gate is:
356+
$$
357+
\begin{bmatrix}
358+
1 & 0 & 0 & 0 \\
359+
0 & 0 & -i & 0 \\
360+
0 & -i & 0 & 0 \\
361+
0 & 0 & 0 & 1
362+
\end{bmatrix}
363+
$$
364+
""",
365+
)
366+
345367
SQRT_ISWAP = ISwapPowGate(exponent=0.5)
346368
document(
347369
SQRT_ISWAP,

cirq-core/cirq/ops/swap_gates_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ def test_iswap_unitary():
103103
# yapf: enable
104104

105105

106+
def test_iswap_inv_unitary():
107+
# yapf: disable
108+
cirq.testing.assert_allclose_up_to_global_phase(
109+
cirq.unitary(cirq.ISWAP_INV),
110+
# Reference for the iswap gate's matrix using +i instead of -i:
111+
# https://quantumcomputing.stackexchange.com/questions/2594/
112+
np.array([[1, 0, 0, 0],
113+
[0, 0, -1j, 0],
114+
[0, -1j, 0, 0],
115+
[0, 0, 0, 1]]),
116+
atol=1e-8)
117+
# yapf: enable
118+
119+
106120
def test_sqrt_iswap_unitary():
107121
# yapf: disable
108122
cirq.testing.assert_allclose_up_to_global_phase(
@@ -138,6 +152,9 @@ def test_repr():
138152
assert repr(cirq.ISWAP) == 'cirq.ISWAP'
139153
assert repr(cirq.ISWAP**0.5) == '(cirq.ISWAP**0.5)'
140154

155+
assert repr(cirq.ISWAP_INV) == 'cirq.ISWAP_INV'
156+
assert repr(cirq.ISWAP_INV**0.5) == '(cirq.ISWAP**-0.5)'
157+
141158

142159
def test_str():
143160
assert str(cirq.SWAP) == 'SWAP'
@@ -146,6 +163,9 @@ def test_str():
146163
assert str(cirq.ISWAP) == 'ISWAP'
147164
assert str(cirq.ISWAP**0.5) == 'ISWAP**0.5'
148165

166+
assert str(cirq.ISWAP_INV) == 'ISWAP_INV'
167+
assert str(cirq.ISWAP_INV**0.5) == 'ISWAP**-0.5'
168+
149169

150170
def test_iswap_decompose_diagram():
151171
a = cirq.NamedQubit('a')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"cirq_type": "ISwapPowGate",
3+
"exponent": -1,
4+
"global_shift": 0.0
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cirq.ISWAP_INV

cirq-google/cirq_google/serialization/common_serializers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation):
592592
cirq.SQRT_ISWAP_INV,
593593
cirq.SQRT_ISWAP,
594594
cirq.ISWAP,
595-
cirq.ISWAP**-1, # type: ignore
595+
cirq.ISWAP_INV,
596596
SYC,
597597
cirq.CZ,
598598
],
@@ -605,7 +605,7 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation):
605605
cirq.SQRT_ISWAP_INV,
606606
cirq.SQRT_ISWAP,
607607
cirq.ISWAP,
608-
cirq.ISWAP**-1, # type: ignore
608+
cirq.ISWAP_INV,
609609
],
610610
gate_types_to_check=[cirq.ISwapPowGate],
611611
allow_symbols=True,

cirq-google/cirq_google/serialization/common_serializers_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ def test_wait_gate_multi_qubit():
529529
(cirq.ISWAP**1.0, -np.pi / 2, 0),
530530
(cirq.ISWAP**-1.0, np.pi / 2, 0),
531531
(cirq.ISWAP**0.0, 0, 0),
532+
(cirq.ISWAP_INV, np.pi / 2, 0),
532533
(cirq.CZ, 0, np.pi),
533534
(cirq.CZ**-1.0, 0, np.pi),
534535
(cirq.FSimGate(theta=0, phi=0), 0, 0),

0 commit comments

Comments
 (0)