Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Co-authored-by: Luciano Bello <luciano.bello@ibm.com>
  • Loading branch information
Cryoris and Luciano Bello committed Apr 28, 2020
1 parent d5ea6ac commit 4891131
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 49 deletions.
4 changes: 3 additions & 1 deletion qiskit/circuit/library/standard_gates/h.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
ControlledGate: controlled version of this gate.
"""
if num_ctrl_qubits == 1:
return CHGate(label=label, ctrl_state=ctrl_state)
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
5 changes: 3 additions & 2 deletions qiskit/circuit/library/standard_gates/ms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ class MSGate(Gate):
"""

@deprecate_arguments({'n_qubits': 'num_qubits'})
def __init__(self, num_qubits, theta, *, n_qubits=None): # pylint:disable=unused-argument
def __init__(self, num_qubits, theta, *, n_qubits=None, # pylint:disable=unused-argument
label=None):
"""Create new MS gate."""
super().__init__('ms', num_qubits, [theta])
super().__init__('ms', num_qubits, [theta], label=label)

def _define(self):
from .rxx import RXXGate
Expand Down
7 changes: 4 additions & 3 deletions qiskit/circuit/library/standard_gates/rx.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
ControlledGate: controlled version of this gate.
"""
if num_ctrl_qubits == 1:
return CRXGate(self.params[0], label=label, ctrl_state=ctrl_state)
return super().control(num_ctrl_qubits=num_ctrl_qubits, label=label,
ctrl_state=ctrl_state)
gate = CRXGate(self.params[0], 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)

def inverse(self):
r"""Return inverted RX gate.
Expand Down
7 changes: 4 additions & 3 deletions qiskit/circuit/library/standard_gates/ry.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
ControlledGate: controlled version of this gate.
"""
if num_ctrl_qubits == 1:
return CRYGate(self.params[0], label=label, ctrl_state=ctrl_state)
return super().control(num_ctrl_qubits=num_ctrl_qubits, label=label,
ctrl_state=ctrl_state)
gate = CRYGate(self.params[0], 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)

def inverse(self):
r"""Return inverted RY gate.
Expand Down
7 changes: 4 additions & 3 deletions qiskit/circuit/library/standard_gates/rz.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
ControlledGate: controlled version of this gate.
"""
if num_ctrl_qubits == 1:
return CRZGate(self.params[0], label=label, ctrl_state=ctrl_state)
return super().control(num_ctrl_qubits=num_ctrl_qubits, label=label,
ctrl_state=ctrl_state)
gate = CRZGate(self.params[0], 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)

def inverse(self):
r"""Return inverted RZ gate
Expand Down
11 changes: 6 additions & 5 deletions qiskit/circuit/library/standard_gates/swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class SwapGate(Gate):
|a, b\rangle \rightarrow |b, a\rangle
"""

def __init__(self):
def __init__(self, label=None):
"""Create new SWAP gate."""
super().__init__('swap', 2, [])
super().__init__('swap', 2, [], label=label)

def _define(self):
"""
Expand Down Expand Up @@ -88,9 +88,10 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
ControlledGate: controlled version of this gate.
"""
if num_ctrl_qubits == 1:
return CSwapGate(label=None, ctrl_state=ctrl_state)
return super().control(num_ctrl_qubits=num_ctrl_qubits, label=label,
ctrl_state=ctrl_state)
gate = CSwapGate(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)

def inverse(self):
"""Return inverse Swap gate (itself)."""
Expand Down
33 changes: 20 additions & 13 deletions qiskit/circuit/library/standard_gates/u1.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,14 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
ControlledGate: controlled version of this gate.
"""
if num_ctrl_qubits == 1:
return CU1Gate(self.params[0], label=label, ctrl_state=ctrl_state)
if ctrl_state is None and num_ctrl_qubits > 1:
return MCU1Gate(self.params[0], num_ctrl_qubits)
return super().control(num_ctrl_qubits=num_ctrl_qubits, label=label,
ctrl_state=ctrl_state)
gate = CU1Gate(self.params[0], label=label, ctrl_state=ctrl_state)
elif ctrl_state is None and num_ctrl_qubits > 1:
gate = MCU1Gate(self.params[0], num_ctrl_qubits, label=label)
else:
return super().control(num_ctrl_qubits=num_ctrl_qubits, label=label,
ctrl_state=ctrl_state)
gate.base_gate.label = self.label
return gate

def inverse(self):
r"""Return inverted U1 gate (:math:`U1(\lambda){\dagger} = U1(-\lambda)`)"""
Expand Down Expand Up @@ -208,9 +211,10 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
ControlledGate: controlled version of this gate.
"""
if ctrl_state is None:
return MCU1Gate(self.params[0], num_ctrl_qubits=num_ctrl_qubits + 1)
return super().control(num_ctrl_qubits=num_ctrl_qubits, label=label,
ctrl_state=ctrl_state)
gate = MCU1Gate(self.params[0], num_ctrl_qubits=num_ctrl_qubits + 1, label=label)
gate.base_gate.label = self.label
return gate
return super().control(num_ctrl_qubits=num_ctrl_qubits, label=label, ctrl_state=ctrl_state)

def inverse(self):
r"""Return inverted CU1 gate (:math:`CU1(\lambda){\dagger} = CU1(-\lambda)`)"""
Expand Down Expand Up @@ -254,9 +258,10 @@ class MCU1Gate(ControlledGate):
The singly-controlled-version of this gate.
"""

def __init__(self, lam, num_ctrl_qubits):
def __init__(self, lam, num_ctrl_qubits, label=None):
"""Create new MCU1 gate."""
super().__init__('mcu1', num_ctrl_qubits + 1, [lam], num_ctrl_qubits=num_ctrl_qubits)
super().__init__('mcu1', num_ctrl_qubits + 1, [lam], num_ctrl_qubits=num_ctrl_qubits,
label=label)
self.base_gate = U1Gate(lam)

def _define(self):
Expand Down Expand Up @@ -287,9 +292,11 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
ControlledGate: controlled version of this gate.
"""
if ctrl_state is None:
return MCU1Gate(self.params[0], num_ctrl_qubits=num_ctrl_qubits + self.num_ctrl_qubits)
return super().control(num_ctrl_qubits=num_ctrl_qubits, label=label,
ctrl_state=ctrl_state)
gate = MCU1Gate(self.params[0], num_ctrl_qubits=num_ctrl_qubits + self.num_ctrl_qubits,
label=label)
gate.base_gate.label = self.label
return gate
return super().control(num_ctrl_qubits=num_ctrl_qubits, label=label, ctrl_state=ctrl_state)

def inverse(self):
r"""Return inverted MCU1 gate (:math:`MCU1(\lambda){\dagger} = MCU1(-\lambda)`)"""
Expand Down
7 changes: 4 additions & 3 deletions qiskit/circuit/library/standard_gates/u3.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
ControlledGate: controlled version of this gate.
"""
if num_ctrl_qubits == 1:
return CU3Gate(*self.params, label=label, ctrl_state=ctrl_state)
return super().control(num_ctrl_qubits=num_ctrl_qubits, label=label,
ctrl_state=ctrl_state)
gate = CU3Gate(*self.params, 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)

def to_matrix(self):
"""Return a Numpy.array for the U3 gate."""
Expand Down
33 changes: 23 additions & 10 deletions qiskit/circuit/library/standard_gates/x.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
Returns:
ControlledGate: controlled version of this gate.
"""
return MCXGate(num_ctrl_qubits=num_ctrl_qubits, label=label, ctrl_state=ctrl_state)
gate = MCXGate(num_ctrl_qubits=num_ctrl_qubits, label=label, ctrl_state=ctrl_state)
gate.base_gate.label = self.label
return gate

def inverse(self):
r"""Return inverted X gate (itself)."""
Expand Down Expand Up @@ -204,7 +206,9 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
if ctrl_state is None:
ctrl_state = 2**num_ctrl_qubits - 1
new_ctrl_state = (self.ctrl_state << num_ctrl_qubits) | ctrl_state
return MCXGate(num_ctrl_qubits=num_ctrl_qubits + 1, label=label, ctrl_state=new_ctrl_state)
gate = MCXGate(num_ctrl_qubits=num_ctrl_qubits + 1, label=label, ctrl_state=new_ctrl_state)
gate.base_gate.label = self.label
return gate

def inverse(self):
"""Return inverted CX gate (itself)."""
Expand Down Expand Up @@ -363,7 +367,9 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
if ctrl_state is None:
ctrl_state = 2**num_ctrl_qubits - 1
new_ctrl_state = (self.ctrl_state << num_ctrl_qubits) | ctrl_state
return MCXGate(num_ctrl_qubits=num_ctrl_qubits + 2, label=label, ctrl_state=new_ctrl_state)
gate = MCXGate(num_ctrl_qubits=num_ctrl_qubits + 2, label=label, ctrl_state=new_ctrl_state)
gate.base_gate.label = self.label
return gate

def inverse(self):
"""Return an inverted CCX gate (also a CCX)."""
Expand Down Expand Up @@ -401,9 +407,9 @@ class RCCXGate(Gate):
of Fig. 3.
"""

def __init__(self):
def __init__(self, label=None):
"""Create a new simplified CCX gate."""
super().__init__('rccx', 3, [])
super().__init__('rccx', 3, [], label=label)

def _define(self):
"""
Expand Down Expand Up @@ -536,7 +542,9 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
if ctrl_state is None:
ctrl_state = 2**num_ctrl_qubits - 1
new_ctrl_state = (self.ctrl_state << num_ctrl_qubits) | ctrl_state
return MCXGate(num_ctrl_qubits=num_ctrl_qubits + 3, label=label, ctrl_state=new_ctrl_state)
gate = MCXGate(num_ctrl_qubits=num_ctrl_qubits + 3, label=label, ctrl_state=new_ctrl_state)
gate.base_gate.label = self.label
return gate

def inverse(self):
"""Invert this gate. The C3X is its own inverse."""
Expand Down Expand Up @@ -574,9 +582,9 @@ class RC3XGate(Gate):
of Fig. 4.
"""

def __init__(self):
def __init__(self, label=None):
"""Create a new RC3X gate."""
super().__init__('rcccx', 4, [])
super().__init__('rcccx', 4, [], label=label)

def _define(self):
"""
Expand Down Expand Up @@ -716,7 +724,9 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
if ctrl_state is None:
ctrl_state = 2**num_ctrl_qubits - 1
new_ctrl_state = (self.ctrl_state << num_ctrl_qubits) | ctrl_state
return MCXGate(num_ctrl_qubits=num_ctrl_qubits + 4, label=label, ctrl_state=new_ctrl_state)
gate = MCXGate(num_ctrl_qubits=num_ctrl_qubits + 4, label=label, ctrl_state=new_ctrl_state)
gate.base_gate.label = self.label
return gate

def inverse(self):
"""Invert this gate. The C4X is its own inverse."""
Expand Down Expand Up @@ -796,7 +806,10 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
"""
if ctrl_state is None:
# use __class__ so this works for derived classes
return self.__class__(self.num_ctrl_qubits + num_ctrl_qubits)
gate = self.__class__(self.num_ctrl_qubits + num_ctrl_qubits, label=label,
ctrl_state=ctrl_state)
gate.base_gate.label = self.label
return gate
return super().control(num_ctrl_qubits, label=label, ctrl_state=ctrl_state)


Expand Down
7 changes: 4 additions & 3 deletions qiskit/circuit/library/standard_gates/y.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
ControlledGate: controlled version of this gate.
"""
if num_ctrl_qubits == 1:
return CYGate(label=label, ctrl_state=ctrl_state)
return super().control(num_ctrl_qubits=num_ctrl_qubits, label=label,
ctrl_state=ctrl_state)
gate = CYGate(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)

def inverse(self):
r"""Return inverted Y gate (:math:`Y{\dagger} = Y`)"""
Expand Down
7 changes: 4 additions & 3 deletions qiskit/circuit/library/standard_gates/z.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
ControlledGate: controlled version of this gate.
"""
if num_ctrl_qubits == 1:
return CZGate(label=label, ctrl_state=ctrl_state)
return super().control(num_ctrl_qubits=num_ctrl_qubits, label=label,
ctrl_state=ctrl_state)
gate = CZGate(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)

def inverse(self):
"""Return inverted Z gate (itself)."""
Expand Down

0 comments on commit 4891131

Please sign in to comment.