Skip to content

Commit

Permalink
Add duration and unit kwargs to __init__ for all standard gates (#11013)
Browse files Browse the repository at this point in the history
* Add duration and unit to kwargs for all standard_gates

* Add release notes

* Make duration and unit keyword only

* Normalise all standard-library gate signatures

This corrects a couple of places that had `unit=None` instead of
`unit="dt", and makes sure that all gates in a given file have the right
signature.

* Fix bad default argument

---------

Co-authored-by: Jake Lishman <[email protected]>
  • Loading branch information
hunterkemeny and jakelishman authored Oct 19, 2023
1 parent cefd6a3 commit 9f3a7cf
Show file tree
Hide file tree
Showing 20 changed files with 166 additions and 45 deletions.
6 changes: 4 additions & 2 deletions qiskit/circuit/library/standard_gates/global_phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ class GlobalPhaseGate(Gate):
\end{pmatrix}
"""

def __init__(self, phase: ParameterValueType, label: Optional[str] = None):
def __init__(
self, phase: ParameterValueType, label: Optional[str] = None, *, duration=None, unit="dt"
):
"""
Args:
phase: The value of phase it takes.
label: An optional label for the gate.
"""
super().__init__("global_phase", 0, [phase], label=label)
super().__init__("global_phase", 0, [phase], label=label, duration=duration, unit=unit)

def _define(self):
q = QuantumRegister(0, "q")
Expand Down
29 changes: 24 additions & 5 deletions qiskit/circuit/library/standard_gates/p.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ class PhaseGate(Gate):
`1612.00858 <https://arxiv.org/abs/1612.00858>`_
"""

def __init__(self, theta: ParameterValueType, label: str | None = None):
def __init__(
self, theta: ParameterValueType, label: str | None = None, *, duration=None, unit="dt"
):
"""Create new Phase gate."""
super().__init__("p", 1, [theta], label=label)
super().__init__("p", 1, [theta], label=label, duration=duration, unit=unit)

def _define(self):
# pylint: disable=cyclic-import
Expand Down Expand Up @@ -177,6 +179,10 @@ def __init__(
theta: ParameterValueType,
label: str | None = None,
ctrl_state: str | int | None = None,
*,
duration=None,
unit="dt",
_base_label=None,
):
"""Create new CPhase gate."""
super().__init__(
Expand All @@ -186,7 +192,9 @@ def __init__(
num_ctrl_qubits=1,
label=label,
ctrl_state=ctrl_state,
base_gate=PhaseGate(theta),
base_gate=PhaseGate(theta, label=_base_label),
duration=duration,
unit=unit,
)

def _define(self):
Expand Down Expand Up @@ -284,15 +292,26 @@ class MCPhaseGate(ControlledGate):
The singly-controlled-version of this gate.
"""

def __init__(self, lam: ParameterValueType, num_ctrl_qubits: int, label: str | None = None):
def __init__(
self,
lam: ParameterValueType,
num_ctrl_qubits: int,
label: str | None = None,
*,
duration=None,
unit="dt",
_base_label=None,
):
"""Create new MCPhase gate."""
super().__init__(
"mcphase",
num_ctrl_qubits + 1,
[lam],
num_ctrl_qubits=num_ctrl_qubits,
label=label,
base_gate=PhaseGate(lam),
base_gate=PhaseGate(lam, label=_base_label),
duration=duration,
unit=unit,
)

def _define(self):
Expand Down
10 changes: 8 additions & 2 deletions qiskit/circuit/library/standard_gates/r.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ class RGate(Gate):
"""

def __init__(
self, theta: ParameterValueType, phi: ParameterValueType, label: Optional[str] = None
self,
theta: ParameterValueType,
phi: ParameterValueType,
label: Optional[str] = None,
*,
duration=None,
unit="dt",
):
"""Create new r single-qubit gate."""
super().__init__("r", 1, [theta, phi], label=label)
super().__init__("r", 1, [theta, phi], label=label, duration=duration, unit=unit)

def _define(self):
"""
Expand Down
12 changes: 9 additions & 3 deletions qiskit/circuit/library/standard_gates/rx.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ class RXGate(Gate):
\end{pmatrix}
"""

def __init__(self, theta: ParameterValueType, label: Optional[str] = None):
def __init__(
self, theta: ParameterValueType, label: Optional[str] = None, *, duration=None, unit="dt"
):
"""Create new RX gate."""
super().__init__("rx", 1, [theta], label=label)
super().__init__("rx", 1, [theta], label=label, duration=duration, unit=unit)

def _define(self):
"""
Expand Down Expand Up @@ -175,6 +177,10 @@ def __init__(
theta: ParameterValueType,
label: Optional[str] = None,
ctrl_state: Optional[Union[str, int]] = None,
*,
duration=None,
unit="dt",
_base_label=None,
):
"""Create new CRX gate."""
super().__init__(
Expand All @@ -184,7 +190,7 @@ def __init__(
num_ctrl_qubits=1,
label=label,
ctrl_state=ctrl_state,
base_gate=RXGate(theta),
base_gate=RXGate(theta, label=_base_label),
)

def _define(self):
Expand Down
6 changes: 4 additions & 2 deletions qiskit/circuit/library/standard_gates/rxx.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ class RXXGate(Gate):
\end{pmatrix}
"""

def __init__(self, theta: ParameterValueType, label: Optional[str] = None):
def __init__(
self, theta: ParameterValueType, label: Optional[str] = None, *, duration=None, unit="dt"
):
"""Create new RXX gate."""
super().__init__("rxx", 2, [theta], label=label)
super().__init__("rxx", 2, [theta], label=label, duration=duration, unit=unit)

def _define(self):
"""Calculate a subcircuit that implements this unitary."""
Expand Down
14 changes: 11 additions & 3 deletions qiskit/circuit/library/standard_gates/ry.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ class RYGate(Gate):
\end{pmatrix}
"""

def __init__(self, theta: ParameterValueType, label: Optional[str] = None):
def __init__(
self, theta: ParameterValueType, label: Optional[str] = None, *, duration=None, unit="dt"
):
"""Create new RY gate."""
super().__init__("ry", 1, [theta], label=label)
super().__init__("ry", 1, [theta], label=label, duration=duration, unit=unit)

def _define(self):
"""
Expand Down Expand Up @@ -174,6 +176,10 @@ def __init__(
theta: ParameterValueType,
label: Optional[str] = None,
ctrl_state: Optional[Union[str, int]] = None,
*,
duration=None,
unit="dt",
_base_label=None,
):
"""Create new CRY gate."""
super().__init__(
Expand All @@ -183,7 +189,9 @@ def __init__(
num_ctrl_qubits=1,
label=label,
ctrl_state=ctrl_state,
base_gate=RYGate(theta),
base_gate=RYGate(theta, label=_base_label),
duration=duration,
unit=unit,
)

def _define(self):
Expand Down
6 changes: 4 additions & 2 deletions qiskit/circuit/library/standard_gates/ryy.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ class RYYGate(Gate):
\end{pmatrix}
"""

def __init__(self, theta: ParameterValueType, label: Optional[str] = None):
def __init__(
self, theta: ParameterValueType, label: Optional[str] = None, *, duration=None, unit="dt"
):
"""Create new RYY gate."""
super().__init__("ryy", 2, [theta], label=label)
super().__init__("ryy", 2, [theta], label=label, duration=duration, unit=unit)

def _define(self):
"""Calculate a subcircuit that implements this unitary."""
Expand Down
14 changes: 11 additions & 3 deletions qiskit/circuit/library/standard_gates/rz.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ class RZGate(Gate):
`1612.00858 <https://arxiv.org/abs/1612.00858>`_
"""

def __init__(self, phi: ParameterValueType, label: Optional[str] = None):
def __init__(
self, phi: ParameterValueType, label: Optional[str] = None, *, duration=None, unit="dt"
):
"""Create new RZ gate."""
super().__init__("rz", 1, [phi], label=label)
super().__init__("rz", 1, [phi], label=label, duration=duration, unit=unit)

def _define(self):
"""
Expand Down Expand Up @@ -192,6 +194,10 @@ def __init__(
theta: ParameterValueType,
label: Optional[str] = None,
ctrl_state: Optional[Union[str, int]] = None,
*,
duration=None,
unit="dt",
_base_label=None,
):
"""Create new CRZ gate."""
super().__init__(
Expand All @@ -201,7 +207,9 @@ def __init__(
num_ctrl_qubits=1,
label=label,
ctrl_state=ctrl_state,
base_gate=RZGate(theta),
base_gate=RZGate(theta, label=_base_label),
duration=duration,
unit=unit,
)

def _define(self):
Expand Down
6 changes: 4 additions & 2 deletions qiskit/circuit/library/standard_gates/rzx.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ class RZXGate(Gate):
\end{pmatrix}
"""

def __init__(self, theta: ParameterValueType, label: Optional[str] = None):
def __init__(
self, theta: ParameterValueType, label: Optional[str] = None, *, duration=None, unit="dt"
):
"""Create new RZX gate."""
super().__init__("rzx", 2, [theta], label=label)
super().__init__("rzx", 2, [theta], label=label, duration=duration, unit=unit)

def _define(self):
"""
Expand Down
6 changes: 4 additions & 2 deletions qiskit/circuit/library/standard_gates/rzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ class RZZGate(Gate):
\end{pmatrix}
"""

def __init__(self, theta: ParameterValueType, label: Optional[str] = None):
def __init__(
self, theta: ParameterValueType, label: Optional[str] = None, *, duration=None, unit="dt"
):
"""Create new RZZ gate."""
super().__init__("rzz", 2, [theta], label=label)
super().__init__("rzz", 2, [theta], label=label, duration=duration, unit=unit)

def _define(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions qiskit/circuit/library/standard_gates/s.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class SGate(SingletonGate):

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

_singleton_lookup_key = stdlib_singleton_key()

Expand Down Expand Up @@ -124,7 +124,7 @@ class SdgGate(SingletonGate):

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

_singleton_lookup_key = stdlib_singleton_key()

Expand Down
13 changes: 11 additions & 2 deletions qiskit/circuit/library/standard_gates/u.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ def __init__(
phi: ParameterValueType,
lam: ParameterValueType,
label: Optional[str] = None,
*,
duration=None,
unit="dt",
):
"""Create new U gate."""
super().__init__("u", 1, [theta, phi, lam], label=label)
super().__init__("u", 1, [theta, phi, lam], label=label, duration=duration, unit=unit)

def inverse(self):
r"""Return inverted U gate.
Expand Down Expand Up @@ -234,6 +237,10 @@ def __init__(
gamma: ParameterValueType,
label: Optional[str] = None,
ctrl_state: Optional[Union[str, int]] = None,
*,
duration=None,
unit="dt",
_base_label=None,
):
"""Create new CU gate."""
super().__init__(
Expand All @@ -243,7 +250,9 @@ def __init__(
num_ctrl_qubits=1,
label=label,
ctrl_state=ctrl_state,
base_gate=UGate(theta, phi, lam),
base_gate=UGate(theta, phi, lam, label=_base_label),
duration=duration,
unit=unit,
)

def _define(self):
Expand Down
22 changes: 18 additions & 4 deletions qiskit/circuit/library/standard_gates/u1.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ class U1Gate(Gate):
`1612.00858 <https://arxiv.org/abs/1612.00858>`_
"""

def __init__(self, theta: ParameterValueType, label: str | None = None):
def __init__(
self, theta: ParameterValueType, label: str | None = None, *, duration=None, unit="dt"
):
"""Create new U1 gate."""
super().__init__("u1", 1, [theta], label=label)
super().__init__("u1", 1, [theta], label=label, duration=duration, unit=unit)

def _define(self):
# pylint: disable=cyclic-import
Expand Down Expand Up @@ -189,6 +191,10 @@ def __init__(
theta: ParameterValueType,
label: str | None = None,
ctrl_state: str | int | None = None,
*,
duration=None,
unit="dt",
_base_label=None,
):
"""Create new CU1 gate."""
super().__init__(
Expand All @@ -198,7 +204,9 @@ def __init__(
num_ctrl_qubits=1,
label=label,
ctrl_state=ctrl_state,
base_gate=U1Gate(theta),
base_gate=U1Gate(theta, label=_base_label),
duration=duration,
unit=unit,
)

def _define(self):
Expand Down Expand Up @@ -303,6 +311,10 @@ def __init__(
num_ctrl_qubits: int,
label: str | None = None,
ctrl_state: str | int | None = None,
*,
duration=None,
unit="dt",
_base_label=None,
):
"""Create new MCU1 gate."""
super().__init__(
Expand All @@ -312,7 +324,9 @@ def __init__(
num_ctrl_qubits=num_ctrl_qubits,
label=label,
ctrl_state=ctrl_state,
base_gate=U1Gate(lam),
base_gate=U1Gate(lam, label=_base_label),
duration=duration,
unit=unit,
)

def _define(self):
Expand Down
10 changes: 8 additions & 2 deletions qiskit/circuit/library/standard_gates/u2.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,16 @@ class U2Gate(Gate):
"""

def __init__(
self, phi: ParameterValueType, lam: ParameterValueType, label: Optional[str] = None
self,
phi: ParameterValueType,
lam: ParameterValueType,
label: Optional[str] = None,
*,
duration=None,
unit="dt",
):
"""Create new U2 gate."""
super().__init__("u2", 1, [phi, lam], label=label)
super().__init__("u2", 1, [phi, lam], label=label, duration=duration, unit=unit)

def _define(self):
# pylint: disable=cyclic-import
Expand Down
Loading

0 comments on commit 9f3a7cf

Please sign in to comment.