Skip to content

Commit

Permalink
Revert "Use singletons for standard library unparameterized, non-cont…
Browse files Browse the repository at this point in the history
…rolled gates (Qiskit#10314)"

This reverts commit e62c86b.
  • Loading branch information
kevinhartman committed Sep 25, 2023
1 parent afecc5d commit 5af7554
Show file tree
Hide file tree
Showing 36 changed files with 169 additions and 950 deletions.
2 changes: 0 additions & 2 deletions qiskit/circuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@
InstructionSet
Operation
EquivalenceLibrary
SingletonGate
Control Flow Operations
-----------------------
Expand Down Expand Up @@ -367,7 +366,6 @@

# pylint: disable=cyclic-import
from .controlledgate import ControlledGate
from .singleton_gate import SingletonGate
from .instruction import Instruction
from .instructionset import InstructionSet
from .operation import Operation
Expand Down
4 changes: 1 addition & 3 deletions qiskit/circuit/add_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ def add_control(
# attempt decomposition
operation._define()
cgate = control(operation, num_ctrl_qubits=num_ctrl_qubits, label=label, ctrl_state=ctrl_state)
if operation.label is not None:
cgate.base_gate = cgate.base_gate.to_mutable()
cgate.base_gate.label = operation.label
cgate.base_gate.label = operation.label
return cgate


Expand Down
12 changes: 2 additions & 10 deletions qiskit/circuit/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,7 @@
class Gate(Instruction):
"""Unitary gate."""

def __init__(
self,
name: str,
num_qubits: int,
params: list,
label: str | None = None,
duration=None,
unit="dt",
) -> None:
def __init__(self, name: str, num_qubits: int, params: list, label: str | None = None) -> None:
"""Create a new gate.
Args:
Expand All @@ -42,7 +34,7 @@ def __init__(
label: An optional label for the gate.
"""
self.definition = None
super().__init__(name, num_qubits, 0, params, label=label, duration=duration, unit=unit)
super().__init__(name, num_qubits, 0, params, label=label)

# Set higher priority than Numpy array and matrix classes
__array_priority__ = 20
Expand Down
33 changes: 3 additions & 30 deletions qiskit/circuit/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,43 +94,16 @@ def __init__(self, name, num_qubits, num_clbits, params, duration=None, unit="dt
self._label = label
# tuple (ClassicalRegister, int), tuple (Clbit, bool) or tuple (Clbit, int)
# when the instruction has a conditional ("if")
self._condition = None
self.condition = None
# list of instructions (and their contexts) that this instruction is composed of
# empty definition means opaque or fundamental instruction
self._definition = None

self._duration = duration
self._unit = unit

self.params = params # must be at last (other properties may be required for validation)

@property
def mutable(self) -> bool:
"""Is this instance is a mutable unique instance or not.
If this attribute is ``False`` the gate instance is a shared singleton
and is not mutable.
"""
return True

def to_mutable(self):
"""Return a mutable copy of this gate.
This method will return a new mutable copy of this gate instance.
If a singleton instance is being used this will be a new unique
instance that can be mutated. If the instance is already mutable it
will be a deepcopy of that instance.
"""
return self.copy()

@property
def condition(self):
"""The classical condition on the instruction."""
return self._condition

@condition.setter
def condition(self, condition):
self._condition = condition

def __eq__(self, other):
"""Two instructions are the same if they have the same name,
same dimensions, and same params.
Expand Down Expand Up @@ -436,7 +409,7 @@ def c_if(self, classical, val):
# Casting the conditional value as Boolean when
# the classical condition is on a classical bit.
val = bool(val)
self._condition = (classical, val)
self.condition = (classical, val)
return self

def copy(self, name=None):
Expand Down
2 changes: 1 addition & 1 deletion qiskit/circuit/instructionset.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def c_if(self, classical: Clbit | ClassicalRegister | int, val: int) -> "Instruc
if self._requester is not None:
classical = self._requester(classical)
for instruction in self._instructions:
instruction.operation = instruction.operation.c_if(classical, val)
instruction.operation.c_if(classical, val)
return self

# Legacy support for properties. Added in Terra 0.21 to support the internal switch in
Expand Down
13 changes: 4 additions & 9 deletions qiskit/circuit/library/standard_gates/dcx.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

"""Double-CNOT gate."""

from qiskit.circuit.singleton_gate import SingletonGate
from qiskit.circuit.gate import Gate
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit._utils import with_gate_array


@with_gate_array([[1, 0, 0, 0], [0, 0, 0, 1], [0, 1, 0, 0], [0, 0, 1, 0]])
class DCXGate(SingletonGate):
class DCXGate(Gate):
r"""Double-CNOT gate.
A 2-qubit Clifford gate consisting of two back-to-back
Expand Down Expand Up @@ -48,14 +48,9 @@ class DCXGate(SingletonGate):
\end{pmatrix}
"""

def __init__(self, label=None, duration=None, unit=None, _condition=None):
def __init__(self):
"""Create new DCX gate."""
if unit is None:
unit = "dt"

super().__init__(
"dcx", 2, [], label=label, _condition=_condition, duration=duration, unit=unit
)
super().__init__("dcx", 2, [])

def _define(self):
"""
Expand Down
12 changes: 4 additions & 8 deletions qiskit/circuit/library/standard_gates/ecr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
import numpy as np

from qiskit.circuit._utils import with_gate_array
from qiskit.circuit.gate import Gate
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit.singleton_gate import SingletonGate
from .rzx import RZXGate
from .x import XGate


@with_gate_array(
sqrt(0.5) * np.array([[0, 1, 0, 1.0j], [1, 0, -1.0j, 0], [0, 1.0j, 0, 1], [-1.0j, 0, 1, 0]])
)
class ECRGate(SingletonGate):
class ECRGate(Gate):
r"""An echoed cross-resonance gate.
This gate is maximally entangling and is equivalent to a CNOT up to
Expand Down Expand Up @@ -84,13 +84,9 @@ class ECRGate(SingletonGate):
\end{pmatrix}
"""

def __init__(self, label=None, _condition=None, duration=None, unit=None):
def __init__(self):
"""Create new ECR gate."""
if unit is None:
unit = "dt"
super().__init__(
"ecr", 2, [], label=label, _condition=_condition, duration=duration, unit=unit
)
super().__init__("ecr", 2, [])

def _define(self):
"""
Expand Down
30 changes: 8 additions & 22 deletions qiskit/circuit/library/standard_gates/h.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import Optional, Union
import numpy
from qiskit.circuit.controlledgate import ControlledGate
from qiskit.circuit.singleton_gate import SingletonGate
from qiskit.circuit.gate import Gate
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit._utils import with_gate_array, with_controlled_gate_array
from .t import TGate, TdgGate
Expand All @@ -25,7 +25,7 @@


@with_gate_array(_H_ARRAY)
class HGate(SingletonGate):
class HGate(Gate):
r"""Single-qubit Hadamard gate.
This gate is a \pi rotation about the X+Z axis, and has the effect of
Expand Down Expand Up @@ -54,13 +54,9 @@ class HGate(SingletonGate):
\end{pmatrix}
"""

def __init__(self, label: Optional[str] = None, duration=None, unit=None, _condition=None):
def __init__(self, label: Optional[str] = None):
"""Create new H gate."""
if unit is None:
unit = "dt"
super().__init__(
"h", 1, [], label=label, _condition=_condition, duration=duration, unit=unit
)
super().__init__("h", 1, [], label=label)

def _define(self):
"""
Expand Down Expand Up @@ -98,7 +94,8 @@ def control(
ControlledGate: controlled version of this gate.
"""
if num_ctrl_qubits == 1:
gate = CHGate(label=label, ctrl_state=ctrl_state, _base_label=self.label)
gate = CHGate(label=label, ctrl_state=ctrl_state)
gate.base_gate.label = self.label
return gate
return super().control(num_ctrl_qubits=num_ctrl_qubits, label=label, ctrl_state=ctrl_state)

Expand Down Expand Up @@ -165,21 +162,10 @@ class CHGate(ControlledGate):
\end{pmatrix}
"""

def __init__(
self,
label: Optional[str] = None,
ctrl_state: Optional[Union[int, str]] = None,
_base_label=None,
):
def __init__(self, label: Optional[str] = None, ctrl_state: Optional[Union[int, str]] = None):
"""Create new CH gate."""
super().__init__(
"ch",
2,
[],
num_ctrl_qubits=1,
label=label,
ctrl_state=ctrl_state,
base_gate=HGate(label=_base_label),
"ch", 2, [], num_ctrl_qubits=1, label=label, ctrl_state=ctrl_state, base_gate=HGate()
)

def _define(self):
Expand Down
12 changes: 4 additions & 8 deletions qiskit/circuit/library/standard_gates/i.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
"""Identity gate."""

from typing import Optional
from qiskit.circuit.singleton_gate import SingletonGate
from qiskit.circuit.gate import Gate
from qiskit.circuit._utils import with_gate_array


@with_gate_array([[1, 0], [0, 1]])
class IGate(SingletonGate):
class IGate(Gate):
r"""Identity gate.
Identity gate corresponds to a single-qubit gate wait cycle,
Expand All @@ -45,13 +45,9 @@ class IGate(SingletonGate):
└───┘
"""

def __init__(self, label: Optional[str] = None, duration=None, unit=None, _condition=None):
def __init__(self, label: Optional[str] = None):
"""Create new Identity gate."""
if unit is None:
unit = "dt"
super().__init__(
"id", 1, [], label=label, _condition=_condition, duration=duration, unit=unit
)
super().__init__("id", 1, [], label=label)

def inverse(self):
"""Invert this gate."""
Expand Down
12 changes: 4 additions & 8 deletions qiskit/circuit/library/standard_gates/iswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

import numpy as np

from qiskit.circuit.singleton_gate import SingletonGate
from qiskit.circuit.gate import Gate
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit._utils import with_gate_array

from .xx_plus_yy import XXPlusYYGate


@with_gate_array([[1, 0, 0, 0], [0, 0, 1j, 0], [0, 1j, 0, 0], [0, 0, 0, 1]])
class iSwapGate(SingletonGate):
class iSwapGate(Gate):
r"""iSWAP gate.
A 2-qubit XX+YY interaction.
Expand Down Expand Up @@ -85,13 +85,9 @@ class iSwapGate(SingletonGate):
\end{pmatrix}
"""

def __init__(self, label: Optional[str] = None, duration=None, unit=None, _condition=None):
def __init__(self, label: Optional[str] = None):
"""Create new iSwap gate."""
if unit is None:
unit = "dt"
super().__init__(
"iswap", 2, [], label=label, _condition=_condition, duration=duration, unit=unit
)
super().__init__("iswap", 2, [], label=label)

def _define(self):
"""
Expand Down
22 changes: 7 additions & 15 deletions qiskit/circuit/library/standard_gates/s.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import numpy

from qiskit.circuit.controlledgate import ControlledGate
from qiskit.circuit.singleton_gate import SingletonGate
from qiskit.circuit.gate import Gate
from qiskit.circuit.library.standard_gates.p import CPhaseGate, PhaseGate
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit._utils import with_gate_array, with_controlled_gate_array
Expand All @@ -29,7 +29,7 @@


@with_gate_array(_S_ARRAY)
class SGate(SingletonGate):
class SGate(Gate):
r"""Single qubit S gate (Z**0.5).
It induces a :math:`\pi/2` phase, and is sometimes called the P gate (phase).
Expand Down Expand Up @@ -59,13 +59,9 @@ class SGate(SingletonGate):
Equivalent to a :math:`\pi/2` radian rotation about the Z axis.
"""

def __init__(self, label: Optional[str] = None, duration=None, unit=None, _condition=None):
def __init__(self, label: Optional[str] = None):
"""Create new S gate."""
if unit is None:
unit = "dt"
super().__init__(
"s", 1, [], label=label, _condition=_condition, duration=duration, unit=unit
)
super().__init__("s", 1, [], label=label)

def _define(self):
"""
Expand Down Expand Up @@ -94,7 +90,7 @@ def power(self, exponent: float):


@with_gate_array(_SDG_ARRAY)
class SdgGate(SingletonGate):
class SdgGate(Gate):
r"""Single qubit S-adjoint gate (~Z**0.5).
It induces a :math:`-\pi/2` phase.
Expand Down Expand Up @@ -124,13 +120,9 @@ class SdgGate(SingletonGate):
Equivalent to a :math:`-\pi/2` radian rotation about the Z axis.
"""

def __init__(self, label: Optional[str] = None, duration=None, unit=None, _condition=None):
def __init__(self, label: Optional[str] = None):
"""Create new Sdg gate."""
if unit is None:
unit = "dt"
super().__init__(
"sdg", 1, [], label=label, _condition=_condition, duration=duration, unit=unit
)
super().__init__("sdg", 1, [], label=label)

def _define(self):
"""
Expand Down
Loading

0 comments on commit 5af7554

Please sign in to comment.