#
# Copyright © 2022-2024 University of Strasbourg. All Rights Reserved.
# Copyright © 2023-2025 QPerfect. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""iSWAP and iSWAP† gates."""
import mimiqcircuits.operations.gates.gate as mcg
from mimiqcircuits.operations.gates.standard.cpauli import GateCX
from mimiqcircuits.operations.gates.standard.hadamard import GateH
from mimiqcircuits.operations.gates.standard.s import GateS, GateSDG
from symengine import Matrix, I
from mimiqcircuits.operations.inverse import Inverse, canonical_inverse
[docs]
class GateISWAP(mcg.Gate):
r"""Two qubit iSWAP gate.
Swaps the states of the two qubits and adds a phase :math:`i` to
:math:`|01\rangle` and :math:`|10\rangle`.
Matrix Representation:
.. math::
\operatorname{ISWAP} = \begin{pmatrix}
1 & 0 & 0 & 0 \\
0 & 0 & i & 0 \\
0 & i & 0 & 0 \\
0 & 0 & 0 & 1
\end{pmatrix}
Examples:
>>> from mimiqcircuits import *
>>> GateISWAP()
ISWAP
>>> GateISWAP().matrix()
[1.0, 0, 0, 0]
[0, 0, 0.0 + 1.0*I, 0]
[0, 0.0 + 1.0*I, 0, 0]
[0, 0, 0, 1.0]
<BLANKLINE>
>>> c = Circuit().push(GateISWAP(), 0, 1)
>>> c
2-qubit circuit with 1 instruction:
└── ISWAP @ q[0:1]
<BLANKLINE>
>>> GateISWAP().power(2), GateISWAP().inverse()
(ISWAP**2, ISWAP†)
>>> GateISWAP().decompose()
2-qubit circuit with 6 instructions:
├── S @ q[0]
├── S @ q[1]
├── H @ q[0]
├── CX @ q[0], q[1]
├── CX @ q[1], q[0]
└── H @ q[1]
<BLANKLINE>
See Also:
:class:`GateISWAPDG`, :class:`GateSWAP`
"""
_name = "ISWAP"
_num_qubits = 2
_qregsizes = [2]
def _matrix(self):
return Matrix([[1, 0, 0, 0], [0, 0, I, 0], [0, I, 0, 0], [0, 0, 0, 1]])
[docs]
def inverse(self):
return GateISWAPDG()
def _decompose(self, circ, qubits, bits, zvars):
c, t = qubits
circ.push(GateS(), c)
circ.push(GateS(), t)
circ.push(GateH(), c)
circ.push(GateCX(), c, t)
circ.push(GateCX(), t, c)
circ.push(GateH(), t)
return circ
[docs]
@canonical_inverse(GateISWAP)
class GateISWAPDG(Inverse):
r"""Two qubit iSWAP† gate, the inverse of :class:`GateISWAP`.
Matrix Representation:
.. math::
\operatorname{ISWAP}^\dagger = \begin{pmatrix}
1 & 0 & 0 & 0 \\
0 & 0 & -i & 0 \\
0 & -i & 0 & 0 \\
0 & 0 & 0 & 1
\end{pmatrix}
Examples:
>>> from mimiqcircuits import *
>>> GateISWAPDG()
ISWAP†
>>> GateISWAPDG().matrix()
[1.0, 0, 0, 0]
[0, 0, 6.12323399573677e-17 - 1.0*I, 0]
[0, 6.12323399573677e-17 - 1.0*I, 0, 0]
[0, 0, 0, 1.0]
<BLANKLINE>
>>> c = Circuit().push(GateISWAPDG(), 0, 1)
>>> c
2-qubit circuit with 1 instruction:
└── ISWAP† @ q[0:1]
<BLANKLINE>
>>> GateISWAPDG().power(2), GateISWAPDG().inverse()
(ISWAP†**2, ISWAP)
>>> GateISWAPDG().decompose()
2-qubit circuit with 6 instructions:
├── S† @ q[0]
├── S† @ q[1]
├── H @ q[0]
├── CX @ q[0], q[1]
├── CX @ q[1], q[0]
└── H @ q[1]
<BLANKLINE>
See Also:
:class:`GateISWAP`, :class:`GateSWAP`
"""
[docs]
def __init__(self, operation=None):
"""Initialize an ISWAP† gate. The argument is unused."""
super().__init__(GateISWAP())
[docs]
def inverse(self):
return GateISWAP()
[docs]
def isopalias(self):
return True
def _decompose(self, circ, qubits, bits, zvars):
c, t = qubits
circ.push(GateSDG(), c)
circ.push(GateSDG(), t)
circ.push(GateH(), c)
circ.push(GateCX(), c, t)
circ.push(GateCX(), t, c)
circ.push(GateH(), t)
return circ