Source code for mimiqcircuits.operations.gates.standard.interactions

#
# 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.
#
"""Interaction gates (RXX, RYY, RZZ, RZX, XX+YY, XX-YY)."""

from mimiqcircuits.operations.gates.gate import Gate
from mimiqcircuits.operations.gates.standard.cpauli import GateCX
from mimiqcircuits.operations.gates.standard.hadamard import GateH
from mimiqcircuits.operations.gates.standard.rotations import GateRX, GateRY, GateRZ
from mimiqcircuits.operations.gates.standard.s import GateS, GateSDG
from mimiqcircuits.operations.gates.standard.sx import GateSX, GateSXDG
from mimiqcircuits.matrices import cis
from mimiqcircuits import numerics as _nm
import numpy as np
from symengine import I, Matrix, cos, sin
from mimiqcircuits.operations.gates.standard._angles import PI_2, NEG_PI_2


[docs] class GateRXX(Gate): r"""Two qubit RXX gate, a rotation generated by :math:`X \otimes X`. The gate is symmetric in its two qubits and is maximally entangling at :math:`\theta = \frac{\pi}{2}`. Matrix Representation: .. math:: \operatorname{RXX}(\theta) = \begin{pmatrix} \cos(\frac{\theta}{2}) & 0 & 0 & -i\sin(\frac{\theta}{2}) \\ 0 & \cos(\frac{\theta}{2}) & -i\sin(\frac{\theta}{2}) & 0 \\ 0 & -i\sin(\frac{\theta}{2}) & \cos(\frac{\theta}{2}) & 0 \\ -i\sin(\frac{\theta}{2}) & 0 & 0 & \cos(\frac{\theta}{2}) \end{pmatrix} Args: theta: Rotation angle in radians, numeric or symbolic. Examples: >>> from mimiqcircuits import * >>> from symengine import * >>> theta = Symbol('theta') >>> GateRXX(theta) RXX(theta) >>> GateRXX(theta).matrix() [cos((1/2)*theta), 0, 0, -I*sin((1/2)*theta)] [0, cos((1/2)*theta), -I*sin((1/2)*theta), 0] [0, -I*sin((1/2)*theta), cos((1/2)*theta), 0] [-I*sin((1/2)*theta), 0, 0, cos((1/2)*theta)] <BLANKLINE> >>> c = Circuit().push(GateRXX(theta), 0, 1) >>> c 2-qubit circuit with 1 instruction: └── RXX(theta) @ q[0:1] <BLANKLINE> >>> GateRXX(theta).power(2), GateRXX(theta).inverse() (RXX(theta)**2, RXX(-theta)) >>> GateRXX(theta).decompose() 2-qubit circuit with 7 instructions: ├── H @ q[0] ├── H @ q[1] ├── CX @ q[0], q[1] ├── RZ(theta) @ q[1] ├── CX @ q[0], q[1] ├── H @ q[1] └── H @ q[0] <BLANKLINE> """ _name = "RXX" _num_qubits = 2 _qregsizes = [2] _parnames = ("theta",) def __init__(self, theta): self.theta = theta def _matrix(self): theta = self.theta cos2 = cos(theta / 2) sin2 = sin(theta / 2) return Matrix( [ [cos2, 0, 0, -I * sin2], [0, cos2, -I * sin2, 0], [0, -I * sin2, cos2, 0], [-I * sin2, 0, 0, cos2], ] ) def _matrix_numeric(self, theta): import math half = theta / 2.0 / math.pi c = _nm.cospi(half) s = -1j * _nm.sinpi(half) return np.array( [[c, 0, 0, s], [0, c, s, 0], [0, s, c, 0], [s, 0, 0, c]], dtype=np.complex128, )
[docs] def inverse(self): return GateRXX(-self.theta)
def _decompose(self, circ, qubits, bits, zvars): a, b = qubits circ.push(GateH(), a) circ.push(GateH(), b) circ.push(GateCX(), a, b) circ.push(GateRZ(self.theta), b) circ.push(GateCX(), a, b) circ.push(GateH(), b) circ.push(GateH(), a) return circ
[docs] class GateRYY(Gate): r"""Two qubit RYY gate, a rotation generated by :math:`Y \otimes Y`. The gate is symmetric in its two qubits and is maximally entangling at :math:`\theta = \frac{\pi}{2}`. Matrix Representation: .. math:: \operatorname{RYY}(\theta) = \begin{pmatrix} \cos(\frac{\theta}{2}) & 0 & 0 & i\sin(\frac{\theta}{2}) \\ 0 & \cos(\frac{\theta}{2}) & -i\sin(\frac{\theta}{2}) & 0 \\ 0 & -i\sin(\frac{\theta}{2}) & \cos(\frac{\theta}{2}) & 0 \\ i\sin(\frac{\theta}{2}) & 0 & 0 & \cos(\frac{\theta}{2}) \end{pmatrix} Args: theta: Rotation angle in radians, numeric or symbolic. Examples: >>> from mimiqcircuits import * >>> from symengine import * >>> theta = Symbol('theta') >>> GateRYY(theta) RYY(theta) >>> GateRYY(theta).matrix() [cos((1/2)*theta), 0, 0, I*sin((1/2)*theta)] [0, cos((1/2)*theta), -I*sin((1/2)*theta), 0] [0, -I*sin((1/2)*theta), cos((1/2)*theta), 0] [I*sin((1/2)*theta), 0, 0, cos((1/2)*theta)] <BLANKLINE> >>> c = Circuit().push(GateRYY(theta), 0, 1) >>> c 2-qubit circuit with 1 instruction: └── RYY(theta) @ q[0:1] <BLANKLINE> >>> GateRYY(theta).power(2), GateRYY(theta).inverse() (RYY(theta)**2, RYY(-theta)) >>> GateRYY(theta).decompose() 2-qubit circuit with 7 instructions: ├── RX((1/2)*pi) @ q[0] ├── RX((1/2)*pi) @ q[1] ├── CX @ q[0], q[1] ├── RZ(theta) @ q[1] ├── CX @ q[0], q[1] ├── RX((-1/2)*pi) @ q[0] └── RX((-1/2)*pi) @ q[1] <BLANKLINE> """ _name = "RYY" _num_qubits = 2 _qregsizes = [2] _parnames = ("theta",) def __init__(self, theta): self.theta = theta def _matrix(self): theta = self.theta cos2 = cos(theta / 2) sin2 = sin(theta / 2) return Matrix( [ [cos2, 0, 0, I * sin2], [0, cos2, -I * sin2, 0], [0, -I * sin2, cos2, 0], [I * sin2, 0, 0, cos2], ] ) def _matrix_numeric(self, theta): import math half = theta / 2.0 / math.pi c = _nm.cospi(half) s = _nm.sinpi(half) return np.array( [ [c, 0, 0, 1j * s], [0, c, -1j * s, 0], [0, -1j * s, c, 0], [1j * s, 0, 0, c], ], dtype=np.complex128, )
[docs] def inverse(self): return GateRYY(-self.theta)
def _decompose(self, circ, qubits, bits, zvars): a, b = qubits circ.push(GateRX(PI_2), a) circ.push(GateRX(PI_2), b) circ.push(GateCX(), a, b) circ.push(GateRZ(self.theta), b) circ.push(GateCX(), a, b) circ.push(GateRX(NEG_PI_2), a) circ.push(GateRX(NEG_PI_2), b) return circ
[docs] class GateRZZ(Gate): r"""Two qubit RZZ gate, a rotation generated by :math:`Z \otimes Z`. The gate is symmetric in its two qubits and is maximally entangling at :math:`\theta = \frac{\pi}{2}`. Matrix Representation: .. math:: \operatorname{RZZ}(\theta) = \begin{pmatrix} e^{-i\frac{\theta}{2}} & 0 & 0 & 0 \\ 0 & e^{i\frac{\theta}{2}} & 0 & 0 \\ 0 & 0 & e^{i\frac{\theta}{2}} & 0 \\ 0 & 0 & 0 & e^{-i\frac{\theta}{2}} \end{pmatrix} Args: theta: Rotation angle in radians, numeric or symbolic. Examples: >>> from mimiqcircuits import * >>> from symengine import * >>> theta = Symbol('theta') >>> GateRZZ(theta) RZZ(theta) >>> GateRZZ(theta).matrix() [exp(-1/2*I*theta), 0, 0, 0] [0, exp(1/2*I*theta), 0, 0] [0, 0, exp(1/2*I*theta), 0] [0, 0, 0, exp(-1/2*I*theta)] <BLANKLINE> >>> c = Circuit().push(GateRZZ(theta), 0, 1) >>> c 2-qubit circuit with 1 instruction: └── RZZ(theta) @ q[0:1] <BLANKLINE> >>> GateRZZ(theta).power(2), GateRZZ(theta).inverse() (RZZ(theta)**2, RZZ(-theta)) >>> GateRZZ(theta).decompose() 2-qubit circuit with 3 instructions: ├── CX @ q[0], q[1] ├── RZ(theta) @ q[1] └── CX @ q[0], q[1] <BLANKLINE> """ _num_qubits = 2 _name = "RZZ" _parnames = ("theta",) _qregsizes = [2] def __init__(self, theta): self.theta = theta def _matrix(self): return Matrix( [ [cis(-self.theta / 2), 0, 0, 0], [0, cis(self.theta / 2), 0, 0], [0, 0, cis(self.theta / 2), 0], [0, 0, 0, cis(-self.theta / 2)], ] ) def _matrix_numeric(self, theta): import math e_plus = _nm.cispi(theta / 2.0 / math.pi) e_minus = e_plus.conjugate() return np.array( [ [e_minus, 0, 0, 0], [0, e_plus, 0, 0], [0, 0, e_plus, 0], [0, 0, 0, e_minus], ], dtype=np.complex128, )
[docs] def inverse(self): return GateRZZ(-self.theta)
def _decompose(self, circ, qubits, bits, zvars): a, b = qubits circ.push(GateCX(), a, b) circ.push(GateRZ(self.theta), b) circ.push(GateCX(), a, b) return circ
[docs] class GateRZX(Gate): r"""Two qubit RZX gate, a rotation generated by :math:`Z \otimes X`. The :math:`Z` acts on the first qubit and the :math:`X` on the second. The gate is maximally entangling at :math:`\theta = \frac{\pi}{2}`. Matrix Representation: .. math:: \operatorname{RZX}(\theta) = \begin{pmatrix} \cos(\frac{\theta}{2}) & -i\sin(\frac{\theta}{2}) & 0 & 0 \\ -i\sin(\frac{\theta}{2}) & \cos(\frac{\theta}{2}) & 0 & 0 \\ 0 & 0 & \cos(\frac{\theta}{2}) & i\sin(\frac{\theta}{2}) \\ 0 & 0 & i\sin(\frac{\theta}{2}) & \cos(\frac{\theta}{2}) \end{pmatrix} Args: theta: Rotation angle in radians, numeric or symbolic. Examples: >>> from mimiqcircuits import * >>> from symengine import * >>> theta = Symbol('theta') >>> GateRZX(theta) RZX(theta) >>> GateRZX(theta).matrix() [cos((1/2)*theta), -I*sin((1/2)*theta), 0, 0] [-I*sin((1/2)*theta), cos((1/2)*theta), 0, 0] [0, 0, cos((1/2)*theta), I*sin((1/2)*theta)] [0, 0, I*sin((1/2)*theta), cos((1/2)*theta)] <BLANKLINE> >>> c = Circuit().push(GateRZX(theta), 0, 1) >>> c 2-qubit circuit with 1 instruction: └── RZX(theta) @ q[0:1] <BLANKLINE> >>> GateRZX(theta).power(2), GateRZX(theta).inverse() (RZX(theta)**2, RZX(-theta)) >>> GateRZX(theta).decompose() 2-qubit circuit with 5 instructions: ├── H @ q[1] ├── CX @ q[0], q[1] ├── RZ(theta) @ q[1] ├── CX @ q[0], q[1] └── H @ q[1] <BLANKLINE> """ _num_qubits = 2 _name = "RZX" _parnames = ("theta",) _qregsizes = [2] def __init__(self, theta): self.theta = theta def _matrix(self): theta = self.theta cos2 = cos(theta / 2) sin2 = sin(theta / 2) return Matrix( [ [cos2, -I * sin2, 0, 0], [-I * sin2, cos2, 0, 0], [0, 0, cos2, I * sin2], [0, 0, I * sin2, cos2], ] ) def _matrix_numeric(self, theta): import math half = theta / 2.0 / math.pi c = _nm.cospi(half) s = _nm.sinpi(half) return np.array( [ [c, -1j * s, 0, 0], [-1j * s, c, 0, 0], [0, 0, c, 1j * s], [0, 0, 1j * s, c], ], dtype=np.complex128, )
[docs] def inverse(self): return GateRZX(-self.theta)
def _decompose(self, circ, qubits, bits, zvars): a, b = qubits circ.push(GateH(), b) circ.push(GateCX(), a, b) circ.push(GateRZ(self.theta), b) circ.push(GateCX(), a, b) circ.push(GateH(), b) return circ
[docs] class GateXXplusYY(Gate): r"""Two qubit parametric XX+YY gate. Also known as an XY gate. It rotates coherently between :math:`|01\rangle` and :math:`|10\rangle` and leaves :math:`|00\rangle` and :math:`|11\rangle` unchanged. Matrix Representation: .. math:: \operatorname{(XX+YY)}(\theta, \beta)= \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos(\frac{\theta}{2}) & -i\sin(\frac{\theta}{2})e^{i\beta} & 0\\ 0 & -i\sin(\frac{\theta}{2})e^{-i\beta} & \cos(\frac{\theta}{2}) & 0\\ 0 & 0 & 0 & 1 \end{pmatrix} Args: theta: Rotation angle in radians, numeric or symbolic. beta: Phase angle in radians, numeric or symbolic. Examples: >>> from mimiqcircuits import * >>> from symengine import * >>> theta, beta = symbols('theta beta') >>> GateXXplusYY(theta, beta) XXplusYY(theta, beta) >>> GateXXplusYY(theta, beta).matrix() [1.0, 0, 0, 0] [0, cos((1/2)*theta), -I*exp(I*beta)*sin((1/2)*theta), 0] [0, -I*exp(-I*beta)*sin((1/2)*theta), cos((1/2)*theta), 0] [0, 0, 0, 1.0] <BLANKLINE> >>> c = Circuit().push(GateXXplusYY(theta, beta), 0, 1) >>> c 2-qubit circuit with 1 instruction: └── XXplusYY(theta, beta) @ q[0:1] <BLANKLINE> >>> GateXXplusYY(theta, beta).power(2), GateXXplusYY(theta, beta).inverse() (XXplusYY(theta, beta)**2, XXplusYY(-theta, beta)) >>> GateXXplusYY(theta, beta).decompose() 2-qubit circuit with 14 instructions: ├── RZ(beta) @ q[0] ├── RZ((-1/2)*pi) @ q[1] ├── SX @ q[1] ├── RZ((1/2)*pi) @ q[1] ├── S @ q[0] ├── CX @ q[1], q[0] ├── RY((-1/2)*theta) @ q[1] ├── RY((-1/2)*theta) @ q[0] ├── CX @ q[1], q[0] ├── S† @ q[0] ├── RZ((-1/2)*pi) @ q[1] ├── SX† @ q[1] ├── RZ((1/2)*pi) @ q[1] └── RZ(-beta) @ q[0] <BLANKLINE> """ _num_qubits = 2 _name = "XXplusYY" _parnames = ("theta", "beta") _qregsizes = [2] def __init__(self, theta, beta): self.theta = theta self.beta = beta def _matrix(self): theta = self.theta beta = self.beta cos2 = cos(theta / 2) sin2 = sin(theta / 2) return Matrix( [ [1, 0, 0, 0], [0, cos2, -I * sin2 * cis(beta), 0], [0, -I * sin2 * cis(-beta), cos2, 0], [0, 0, 0, 1], ] ) def _matrix_numeric(self, theta, beta): import math half = theta / 2.0 / math.pi c = _nm.cospi(half) s = _nm.sinpi(half) e = _nm.cis(beta) return np.array( [ [1, 0, 0, 0], [0, c, -1j * s * e, 0], [0, -1j * s * e.conjugate(), c, 0], [0, 0, 0, 1], ], dtype=np.complex128, )
[docs] def inverse(self): return GateXXplusYY(-self.theta, self.beta)
def _decompose(self, circ, qubits, bits, zvars): a, b = qubits circ.push(GateRZ(self.beta), a) circ.push(GateRZ(NEG_PI_2), b) circ.push(GateSX(), b) circ.push(GateRZ(PI_2), b) circ.push(GateS(), a) circ.push(GateCX(), b, a) circ.push(GateRY(-self.theta / 2), b) circ.push(GateRY(-self.theta / 2), a) circ.push(GateCX(), b, a) circ.push(GateSDG(), a) circ.push(GateRZ(NEG_PI_2), b) circ.push(GateSXDG(), b) circ.push(GateRZ(PI_2), b) circ.push(GateRZ(-self.beta), a) return circ
[docs] class GateXXminusYY(Gate): r"""Two qubit parametric XX-YY gate. It rotates coherently between :math:`|00\rangle` and :math:`|11\rangle` and leaves :math:`|01\rangle` and :math:`|10\rangle` unchanged. Matrix Representation: .. math:: \operatorname{(XX-YY)}(\theta, \beta)=\begin{pmatrix} \cos(\frac{\theta}{2}) & 0 & 0 & -i\sin(\frac{\theta}{2})e^{-i\beta} \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ -i\sin(\frac{\theta}{2})e^{i\beta} & 0 & 0 & \cos(\frac{\theta}{2}) \end{pmatrix} Args: theta: Rotation angle in radians, numeric or symbolic. beta: Phase angle in radians, numeric or symbolic. Examples: >>> from mimiqcircuits import * >>> from symengine import * >>> theta, beta = symbols('theta beta') >>> GateXXminusYY(theta, beta) XXminusYY(theta, beta) >>> GateXXminusYY(theta, beta).matrix() [cos((1/2)*theta), 0, 0, -I*exp(-I*beta)*sin((1/2)*theta)] [0, 1.0, 0, 0] [0, 0, 1.0, 0] [-I*exp(I*beta)*sin((1/2)*theta), 0, 0, cos((1/2)*theta)] <BLANKLINE> >>> c = Circuit().push(GateXXminusYY(theta, beta), 0, 1) >>> c 2-qubit circuit with 1 instruction: └── XXminusYY(theta, beta) @ q[0:1] <BLANKLINE> >>> GateXXminusYY(theta, beta).power(2), GateXXminusYY(theta, beta).inverse() (XXminusYY(theta, beta)**2, XXminusYY(-theta, beta)) >>> GateXXminusYY(theta, beta).decompose() 2-qubit circuit with 14 instructions: ├── RZ(-beta) @ q[1] ├── RZ((-1/2)*pi) @ q[0] ├── SX @ q[0] ├── RZ((1/2)*pi) @ q[0] ├── S @ q[1] ├── CX @ q[0], q[1] ├── RY((1/2)*theta) @ q[0] ├── RY((-1/2)*theta) @ q[1] ├── CX @ q[0], q[1] ├── S† @ q[1] ├── RZ((-1/2)*pi) @ q[0] ├── SX† @ q[0] ├── RZ((1/2)*pi) @ q[0] └── RZ(beta) @ q[1] <BLANKLINE> """ _num_qubits = 2 _name = "XXminusYY" _parnames = ("theta", "beta") _qregsizes = [2] def __init__(self, theta, beta): self.theta = theta self.beta = beta def _matrix(self): theta = self.theta beta = self.beta cos2 = cos(theta / 2) sin2 = sin(theta / 2) return Matrix( [ [cos2, 0, 0, -I * sin2 * cis(-beta)], [0, 1, 0, 0], [0, 0, 1, 0], [-I * sin2 * cis(beta), 0, 0, cos2], ] ) def _matrix_numeric(self, theta, beta): import math half = theta / 2.0 / math.pi c = _nm.cospi(half) s = _nm.sinpi(half) e = _nm.cis(beta) return np.array( [ [c, 0, 0, -1j * s * e.conjugate()], [0, 1, 0, 0], [0, 0, 1, 0], [-1j * s * e, 0, 0, c], ], dtype=np.complex128, )
[docs] def inverse(self): return GateXXminusYY(-self.theta, self.beta)
def _decompose(self, circ, qubits, bits, zvars): a, b = qubits circ.push(GateRZ(-self.beta), b) circ.push(GateRZ(NEG_PI_2), a) circ.push(GateSX(), a) circ.push(GateRZ(PI_2), a) circ.push(GateS(), b) circ.push(GateCX(), a, b) circ.push(GateRY(self.theta / 2), a) circ.push(GateRY(-self.theta / 2), b) circ.push(GateCX(), a, b) circ.push(GateSDG(), b) circ.push(GateRZ(NEG_PI_2), a) circ.push(GateSXDG(), a) circ.push(GateRZ(PI_2), a) circ.push(GateRZ(self.beta), b) return circ