Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cirq-core/cirq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@
InterchangeableQubitsGate,
ISWAP,
ISwapPowGate,
ISWAP_INV,
KrausChannel,
LinearCombinationOfGates,
LinearCombinationOfOperations,
Expand Down
1 change: 1 addition & 0 deletions cirq-core/cirq/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
from cirq.ops.swap_gates import (
ISWAP,
ISwapPowGate,
ISWAP_INV,
riswap,
SQRT_ISWAP,
SQRT_ISWAP_INV,
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/pauli_string_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ def _decompose_(self, qubits):
cirq.CNOT,
cirq.CZ,
cirq.ISWAP,
cirq.ISWAP**-1,
cirq.ISWAP_INV,
cirq.SWAP,
cirq.XX**0.5,
cirq.YY**0.5,
Expand Down
22 changes: 22 additions & 0 deletions cirq-core/cirq/ops/swap_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
This module creates Gate instances for the following gates:
SWAP: the swap gate.
ISWAP: a swap gate with a phase on the swapped subspace.
ISWAP_INV: the inverse of the ISWAP gate.
SQRT_ISWAP: square root of the ISWAP gate.
SQRT_ISWAP_INV: inverse square root of the ISWAP gate.

Expand Down Expand Up @@ -284,13 +285,17 @@ def _circuit_diagram_info_(
def __str__(self) -> str:
if self._exponent == 1:
return 'ISWAP'
if self._exponent == -1:
return 'ISWAP_INV'
return f'ISWAP**{self._exponent}'

def __repr__(self) -> str:
e = proper_repr(self._exponent)
if self._global_shift == 0:
if self._exponent == 1:
return 'cirq.ISWAP'
if self._exponent == -1:
return 'cirq.ISWAP_INV'
return f'(cirq.ISWAP**{e})'
return f'cirq.ISwapPowGate(exponent={e}, global_shift={self._global_shift!r})'

Expand Down Expand Up @@ -342,6 +347,23 @@ def riswap(rads: value.TParamVal) -> ISwapPowGate:
""",
)

ISWAP_INV = ISwapPowGate(exponent=-1)
document(
ISWAP_INV,
r"""The inverse of the iswap gate.

The unitary matrix of this gate is:
$$
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & 0 & -i & 0 \\
0 & -i & 0 & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}
$$
""",
)

SQRT_ISWAP = ISwapPowGate(exponent=0.5)
document(
SQRT_ISWAP,
Expand Down
20 changes: 20 additions & 0 deletions cirq-core/cirq/ops/swap_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ def test_iswap_unitary():
# yapf: enable


def test_iswap_inv_unitary():
# yapf: disable
cirq.testing.assert_allclose_up_to_global_phase(
cirq.unitary(cirq.ISWAP_INV),
# Reference for the iswap gate's matrix using +i instead of -i:
# https://quantumcomputing.stackexchange.com/questions/2594/
np.array([[1, 0, 0, 0],
[0, 0, -1j, 0],
[0, -1j, 0, 0],
[0, 0, 0, 1]]),
atol=1e-8)
# yapf: enable


def test_sqrt_iswap_unitary():
# yapf: disable
cirq.testing.assert_allclose_up_to_global_phase(
Expand Down Expand Up @@ -138,6 +152,9 @@ def test_repr():
assert repr(cirq.ISWAP) == 'cirq.ISWAP'
assert repr(cirq.ISWAP**0.5) == '(cirq.ISWAP**0.5)'

assert repr(cirq.ISWAP_INV) == 'cirq.ISWAP_INV'
assert repr(cirq.ISWAP_INV**0.5) == '(cirq.ISWAP**-0.5)'


def test_str():
assert str(cirq.SWAP) == 'SWAP'
Expand All @@ -146,6 +163,9 @@ def test_str():
assert str(cirq.ISWAP) == 'ISWAP'
assert str(cirq.ISWAP**0.5) == 'ISWAP**0.5'

assert str(cirq.ISWAP_INV) == 'ISWAP_INV'
assert str(cirq.ISWAP_INV**0.5) == 'ISWAP**-0.5'


def test_iswap_decompose_diagram():
a = cirq.NamedQubit('a')
Expand Down
5 changes: 5 additions & 0 deletions cirq-core/cirq/protocols/json_test_data/ISWAP_INV.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cirq_type": "ISwapPowGate",
"exponent": -1,
"global_shift": 0.0
}
1 change: 1 addition & 0 deletions cirq-core/cirq/protocols/json_test_data/ISWAP_INV.repr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cirq.ISWAP_INV
4 changes: 2 additions & 2 deletions cirq-google/cirq_google/serialization/common_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation):
cirq.SQRT_ISWAP_INV,
cirq.SQRT_ISWAP,
cirq.ISWAP,
cirq.ISWAP**-1, # type: ignore
cirq.ISWAP_INV,
SYC,
cirq.CZ,
],
Expand All @@ -605,7 +605,7 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation):
cirq.SQRT_ISWAP_INV,
cirq.SQRT_ISWAP,
cirq.ISWAP,
cirq.ISWAP**-1, # type: ignore
cirq.ISWAP_INV,
],
gate_types_to_check=[cirq.ISwapPowGate],
allow_symbols=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ def test_wait_gate_multi_qubit():
(cirq.ISWAP**1.0, -np.pi / 2, 0),
(cirq.ISWAP**-1.0, np.pi / 2, 0),
(cirq.ISWAP**0.0, 0, 0),
(cirq.ISWAP_INV, np.pi / 2, 0),
(cirq.CZ, 0, np.pi),
(cirq.CZ**-1.0, 0, np.pi),
(cirq.FSimGate(theta=0, phi=0), 0, 0),
Expand Down