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

#
# 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.
#
"""S and SDG gates."""

from fractions import Fraction

from mimiqcircuits.operations.gates.standard.pauli import GateZ
from mimiqcircuits.operations.gates.standard.u import GateU
from mimiqcircuits.operations.inverse import (
    Inverse,
    canonical_inverse,
    register_inverse_decomposition,
)
from mimiqcircuits.operations.power import (
    Power,
    canonical_power,
    register_power_alias,
    register_power_decomposition,
)
from mimiqcircuits.operations.gates.standard._angles import PI_2, NEG_PI_2


# Fractions are immutable, so all instances share this one: building a
# Fraction costs more than the rest of the constructor.
_EXPONENT = Fraction(1, 2)


[docs] @canonical_power(GateZ, _EXPONENT) class GateS(Power): r"""Single qubit S gate, a phase gate with angle :math:`\pi/2`. Equal to ``GateZ().power(1/2)`` and to ``GateP(pi/2)``. Matrix Representation: .. math:: \operatorname{S} = \begin{pmatrix} 1 & 0 \\ 0 & i \end{pmatrix} Examples: >>> from mimiqcircuits import * >>> GateS() S >>> GateS().matrix() [1.0, 0] [0, 0.0 + 1.0*I] <BLANKLINE> >>> c = Circuit().push(GateS(), 0) >>> c 1-qubit circuit with 1 instruction: └── S @ q[0] <BLANKLINE> >>> GateS().power(2), GateS().inverse() (Z, S†) >>> GateS().decompose() 1-qubit circuit with 1 instruction: └── U(0, 0, (1/2)*pi, 0.0) @ q[0] <BLANKLINE> """
[docs] def __init__(self, operation=None, exponent=None): """Initialize an S gate. The arguments are unused.""" super().__init__(GateZ(), _EXPONENT)
register_power_alias(GateZ, 1 / 2, "S") @register_power_decomposition(GateZ, 1 / 2) def _decompose_gates(self, circ, qubits, bits, zvars): q = qubits[0] circ.push(GateU(0, 0, PI_2), q) return circ
[docs] @canonical_inverse(GateS) class GateSDG(Inverse): r"""Single qubit S† gate, the inverse of :class:`GateS`. Matrix Representation: .. math:: \operatorname{S}^\dagger = \begin{pmatrix} 1 & 0 \\ 0 & -i \end{pmatrix} Examples: >>> from mimiqcircuits import * >>> GateSDG() S† >>> GateSDG().matrix() [1.0, 0] [0, 6.12323399573677e-17 - 1.0*I] <BLANKLINE> >>> c = Circuit().push(GateSDG(), 0) >>> c 1-qubit circuit with 1 instruction: └── S† @ q[0] <BLANKLINE> >>> GateSDG().power(2), GateSDG().inverse() ((S†)**2, S) >>> GateSDG().decompose() 1-qubit circuit with 1 instruction: └── U(0, 0, (-1/2)*pi, 0.0) @ q[0] <BLANKLINE> """
[docs] def __init__(self, operation=None): """Initialize an SDG gate. The argument is unused.""" super().__init__(GateS())
@register_inverse_decomposition((Power, GateZ, 1 / 2)) def _decompose_gatesdg(self, circ, qubits, bits, zvars): q = qubits[0] circ.push(GateU(0, 0, NEG_PI_2), q) return circ