From 13bfd8ce86cca7757da8ceb0ba7b578bafef6cd9 Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Fri, 24 Jun 2022 22:17:17 +0000 Subject: [PATCH 1/4] Add ISWAP_INV constant --- cirq-core/cirq/__init__.py | 1 + cirq-core/cirq/ops/__init__.py | 1 + cirq-core/cirq/ops/pauli_string_test.py | 2 +- cirq-core/cirq/ops/swap_gates.py | 18 ++++++++++++++++++ .../serialization/common_serializers.py | 4 ++-- .../serialization/common_serializers_test.py | 1 + docs/gatezoo.ipynb | 2 +- 7 files changed, 25 insertions(+), 4 deletions(-) diff --git a/cirq-core/cirq/__init__.py b/cirq-core/cirq/__init__.py index 65588f17ea6..d08032ab913 100644 --- a/cirq-core/cirq/__init__.py +++ b/cirq-core/cirq/__init__.py @@ -241,6 +241,7 @@ InterchangeableQubitsGate, ISWAP, ISwapPowGate, + ISWAP_INV, KrausChannel, LinearCombinationOfGates, LinearCombinationOfOperations, diff --git a/cirq-core/cirq/ops/__init__.py b/cirq-core/cirq/ops/__init__.py index cd55dc38f4e..cebe8373119 100644 --- a/cirq-core/cirq/ops/__init__.py +++ b/cirq-core/cirq/ops/__init__.py @@ -180,6 +180,7 @@ from cirq.ops.swap_gates import ( ISWAP, ISwapPowGate, + ISWAP_INV, riswap, SQRT_ISWAP, SQRT_ISWAP_INV, diff --git a/cirq-core/cirq/ops/pauli_string_test.py b/cirq-core/cirq/ops/pauli_string_test.py index dd80eca31a2..78e26ae17ed 100644 --- a/cirq-core/cirq/ops/pauli_string_test.py +++ b/cirq-core/cirq/ops/pauli_string_test.py @@ -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, diff --git a/cirq-core/cirq/ops/swap_gates.py b/cirq-core/cirq/ops/swap_gates.py index f41aed7d91e..b61aa1866cf 100644 --- a/cirq-core/cirq/ops/swap_gates.py +++ b/cirq-core/cirq/ops/swap_gates.py @@ -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_SQRT: the inverse of the ISWAP gate. SQRT_ISWAP: square root of the ISWAP gate. SQRT_ISWAP_INV: inverse square root of the ISWAP gate. @@ -342,6 +343,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, diff --git a/cirq-google/cirq_google/serialization/common_serializers.py b/cirq-google/cirq_google/serialization/common_serializers.py index e3f8fb57835..1d3b41a5a6a 100644 --- a/cirq-google/cirq_google/serialization/common_serializers.py +++ b/cirq-google/cirq_google/serialization/common_serializers.py @@ -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, ], @@ -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, diff --git a/cirq-google/cirq_google/serialization/common_serializers_test.py b/cirq-google/cirq_google/serialization/common_serializers_test.py index a4d2cd60f57..69fbc9ea013 100644 --- a/cirq-google/cirq_google/serialization/common_serializers_test.py +++ b/cirq-google/cirq_google/serialization/common_serializers_test.py @@ -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), diff --git a/docs/gatezoo.ipynb b/docs/gatezoo.ipynb index c561e60f261..c83e0b9eda4 100644 --- a/docs/gatezoo.ipynb +++ b/docs/gatezoo.ipynb @@ -207,7 +207,7 @@ }, "outputs": [], "source": [ - "display_gates(\"CX\", \"CZ\", \"SWAP\", \"ISWAP\", \"SQRT_ISWAP\", \"SQRT_ISWAP_INV\")" + "display_gates(\"CX\", \"CZ\", \"SWAP\", \"ISWAP\", \"ISWAP_INV\", \"SQRT_ISWAP\", \"SQRT_ISWAP_INV\")" ] }, { From b4ee2f1c2c4a3c4887ab6382bf6f6d50aa2b4774 Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Fri, 24 Jun 2022 22:34:08 +0000 Subject: [PATCH 2/4] fixes --- cirq-core/cirq/ops/swap_gates.py | 4 ++++ cirq-core/cirq/ops/swap_gates_test.py | 20 +++++++++++++++++++ .../protocols/json_test_data/ISWAP_INV.json | 5 +++++ .../protocols/json_test_data/ISWAP_INV.repr | 1 + 4 files changed, 30 insertions(+) create mode 100644 cirq-core/cirq/protocols/json_test_data/ISWAP_INV.json create mode 100644 cirq-core/cirq/protocols/json_test_data/ISWAP_INV.repr diff --git a/cirq-core/cirq/ops/swap_gates.py b/cirq-core/cirq/ops/swap_gates.py index b61aa1866cf..7871bacc20d 100644 --- a/cirq-core/cirq/ops/swap_gates.py +++ b/cirq-core/cirq/ops/swap_gates.py @@ -285,6 +285,8 @@ 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: @@ -292,6 +294,8 @@ def __repr__(self) -> str: 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})' diff --git a/cirq-core/cirq/ops/swap_gates_test.py b/cirq-core/cirq/ops/swap_gates_test.py index 61892eed148..02f6488ef06 100644 --- a/cirq-core/cirq/ops/swap_gates_test.py +++ b/cirq-core/cirq/ops/swap_gates_test.py @@ -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( @@ -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' @@ -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') diff --git a/cirq-core/cirq/protocols/json_test_data/ISWAP_INV.json b/cirq-core/cirq/protocols/json_test_data/ISWAP_INV.json new file mode 100644 index 00000000000..be9ddb452f6 --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/ISWAP_INV.json @@ -0,0 +1,5 @@ +{ + "cirq_type": "ISwapPowGate", + "exponent": -1, + "global_shift": 0.0 +} \ No newline at end of file diff --git a/cirq-core/cirq/protocols/json_test_data/ISWAP_INV.repr b/cirq-core/cirq/protocols/json_test_data/ISWAP_INV.repr new file mode 100644 index 00000000000..81aa24eb81c --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/ISWAP_INV.repr @@ -0,0 +1 @@ +cirq.ISWAP_INV \ No newline at end of file From 28f0345dc1749b8ee858670b9b0a134a279487a7 Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Fri, 24 Jun 2022 22:38:23 +0000 Subject: [PATCH 3/4] chicken meet egg --- docs/gatezoo.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/gatezoo.ipynb b/docs/gatezoo.ipynb index c83e0b9eda4..c561e60f261 100644 --- a/docs/gatezoo.ipynb +++ b/docs/gatezoo.ipynb @@ -207,7 +207,7 @@ }, "outputs": [], "source": [ - "display_gates(\"CX\", \"CZ\", \"SWAP\", \"ISWAP\", \"ISWAP_INV\", \"SQRT_ISWAP\", \"SQRT_ISWAP_INV\")" + "display_gates(\"CX\", \"CZ\", \"SWAP\", \"ISWAP\", \"SQRT_ISWAP\", \"SQRT_ISWAP_INV\")" ] }, { From 140365414d73bfa77af8a0d63637c1a7c0cb7d53 Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Fri, 24 Jun 2022 22:57:58 +0000 Subject: [PATCH 4/4] typo --- cirq-core/cirq/ops/swap_gates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cirq-core/cirq/ops/swap_gates.py b/cirq-core/cirq/ops/swap_gates.py index 7871bacc20d..6d5405eaa0d 100644 --- a/cirq-core/cirq/ops/swap_gates.py +++ b/cirq-core/cirq/ops/swap_gates.py @@ -16,7 +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_SQRT: the inverse of the ISWAP gate. + 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.