Skip to content

Commit

Permalink
feat: reduce number of classes
Browse files Browse the repository at this point in the history
  • Loading branch information
king-p3nguin committed May 9, 2024
1 parent 5d549b3 commit 9e34d41
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 439 deletions.
60 changes: 36 additions & 24 deletions qiskit/circuit/library/standard_gates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,55 @@
Standard gates
"""

from .h import HGate, CHGate
from .dcx import DCXGate
from .ecr import ECRGate
from .global_phase import GlobalPhaseGate
from .h import CHGate, HGate
from .i import IGate
from .p import PhaseGate, CPhaseGate, MCPhaseGate
from .iswap import iSwapGate
from .p import CPhaseGate, MCPhaseGate, PhaseGate
from .r import RGate
from .rx import RXGate, CRXGate, MCRXPUCXBasis, MCRXGate
from .rx import CRXGate, MCRXGate, RXGate
from .rxx import RXXGate
from .ry import RYGate, CRYGate, MCRYGate, MCRYPUCXBasis, MCRYVChain
from .ry import CRYGate, MCRYGate, RYGate
from .ryy import RYYGate
from .rz import RZGate, CRZGate, MCRZGate, MCRZPUCXBasis
from .rzz import RZZGate
from .rz import CRZGate, MCRZGate, RZGate
from .rzx import RZXGate
from .rzz import RZZGate
from .s import CSdgGate, CSGate, SdgGate, SGate
from .swap import CSwapGate, SwapGate
from .sx import CSXGate, SXdgGate, SXGate
from .t import TdgGate, TGate
from .u import CUGate, UGate
from .u1 import CU1Gate, MCU1Gate, U1Gate
from .u2 import U2Gate
from .u3 import CU3Gate, U3Gate
from .x import (
C3SXGate,
C3XGate,
C4XGate,
CCXGate,
CXGate,
MCXGate,
MCXGrayCode,
MCXRecursive,
MCXVChain,
RC3XGate,
RCCXGate,
XGate,
)
from .xx_minus_yy import XXMinusYYGate
from .xx_plus_yy import XXPlusYYGate
from .ecr import ECRGate
from .s import SGate, SdgGate, CSGate, CSdgGate
from .swap import SwapGate, CSwapGate
from .iswap import iSwapGate
from .sx import SXGate, SXdgGate, CSXGate
from .dcx import DCXGate
from .t import TGate, TdgGate
from .u import UGate, CUGate
from .u1 import U1Gate, CU1Gate, MCU1Gate
from .u2 import U2Gate
from .u3 import U3Gate, CU3Gate
from .x import XGate, CXGate, CCXGate, C3XGate, C3SXGate, C4XGate, RCCXGate, RC3XGate
from .x import MCXGate, MCXGrayCode, MCXRecursive, MCXVChain
from .y import YGate, CYGate
from .z import ZGate, CZGate, CCZGate
from .global_phase import GlobalPhaseGate
from .y import CYGate, YGate
from .z import CCZGate, CZGate, ZGate


def get_standard_gate_name_mapping():
"""Return a dictionary mapping the name of standard gates and instructions to an object for
that name."""
from qiskit.circuit.parameter import Parameter
from qiskit.circuit.measure import Measure
from qiskit.circuit.delay import Delay
from qiskit.circuit.measure import Measure
from qiskit.circuit.parameter import Parameter
from qiskit.circuit.reset import Reset

# Standard gates library mapping, multicontrolled gates not included since they're
Expand Down
92 changes: 18 additions & 74 deletions qiskit/circuit/library/standard_gates/rx.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ def __init__(
label: Optional[str] = None,
ctrl_state: Optional[Union[str, int]] = None,
*,
use_basis_gates: bool = False,
duration=None,
unit="dt",
_base_label=None,
Expand All @@ -322,6 +323,7 @@ def __init__(
duration=duration,
unit=unit,
)
self._use_basis_gates = use_basis_gates

def inverse(self, annotated: bool = False):
r"""Return inverse MCRX gate (i.e. with the negative rotation angle).
Expand All @@ -335,9 +337,11 @@ def inverse(self, annotated: bool = False):
Returns:
MCRXGate: inverse gate.
"""
# use __class__ so this works for derived classes
return self.__class__(
-self.params[0], num_ctrl_qubits=self.num_ctrl_qubits, ctrl_state=self.ctrl_state
return MCRXGate(
-self.params[0],
num_ctrl_qubits=self.num_ctrl_qubits,
ctrl_state=self.ctrl_state,
use_basis_gates=self._use_basis_gates,
)

def _define(self):
Expand All @@ -349,7 +353,15 @@ def _define(self):
q_controls = list(range(self.num_ctrl_qubits))
q_target = self.num_ctrl_qubits
if self.num_ctrl_qubits == 1:
qc.crx(self.params[0], q_controls[0], q_target)
_apply_cu(
qc,
self.params[0],
-pi / 2,
pi / 2,
q_controls[0],
q_target,
use_basis_gates=self._use_basis_gates,
)
elif self.num_ctrl_qubits < 4:
theta_step = self.params[0] * (1 / (2 ** (self.num_ctrl_qubits - 1)))
_apply_mcu_graycode(
Expand All @@ -359,13 +371,13 @@ def _define(self):
pi / 2,
q_controls,
q_target,
use_basis_gates=False,
use_basis_gates=self._use_basis_gates,
)
else:
cgate = _mcsu2_real_diagonal(
RXGate(self.params[0]).to_matrix(),
num_controls=self.num_ctrl_qubits,
use_basis_gates=False,
use_basis_gates=self._use_basis_gates,
)
qc.compose(cgate, q_controls + [q_target], inplace=True)
self.definition = qc
Expand Down Expand Up @@ -405,74 +417,6 @@ def control(
return gate


class MCRXPUCXBasis(MCRXGate):
r"""The general, multi-controlled X rotation gate using p, u, and cx as basis gates.
Can be applied to a :class:`~qiskit.circuit.QuantumCircuit`
with the :meth:`~qiskit.circuit.QuantumCircuit.mcrx` method.
"""

def __init__(
self,
theta: ParameterValueType, # type: ignore
num_ctrl_qubits: int,
label: Optional[str] = None,
ctrl_state: Optional[Union[str, int]] = None,
*,
duration=None,
unit="dt",
_base_label=None,
):
"""Create new MCRXPUCXBasis gate."""
super().__init__(
theta=theta,
num_ctrl_qubits=num_ctrl_qubits,
label=label,
ctrl_state=ctrl_state,
duration=duration,
unit=unit,
_base_label=_base_label,
)

def _define(self):
# pylint: disable=cyclic-import
from qiskit.circuit.quantumcircuit import QuantumCircuit

q = QuantumRegister(self.num_qubits, name="q")
qc = QuantumCircuit(q)
q_controls = list(range(self.num_ctrl_qubits))
q_target = self.num_ctrl_qubits
if self.num_ctrl_qubits == 1:
_apply_cu(
qc,
self.params[0],
-pi / 2,
pi / 2,
q_controls[0],
q_target,
use_basis_gates=True,
)
elif self.num_ctrl_qubits < 4:
theta_step = self.params[0] * (1 / (2 ** (self.num_ctrl_qubits - 1)))
_apply_mcu_graycode(
qc,
theta_step,
-pi / 2,
pi / 2,
q_controls,
q_target,
use_basis_gates=True,
)
else:
cgate = _mcsu2_real_diagonal(
RXGate(self.params[0]).to_matrix(),
num_controls=self.num_ctrl_qubits,
use_basis_gates=True,
)
qc.compose(cgate, q_controls + [q_target], inplace=True)
self.definition = qc


def _apply_cu(circuit, theta, phi, lam, control, target, use_basis_gates=True):
if use_basis_gates:
# ┌──────────────┐
Expand Down
Loading

0 comments on commit 9e34d41

Please sign in to comment.